canvas写简易画板和canvas写简易刮刮乐(pc端)

简易画板:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        #cvs{
            border: 1px solid #ddd;
        }
    </style>
</head>
<body>
    <canvas id="cvs" width="500" height="400"></canvas>
    <button id="btn">画画</button>
    <button id="btn1">清除</button>
    <button id="btn2">重画</button>
</body>
<script>
    var cvs=document.getElementById("cvs");
    var ctx=cvs.getContext("2d");
    var btn=document.getElementById("btn")
    var btn1=document.getElementById("btn1")
    var btn2=document.getElementById("btn2")
    btn.onclick=function(){
        cvs.onmousedown=function(e){
        ctx.beginPath();
        ctx.moveTo(e.clientX-cvs.offsetLeft,e.clientY-cvs.offsetTop);
        document.onmousemove=function(e){
            ctx.lineTo(e.clientX-cvs.offsetLeft,e.clientY-cvs.offsetTop);
            ctx.strokeStyle="red";
            ctx.lineWidth=3;
            ctx.stroke()
        }
        document.onmouseup=function(){
            document.onmousedown=null;
            document.onmousemove=null;
        }
      }
    }

    btn1.onclick=function(){
        cvs.onmousedown=function(){
            document.onmousemove=function(e){
                ctx.clearRect(e.clientX-cvs.offsetLeft,e.clientY-cvs.offsetTop,10,10)
            }
        }
        document.onmouseup=function(){
            document.onmousedown=null;
            document.onmousemove=null;
        }
    }
    btn2.onclick=function(){
        ctx.clearRect(0,0,cvs.width,cvs.height);
    }
</script>
</html>

简易pc端刮刮乐:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
    *{
        padding: 0;
        margin: 0;
    }
    #cvs{
        border: 1px solid #ddd;
        position: absolute;
        z-index: 9;
        left: 20%;
        top: 20%;
    }
    #con{
        width: 200px;
        height: 100px;
        position: absolute;
        font-size: 24px;
        line-height: 100px;
        text-align: center;
        left: 20%;
        top: 20%;
    }
    </style>
</head>
<body>
    <canvas id="cvs" width="200" height="100">你的浏览器不支持canvas插件,请更换后重试</canvas>
    <div id="con">大奖</div>
</body>
<script>
    var cvs=document.getElementById("cvs");
    var con=document.getElementById("con");
    var ctx=cvs.getContext("2d");
    var arr=["一等奖","劳斯莱斯幻影","谢谢惠顾","辣条一包","对象一个","谢谢惠顾","谢谢惠顾","谢谢惠顾","谢谢惠顾"]
    var i=Math.floor(Math.random()*arr.length);
    con.innerHTML=arr[i];
    ctx.beginPath();
    ctx.fillStyle="#f1f1f1";
   
    ctx.fillRect(0,0,200,100);
    ctx.closePath();

    ctx.beginPath();
    ctx.font="bold 40px 宋体";
    ctx.strokeStyle="#000";
    ctx.strokeText("刮刮乐",50,60);
    ctx.beginPath();

    cvs.onmousedown=function(){
        document.onmousemove=function(e){ 
            var x=e.clientX-cvs.offsetLeft;
            var y=e.clientY-cvs.offsetTop;
            ctx.clearRect(x,y,10,10)
        }
        document.onmouseup=function(){
            document.onmousedown=null;
            document.onmousemove=null;
        }
    }

</script>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值