canvas点击切换

<!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>
    <script src="jquery-1.8.3.min.js"></script>
</head>
<style>
    *{margin: 0;
    padding: 0;}
body{text-align: center}
canvas{border: 1px solid;
}

#can2{margin-top: -500px;
 display: none;}
</style>
<body>
    <canvas id="can1" width="800" height=" 500"></canvas>
    <canvas id="can2" width="800" height=" 500"></canvas>
    <button id="but">变脸</button>
    <script>
    let ctx=document.getElementById("can1").getContext("2d");
    let ctx1=document.getElementById("can1").getContext("2d");
    ctx.beginPath();
ctx.arc(400,250,200,0,2*Math.PI);
ctx1.strokeStyle='black';
ctx.stroke();
ctx.closePath();

ctx.beginPath();
ctx.arc(300,200,50,0,2*Math.PI);
ctx.fill();
ctx.closePath();
 

ctx.beginPath();
ctx.arc(500,200,50,0,2*Math.PI);
ctx.fill();
ctx.closePath();


ctx.beginPath();
ctx.arc(400,300,50,0,Math.PI,false);
ctx.strokeStyle='red';
ctx.stroke();
ctx.closePath();
   $("#but").toggle(function(){
    ctx.clearRect(0,0,800,500);
    ctx.beginPath();
ctx.arc(400,250,200,0,2*Math.PI);
ctx1.strokeStyle='black';
ctx.stroke();
ctx.closePath();

ctx.beginPath();
ctx.arc(300,200,50,0,2*Math.PI);
ctx.fill();
ctx.closePath();
 

ctx.beginPath();
ctx.arc(500,200,50,0,2*Math.PI);
ctx.fill();
ctx.closePath();


ctx.beginPath();
ctx.arc(400,300,50,0,Math.PI,false);
ctx.strokeStyle='red';
ctx.stroke();
ctx.closePath();

   },
   function(){
    ctx.clearRect(0,0,800,500);
    ctx1.beginPath();
ctx1.arc(400,250,200,0,2*Math.PI);
ctx1.strokeStyle='black';
ctx1.stroke();
ctx1.closePath();

ctx1.beginPath();
ctx1.arc(300,200,50,0,2*Math.PI);
ctx1.fill();
ctx1.closePath();
 

ctx1.beginPath();
ctx1.arc(500,200,50,0,2*Math.PI);
ctx1.fill();
ctx1.closePath();


ctx1.beginPath();
ctx1.arc(400,300,50,0,Math.PI,true);
ctx1.strokeStyle='red';
ctx1.stroke();
ctx1.closePath()
   })
    </script>
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值