HTML5執行個體-簡單實作類別似刮刮卡的功能

來源:互聯網
上載者:User


注意要點設定:


1.設定使用者縮放:user-scalable=no|yes

 代碼如下 複製代碼
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" /> 


2.禁止拖動:

 代碼如下 複製代碼
document.ontouchmove = function(e){ e.preventDefault(); }; //文檔禁止 touchmove事件 


3.禁止彈出選擇菜單:
 代碼如下 複製代碼

document.documentElement.style.webkitTouchCallout = "none"; 




現在貼上完整代碼,想學習的同學自己去測試吧。
 代碼如下 複製代碼


<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8" />
        <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" />
        <title>志玲傳說</title>
        <style type="text/css">
            body {
                width: 320px;
                min-height: 568px;
                overflow: hidden;
                margin: 0;
            }
            #canvas {
                background: url(img/lzl.jpg);
                /*獎品圖片*/
                fixed center center no-repeat;
                background-size: cover;
                width: 320px;
                min-height: 480px;
                overflow: hidden;
                position: relative;
            }
            .before {
                background: none !important;
            }
            #canvas canvas {
                cursor: url("img/bird.png") 0 0, auto;
                /*PC端的手勢圖片*/
            }
        </style>
    </head>

    <body oncontextmenu="return false;" onselectstart="return false;">
        <div id="canvas">
        </div>
    </body>
    <script type="text/javascript">
        (function() {

            window.onload = function() {
                /**禁止拖動設定*/
                document.ontouchmove = function(e) {
                    e.preventDefault();
                };
               
                /**判斷瀏覽器是否支援canvas**/

                try {

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

                } catch (e) {

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

                    alert('您的手機不支援刮刮卡效果哦~!');

                }

            };

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

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

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

            function createCanvas(parent, width, height) {

                var canvas = {};

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

                canvas.context = canvas.node.getContext('2d');
                //此處可以自己給標籤添加
                canvas.node.width = width || 320;

                canvas.node.height = height || 480;
                //給canvas標籤添加Id
                canvas.node.id = 'canvasTag';

                parent.appendChild(canvas.node);

                return canvas;

            }

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

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

                var ctx = canvas.context;

                // define a custom fillCircle method

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

                    this.fillStyle = fillColor;

                    this.beginPath();

                    this.moveTo(x, y);

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

                    this.fill();

                };

                ctx.clearTo = function(fillColor) {

                    ctx.fillStyle = fillColor;

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

                };

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

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

                    canvas.isDrawing = true;

                }, false);

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

                    canvas.isDrawing = false;

                }, false);

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

                    if (!canvas.isDrawing) {

                        return;

                    }

                    if (type == 'Android') {

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

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

                    } else {

                        var x = e.pageX - this.offsetLeft;

                        var y = e.pageY - this.offsetTop;

                    }

                    var radius = 20;

                    var fillColor = '#ff0000';

                    ctx.globalCompositeOperation = 'destination-out';

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

                }, false);

            }

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

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

        })();
    </script>

</html>

 

瀏覽效果如下:

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.