美文网首页
canvas移动图形

canvas移动图形

作者: 姜治宇 | 来源:发表于2022-08-28 19:30 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <canvas id="canvas" width="300" height="200" style="border:1px dashed gray;">
        </canvas>
        <button id="btn">移动</button>
    </body>
    </html>
    <script>
    var cnv = document.getElementById('canvas');
    var cxt = cnv.getContext('2d');
    var count = 0;
    cxt.fillStyle = 'blue';
    cxt.fillRect(30,30,50,50);
    
    document.getElementById('btn').onclick = function(){
        cxt.clearRect(0,0,cnv.width,cnv.height);//清除画布
        cxt.translate(10,10);//平移
        cxt.scale(1.5,1.5);//放大
        cxt.rotate(count * Math.PI / 180); //旋转
        cxt.fillStyle = 'blue';
        cxt.fillRect(30,30,50,50);//画出图形
        count+=10;
    }
    </script>
    
    
    GIF8.gif

    相关文章

      网友评论

          本文标题:canvas移动图形

          本文链接:https://www.haomeiwen.com/subject/rjrknrtx.html