您好,登錄后才能下訂單哦!
這篇文章主要介紹JavaScript+HTML5使用canvas實現放大鏡效果的示例,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體如下:
效果:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>www.jb51.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對象 context = canvas.getContext('2d'), //獲取上下文 copycanvas = document.getElementById('copycanvas'), //獲取copycanvas copycontext = copycanvas.getContext('2d'), square = document.getElementById('square'), //獲取透明框 squaredata = {}, //用來保存選擇框數據 box = canvas.getBoundingClientRect(); //getBoundingClientRect方法可以獲取元素上、下、左、右分別相對瀏覽器的坐標位置 //創建圖像對象,并加載 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, //獲取鼠標實時坐標 y = e.clientY; createSquare(x,y); //保存透明選擇框屬性 }; window.onmousemove = function(e){ var x = e.clientX, y = e.clientY; //判斷鼠標是否移出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){ //控制選擇框不移動出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>
1、能夠嵌入動態文本于HTML頁面。2、對瀏覽器事件做出響應。3、讀寫HTML元素。4、在數據被提交到服務器之前驗證數據。5、檢測訪客的瀏覽器信息。6、控制cookies,包括創建和修改等。7、基于Node.js技術進行服務器端編程。
以上是“JavaScript+HTML5使用canvas實現放大鏡效果的示例”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。