[英]Javascript using callback to draw an image to canvas
I think I'm having a similar issue to this question , but I would like a little help understanding how to use a callback function and how to implement it for my particular situation. 我想我有一个类似的问题到了这个问题 ,但我想一点点帮助了解如何使用一个回调函数,以及如何实现它为我的特殊情况。 I'm trying to have an image get drawn to a canvas "on demand", ie any time a certain function is called. 我试图让图像“按需”绘制到画布上,即在任何时候调用某个函数。 The image is associated with an object I call a Block. 该图像与我称为“块”的对象相关联。 A Block has dimensions, coordinates, and a url. 块具有尺寸,坐标和URL。
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
function Block(x, y, width, height, url) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.url = url;
this.drawBlock = drawBlock;
function drawBlock() {
var blockSprite = new Image();
blockSprite.src = url;
blockSprite.onload = context.drawImage(blockSprite,x,y,width,height);
}
}
You can see a demo here . 您可以在此处查看演示。 Oddly enough, it works if I link to an image on the web, but fails if I link to an image on my disk. 奇怪的是,如果我链接到网络上的图像,它就可以工作,但是如果我链接到磁盘上的图像,它就不能工作。 I believe that this is because the onload event occurs before drawBlock is called, and that I need to use callbacks or promises to fix this, but I'm very new to Javascript so I'd like a little guidance here. 我相信这是因为onload事件发生在调用drawBlock之前,并且我需要使用回调或Promise来解决此问题,但是我对Javascript还是很陌生,所以我想在这里提供一些指导。 Any help or advice would be appreciated. 任何帮助或建议,将不胜感激。
Try it this way: 尝试这种方式:
function Block(x, y, width, height, url) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.url = url;
this.drawBlock=function drawBlock(){
var blockSprite = new Image();
blockSprite.src = url;
blockSprite.onload = function(){
context.drawImage(blockSprite,x,y,width,height);
}
}
}
var block=new Block(0,0,32,32,"house32x32.png");
block.drawBlock();
A callback is simply a function that is executed at a later time--usually after a long-running task has finally been completed. 回调只是一个在以后的时间执行的函数-通常在长时间运行的任务最终完成之后。
Loading an image is a good example. 加载图像是一个很好的例子。 Consider this code: 考虑以下代码:
function someCallbackFunction(){
alert("The image has finally been fully loaded");
}
var image=new Image();
image.onload=someCallbackFunction;
image.src="yourImage.png";
alert("This code is last, but will execute before the onload function");
It will actually execute in this order: 它实际上将按以下顺序执行:
The image object is created. 图像对象已创建。
var image=new Image(); var image = new Image();
The image object is told that when it's done fully loading the image it should execute the function called someCallbackFunction. 图像对象被告知,在完全加载图像之后,它应该执行称为someCallbackFunction的函数。
image.onload=someCallbackFunction image.onload = someCallbackFunction
The image is given a .src URL and begins the long process of downloading the image 为图像提供一个.src URL,并开始漫长的下载图像过程
image.src="yourImage.png"; image.src = “yourImage.png”;
Any code after image.src="yourImage.png" will execute. image.src =“ yourImage.png”之后的任何代码都将执行。
...Sometime later...After the image is fully loaded, the image object will execute someCallbackFunction() and the alert will sound. ...有时稍后...完全加载图像后,图像对象将执行someCallbackFunction()并发出警报。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.