简体   繁体   English

在将img.src绘制到画布时如何在不影响绘制对象的情况下替换img.src?

[英]How to alternate an img.src when drawing it to canvas without affecting drawn objects?

The JavaScript file is borrowed from this CodePen many thanks Jason (jsndks). 非常感谢Jason(jsndks),从此CodePen借来了JavaScript文件。

My draw function which has been edited hence the commented out section: 我的绘图功能已被编辑,因此被注释掉了:

proto.draw = function() {
    var img = new Image();

    if (!this.context) {
        return;
    }

    this.context.clearRect(0, 0, this.canvasWidth, this.canvasHeight);
    //this.context.strokeStyle = PARTICLE_COLOR;

    for(var i= 0; i < PARTICLE_QUANT; i++) {
        //img.src = 'http://www.clipartbest.com/cliparts/nTX/b6R/nTXb6RqTB.png';

        if (i%2 == 0){
            img.src = 'http://www.clipartbest.com/cliparts/nTX/b6R/nTXb6RqTB.png';
        }else {
            img.src = 'http://www.clker.com/cliparts/q/j/I/0/8/d/green-circle-icon-hi.png';
        }


        var particle = this.particles[i];
        //this.context.save();
        //this.context.beginPath();
        ////this.context.arc(particle.x, particle.y, ARC_RADIUS, 0, Math.PI * 2);
        //this.context.stroke();
        //this.context.restore();
        this.context.drawImage(img, particle.x , particle.y, 20 ,20);
    }

};

Any help would be appreciated. 任何帮助,将不胜感激。 My goal is a canvas partial animation with naughts and crosses floating in the scene. 我的目标是在场景中漂浮零星和十字架的画布局部动画。

Many thanks 非常感谢

Just got help from another source, all i had to do was move the "var img = new Image();" 刚从另一个来源得到帮助,我要做的就是移动“ var img = new Image();”。 inside the for loop. 在for循环中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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