时间:2021-05-28
效果:
点击RESET:随机显示剪辑区域;
点击SHOW:显示完整清晰图片;
图(1)点击RESET
图(2)点击SHOW
代码如下:
<!doctype html><html><head><meta charset="utf-8"><title>canvas红包照片</title><script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script><style type="text/css"> #blur_div{ width: 500px; height: 334px; margin: 0 auto; position: relative; margin-top:100px; } #blur_img{ display: block; width: 500px; height: 334px; margin: 0 auto; filter: blur(15px); -webkit-filter: blur(15px); -moz-filter: blur(15px); -ms-filter: blur(15px); -o-filter: blur(15px); position:absolute; left: 0px; top: 0px; z-index: 0; } #canvas{ display: block; margin: 0 auto; position:absolute; left: 0px; top: 0px; z-index: 100; } .button{ display:block; position:absolute; z-index:200; width:80px; height:30px; color:white; text-decoration:none; text-align:center; line-height:30px; border-radius:5px; } #reset_button{ left:100px; bottom:20px; background-color:#058; } #reset_button:hover{ background-color:#047; } #show_button{ right:100px; bottom:20px; background-color:#085; } #show_button:hover{ background-color:#074; }</style></head><body> <div id="blur_div"> <img id="blur_img" src="http://cdn.attach.qdfuns.com/notes/pics/201702/27/221935e35ugwllljg10912.jpg" /> <canvas id="canvas"></canvas> <a href="javascript:reset()" rel="external nofollow" class="button" id="reset_button">RESET</a> <a href="javascript:show()" rel="external nofollow" class="button" id="show_button">SHOW</a> </div> <script> var canvasWidth = 500; var canvasHeight = 334; var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); canvas.width = canvasWidth; canvas.height = canvasHeight; var image = new Image(); var radius = 40; var clippingRegion = {x:-1,y:-1,r:radius}; image.src = "http://cdn.attach.qdfuns.com/notes/pics/201702/27/221935e35ugwllljg10912.jpg"; image.onload = function(e){ initCanvas(); } function initCanvas(){ clippingRegion = {x:Math.random()*(canvas.width-2*radius)+radius, y:Math.random()*(canvas.height-2*radius)+radius, r:radius}; draw(image,clippingRegion); } function setClippingRegion(clippingRegion){ context.beginPath(); context.arc(clippingRegion.x,clippingRegion.y,clippingRegion.r,0,2*Math.PI,false); context.clip(); } function draw(image,clippingRegion){ context.clearRect(0,0,canvas.width,canvas.height); context.save(); setClippingRegion(clippingRegion); context.drawImage(image,0,0); context.restore(); } function reset(){ initCanvas(); } function show(){ var theAnimation = setInterval( function(){ clippingRegion.r += 20; if(clippingRegion.r>2*Math.max(canvas.width,canvas.height)){ clearInterval(theAnimation); } draw(image,clippingRegion); },20) } </script></body></html>以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本篇文章来源慕课网课程《canvas玩转红包照片》,用canvas及css3结合,实现红包照片的效果,并不做支付过程,代替的是使用2个按钮,显示清晰图片和重置圆
本文实例为大家分享了canvas图像截取的具体代码,供大家参考,具体内容如下canvas-图像截取canvas{border:1pxdashedred;floa
本文实例为大家分享了canvas图像布局填充的具体代码,供大家参考,具体内容如下canvas-图像布局填充#canvas{border:1pxsolidpale
本文实例讲述了JavaScript+html5canvas绘制时钟效果。分享给大家供大家参考,具体如下:HTML部分:canvas绘图JavaScript部分:
本文实例为大家分享了Canvas时钟效果展示的具体代码,供大家参考,具体内容如下canvas{display:block;margin:0auto;backgr