用canvas写一个股票箭头上升的动态路径特效
时间: 2023-05-14 10:07:32 浏览: 262
canvas画箭头
好的,以下是一个简单的用canvas实现股票箭头上升的动态路径特效的代码:
```html
<canvas id="myCanvas"></canvas>
```
```javascript
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
// 设置画布大小
canvas.width = 400;
canvas.height = 400;
// 绘制箭头
function drawArrow(x, y, width, height) {
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + width / 2, y + height);
ctx.lineTo(x - width / 2, y + height);
ctx.closePath();
ctx.fill();
}
// 绘制路径
function drawPath() {
ctx.beginPath();
ctx.moveTo(50, 350);
ctx.lineTo(150, 250);
ctx.lineTo(250, 350);
ctx.strokeStyle = '#000';
ctx.lineWidth = 5;
ctx.stroke();
}
// 动态绘制箭头
let arrowX = 50;
let arrowY = 350;
let arrowWidth = 30;
let arrowHeight = 50;
let arrowDirection = 1;
function animateArrow() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPath();
drawArrow(arrowX, arrowY, arrowWidth, arrowHeight);
if (arrowX >= 250) {
arrowDirection = -1;
} else if (arrowX <= 50) {
arrowDirection = 1;
}
arrowX += arrowDirection * 5;
requestAnimationFrame(animateArrow);
}
animateArrow();
```
这段代码会在画布上绘制一条路径和一个箭头,箭头会沿着路径上下移动。
阅读全文