img.width/height inside constructor returns zero
up vote
0
down vote
favorite
I have...
var img = new Image();
img.src = 'img.png';
function Class(img){
this.img = img;
console.log(this.img.width); //0
}
var instance = new Class(img);
The console log from the code above returns 0 regardless of the image size.
If I put the console log in the update function and have it generate every frame, it does return the right value
Element.prototype.update = function(){
console.log(this.img.width); //581
}
But I don't want it to return the width every frame. I just want it once during construction. Any idea why img.width/height is always returning 0 inside the constructor?
javascript
add a comment |
up vote
0
down vote
favorite
I have...
var img = new Image();
img.src = 'img.png';
function Class(img){
this.img = img;
console.log(this.img.width); //0
}
var instance = new Class(img);
The console log from the code above returns 0 regardless of the image size.
If I put the console log in the update function and have it generate every frame, it does return the right value
Element.prototype.update = function(){
console.log(this.img.width); //581
}
But I don't want it to return the width every frame. I just want it once during construction. Any idea why img.width/height is always returning 0 inside the constructor?
javascript
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have...
var img = new Image();
img.src = 'img.png';
function Class(img){
this.img = img;
console.log(this.img.width); //0
}
var instance = new Class(img);
The console log from the code above returns 0 regardless of the image size.
If I put the console log in the update function and have it generate every frame, it does return the right value
Element.prototype.update = function(){
console.log(this.img.width); //581
}
But I don't want it to return the width every frame. I just want it once during construction. Any idea why img.width/height is always returning 0 inside the constructor?
javascript
I have...
var img = new Image();
img.src = 'img.png';
function Class(img){
this.img = img;
console.log(this.img.width); //0
}
var instance = new Class(img);
The console log from the code above returns 0 regardless of the image size.
If I put the console log in the update function and have it generate every frame, it does return the right value
Element.prototype.update = function(){
console.log(this.img.width); //581
}
But I don't want it to return the width every frame. I just want it once during construction. Any idea why img.width/height is always returning 0 inside the constructor?
javascript
javascript
asked Nov 8 at 4:01
snoozerviii
31
31
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Because the loading of an image resource is always asynchronous. You need to wait for its onload
event:
var img = new Image();
img.src = 'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png';
function Class(img){
this.img = img;
img.onload = e => {
console.log(this.img.width);
};
}
var instance = new Class(img);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Because the loading of an image resource is always asynchronous. You need to wait for its onload
event:
var img = new Image();
img.src = 'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png';
function Class(img){
this.img = img;
img.onload = e => {
console.log(this.img.width);
};
}
var instance = new Class(img);
add a comment |
up vote
2
down vote
accepted
Because the loading of an image resource is always asynchronous. You need to wait for its onload
event:
var img = new Image();
img.src = 'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png';
function Class(img){
this.img = img;
img.onload = e => {
console.log(this.img.width);
};
}
var instance = new Class(img);
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Because the loading of an image resource is always asynchronous. You need to wait for its onload
event:
var img = new Image();
img.src = 'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png';
function Class(img){
this.img = img;
img.onload = e => {
console.log(this.img.width);
};
}
var instance = new Class(img);
Because the loading of an image resource is always asynchronous. You need to wait for its onload
event:
var img = new Image();
img.src = 'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png';
function Class(img){
this.img = img;
img.onload = e => {
console.log(this.img.width);
};
}
var instance = new Class(img);
var img = new Image();
img.src = 'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png';
function Class(img){
this.img = img;
img.onload = e => {
console.log(this.img.width);
};
}
var instance = new Class(img);
var img = new Image();
img.src = 'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png';
function Class(img){
this.img = img;
img.onload = e => {
console.log(this.img.width);
};
}
var instance = new Class(img);
answered Nov 8 at 4:06
Kaiido
37.7k45597
37.7k45597
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201382%2fimg-width-height-inside-constructor-returns-zero%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown