canvas学习笔记【2021.10.25】

今天运用canvas实现了两个案例,一个是冒热气的杯子,一个是自动画圆。

一、冒热气的杯子

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        #canvas{
            display: block;
            margin: 100px auto;
            border: 5px black solid;
        }
    </style>
</head>
<body>
    <canvas id="canvas" width="1200px" height="800px">
    您的浏览器不支持canvas,请升级。
    </canvas>
</body>
<script>
    /** @type {HTMLCanvasElement} */ 
    var canvas = document.querySelector("#canvas");
    var ctx = canvas.getContext('2d');

    //四条气体
    ctx.beginPath();
    ctx.arc(500,200,25,Math.PI*1.5,Math.PI/2,true);
    ctx.lineWidth = 4;
    ctx.stroke();
    ctx.arc(500,250,25,Math.PI*1.5,Math.PI/2,false);
    ctx.lineWidth = 4;
    ctx.stroke();

    ctx.beginPath();
    ctx.arc(550,200,25,Math.PI*1.5,Math.PI/2,true);
    ctx.lineWidth = 4;
    ctx.stroke();
    ctx.arc(550,250,25,Math.PI*1.5,Math.PI/2,false);
    ctx.lineWidth = 4;
    ctx.stroke();

    ctx.beginPath();
    ctx.arc(600,200,25,Math.PI*1.5,Math.PI/2,true);
    ctx.lineWidth = 4;
    ctx.stroke();
    ctx.arc(600,250,25,Math.PI*1.5,Math.PI/2,false);
    ctx.lineWidth = 4;
    ctx.stroke();

    ctx.beginPath();
    ctx.arc(650,200,25,Math.PI*1.5,Math.PI/2,true);
    ctx.lineWidth = 4;
    ctx.stroke();
    ctx.arc(650,250,25,Math.PI*1.5,Math.PI/2,false);
    ctx.lineWidth = 4;
    ctx.stroke();
    
    //杯身
    ctx.beginPath();
    ctx.moveTo(440,325);
    ctx.lineTo(710,325);
    ctx.lineTo(710,575);
    ctx.lineTo(440,575);
    ctx.lineTo(440,325);
    ctx.strokeStyle = 'black';
    ctx.lineWidth = 4;
    ctx.fillStyle = 'red';
    ctx.fill();
    ctx.stroke();

    //把手
    ctx.beginPath();
    ctx.arc(712,450,60,Math.PI*1.5,Math.PI/2,false);
    ctx.strokeStyle = 'orange';
    ctx.lineWidth = 10;
    ctx.stroke();
</script>
</html>

二、自动画圆

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        #canvas{
            display: block;
            margin: 100px auto;
            border: 5px black solid;
        }
    </style>
</head>
<body>
    <canvas id="canvas" width="1200px" height="800px">
    您的浏览器不支持canvas,请升级。
    </canvas>
</body>
<script>
    /** @type {HTMLCanvasElement} */ 
    var canvas = document.querySelector("#canvas");
    var ctx = canvas.getContext('2d');

    ctx.arc(600,400,100,0,Math.PI*2,false);
    ctx.lineWidth = 10;
    ctx.stroke();
    ctx.closePath();

    let deg=0;
    function dawYuan(){
    ctx.beginPath();
    deg+=0.01;
    ctx.arc(600,400,90,0,Math.PI*(0+deg),false);
    ctx.strokeStyle = 'red';
    ctx.lineWidth = 10;
    ctx.stroke();
    if(deg==2){
     clearInterval(dawYuanJiShi);
    }
    }
    let dawYuanJiShi = setInterval(dawYuan,20);
</script>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值