Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

MooTools Flashlight Effect

26 Responses »

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.

Discussion

  1. March 30, 2009 @ 8:55 am

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

  2. March 30, 2009 @ 8:58 am

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

  3. alelo
    March 30, 2009 @ 9:18 am

    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

  4. alelo
    March 30, 2009 @ 9:27 am

    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

  5. salih gedİk
    March 30, 2009 @ 10:07 am

    Cool. ;)

  6. March 30, 2009 @ 11:45 am

    haha this is dope. Very clever~

  7. March 30, 2009 @ 12:47 pm

    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

  8. alelo
    March 30, 2009 @ 12:56 pm

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

  9. March 30, 2009 @ 4:41 pm

    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’.

  10. March 30, 2009 @ 5:21 pm

    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.

  11. March 30, 2009 @ 6:57 pm

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

  12. March 30, 2009 @ 10:31 pm

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

  13. March 30, 2009 @ 10:42 pm

    Good catch everyone — updated.

  14. gopinathan
    March 31, 2009 @ 8:45 am

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

  15. March 31, 2009 @ 9:38 am

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

  16. April 7, 2009 @ 8:32 pm

    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 ).

  17. June 2, 2009 @ 3:17 pm

    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

  18. July 10, 2009 @ 11:33 am

    Cool …Very useful to me…
    Thanks ..

  19. chris martin
    July 28, 2009 @ 11:11 am

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

  20. ankie
    July 30, 2009 @ 12:29 pm

    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

  21. ivan
    August 5, 2009 @ 2:59 am

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

    http://www.stonewall.co.za

  22. August 27, 2009 @ 5:02 pm

    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.

  23. dina
    September 1, 2009 @ 1:32 pm

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

  24. January 2, 2010 @ 1:44 pm

    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

  25. August 26, 2010 @ 6:27 pm

    Thanks for this tutorial dave. I separated the code into external files. I couldn’t get the flashlight to work. I think the problem is with the calling of the Mootools. How do I call Mootools in an HTML document?

    I used to call the style sheet and it works fine.

    I think this will be a useful feature for a new product launch page where teaser images are shown for one to see the status of the product. Just fun.

    Will appreciate a reply from anybody who can help me out.

  26. August 26, 2010 @ 6:29 pm

    Second paragraph should have been: I used “” to call the style sheet.

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!