canvas之刮刮卡效果

单页面实现刮刮卡效果。。

HTML页面:

 

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>刮刮卡效果</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
    <style>
        .content,.cover{width:400px; height:400px; position:absolute; left:50%; top:50%; margin:-200px 0 0 -200px;}
        .content{ font-size:48px; line-height:400px; text-align:center;}
        h3{ text-align:center; line-height:200px;}
    </style>


</head>


<body>
<h3>添喜你中奖了!!!</h3>
<div class="content" >中奖啦~!</div>
<canvas id="cover" class="cover" width="400" height="400"></canvas>
</body>


<script>
    var isdown = false,
            cover = document.getElementById("cover"),

            covercanvas = cover.getContext("2d");
    //
    covercanvas.fillStyle="transparent";
    covercanvas.fillRect(0,0,400,400);

    function fillter( canvas ){
        canvas.fillStyle="#ccc";
        canvas.fillRect(0,0,400,400);
    }
    function isDown(e){
        e.preventDefault();
        isdown=true;
    }
    function isUp(e){
        isdown=false;
    }
    function draw( e ){
        e.preventDefault();
        if(isdown){
            if(e.changedTouches){
                e=e.changedTouches[e.changedTouches.length-1];
            }


            var _height= parseInt((window.innerHeight-400)/2),
                    _width= parseInt((window.innerWidth-400)/2),
                    touchTop=e.clientY - _height,
                    touchLeft=e.clientX - _width;

            with(covercanvas){
                beginPath();
                arc(touchLeft, touchTop, 10, 0, Math.PI * 2);
                fill();
            }
        }
//alert(touchTop);
    }
    fillter(covercanvas);
    covercanvas.globalCompositeOperation = 'destination-out';
    cover.addEventListener('touchstart',isDown);
    cover.addEventListener('touchmove',draw);
    cover.addEventListener('touchend',isUp);
    cover.addEventListener('mousemove',draw);
    cover.addEventListener('mousedown',isDown);
    cover.addEventListener('mouseup',isUp);

</script>
</html>

 

欢迎关注技术开发分享录:http://fenxianglu.cn/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天空还下着雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值