为正在运行的物体创建一个“尾巴”

问题描述

修改

这是一个JSFiddle,其中注释了“ tail”函数的代码。Solar System JSFiddle

我有一个正在处理的物体,其中一个物体绕着中心质量运行。那很好。

small red planet orbiting sun

我现在正在尝试在该行星后面添加一条拖尾线或“尾巴”。 我的尾巴对象看起来像这样:

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点。

我不知道如何将点添加到点数组,然后根据这些坐标进行绘制。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)