Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animated image mask following mouse in HTML

If I change the cursor of a page into a 'flashlight' (say, a circle), and I want to reveal an image only when passing the light (the circle) over it, what would be the best way to go about this? Using css clip? But then it can only do rectangles, so I'd have to use canvas? Perhaps there's an easy way to intersect the two images?

like image 220
scottkf Avatar asked Feb 21 '23 00:02

scottkf


1 Answers

You can do this with a canvas easily.

Here's an example:

http://jsfiddle.net/gfZ5C/

On every mouse move, we clear the canvas, redraw the image, make a clipping region that is a circle cut out of a rectangle, and draw black on the entire canvas (which will draw only on the clipping region)

Make sense?

There are a lot of ways to achieve this effect and similar effects. You can also make much fancier light sources with a bit of canvas sorcery. See for instance my answer here: Canvas - Fill a rectangle in all areas that are fully transparent

like image 168
Simon Sarris Avatar answered Feb 26 '23 18:02

Simon Sarris