Reputation: 32374
So, I create a few images like this:
images = [];
images[0] = new Image();
images[0].src = "bla.jpg";
images[1] = new Image();
images[1].src = "bla1.jpg";
images[2] = new Image();
images[2].src = "bla2.jpg";
This isn't the actual loading algorithm, but the product is the same: I'm left with an array called "images", which, of course, contains a few images.
I'm making a game, and I need to resize the canvas, to fit the screen size of the player. I know how to do that, but now, I need to resize all of the images as well. I don't know how to do that.
My initial approach was this:
for (var i = 0; i < images.length; i ++)
{
images[i].width *= ratio; // newWidth/defaultWidth
images[i].height *= ratio;
}
I didn't notice a change, though, so I did this, just to be sure:
for (var i = 0; i < images.length; i ++)
{
images[i].width = 3; //I'd probably notice if every image in the game was shrunk to this size
images[i].height = 3;
}
This was even stranger. The images, again, remained unchanged, however, the game slowed a lot. The FPS went down to 0.5 or something similar.
Now, how do I go about resizing images?
EDIT: I do not, of course, wish to resize the images in real time like this:
context.drawImage(image, x, y, image.w*r, image.h*r);
Because in my experience, it greatly reduces the performance.
Upvotes: 0
Views: 355
Reputation: 12983
When you set the dimensions in Javascript, they should be assigned as strings. Numbers won't work. If you need to do calculations, then convert the result to a string.
images[i].width = (images[i].width*ratio).toString(); // newWidth/defaultWidth
images[i].height = (images[i].height*ratio).toString();
Upvotes: 1
Reputation: 819
Instead of setting width & height attributes, set the css properties instead:
var i, image;
for ( i = 0; i < images.length; i++ )
{
image = $( images[i] );
image.css( 'width', image.width() * ratio );
image.css( 'height', image.height() * ratio );
}
Upvotes: 2