Disable Interpolation when Scaling a

Last Updated: 2014-09-12

Is there a canvas property or browser setting I can change programmatically to disable interpolation when scaling elements?

The answer is maybe some day. For now, you’ll have to resort to hack-arounds to get what you want.


image-rendering

The working draft of CSS3 outlines a new property, image-rendering that should do what I want:

The image-rendering property provides a hint to the user-agent about what aspects of an image are most important to preserve when the image is scaled, to aid the user-agent in the choice of an appropriate scaling algorithm.

The specification outlines three accepted values: auto, crisp-edges, and pixelated.

pixelated:

When scaling the image up, the “nearest neighbor” or similar algorithm must be used, so that the image appears to be simply composed of very large pixels. When scaling down, this is the same as auto.

Standard? Cross-browser?

Since this is merely a working draft, there’s no guarantee that this will become standard. Browser support is currently spotty, at best.

The Mozilla Developer Network has a pretty thorough page dedicated to the current state of the art which I highly recommend reading.

The Webkit developers initially chose to tentatively implement this as -webkit-optimize-contrast, but Chromium/Chrome don’t seem to be using a version of Webkit that implements this.

Update: 2014-09-12

Chrome 38 now supports image-rendering: pixelated!

Firefox has a bug report open to get image-rendering: pixelated implemented, but -moz-crisp-edges works for now.

Solution?

The most cross-platform, CSS-only solution so far is thus:

canvas {
  image-rendering: optimizeSpeed;             /* Older versions of FF          */
  image-rendering: -moz-crisp-edges;          /* FF 6.0+                       */
  image-rendering: -webkit-optimize-contrast; /* Safari                        */
  image-rendering: -o-crisp-edges;            /* OS X & Windows Opera (12.02+) */
  image-rendering: pixelated;                 /* Awesome future-browsers       */
  -ms-interpolation-mode: nearest-neighbor;   /* IE                            */
}

Sadly this wont work on all major HTML5 platforms yet (Chrome, in particular).

Of course, one could manually scale up images using nearest-neighbor interpolation onto high-resolution canvas surfaces in javascript, or even pre-scale images server-side, but in my case this will be forbiddingly costly so it is not a viable option.

ImpactJS uses a texture pre-scaling technique to get around all this FUD. Impact’s developer, Dominic Szablewski, wrote a very in-depth article about this (he even ended up citing this question in his research).

See Simon’s answer for a canvas-based solution that relies on the imageSmoothingEnabled property (not available in older browsers, but simpler than pre-scaling and pretty widely-supported).

Live Demo

If you’d like to test the CSS properties discussed in the MDN article on canvas elements, I’ve made this fiddle which should display something like this, blurry or not, depending on your browser:
a 4:1 (64x64 to 256x256) image of an isometric pixel-art style TV

Leave a Comment