HTML5实现刮刮卡功能

时间:2021-12-01 10:20:36
HTML5实现类似刮刮卡的功能

有这样一个功能,当我们使用微信公众号,发送图片时......此处省略300字!

注意要点设置:

1.设置用户缩放:user-scalable=no|yes

[java] view plaincopyprint?
  1. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />  

2.禁止拖动:

[java] view plaincopyprint?
  1. document.ontouchmove = function(e){ e.preventDefault(); }; //文档禁止 touchmove事件  

3.禁止弹出选择菜单: [java] view plaincopyprint?
  1. document.documentElement.style.webkitTouchCallout = "none";  

具体实现代码: [html] view plaincopyprint?
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3.   
  4.     <head>  
  5.         <meta charset="UTF-8" />  
  6.         <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" />  
  7.         <title></title>  
  8.         <style type="text/css">  
  9.             body {  
  10.                 width: 320px;  
  11.                 min-height: 568px;  
  12.                 overflow: hidden;  
  13.                 margin: 0;  
  14.             }  
  15.             #canvas {  
  16.                 background: url(img/lzl.jpg);  
  17.                 /*奖品图片*/  
  18.                 fixed center center no-repeat;  
  19.                 background-size: cover;  
  20.                 width: 320px;  
  21.                 min-height: 480px;  
  22.                 overflow: hidden;  
  23.                 position: relative;  
  24.             }  
  25.             .before {  
  26.                 background: none !important;  
  27.             }  
  28.             #canvas canvas {  
  29.                 cursor: url("img/bird.png") 0 0, auto;  
  30.                 /*PC端的手势图片*/  
  31.             }  
  32.         </style>  
  33.     </head>  
  34.   
  35.     <body oncontextmenu="return false;" onselectstart="return false;">  
  36.         <div id="canvas">  
  37.         </div>  
  38.     </body>  
  39.     <script type="text/javascript">  
  40.         (function() {  
  41.   
  42.             window.onload = function() {  
  43.                 /**禁止拖动设置*/  
  44.                 document.ontouchmove = function(e) {  
  45.                     e.preventDefault();  
  46.                 };  
  47.                   
  48.                 /**判断浏览器是否支持canvas**/  
  49.   
  50.                 try {  
  51.   
  52.                     document.createElement('canvas').getContext('2d');  
  53.   
  54.                 } catch (e) {  
  55.   
  56.                     var addDiv = document.createElement('div');  
  57.   
  58.                     alert('您的手机不支持刮刮卡效果哦~!');  
  59.   
  60.                 }  
  61.   
  62.             };  
  63.   
  64.             var u = navigator.userAgent,  
  65.                 mobile = 'PC';  
  66.   
  67.             if (u.indexOf('iPhone') > -1) mobile = 'iphone';  
  68.   
  69.             if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) mobile = 'Android';  
  70.   
  71.             function createCanvas(parent, width, height) {  
  72.   
  73.                 var canvas = {};  
  74.   
  75.                 canvas.node = document.createElement('canvas');  
  76.   
  77.                 canvas.context = canvas.node.getContext('2d');  
  78.                 //此处可以自己给标签添加  
  79.                 canvas.node.width = width || 320;  
  80.   
  81.                 canvas.node.height = height || 480;  
  82.                 //给canvas标签添加Id  
  83.                 canvas.node.id = 'canvasTag';  
  84.   
  85.                 parent.appendChild(canvas.node);  
  86.   
  87.                 return canvas;  
  88.   
  89.             }  
  90.   
  91.             function init(container, width, height, fillColor, type) {  
  92.   
  93.                 var canvas = createCanvas(container, width, height);  
  94.   
  95.                 var ctx = canvas.context;  
  96.   
  97.                 // define a custom fillCircle method  
  98.   
  99.                 ctx.fillCircle = function(x, y, radius, fillColor) {  
  100.   
  101.                     this.fillStyle = fillColor;  
  102.   
  103.                     this.beginPath();  
  104.   
  105.                     this.moveTo(x, y);  
  106.   
  107.                     this.arc(x, y, radius, 0, Math.PI * 2, false);  
  108.   
  109.                     this.fill();  
  110.   
  111.                 };  
  112.   
  113.                 ctx.clearTo = function(fillColor) {  
  114.   
  115.                     ctx.fillStyle = fillColor;  
  116.   
  117.                     ctx.fillRect(0, 0, width, height);  
  118.   
  119.                 };  
  120.   
  121.                 ctx.clearTo(fillColor || "#ddd");  
  122.   
  123.                 canvas.node.addEventListener(mobile == "PC" ? "mousedown" : "touchstart", function(e) {  
  124.   
  125.                     canvas.isDrawing = true;  
  126.   
  127.                 }, false);  
  128.   
  129.                 canvas.node.addEventListener(mobile == "PC" ? "mouseup" : "touchend", function(e) {  
  130.   
  131.                     canvas.isDrawing = false;  
  132.   
  133.                 }, false);  
  134.   
  135.                 canvas.node.addEventListener(mobile == "PC" ? "mousemove" : "touchmove", function(e) {  
  136.   
  137.                     if (!canvas.isDrawing) {  
  138.   
  139.                         return;  
  140.   
  141.                     }  
  142.   
  143.                     if (type == 'Android') {  
  144.   
  145.                         var x = e.changedTouches[0].pageX - this.offsetLeft;  
  146.   
  147.                         var y = e.changedTouches[0].pageY - this.offsetTop;  
  148.   
  149.                     } else {  
  150.   
  151.                         var x = e.pageX - this.offsetLeft;  
  152.   
  153.                         var y = e.pageY - this.offsetTop;  
  154.   
  155.                     }  
  156.   
  157.                     var radius = 20;  
  158.   
  159.                     var fillColor = '#ff0000';  
  160.   
  161.                     ctx.globalCompositeOperation = 'destination-out';  
  162.   
  163.                     ctx.fillCircle(x, y, radius, fillColor);  
  164.   
  165.                 }, false);  
  166.   
  167.             }  
  168.   
  169.             var container = document.getElementById('canvas');  
  170.   
  171.             init(container, 320, 568, '#696868', mobile);  
  172.   
  173.         })();  
  174.     </script>  
  175.   
  176. </html>