【发布时间】:2020-09-09 21:01:50
【问题描述】:
编辑
这是一个 JSFiddle,其中“tail”函数的代码被注释掉了。Solar System JSFiddle
我正在研究这个物体,它有一个围绕中心质量运行的物体。这很完美。
我现在正在尝试在行星后面添加一条尾随线或“尾巴”。 我的尾部对象如下所示:
function Tail(maxLength){
this.points = [];
this.maxLength = maxLength;
this.addPoint = function(point){
for(var i = Math.min(maxLength, this.points.length); i < maxLength; i++){
this.points[i] = this.points[i - 1];
}
this.points[0] = point;
}
this.draw = function(ctx){
for(var i = 1; Math.min(maxLength, this.points.length); i++){
if(i < maxLength - 20){
ctx.globalAlpha = 1;
} else {
ctx.globalAlpha = (this.maxLength - i) / 20;
}
ctx.beginPath();
ctx.moveTo(this.points[i - 1].x, this.points[i - 1].y);
ctx.lineTo(this.points[i].x, this.points[i].y);
ctx.stroke();
}
ctx.globalAlpha = 1;
}
}
addPoint 函数接受一个看起来像 '{x: currentX, y: currentY} 的对象 currentX 和 currentY 是对象被调用时的 x 和 y 点。
我不知道如何将点添加到点数组中,然后根据这些坐标进行绘制。
【问题讨论】:
-
尝试为我们创建一个Minimal, Reproducible Example,以便我们运行您的代码并在必要时提出建议。
-
循环条件看起来很奇怪。您是否打算从当前位置倒数?我不应该递减循环变量吗?考虑使用 Array.slice()
-
我更新了帖子以提供指向运行模拟代码的 jsfiddle 的链接。
标签: javascript canvas orbital-mechanics