【html5】Canvas 刮刮卡
2015-06-11 20:14
633 查看
<!DOCTYPE html> <html> <head> <title>刮刮卡</title> <meta name="viewport" content="width=device-width"/> <style type="text/css"> #canvas1{ background: url(http://img.my.csdn.net/uploads/201506/11/1434025119_9005.jpg) no-repeat; background-size: 100% 100%; background-position: center; } button{ width: 100px; height: 44px; margin-top: 50px; } </style> </head> <body> <canvas id="canvas1" width="250px" height="100px"></canvas> <button id="clear">Clear</button> <script> var canvas = document.getElementById('canvas1'); var ctx = canvas.getContext('2d'), width = 250, height = 100, radius = 10, lineWidth = radius*2; var ol = canvas.offsetLeft, ot = canvas.offsetTop; function fill(reveal){ ctx.beginPath(); if(reveal !== true){ ctx.globalCompositeOperation = 'source-over'; } ctx.fillStyle = '#ccc'; ctx.fillRect(0, 0, width, height); ctx.globalCompositeOperation = 'destination-out'; }; fill(); ctx.lineCap = 'round'; ctx.lineWidth = lineWidth; ctx.lineJoin = 'round'; canvas.addEventListener('touchmove',function(e){ var touch = e.touches[0]; var x = touch.clientX - ol, y = touch.clientY - ot; ctx.lineTo(x,y); ctx.stroke(); }); canvas.addEventListener('touchend',function(e){ var data = ctx.getImageData(0,0,width,height).data; for(var i=0,j=0;i< data.length;i+=4){ if(data[i] && data[i+1] && data[i+2] && data[i+3]){ j++; } //console.log(data[i] +','+ data[i+1] +','+ data[i+2] +','+ data[i+3]); } if(j <= width * height * 0.5){ alert('ok'); fill(true); } }); document.querySelector('#clear').addEventListener('click',fill); document.body.addEventListener('touchmove',function(e){ e.preventDefault(); }); </script> </body> </html>
相关文章推荐
- 关于HTML5数据存储
- html5+css3动画
- HTML5学习笔记 Web存储
- HTML5新特性展示利用history.replaceState()修改历史记录
- HTML5+CSS3的响应式网页设计:自动适应屏幕宽度
- html5,audio标签的autoplay="autoplay"在iphone上无法播放的问题
- 各大浏览器 CSS3 和 HTML5 兼容速查表
- html5实现的一些效果
- 7个混合式HTML5移动开发框架
- 慕课网HTML5学习笔记 (下)
- HTML5 WebSocket 简单入门
- HTML5之FileReader的使用
- HTML5 文字及图片标签解析
- HTML5漂亮实用的电子书
- html5实现录音、拍摄功能
- 第二课:html5基础
- 用HTML5 实现橡皮擦的涂抹效果的教程_html5教程技巧
- html5 canvas 绘制五星红旗
- html5新的选择器
- Swiper滑动Html5手机浏览器自适应