画布动画边框线

时间:2019-09-25 15:58:24

标签: javascript animation canvas line

所以我有一个显示某些值的canvas元素,我使用window.requestAnimationFrame更新画布。现在,我想添加一条动画线,使其在画布上循环播放。 This is how my canvas looks,这是到目前为止的代码:

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;

但是当线到达边缘时会发生问题,它无法进行无缝过渡,不确定我应该如何进行。

1 个答案:

答案 0 :(得分:0)

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