Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  145] [ 7]  / answers: 1 / hits: 85009  / 13 Years ago, fri, september 30, 2011, 12:00:00

NOTE: This has to do with how existing canvas elements are rendered when scaled up, not to do with how lines or graphics are rendered onto a canvas surface. In other words, this has everything to do with interpolation of scaled elements, and nothing to do with antialiasing of graphics being drawn on a canvas. I'm not concerned with how the browser draws lines; I care about how the browser renders the canvas element itself when it is scaled up.






Is there a canvas property or browser setting I can change programmatically to disable interpolation when scaling <canvas> elements? A cross-browser solution is ideal but not essential; Webkit-based browsers are my main target. Performance is very important.



This question is most similar but does not illustrate the problem sufficiently. For what it's worth, I have tried image-rendering: -webkit-optimize-contrast to no avail.



The application will be a retro 8-bit styled game written in HTML5+JS to make it clear what I need.






To illustrate, here is an example. (live version)



Suppose I have a 21x21 canvas...



<canvas id='b' width='21' height='21'></canvas>


...which has css that makes the element 5 times larger (105x105):



canvas { border: 5px solid #ddd; }
canvas#b { width: 105px; height: 105px; } /* 5 * 21 = 105 */


I draw a simple 'X' on the canvas like so:



$('canvas').each(function () {
var ctx = this.getContext(2d);
ctx.moveTo(0,0);
ctx.lineTo(21,21);
ctx.moveTo(0,21);
ctx.lineTo(21,0);
ctx.stroke();
});


The image on the left is what Chromium (14.0) renders. The image on the right is what I want (hand-drawn for illustrative purposes).



Chrome A


More From » html

 Answers
3

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


[#89840] Thursday, September 29, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anabeldaliad

Total Points: 552
Total Questions: 107
Total Answers: 120

Location: Hungary
Member since Mon, Feb 21, 2022
2 Years ago
;