MooTools Flashlight Effect

Written by David Walsh on Monday, March 30, 2009


This article may feature code that is no longer best practice in MooTools.
Click here to learn what has changed to make your code framework-compatible.

Another reason that I love Twitter so much is that I’m able to check out what fellow developers think is interesting. Chris Coyier posted about a flashlight effect he found built with jQuery. While I agree with Chris that it’s a little corny, it is a neat effect so I ported it over to MooTools.

The Imagery

Flashlight

The CSS

	body { background:#00022a url(flashlight.jpg) 50% 50% no-repeat; }

The MooTools Javascript

window.addEvent('domready',function() {
	$(document.body).addEvent('mousemove',function(e) {
		this.setStyle('background-position',[e.page.x - 250,e.page.y - 250]);
	});
});

What’s really impressive about this effect is how little code is involved. While I wouldn’t recommend this for a commercial website, it doesn’t have some fun value.


Epic Discussion

Commenter Avatar March 30 / #
Timothy says:

I don’t know when this would be useful. Probably never. But it still is pretty interesting.

David Walsh March 30 / #

Yeah, probably not the most useful thing in the world, but fun none the less.

Commenter Avatar March 30 / #
Alelo says:

this can be usefull for smal background images i think, or if u want to highlighting some spots in text @ mouseover, for example a line from a long text

Commenter Avatar March 30 / #
Alelo says:

ps: didnt work right in FF3.1 beta wind/vis if u scrool is see, scrooled down a little bit and the background image is not on mouse position (cant post image because curser is not on screen

Commenter Avatar March 30 / #
Salih GEDİK says:

Cool. ;)

Commenter Avatar March 30 / #

haha this is dope. Very clever~

Commenter Avatar March 30 / #

This is a sweet MooTools effect. It seems that Chris code is running smoother within my browser rather then yours. Your background image is cooler though David. :DD

Commenter Avatar March 30 / #
Alelo says:

ps2nd: in the new safari beta it has the same bug as FF3.1b

Commenter Avatar March 30 / #

Nice one, very creative!
BTW, You should add scroll offset to the position.
And MooTools Element#setStyle supports getting an array of integers so you could do

element.setStyle(“background-position”,[x,y]);

without concatenating ‘px’.

Commenter Avatar March 30 / #
Will says:

Agreed; not useful, but pretty cool. Ran into the same problem as a few people above, but it looks like Elad has suggested a fix.

Commenter Avatar March 30 / #

Agreed with Elad. David, try changing ‘e.client’ to ‘e.page’.

David Walsh March 30 / #

Thanks for the tips everyone. I put about 5 minutes into this whole thing. I’ll update as soon as I get the chance.

David Walsh March 30 / #

Good catch everyone — updated.

Commenter Avatar March 31 / #
gopinathan says:

we have download mootool.js and then its not working properly the flash light strucking some where

David Walsh March 31 / #

Oh, and by the way everyone, I know this is a total “webmaster” technique. I would never use it. Hahaha.

Commenter Avatar April 07 / #

Hey David, Neat script.

Is there a way that you can do this with a DIV or Image rather than the body ?

I have this script I am working on, http://dev.lendrum.co.nz/image/index.php?l=&t=180&x=500&y=180

and want to put a box over the image on mouse over. ( as a target crop box ).

Commenter Avatar June 02 / #

Actually, I think it’s totally useful for a ‘coming soon’ or ‘under construction’ site – thanks a bunch David!

I’m using it here: http://www.thecircuitsound.com

Commenter Avatar July 10 / #
vamsi says:

Cool …Very useful to me…
Thanks ..

Commenter Avatar July 28 / #
Chris Martin says:

This would be great for posting spoilers on IMDB or similar.

Commenter Avatar July 30 / #
Ankie says:

Seems like the links don’t hide in the darkness in Chrome and FF3…hope u can fix this. And thanks a lot for this

Commenter Avatar August 05 / #
Ivan says:

Here is an interesting real world example of this effect on a South African digital agency called Stonewall+:

http://www.stonewall.co.za

Commenter Avatar August 27 / #
vonholdt says:

Wow, I wrote that page months ago just for grins and put it on the CSS_Tricks forum. Glad to hear that people like the idea, though I’m the first to admit it has little practical application.

Commenter Avatar September 01 / #
dina says:

I can imagine the effect utilized on kids’ site, count the cows, or where is daddy’s key for example :P.

Commenter Avatar January 02 / #

David, Hi. Thanks for making this available. I’m interested in modifying the javascript to to trigger upon right click. I’m using the jQuery version. Any solutions? Thanks. Rob

Be Heard!

I want to hear what you have to say! Share your comments and questions below.

Name*:
Email*:
Website:  


© David Walsh 2007-2010. Contact David Walsh. Powered by the remarkable MooTools javascript framework.