Canvas 刮刮乐

1、先在页面里创建一个canvas;

<canvas id='canvas' width="500" height="300"></canvas>

2、css样式

        #canvas{
            border: 1px solid gray;
            display: block;
            margin: 0 auto;
            background: url('img/0.jpg');
        } 

3、js完整代码:


        var ctx=document.getElementById('canvas').getContext('2d');
        ctx.fillStyle='black';
        ctx.fillRect(0,0,800,400);
        console.log("dsfndftnfgyj" ); 

        //这个属性设定了在画新图形时采用的遮盖策略,其值是一个标识12种遮盖方式的字符串
        ctx.globalCompositeOperation ='destination-out';//现有内容保持在新图形不重叠的地方。
        //鼠标按下
        canvas.onmousedown=function(){
            // console.log(hhhhh);
            //鼠标移动
            canvas.onmousemove=function(event){
                console.log(event.offsetX,event.offsetY);//获取鼠标在当前
                ctx.beginPath();
                ctx.arc(event.offsetX,event.offsetY,50,0,2*Math.PI,false);
                ctx.fillStyle='red';
                ctx.fill();
            }
        }//鼠标抬起
        canvas.onmouseup=function(){
            canvas.onmousemove=null;//清除移动
        }    

4、效果图:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值