I am trying to make a pixel art themed game in HTML5 canvas, and as part of that I take 10x20 or so sized images and draw them onto the canvas with the following code:
ctx.drawImage(image, 20, 20, 100, 200);
However the canvas uses bicubic image scaling and hence the pixel art images look terrible at 2x and up. Is there a way to force canvas to use nearest neighbor scaling or possibly use a custom method to scale images? If not does that mean the images have to be scaled beforehand in something like Paint.net?
Choose any one of the following:
Via JavaScript:
Source: http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#image-smoothing
On Gecko, you'll need
Source: https://developer.mozilla.org/en/DOM/CanvasRenderingContext2D#Gecko-specific_attributes
On Webkit, you'll need
Source: https://bugs.webkit.org/show_bug.cgi?id=82804
I couldn't find information on support of this property on other browsers, so they probably don't support it.
Via CSS:
Another option is to use a set of CSS rules on the canvas. For example:
Source: https://developer.mozilla.org/en/CSS/image-rendering
Source: https://bugs.webkit.org/show_bug.cgi?id=56627
Via pixel routines:
Yet another option is to do it yourself using the canvas pixel manipulation routines: http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#pixel-manipulation. That's a lot more work, though.