HTML5实现类似刮刮卡的功能

来源:互联网 发布:ubuntu交叉编译链路径 编辑:程序博客网 时间:2024/05/19 13:58
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.                                 width: 320px;
  20.                                 min-height: 480px;
  21.                                 overflow: hidden;
  22.                                 position: relative;
  23.                         }
  24.                         .before {
  25.                                 background: none !important;
  26.                         }
  27.                         #canvas canvas {
  28.                                 cursor: url("img/bird.png") 0 0, auto;
  29.                                 /*PC端的手势图片*/
  30.                         }
  31.                 </style>
  32.         </head>

  33.         <body oncontextmenu="return false;" onselectstart="return false;">
  34.                 <div id="canvas">
  35.                 </div>
  36.         </body>
  37.         <script type="text/javascript">
  38.                 (function() {

  39.                         window.onload = function() {
  40.                                 /**禁止拖动设置*/
  41.                                 document.ontouchmove = function(e) {
  42.                                         e.preventDefault();
  43.                                 };
  44.                                 
  45.                                 /**判断浏览器是否支持canvas**/

  46.                                 try {

  47.                                         document.createElement('canvas').getContext('2d');

  48.                                 } catch (e) {

  49.                                         var addDiv = document.createElement('div');

  50.                                         alert('您的手机不支持刮刮卡效果哦~!');

  51.                                 }

  52.                         };

  53.                         var u = navigator.userAgent,
  54.                                 mobile = 'PC';

  55.                         if (u.indexOf('iPhone') > -1) mobile = 'iphone';

  56.                         if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) mobile = 'Android';

  57.                         function createCanvas(parent, width, height) {

  58.                                 var canvas = {};

  59.                                 canvas.node = document.createElement('canvas');

  60.                                 canvas.context = canvas.node.getContext('2d');
  61.                                 //此处可以自己给标签添加
  62.                                 canvas.node.width = width || 320;

  63.                                 canvas.node.height = height || 480;
  64.                                 //给canvas标签添加Id
  65.                                 canvas.node.id = 'canvasTag';

  66.                                 parent.appendChild(canvas.node);

  67.                                 return canvas;

  68.                         }

  69.                         function init(container, width, height, fillColor, type) {

  70.                                 var canvas = createCanvas(container, width, height);

  71.                                 var ctx = canvas.context;

  72.                                 // define a custom fillCircle method

  73.                                 ctx.fillCircle = function(x, y, radius, fillColor) {

  74.                                         this.fillStyle = fillColor;

  75.                                         this.beginPath();

  76.                                         this.moveTo(x, y);

  77.                                         this.arc(x, y, radius, 0, Math.PI * 2, false);

  78.                                         this.fill();

  79.                                 };

  80.                                 ctx.clearTo = function(fillColor) {

  81.                                         ctx.fillStyle = fillColor;

  82.                                         ctx.fillRect(0, 0, width, height);

  83.                                 };

  84.                                 ctx.clearTo(fillColor || "#ddd");

  85.                                 canvas.node.addEventListener(mobile == "PC" ? "mousedown" : "touchstart", function(e) {

  86.                                         canvas.isDrawing = true;

  87.                                 }, false);

  88.                                 canvas.node.addEventListener(mobile == "PC" ? "mouseup" : "touchend", function(e) {

  89.                                         canvas.isDrawing = false;

  90.                                 }, false);

  91.                                 canvas.node.addEventListener(mobile == "PC" ? "mousemove" : "touchmove", function(e) {

  92.                                         if (!canvas.isDrawing) {

  93.                                                 return;

  94.                                         }

  95.                                         if (type == 'Android') {

  96.                                                 var x = e.changedTouches[0].pageX - this.offsetLeft;

  97.                                                 var y = e.changedTouches[0].pageY - this.offsetTop;

  98.                                         } else {

  99.                                                 var x = e.pageX - this.offsetLeft;

  100.                                                 var y = e.pageY - this.offsetTop;

  101.                                         }

  102.                                         var radius = 20;

  103.                                         var fillColor = '#ff0000';

  104.                                         ctx.globalCompositeOperation = 'destination-out';

  105.                                         ctx.fillCircle(x, y, radius, fillColor);

  106.                                 }, false);

  107.                         }

  108.                         var container = document.getElementById('canvas');

  109.                         init(container, 320, 568, '#696868', mobile);

  110.                 })();
  111.         </script>

  112. </html>
复制代码
0 0
原创粉丝点击