canvas教程

canvas实现刮刮卡效果

字号+ 作者:H5之家 来源:H5之家 2017-11-22 17:00 我要评论( )

本文主要介绍了使用canvas实现刮刮卡效果的实例。具有很好的参考价值。下面跟着小编一起来看下吧

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" /> <style> .content,.cover{width:400px; height:400px; position:absolute; left:50%; top:50%; margin:-200px 0 0 -200px;} .content{ font-size:48px; line-height:400px; text-align:center;} h3{ text-align:center; line-height:200px;} </style> </head> <body> <h3>快来刮开!!!</h3> <div >中奖啦~!</div> <canvas></canvas> </body> <script> var isdown = false, cover = document.getElementById("cover"), covercanvas = cover.getContext("2d"); // covercanvas.fillStyle="transparent"; covercanvas.fillRect(0,0,400,400); function fillter( canvas ){ canvas.fillStyle="#ccc"; canvas.fillRect(0,0,400,400); } function isDown(e){ e.preventDefault(); isdown=true; } function isUp(e){ isdown=false; } function draw( e ){ e.preventDefault(); if(isdown){ if(e.changedTouches){ e=e.changedTouches[e.changedTouches.length-1]; } var _height= parseInt((window.innerHeight-400)/2), _width= parseInt((window.innerWidth-400)/2), touchTop=e.clientY - _height, touchLeft=e.clientX - _width; with(covercanvas){ beginPath(); arc(touchLeft, touchTop, 10, 0, Math.PI * 2); fill(); } } //alert(touchTop); } fillter(covercanvas); covercanvas.globalCompositeOperation = 'destination-out'; cover.addEventListener('touchstart',isDown); cover.addEventListener('touchmove',draw); cover.addEventListener('touchend',isUp); cover.addEventListener('mousemove',draw); cover.addEventListener('mousedown',isDown); cover.addEventListener('mouseup',isUp); </script> </html>

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 使用HTML5 canvas标签实现刮刮卡效果

    使用HTML5 canvas标签实现刮刮卡效果

    2015-10-30 16:14

  • HTML5 canvas标签实现刮刮卡效果

    HTML5 canvas标签实现刮刮卡效果

    2015-10-05 17:35

网友点评