js canvas 自定义飞行轨迹 ,线性流动

<!DOCTYPE html>
<html>
    <head>
        <style>
            canvas {
                border: 1px solid black;
            }
        </style>
    </head>
    <body>
        <canvas id="myCanvas" width="300" height="200"></canvas>
        <script>
            const canvas = document.getElementById('myCanvas');
            const ctx = canvas.getContext('2d');

            // 三个点的坐标
            const points = [
                { x: 25, y: 121 },
                { x: 135, y: 34 },
                { x: 259, y: 141 }
            ];

            let t = 0;
            const animationSpeed = 0.005;
            // let isAnimating = true;
            // const animationDuration = 3000; // 每次动画的持续时间(毫秒)


            function drawCurve() {
                // if (!isAnimating) return;

                ctx.clearRect(0, 0, canvas.width, canvas.height);

                // 绘制控制点
                ctx.fillStyle = 'blue';
                for (const point of points) {
                    ctx.beginPath();
                    ctx.arc(point.x, point.y, 5, 0, Math.PI * 2);
                    ctx.fill();
                }

                // 计算贝塞尔曲线上的坐标
                const x = (1 - t) * (1 - t) * points[0].x + 2 * (1 - t) * t * points[1].x + t * t * points[2].x;
                const y = (1 - t) * (1 - t) * points[0].y + 2 * (1 - t) * t * points[1].y + t * t * points[2].y;

                // 绘制移动的点
                ctx.fillStyle = 'red';
                ctx.beginPath();
                ctx.arc(x, y, 8, 0, Math.PI * 2);
                ctx.fill();

                // 绘制贝塞尔曲线
                ctx.strokeStyle = 'black';
                ctx.lineWidth = 2;
                ctx.beginPath();
                ctx.moveTo(points[0].x, points[0].y);
                ctx.quadraticCurveTo(points[1].x, points[1].y, points[2].x, points[2].y);
                ctx.stroke();

                t += animationSpeed;
                if (t > 1) {
                    t = 0;
                    // isAnimating = false; // 暂停动画

                    // setTimeout(() => {
                        // isAnimating = true; // 重新开始动画
                        // requestAnimationFrame(drawCurve);
                    // }, animationDuration);
                }

                requestAnimationFrame(drawCurve);
            }

            // 点击获取坐标
            canvas.addEventListener('click', (event) => {
                const rect = canvas.getBoundingClientRect();
                const mouseX = event.clientX - rect.left;
                const mouseY = event.clientY - rect.top;
                console.log('点击坐标:', mouseX, mouseY);
            });

            drawCurve();
        </script>
    </body>
</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
canvas元素本身是没有样式的,但可以通过CSS样式表来控制canvas元素的大小和位置。同时,canvas元素的绘制内容可以通过JavaScript来控制,因此可以通过编写JavaScript代码来实现自定义的样式效果。 以下是一些常用的自定义样式技巧: 1. 设置canvas元素的宽高属性。可以通过CSS样式表或JavaScript代码来设置canvas元素的宽高属性,以控制canvas元素的大小。例如: ```css canvas { width: 500px; height: 300px; } ``` ```javascript const canvas = document.querySelector('canvas'); canvas.width = 500; canvas.height = 300; ``` 2. 控制canvas元素的位置。可以通过CSS样式表来控制canvas元素的位置,例如: ```css canvas { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } ``` 这样可以将canvas元素居中显示在父元素中。 3. 绘制自定义形状。可以通过JavaScript代码来绘制自定义形状,例如: ```javascript const canvas = document.querySelector('canvas'); const ctx = canvas.getContext('2d'); ctx.beginPath(); ctx.moveTo(50, 50); ctx.lineTo(100, 100); ctx.lineTo(50, 150); ctx.closePath(); ctx.fillStyle = '#ff0000'; ctx.fill(); ``` 这段代码将在canvas元素中绘制一个三角形,并设置其填充颜色为红色。 4. 使用图片作为canvas的背景。可以在canvas元素中绘制图片,作为canvas的背景。 ```javascript const canvas = document.querySelector('canvas'); const ctx = canvas.getContext('2d'); const img = new Image(); img.onload = function() { ctx.drawImage(img, 0, 0); }; img.src = 'image.jpg'; ``` 这段代码将在canvas元素中绘制一张图片,并将其作为canvas的背景。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值