小编给大家分享一下js+canvas实现放大镜查看图片功能的示例,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
具体内容如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body > <canvas id="myCanvas" > 你的浏览器不支持canvas </canvas> <canvas id="offCanvas" ></canvas> <script> var myCanvas = document.getElementById("myCanvas"); var context = myCanvas.getContext("2d"); var offCanvas = document.getElementById("offCanvas"); var offContext = offCanvas.getContext("2d"); var isMouseDown = false; var scale; var image = new Image(); window.onload = function () { myCanvas.width = 300; myCanvas.height = 200; image.src = "1.jpg"; image.onload = function () { offCanvas.width = image.width; offCanvas.height = image.height; scale = offCanvas.width / myCanvas.width ; context.drawImage(image,0,0,myCanvas.width,myCanvas.height); offContext.drawImage(image,0,0); } }; function windowToCanvas(x,y) { var bbox = myCanvas.getBoundingClientRect(); return { x : x - bbox.left , y : y - bbox.top}; } myCanvas.onmousedown = function (e) { e.preventDefault(); var point = windowToCanvas(e.clientX , e.clientY); console.log(point); isMouseDown = true; drawCanvasWithMagnifier( true , point ); }; myCanvas.onmousemove = function (e) { e.preventDefault(); if (isMouseDown ){ var point = windowToCanvas(e.clientX , e.clientY); drawCanvasWithMagnifier( true , point ); } }; myCanvas.onmouseup = function (e) { e.preventDefault(); isMouseDown = false; drawCanvasWithMagnifier( false ); }; myCanvas.onmouseout = function (e) { e.preventDefault(); isMouseDown = false; drawCanvasWithMagnifier( false ); }; function drawCanvasWithMagnifier( isShowMagnifier , point) { context.clearRect(0,0,myCanvas.width,myCanvas.height); context.drawImage(image,0,0,myCanvas.width,myCanvas.height); if(isShowMagnifier == true ){ drawMagnifier( point ); } } function drawMagnifier( point ) { var imageLG_cx = point.x * scale ; var imageLG_cy = point.y * scale ; var mr = 20 ; var sx = imageLG_cx - mr ; var sy = imageLG_cy - mr ; var dx = point.x - mr ; var dy = point.y - mr ; context.save(); context.lineWidth = 5.0; context.strokeStyle = "#069"; context.beginPath(); context.arc(point.x,point.y,mr,0,Math.PI*2); context.stroke(); context.clip(); context.drawImage(offCanvas,sx,sy,2*mr,2*mr,dx,dy,2*mr,2*mr); context.restore(); } </script> </body> </html>
1.可以使网页具有交互性,例如响应用户点击,给用户提供更好的体验。 2.可以处理表单,检验用户的输入,并提供及时反馈节省用户时间。 3.可以根据用户的操作,动态的创建页面。 4使用JavaScript可以通过设置cookie存储在浏览器上的一些临时信息。
以上是“js+canvas实现放大镜查看图片功能的示例”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。