JavaScript+HTML5 canvas實(shí)現(xiàn)放大鏡效果完整示例
本文實(shí)例講述了JavaScript+HTML5 canvas實(shí)現(xiàn)放大鏡效果。分享給大家供大家參考,具體如下:
效果:

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>www.fzitv.net canvas放大鏡</title>
<style>
#copycanvas {
border: 1px solid #000;
display: none;
}
#square {
width: 90px;
height: 90px;
background-color: #cc3;
border: 1px solid #f00;
opacity: 0.5;
position: absolute;
z-index: 999;
display: none;
cursor: crosshair;
}
</style>
</head>
<body>
<canvas id="canvas" width="450" height="676"></canvas>
<canvas id="copycanvas" width="300" height="300"></canvas>
<div id="square"></div>
<script>
var canvas = document.getElementById('canvas'), //獲取canvas對(duì)象
context = canvas.getContext('2d'), //獲取上下文
copycanvas = document.getElementById('copycanvas'), //獲取copycanvas
copycontext = copycanvas.getContext('2d'),
square = document.getElementById('square'), //獲取透明框
squaredata = {}, //用來(lái)保存選擇框數(shù)據(jù)
box = canvas.getBoundingClientRect();
//getBoundingClientRect方法可以獲取元素上、下、左、右分別相對(duì)瀏覽器的坐標(biāo)位置
//創(chuàng)建圖像對(duì)象,并加載
image = new Image();
image.src = "3.jpg";
image.onload = function(){
context.drawImage(image,0,0,canvas.width,canvas.height);
};
canvas.onmouseover = function(e){
var x = e.clientX, //獲取鼠標(biāo)實(shí)時(shí)坐標(biāo)
y = e.clientY;
createSquare(x,y); //保存透明選擇框?qū)傩?
};
window.onmousemove = function(e){
var x = e.clientX,
y = e.clientY;
//判斷鼠標(biāo)是否移出canvas
if(x >= canvas.offsetLeft && x <= canvas.offsetLeft + canvas.width &&
y >= canvas.offsetTop && y <= canvas.offsetTop + canvas.height){
createSquare(x,y);
}else{
hideSquare();
hideCanvas();
}
}
function showSquare(){
square.style.display = 'block';
}
function hideSquare(){
square.style.display = 'none';
}
function showCanvas(){
copycanvas.style.display = "inline";
}
function hideCanvas(){
copycanvas.style.display = "none";
}
function createSquare(x,y){
//控制選擇框不移動(dòng)出canvas
x = x - 45 < canvas.offsetLeft ? canvas.offsetLeft:x - 45;
y = y - 45 < canvas.offsetTop ? canvas.offsetTop:y - 45;
x = x + 90 < box.right ? x:box.right - 90;
y = y + 90 < box.bottom ? y:box.bottom - 90;
squaredata.left = x;
squaredata.top = y;
moveSquare(x,y);
}
function moveSquare(x,y){
square.style.left = x + "px";
square.style.top = y + "px";
showCanvas();
showSquare();
copy();
}
function copy(){
copycontext.drawImage(
canvas,
squaredata.left - box.left,
squaredata.top - box.top,
90,
90,
0,
0,
copycanvas.width,
copycanvas.height
);
}
</script>
</body>
</html>
感興趣的朋友可使用在線(xiàn)HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試一下運(yùn)行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《JavaScript圖片操作技巧大全》、《JavaScript運(yùn)動(dòng)效果與技巧匯總》、《JavaScript+HTML5特效與技巧匯總》、《JavaScript圖形繪制技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- JavaScript實(shí)現(xiàn)拖動(dòng)滑塊拼圖驗(yàn)證功能(html5、canvas)
- javascript實(shí)現(xiàn)移動(dòng)端 HTML5 圖片上傳預(yù)覽和壓縮功能示例
- JS+HTML5本地存儲(chǔ)Localstorage實(shí)現(xiàn)注冊(cè)登錄及驗(yàn)證功能示例
- JS+html5實(shí)現(xiàn)異步上傳圖片顯示上傳文件進(jìn)度條功能示例
- js+HTML5 canvas 實(shí)現(xiàn)簡(jiǎn)單的加載條(進(jìn)度條)功能示例
- javascript+HTML5 canvas繪制時(shí)鐘功能示例
- JavaScript利用html5新方法操作元素類(lèi)名詳解
相關(guān)文章
JavaScript的類(lèi)型、值和變量小結(jié)
這篇文章主要介紹了JavaScript的類(lèi)型、值和變量小結(jié)的相關(guān)資料,需要的朋友可以參考下2015-07-07
跟我學(xué)習(xí)javascript的for循環(huán)和for...in循環(huán)
跟我學(xué)習(xí)javascript的for循環(huán)和for...in循環(huán),它們是JavaScript中提供了兩種方式迭代對(duì)象,本文就和大家一起學(xué)習(xí)for循環(huán)和for...in循環(huán),感興趣的小伙伴們可以參考一下2015-11-11
使用svg實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果
這篇文章主要為大家詳細(xì)介紹了如何使用svg實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Thinkphp模板沒(méi)有解析直接原樣輸出的解決方法
這篇文章主要介紹了Thinkphp模板沒(méi)有解析直接原樣輸出的解決方法,是很多開(kāi)發(fā)者都遇到的問(wèn)題,非常實(shí)用,需要的朋友可以參考下2014-10-10
微信小程序適配iphoneX的實(shí)現(xiàn)方法
這篇文章主要介紹了微信小程序適配iphoneX的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09

