0

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.

1
  • If you use the browser to dynamically scale images like you are trying to do, rendering performance will be substantially reduced because the browser has to scale the image at draw time. Some browsers may be better than others at caching the pre-scaled bits, but some are not good at it and performance definitely deteriorates when you scale images like this. Commented Apr 6, 2012 at 16:23

2 Answers 2

2

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 );
}
Sign up to request clarification or add additional context in comments.

3 Comments

Note that you can also do this with plain JavaScript (if jQuery is not being used) as images[i].style.width = images[i].style.width * ratio.
Good point, I've been doing so much jQuery development lately I just defaulted to it :)
Perhaps there is another issue at hand, could you post a more comprehensive example? Also which browser are you testing with?
1

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();

3 Comments

Works for me (in Firefox, at least). I test things before writing answers.
@Bane: Well this is embarrassing. I've no idea what I did to get the result I got. The best I can come up with is jsfiddle.net/HVqKd/1 It is apparent though that Javascript Image objects are not related to the browser <img> element which renders images: dimensions can be set (and don't need .toString() -- what?!) but don't affect what the browser does. How are you displaying your Image objects?
Actually, no problem. I chose to go with fixed dimensions, it's simply much simpler. Can't please everyone...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.