Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / HTML5-Canvas实例:刮刮乐游戏

实现方法: (1)利用canvas画布,fillRect()描绘出一个矩形(不是透明),定位盖在某个标签如div上面(这个标签写着中奖的信息) (2)globalCompositeOperation = "destination-out";利用此属性,手指划过画布,arc(x,y, radius, 0, Math.PI*2, true)绘画圆形,那么这个圆形会把之前描绘出的矩形穿透,看到div标签的内容globalCompositeOperation属性: globalCompositeOperation 属性设置或返回如何将一个源(新的)图像绘制到目标(已有)的图像上。 源图像 = 您打算放置到画布上的绘图。 目标图像 = 您已经放置在画布上的绘图。
描述
source-over默认。在目标图像上显示源图像。
source-atop在目标图像顶部显示源图像。源图像位于目标图像之外的部分是不可见的。
source-in在目标图像中显示源图像。只有目标图像内的源图像部分会显示,目标图像是透明的。
source-out在目标图像之外显示源图像。只会显示目标图像之外源图像部分,目标图像是透明的。
destination-over在源图像上方显示目标图像。
destination-atop在源图像顶部显示目标图像。源图像之外的目标图像部分不会被显示。
destination-in在源图像中显示目标图像。只有源图像内的目标图像部分会被显示,源图像是透明的。
destination-out在源图像外显示目标图像。只有源图像外的目标图像部分会被显示,源图像是透明的。
lighter显示源图像 + 目标图像。
copy显示源图像。忽略目标图像。
source-over使用异或操作对源图像与目标图像进行组合。
如下图,蓝色是目标图像(就是矩形),红色就是源图像(即手指划过的圆形)  从图中可以看出,globalCompositeOperation = "destination-out"就是我们要的属性例子完整代码:<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>www.linuxidc.com</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <style type="text/css">
    .wraper{width: 300px;height: 100px;position: relative;margin: 150px auto;}
    .inner{width: 300px;height: 100px;background: #AA0707;color: #fff;font-size: 36px;line-height: 100px;text-align: center;}
    #j_canvas{position: absolute;left: 0;top: 0;}
    </style>
</head>
<body>
    <div class="wraper">
        <div class="inner">恭喜您中奖www.linuxidc.com</div>
        <canvas id="j_canvas" width="300" height="100"></canvas>
    </div>
    <script type="text/javascript">    var scratchGame = (function(){
        var target = document.getElementById("j_canvas"),
            ctx = target.getContext("2d"),
            w = target.width,
            h = target.height,
            radius = 15,
            target_offset_x = target.getBoundingClientRect().left,
            target_offset_y = target.getBoundingClientRect().top,
            isTouch = false;   
        return{
            init:function(){
                ctx.fillStyle = "#999";
                ctx.fillRect(0, 0,w,h);
                //属性设置或返回如何将一个源(新的)图像绘制到目标(已有)的图像上。
                ctx.globalCompositeOperation = "destination-out";
                // 事件
                target.addEventListener("touchstart", this.eventDown,false);
                target.addEventListener("touchmove", this.eventMove,false);
                target.addEventListener("touchend", this.eventUp,false);
            },
            eventDown:function(e){
                e.preventDefault();
                isTouch = true;
            },
            eventUp:function(e){
                e.preventDefault();
                isTouch = false;
            },
            eventMove:function(e){
                e.preventDefault();
                if(!isTouch||!e.touches.length) return;                var touch = e.touches[0],
                    x = touch.pageX - target_offset_x,
                    y = touch.pageY - target_offset_y;                ctx.beginPath();
                ctx.arc(x,y, radius, 0, Math.PI*2, true); 
                ctx.fill();               
            }        }
    })();
    scratchGame.init();
    </script>
</body>
</html>--------------------------------------分割线 --------------------------------------HTML5 地理位置定位(HTML5 Geolocation)原理及应用 http://www.linuxidc.com/Linux/2012-07/65129.htmHTML5移动开发即学即用(双色) PDF+源码 http://www.linuxidc.com/Linux/2013-09/90351.htmHTML5入门学习笔记 http://www.linuxidc.com/Linux/2013-09/90089.htmHTML5移动Web开发笔记 http://www.linuxidc.com/Linux/2013-09/90088.htmHTML5 开发中的本地存储的安全风险 http://www.linuxidc.com/Linux/2013-06/86486.htm《HTML5与CSS3权威指南》及相配套源码 http://www.linuxidc.com/Linux/2013-02/79950.htm关于 HTML5 令人激动的 10 项预测 http://www.linuxidc.com/Linux/2013-02/79917.htmHTML5与CSS3实战指南 PDF http://www.linuxidc.com/Linux/2013-02/79910.htm--------------------------------------分割线 --------------------------------------本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-02/113521.htm