用canvas写一个股票路线上升的动态路径特效,要在起始端添加一个箭头箭头在上升的过程箭头要跟随上升
时间: 2023-05-15 11:07:21 浏览: 109
html5 canvas实现跟随鼠标旋转的箭头
非常感谢您的问题!以下是我为您提供的代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>股票路线上升的动态路径特效</title>
<style>
canvas {
border: 1px solid #ccc;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="500" height="300"></canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var x = 50;
var y = 250;
var arrowSize = 10;
var arrowAngle = Math.PI / 6;
var arrowX = x + arrowSize * Math.cos(arrowAngle);
var arrowY = y - arrowSize * Math.sin(arrowAngle);
var arrowDirection = 1;
var arrowSpeed = 2;
var lineColor = "#00f";
var lineWidth = 2;
var lineSpeed = 1;
var linePoints = [
{x: x, y: y},
{x: x + 50, y: y - 50},
{x: x + 100, y: y - 100},
{x: x + 150, y: y - 150},
{x: x + 200, y: y - 200},
{x: x + 250, y: y - 250},
{x: x + 300, y: y - 300},
{x: x + 350, y: y - 350},
{x: x + 400, y: y - 400},
{x: x + 450, y: y - 450},
{x: x + 500, y: y - 500}
];
function drawLine() {
ctx.beginPath();
ctx.moveTo(linePoints[0].x, linePoints[0].y);
for (var i = 1; i < linePoints.length; i++) {
ctx.lineTo(linePoints[i].x, linePoints[i].y);
}
ctx.strokeStyle = lineColor;
ctx.lineWidth = lineWidth;
ctx.stroke();
}
function drawArrow() {
ctx.beginPath();
ctx.moveTo(arrowX, arrowY);
ctx.lineTo(arrowX + arrowSize * Math.cos(arrowAngle), arrowY + arrowSize * Math.sin(arrowAngle));
ctx.lineTo(arrowX - arrowSize * Math.cos(arrowAngle), arrowY + arrowSize * Math.sin(arrowAngle));
ctx.closePath();
ctx.fillStyle = lineColor;
ctx.fill();
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawLine();
drawArrow();
if (arrowDirection == 1) {
arrowY -= arrowSpeed;
if (arrowY <= linePoints[0].y) {
arrowDirection = -1;
}
} else {
arrowY += arrowSpeed;
if (arrowY >= y) {
arrowDirection = 1;
}
}
for (var i = 0; i < linePoints.length; i++) {
linePoints[i].x += lineSpeed;
}
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```
这是一个使用 canvas 绘制股票路线上升的动态路径特效的示例。在起始端添加了一个箭头,箭头会跟随上升的过程移动。您可以根据需要调整箭头的大小、颜色和移动速度等参数。希望对您有所帮助!
阅读全文