Javascript 画布动画边界线

Javascript 画布动画边界线,javascript,animation,canvas,line,Javascript,Animation,Canvas,Line,因此,我有一个canvas元素来显示一些值,我使用window.requestAnimationFrame来更新画布。现在我想添加一条动画线条,它将在画布上循环。这是目前为止该行的代码: this.ctx.lineWidth = 1; this.ctx.strokeStyle = 'rgba(47, 48, 63, 0.3)'; this.ctx.setLineDash([]); this.ctx.beginPath() this.ctx.rect(0, 0, this.canvasWidth

因此,我有一个canvas元素来显示一些值,我使用
window.requestAnimationFrame
来更新画布。现在我想添加一条动画线条,它将在画布上循环。这是目前为止该行的代码:

this.ctx.lineWidth = 1;
this.ctx.strokeStyle = 'rgba(47, 48, 63, 0.3)';
this.ctx.setLineDash([]);

this.ctx.beginPath()
this.ctx.rect(0, 0, this.canvasWidth, this.canvasHeight)
this.ctx.beginPath()
this.ctx.lineWidth = 5 * this.ratio;
this.ctx.strokeStyle = this._gradient

let topEdge = this._loaderX + this._loaderWidth;
this.ctx.moveTo(this._loaderX, 0);
this.ctx.lineTo(topEdge, 0);

let bottomLeftEdge = topEdge > this.canvasWidth ? topEdge - this.canvasWidth : -this._loaderWidth;
this.ctx.moveTo(this.canvasWidth, bottomLeftEdge)
this.ctx.lineTo(this.canvasWidth, bottomLeftEdge + this._loaderWidth)
this.ctx.stroke()

this._loaderX += 5;

但是问题发生在行到达边缘时,它不会进行无缝转换,不知道该如何继续。

很抱歉,我忘了在问题中添加javascript标记,我还需要行在整个框中进行完整循环
property vector2d firstPoint: Qt.vector2d(lineWidth / 2, 0)                     
property vector2d secondPoint: Qt.vector2d(width, lineWidth / 2)                
property int lineWidth: 5 * width / height                                      
property int _loaderWidth: width / 2                                            

onPaint:                                                                        
{                                                                               
    var ctx = getContext("2d")                                                  
    ctx.reset()                                                                 

    ctx.beginPath()                                                             
    ctx.lineWidth = lineWidth                                                   
    ctx.strokeStyle = "green"                                                   

    var topEdge = this.firstPoint.x + _loaderWidth;                             
    ctx.moveTo(this.firstPoint.x, lineWidth / 2);                               
    ctx.lineTo(topEdge, lineWidth / 2);                                         

    var bottomLeftEdge = topEdge > this.width ? topEdge - width : -_loaderWidth;
    ctx.moveTo(this.width - lineWidth / 2, bottomLeftEdge - lineWidth / 2)      
    ctx.lineTo(this.width - lineWidth / 2, bottomLeftEdge - _loaderWidth)       
    ctx.stroke()                                                                

    this.firstPoint.x += 5;                                                     
}