使用HTML5實現刮刮卡效果

來源:互聯網
上載者:User

標籤:des   style   ar   io   color   os   使用   sp   java   

我們利用HTML5的畫布Canvas,結合其提供的API,在Canvas元素上繪製一個灰色蒙層,然後通過檢測使用者滑鼠移到和手勢來繪製一個透明的圖形,這樣就能看到Canvas背景下的真實圖片,就達到刮刮卡效果。

HTML

我們只需要在頁面中加入canvas標籤元素,其他的就看javascript的了。注意canvas元素是HTML5才有的元素,運行在支援HTML5的現代瀏覽器上。

 
<canvas></canvas> 
Javascript

首先,我們要禁用頁面的滑鼠選中拖動的事件,就是不運行執行選中操作。

 
var bodyStyle = document.body.style; 
bodyStyle.mozUserSelect = ‘none‘; 
bodyStyle.webkitUserSelect = ‘none‘; 

接著我們定義圖片類,擷取canvas元素,並設定背景和位置屬性。我們在本例中用到兩張隨機照片,每次重新整理隨機一張圖片作為背景。

 
var img = new Image(); 
var canvas = document.querySelector(‘canvas‘); 
canvas.style.backgroundColor=‘transparent‘; 
canvas.style.position = ‘absolute‘; 
var imgs = [‘p_0.jpg‘,‘p_1.jpg‘]; 
var num = Math.floor(Math.random()*2); 
img.src = imgs[num]; 

然後進入主體,當檢測到圖片載入完的時候,首先定義一些屬性和函數,函數layer()用來繪製一個灰色的正方形,eventDown()定義了按下事件eventUp()定義了鬆開事件,eventMove()定義了移動事件,其中當按下時,擷取座標位移,並通過arc(x, y, 10, 0, Math.PI * 2)來繪製小圓點。

 
img.addEventListener(‘load‘, function(e) { 
    var ctx; 
    var w = img.width, 
        h = img.height; 
    var offsetX = canvas.offsetLeft, 
        offsetY = canvas.offsetTop; 
    var mousedown = false; 
 
    function layer(ctx) { 
        ctx.fillStyle = ‘gray‘; 
        ctx.fillRect(0, 0, w, h); 
    } 
 
    function eventDown(e){ 
        e.preventDefault(); 
        mousedown=true; 
    } 
 
    function eventUp(e){ 
        e.preventDefault(); 
        mousedown=false; 
    } 
 
    function eventMove(e){ 
        e.preventDefault(); 
        if(mousedown) { 
             if(e.changedTouches){ 
                 e=e.changedTouches[e.changedTouches.length-1]; 
             } 
             var x = (e.clientX + document.body.scrollLeft || e.pageX) - offsetX || 0, 
                 y = (e.clientY + document.body.scrollTop || e.pageY) - offsetY || 0; 
             with(ctx) { 
                 beginPath() 
                 arc(x, y, 10, 0, Math.PI * 2);//繪製圓點 
                 fill(); 
             } 
        } 
    } 
 
    //... 
}); 

最後,通過canvas調用以上函數,繪製圖形,並且偵聽觸控及滑鼠事件,調用相應的函數,請看代碼:

 
img.addEventListener(‘load‘, function(e) { 
    //..接上段代碼 
    canvas.width=w; 
    canvas.height=h; 
    canvas.style.backgroundImage=‘url(‘+img.src+‘)‘; 
    ctx=canvas.getContext(‘2d‘); 
    ctx.fillStyle=‘transparent‘; 
    ctx.fillRect(0, 0, w, h);//繪製矩形 
    layer(ctx); 
 
    ctx.globalCompositeOperation = ‘destination-out‘; 
 
    canvas.addEventListener(‘touchstart‘, eventDown); 
    canvas.addEventListener(‘touchend‘, eventUp); 
    canvas.addEventListener(‘touchmove‘, eventMove); 
    canvas.addEventListener(‘mousedown‘, eventDown); 
    canvas.addEventListener(‘mouseup‘, eventUp); 
    canvas.addEventListener(‘mousemove‘, eventMove); 

使用HTML5實現刮刮卡效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.