Text drop shadows can be used to improve headline legibility or simply for decorative purpose. In my latest project, I’ve had to use drop shadows just for the reasons above and ended up writing a Mootools class.
There are a few techniques to generate text drop shadows, all of which are based on creating a duplicate text layer to simulate a drop shadow, but not all of them are scalable; meaning that when user resizes web page text, using browser controls (ctr +/-,ctr + mouse scroll, etc), the drop shadow may not always follow the element it’s supposed to shadow. In the particular technique I’m using, however, a container div is used to hold both text elements, insuring a uniform look, regardless of the size. Although, it may not scale as nicely as one would expect, in certain situations…

You can take a look at the demo here. If that’s something you may be interested in, here is how to use it (also check out the demo’s source code for more examples):

<script type="text/javascript" src="js/mootools.v1.11.js"></script>
<script type="text/javascript" src="js/mootools.tds.js"></script>

<script type="text/javascript">
//<![CDATA[

	var Site = {

		start: function(){
			//simple ds with no options
			new TextDropShadow($('element'));

			//all elements with particular class name
			new TextDropShadow($$('element.classname'));

			//set ds color to #222 and it's opacity to 50%
			//when using apacity, you must specify background color or IE will output junk
			new TextDropShadow($('element'), {color:'#222', opacity: 0.5, background: '#fff'});

			//specify color and direction
			new TextDropShadow($('element'), {color:'#222', top:2, left:2});

			//shadow direction can take negative values as well
			new TextDropShadow($('element'), {color:'#222', top:-1, left:-1});
		}

	};

	window.addEvent('domready', Site.start);
//]]>
</script>

The script is available for download here


13 Responses to “Mootools text drop-shadow”

  • […] pr0digy.com » Mootools text drop-shadow 3 hours agoAs always - an addition to the mootools repotoire because you know how designers love […]

  • Gravatar
    May 13th, 2008 at 1:26 pm
    magoo101 says:

    Just wanted to let you know that you have a couple code errors in your example above.


    //specify color and direction
    new TextDropShadow($('element'), {color:'#222', top:2 left:2);

    …should actually be…


    //specify color and direction
    new TextDropShadow($('element'), {color:'#222', top:2, left:2});

  • Yep thanks - corrected the typo.

  • hi,

    will you touch it again to make it also working with mootools 1.2? would be fantastic! :-)
    thank you for your great work.

  • Hi Florian, I’m thinking of rewriting (adjusting) the scripts for 1.2. Though I can’t really give you the time frame on that :(

  • Gravatar
    August 7th, 2008 at 12:36 pm
    batman42ca says:

    I just converted it to 1.2. All you have to do is the following:

    Inside applyDropShadow(), do the following

    add this:

    var size = el.getSize();

    replace this

    height: el.getSize().size.y + offsetY,
    width: el.getSize().size.x + offsetX

    with this

    height: el.size.y + offsetY,
    width: el.size.x + offsetX

    and replace this
    el.remove();

    with this
    el.destroy();

  • Thanks for that, Batman :) I’ll be converting most of the scripts to 1.2, soon :)

  • I am kind of frustrated, even before batman42ca’s post I was tinkering with things to try and get it working with 1.2 and kept getting this:


    TextDropShadow is not defined

    start()
    E()mootools-1.2-core... (line 56)
    extend(undefined)mootools-1.2-core... (line 57)
    fireEvent(function())mootools-1.2-core... (line 173)
    fireEvent("domready", undefined, undefined)mootools-1.2-core... (line 174)
    onAdd()mootools-1.2-core... (line 248)
    C()mootools-1.2-core... (line 168)
    [Break on this error] new TextDropShadow($$('.textshadow'), {color:'#fff', top:1, left:1})...

    even after the changes posted I still get the undefined error.

    I have the exact same instantiation as the demo, save my elements switched in under the object's, and it blows up every time.

    for some reason the instantiation of this class looks wierd...

    here is my page's script init:

    var Site = {

    start: function(){
    new TextDropShadow($$('.textshadow'), {color:'#fff', top:1, left:1});
    }
    };

    window.addEvent('domready', Site.start);

    Any help here would be greatly appreciated

  • Hmm, looks like you forgot to include the drop shadow script… Essentially, the error means, that TextDropShadow object does not exist.

  • Wow, that was stupid, I was missing one ellipsis dot for the JS file path!

    But when that was fixed I did get one error that needed correction from what batman42ca posted:

    height: el.size.y + offsetY,
    width: el.size.x + offsetX

    his fix for this section should not have the el before size.x and size.y, I got a firebug break on that and it cleared right up when the el was erased.

    Anywho, sorry for that last dumbassical post, great script Alex and thanks for the help with the 1.2 convert Batman - Blamo, Pow!

    old school batman stuff, in case you didn’t get that :)

  • Gravatar
    August 8th, 2008 at 11:56 am
    batman42ca says:

    Daniel,

    Thanks for spotting the typo. The corrected changes to convert from 1.1 to 1.2 are:

    Inside applyDropShadow(), do the following

    add this:


    var size = el.getSize();

    replace this


    height: el.getSize().size.y + offsetY,
    width: el.getSize().size.x + offsetX

    with this


    height: size.y + offsetY,
    width: size.x + offsetX

    and replace this


    el.remove();

    with this


    el.destroy();

  • I wonder if the same technique can be used to add glow effect. Some browsers will not display the CSS glow effect you know.

  • It’s doable. Personally, I don’t see much use for it.

Leave a Reply


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>