Finn Rudolph

24
Oct
2010
CanvasReflection.js - Client-side image reflections using the HTML5 canvas element

CanvasReflection, JavaScript, Canvas von Finn Rudolph

i_blog_20101024_CanvasReflection.jpg

I was always interested if the pixel based canvas element is fast enough to provide client-side image reflections. This is my first experiment on that topic. You can try the demo and download the whole thing from GitHub.

Reflections are pretty simple with the canvas element (18 lines of code): Just create a canvas and set the height to the source image height plus the height of the reflection. Then draw the source image and a mirrored source image. Place a gradient over the mirrored image and set the globalCompositeOperation to destination-out.

The problem is, that if you want to scale the canvas you have to redo all the canvas drawing, since it is pixel based.

i_blog_20101024_CanvasReflectionPNGs.jpg
Icons by: FastIcon.com

Update: Also works nicely with eleven transparent PNG images.