【问题标题】:Is it possible to draw on the canvas from inside an object function?是否可以从对象函数内部在画布上绘制?
【发布时间】:2015-04-25 22:57:18
【问题描述】:

我在 JavaScript 中创建了一个对象,其中包含 context.drawImage() 所需的所有数据。那么是否可以调用这些值并在对象函数中运行 context.drawImage() ?

world = new Sprite(0, 0, 100, 100, 0.4, 0, 0, 0);
world.draw();

function Sprite(spriteX, spriteY, spriteW, spriteH, scale, positionX, positionY, direction)
            {
                this.imagePath = world_sprite;
                this.spriteX = spriteX;
                this.spriteY = spriteY;
                this.spriteW = spriteW;
                this.spriteH = spriteH;
                this.scale = scale;
                this.positionX = positionX;
                this.positionY = positionY;
                this.direction = direction;
                this.speed = 5;

                this.noGravity = false;
                this.direction = 0;

                //Physics stuff
                this.velX = 0;
                this.velY = 0;
                this.friction = 0.98;
            };
Sprite.prototype.draw = function()
            {
                context.drawImage(this.imagePath, this.spriteX, this.spriteY, this.spriteW, this.spriteH, this.positionX, this.positionY, this.spriteW * this.scale, this.spriteH * this.scale);
            };

【问题讨论】:

  • 当然可以。只要定义了您使用的所有属性。在您的简短示例中,我看不到 context 来自哪里。
  • 对于drawImage(),它看起来像是一个加载的图像对象,而不是路径。
  • this.imagePath 指的是world_sprite,它是一个图像对象。我看看它是否不喜欢this.imagePath
  • 不,这不是问题...
  • 好的,imagePath 是图像对象的错误名称。您没有评论 context 变量的来源。

标签: javascript oop object canvas prototype


【解决方案1】:

是的,您当然可以从对象函数内部在画布上绘图...

这段代码有问题:

var world_sprite=new Image();
world_sprite.src='someImage.png';

// This next line is executed too early!
// The code attempts (fails) to drawImage world_sprite
// because it has not fully loaded yet
world = new Sprite(0, 0, 100, 100, 0.4, 0, 0, 0);
world.draw();

浏览器总是异步加载图像。所以在上面的代码中,world_sprite.src='someImage.png'之后的那一行总是在图片完全加载之前执行。

您始终需要使用onload 回调给您的new Images 时间来加载

var world_sprite=new Image();
world_sprite.onload=function(){
    alert('I am displayed when world_sprite is fully loaded and ready to be used in `drawImage`);
};
world_sprite.src='someImage.png';

因此,您可以在代码中使用onload,如下所示:

// Create your Sprite "class"
function Sprite(spriteX, spriteY, spriteW, spriteH, scale, positionX, positionY, direction){
    this.imagePath = world_sprite;
    this.spriteX = spriteX;
    this.spriteY = spriteY;
    this.spriteW = spriteW;
    this.spriteH = spriteH;
    this.scale = scale;
    this.positionX = positionX;
    this.positionY = positionY;
    this.direction = direction;
    this.speed = 5;

    this.noGravity = false;
    this.direction = 0;

    //Physics stuff
    this.velX = 0;
    this.velY = 0;
    this.friction = 0.98;
};
Sprite.prototype.draw = function(){
    context.drawImage(this.imagePath, this.spriteX, this.spriteY, this.spriteW, this.spriteH, this.positionX, this.positionY, this.spriteW * this.scale, this.spriteH * this.scale);
};


// create and load world_sprite
var world_sprite=new Image();
world_sprite.onload=function(){
    // Only now is the world_sprite fully loaded and
    // ready to be used in drawImage.
    // So now create a new Sprite and do .draw()
    world = new Sprite(0, 0, 100, 100, 0.4, 0, 0, 0);
    world.draw();
};
world_sprite.src='someImage.png';

【讨论】:

  • 感谢您冗长而详细的回答。我没有提供足够代码的原因是因为我的程序有 600 行长......而且要显示与该问题相关的所有代码,将是相当多的。所以我把它浓缩了很多,错过了一些东西。不幸的是,这并没有解决我的问题。 :/ 我会继续研究这个。但是 +1 的好答案
猜你喜欢
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 2016-05-22
  • 2013-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-11
相关资源
最近更新 更多