As part of my Draw Something Solver, I’ve been working with HTML 5 Canvas objects quite a bit. As I want my application to work on all iPhones, I need to support both regular and retina displays. As I need to detect the letters on the image, I really need the images to be of the same size, however a regular image is 320×480, whereas a retina image is 640×960.
The eagle eyed amongst you will see that this is a factor of 2 difference between the two.
The Canvas Context object provides a scale() method, that you pass in x and y scaling factors into.
So if I have a Canvas object called screenshotCanvas, and an Image object called screenshotImage I want to scale down by half, I can use the following JavaScript code.
screenshotCanvasContext = screenshotCanvas.getContext('2d'); screenshotCanvasContext.scale(0.5, 0.5); screenshotCanvasContext.drawImage(screenshotImage, 0, 0, screenshotImage.width, screenshotImage.height);
If I want to double the image in size, I just change the scale factor to 2.
screenshotCanvasContext = screenshotCanvas.getContext('2d'); screenshotCanvasContext.scale(2, 2); screenshotCanvasContext.drawImage(screenshotImage, 0, 0, screenshotImage.width, screenshotImage.height);