canvas移动图形

2022-08-28  本文已影响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
上一篇下一篇

猜你喜欢

热点阅读