简体   繁体   中英

Canvas animated line over border

so i have a canvas element that displays some values, i use window.requestAnimationFrame to update the canvas. Now i would like to add an animated line that would loop over the canvas. This is how my canvas looks and this is the code for the line so far:

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;

But the problem occurs when the line gets to the edge, it doesn't make a seamless transition, not sure how should i proceed.

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;                                                     
}                                                                               

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM