Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better canvas motion blur

It's been asked before, but the accepted solution doesn't work for me (literally, nothing is blurring for me in the linked demo), and it's a bit of a kludge involving two canvas elements.

I'm currently using the "poor man's" motion blur technique, which basically involves blitting the source image to the canvas over and over, and dropping a semi-transparent rectangle the same color as the background on top after each iteration.

Here's a demo: http://jsfiddle.net/YmABP/

As you can see, it works nicely for the edges of the image, but the inner parts of the image don't end up blurring at all, and it looks terrible with images that have partial transparency.

Is there a better technique for motion blur? Ideally, I'd like to be able to do something like context.drawImage and pass an opacity parameter in, but AFAIK nothing like that exists. Some of the images may be hosted on third-party domains, so I won't have access to the individual pixel data. If it comes down to it, we can pull the images onto our server and then I could iterate over each pixel and draw it as a semi-transparent tiny rectangle, but this seems like overkill.

Does anyone know of a better motion blur solution, preferably one that I can use with remote images?

I doubt this matters, but for my current purposes, things only move upwards.

like image 548
Dagg Nabbit Avatar asked May 21 '12 22:05

Dagg Nabbit


People also ask

How can I improve my motion blur?

Camera SettingsUsing a slower shutter speed will increase blur; while a faster shutter speed will reduce blur. The key is to find the shutter setting that allows your subject to appear sharp, but the background to appear blurred. If your shutter speed is too slow, too much of your image will be blurred.

How do you put a blur on canvas?

You can experiment with blurring photos with any photo from our library or from your uploads. Simply select the photo, then click “filter” and “advanced options.” Slide to the right to blur, and to the left to sharpen.

What is the benefit of motion blur?

Motion blur in photography can imbue a still image with a sense of speed or convey a passage of time. When photographing objects in fast motion, such as vehicles or carnival rides as in this example image, the blur can turn the lights on the moving object into beautiful streaks of color.


1 Answers

Just set the globalAlpha property of the context before drawing your image repeatedly:

Demo: http://jsfiddle.net/qfEUt/

var img = new Image,
    ctx = document.querySelector('canvas').getContext('2d');
    ctx.globalAlpha = 0.1;

img.onload=function(){
  for (var y=0;y<10;++y) ctx.drawImage(img,0,y);
}
img.src = 'http://phrogz.net/tmp/gkhead-small.png';​
like image 168
Phrogz Avatar answered Sep 22 '22 13:09

Phrogz