Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a technical reason why canvas's drawImage() doesn't do sub-pixel rendering / antialiasing?

Ttl;dr: I need to move an image VERY slowly in canvas without obvious pixel by pixel movement. I need some sort of anti-aliasing.


Recently I was tasked with animating some "cloud" drawings horizontally in a webpage.

Easily enough I just threw the image into the DOM and used CSS3 transforms with a fallback to jQuery animation for browsers that don't support CSS transforms yet.

Everything looked pretty good. I had some clouds moving smoothly moving across the page.

Then I kept getting requests from the designer to slow the movement down...way down.

Because browsers don't do sub-pixel rendering for DOM objects the animation appears to run at 6 FPS.

So, I slap the image into canvas for some quick tests and find out that it doesn't do sub-pixel rendering automatically, either.

My quick canvas animation demo (it doesn't accurately time the movements, deal with it. :-p )

like image 535
David Murdoch Avatar asked Jun 14 '11 21:06

David Murdoch


2 Answers

For older browsers, you could animate a sprite. Create maybe 4 versions of your image, each shifted 0.25px to the left from the previous one. Paste those together in a sprite and then animate the background-position.

function moveClouds(n)
{
    var v = (n % 4) * 417;
    var h = Math.ceil(n / 4);
    clouds.style.backgroundPosition = h + " " + v;
}
like image 124
gilly3 Avatar answered Oct 05 '22 08:10

gilly3


This is a known Chrome issue as documented at http://code.google.com/p/chromium/issues/detail?id=7508

still no resolution or workaround for it..

like image 39
Gabriele Petrioli Avatar answered Oct 05 '22 08:10

Gabriele Petrioli