HTML5   发布时间:2022-04-25  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了HTML5实现类似刮刮卡的功能大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
HTML5实现类似刮刮卡的功能


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



[java]


  1. <Meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" />  
复制代码


2.禁止拖动:


[java]


  1. document.ontouchmove = function(e){ e.preventDefault(); }; //文档禁止 touchmove事件  
复制代码


3.禁止弹出选择菜单


[java]


  1. document.documentElement.style.webkitTouchCallout = "none";  
复制代码


具体实现代码


[html]


  1. <!DOCTYPE html>
  2. <html lang="en">

  3.         <head>
  4.                 <Meta charset="UTF-8" />
  5.                 <Meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0;" name="viewport" />
  6.                 <title>志玲传说</title>
  7.                 <style type="text/css">
  8.                         body {
  9.                                 width: 320px;
  10.                                 min-height: 568px;
  11.                                 overflow: hidden;
  12.                                 margin: 0;
  13.                         }
  14.                         #canvas {
  15.                                 background: url(img/lzl.jpg);
  16.                                 /*奖品图片*/
  17.                                 fixed center center no-repeat;
  18.                                 background-size: cover;
  19.                                 min-height: 480px;
  20.                                 position: relative;
  21.                         .before {
  22.                                 background: none !important;
  23.                         #canvas canvas {
  24.                                 cursor: url("img/bird.png") 0 0,auto;
  25.                                 /*PC端的手势图片*/
  26.                 </style>
  27.         </head>
  28.         <body oncontextmenu="return false;" onselectstart="return false;">
  29.                 <div id="canvas">
  30.                 </div>
  31.         </body>
  32.         <script type="text/javascript">
  33.                 (function() {
  34.                         window.onload = function() {
  35.                                 /**禁止拖动设置*/
  36.                                 document.ontouchmove = function(e) {
  37.                                         e.preventDefault();
  38.                                 };
  39.                                 
  40.                                 /**判断浏览器是否支持canvas**/
  41.                                 try {
  42.                                         document.createElement('canvas').getContext('2d');
  43.                                 } catch (e) {
  44.                                         var addDiv = document.createElement('div');
  45.                                         alert('您的手机不支持刮刮卡效果哦~!');
  46.                                 }
  47.                         };
  48.                         var u = navigator.userAgent,
  49.                                 mobile = 'PC';
  50.                         if (u.indexOf('iPhone') > -1) mobile = 'iphone';
  51.                         if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) mobile = 'Android';
  52.                         function createCanvas(parent,width,height) {
  53.                                 var canvas = {};
  54.                                 canvas.node = document.createElement('canvas');
  55.                                 canvas.context = canvas.node.getContext('2d');
  56.                                 //此处可以自己给标签添加
  57.                                 canvas.node.width = width || 320;
  58.                                 canvas.node.height = height || 480;
  59.                                 //给canvas标签添加Id
  60.                                 canvas.node.id = 'canvasTag';
  61.                                 parent.appendChild(canvas.node);
  62.                                 return canvas;
  63.                         function init(container,height,fillColor,type) {
  64.                                 var canvas = createCanvas(container,height);
  65.                                 var ctx = canvas.context;
  66.                                 // define a custom fillCircle method
  67.                                 ctx.fillCircle = function(x,y,radius,fillColor) {
  68.                                         this.fillStyle = fillColor;
  69.                                         this.beginPath();
  70.                                         this.moveTo(x,y);
  71.                                         this.arc(x,Math.PI * 2,false);
  72.                                         this.fill();
  73.                                 ctx.clearTo = function(fillColor) {
  74.                                         ctx.fillStyle = fillColor;
  75.                                         ctx.fillRect(0,serif; font-size:12px; line-height:1.8em">                                 ctx.clearTo(fillColor || "#ddd");
  76.                                 canvas.node.addEventListener(mobile == "PC" ? "mousedown" : "touchstart",function(e) {
  77.                                         canvas.isDrawing = true;
  78.                                 },serif; font-size:12px; line-height:1.8em">                                 canvas.node.addEventListener(mobile == "PC" ? "mouseup" : "touchend",serif; font-size:12px; line-height:1.8em">                                         canvas.isDrawing = false;
  79.                                 canvas.node.addEventListener(mobile == "PC" ? "mousemove" : "touchmove",serif; font-size:12px; line-height:1.8em">                                         if (!canvas.isDrawing) {
  80.                                                 return;
  81.                                         }
  82.                                         if (type == 'Android') {
  83.                                                 var x = e.changedTouches[0].pageX - this.offsetLeft;
  84.                                                 var y = e.changedTouches[0].pageY - this.offsetTop;
  85.                                         } else {
  86.                                                 var x = e.pageX - this.offsetLeft;
  87.                                                 var y = e.pageY - this.offsetTop;
  88.                                         var radius = 20;
  89.                                         var fillColor = '#ff0000';
  90.                                         ctx.globalCompositeOperation = 'destination-out';
  91.                                         ctx.fillCircle(x,fillColor);
  92.                         var container = document.getElementById('canvas');
  93.                         init(container,320,568,'#696868',mobile);
  94.                 })();
  95.         </script>
  96. </html>
复制代码

大佬总结

以上是大佬教程为你收集整理的HTML5实现类似刮刮卡的功能全部内容,希望文章能够帮你解决HTML5实现类似刮刮卡的功能所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: