程序员前端学习笔记

canvas的transform(3)

2019-10-10  本文已影响0人  _南宫逸轩

canvas的transform的一个实例:旋转矩形

canvas的transform.gif
<!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>
        body {
            background: gray;
            text-align: center;
        }
    </style>
    <script>
        window.onload = function () {
            let c1 = document.getElementsByTagName('canvas')[0];
            let gd = c1.getContext('2d');
            function d2a(n) {//角度转弧度
                return n * Math.PI / 180;
            }
            function a2d(n) {//弧度转角度
                return n * 180 / Math.PI;
            }
            let r=0;

            requestAnimationFrame(next);

            function next() {
                
                gd.clearRect(0, 0, c1.width, c1.height);

                gd.save();//保存当前的画布状态
                gd.translate(500, 275);
                gd.rotate(d2a(r++));
                gd.strokeStyle='orange';
                gd.strokeRect(-100, -75, 200, 150);
                gd.restore();//恢复上一次保存的画布状态

                requestAnimationFrame(next);
            }
        }
    </script>
</head>
<body>
    <canvas width="800" height="600" style="background:white;"></canvas>
</body>
</html>

下面这个是可以转多个矩形的:

<!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>
        body {
            background: gray;
            text-align: center;
        }
    </style>
    <script>
        window.onload = function () {
            let c1 = document.getElementsByTagName('canvas')[0];
            let gd = c1.getContext('2d');

            function d2a(n) {//角度转弧度
                return n * Math.PI / 180;
            }
            function a2d(n) {//弧度转角度
                return n * 180 / Math.PI;
            }

            let datas = [
                { x: 300, y: 300, w: 200, h: 100, s: 1 },
                { x: 200, y: 100, w: 100, h: 50, s: 1 },
                { x: 250, y: 50, w: 50, h: 100, s: 5 }
            ];//矩形数据,s表示speed,旋转速度


            requestAnimationFrame(next);
            let r=0;
            function next() {
                gd.clearRect(0, 0, c1.width, c1.height);//清除画布
                
                datas.forEach(data => {
                    
                    gd.save();//保存当前的画布状态

                    gd.translate(data.x, data.y);
                    gd.rotate(d2a(r++*data.s/10));//旋转速度在这里调
                    gd.strokeStyle = 'orange';
                    gd.strokeRect(-data.w / 2, -data.h / 2, data.w, data.h);
                    gd.restore();//恢复上一次保存的画布状态

                });
                requestAnimationFrame(next);
            }

        }
    </script>
</head>
<body>
    <canvas width="800" height="600" style="background:white;"></canvas>
</body>
</html>
canvas多个物体旋转.gif
上一篇下一篇

猜你喜欢

热点阅读