2
I need to draw several images on several canvas, so I would like to do a generic function that allows me to pass the arguments to draw the positioned images.
I thought about the following generic function:
var drawIt = new function(CanvasCtx,drawObj,x,y) {
this.CanvasCtx = CanvasCtx;
this.drawObj = drawObj;
this.x = x;
this.y = y;
this.drawObj.onload = function(){
this.CanvasCtx.drawImage(this.drawObj,this.x,this.y)
}
}
So when I set the canvas and the image:
var canvas = document.getElementById("mycanvas");
var ctx = canvas.getContext("2d");
var Image1 = new Image();
Image1.src = "https://www.google.com/images/srpr/logo11w.png";
I could set a function to start on body load:
function drawScreen(){
drawIt(ctx,Image1,100,100);
}
But I get the error that the onload property cannot be assigned to an unidentified object.
Can someone help me build this generic function?
There are some problems in the code: what is the value assigned here?
this.y = ;
That onenew
is not necessary:new function(...)
– Gustavo Rodrigues
Interestingly browsers run the code from
new function
but ignore the arguments. The nodejs creates an object that cannot be called:typeof drawIt === 'object'
– Gustavo Rodrigues
I found out why the
new function
is valid: https://gist.github.com/qgustavor/9336704– Gustavo Rodrigues
@Gustavorodrigues
new function(){}
is the same thing asnew referenciaDeFuncaoQualquer()
. The point is that it runs immediately. Basically, this code defines the constructor and invokes it right away.– bfavaretto
That’s kind of what I put in gist.
– Gustavo Rodrigues