Lightsource Enter some values!

Light & Shadow

... is a javascript framework to deal with the imagination of shadow throwing layers.

I was mad about drawing shaded images around divs with no good possibility to keep this styling stuff out of the document. CSS does not really work for that, so I decided to modify the DOM-tree after loading.

Make a document, set a class 'shadowThrowing' to some elements, set light parameters, and off we go

Thanks to the PROTOTYPE-Team for the perfect work on javascript.

And the funny thing is ... it moves There's some work to do on correct shadow calculations ...

How it works

Elements get the class 'shadowThrowing', their z-index is set so some good value. This document has a z-index of 10. The div named 'Lightsource' has 11. After loading a lightsource is generated

light = new Lightsource({
        intensity:0.5,
        spherical: {
                distance: 500,
                polar: 45,
                azimuth: 90
        },
        lightSize: 500
});
This sets up the position of the light in spherical coordinates. Origin is left upper corner. Intensity means the 'darkness' of the shadow. The Lightsize determines the 'twilight', it could also be set up with { width:500, height: 250 } which creates a asymmetric twilight.
Then the DOM-tree is examinated for elements with class-name 'shadowThrowing'. Each of them gets a 'shadow' by generating a div containing 9 absolutely positioned png-images. These images contain 4 corners and 4 borders with shading black to transparent and a black 'core-shadow'.
The shadows are inserted before the shadowThrowing-elements with the same z-index and are registered to the lightsource-instance.
After generating the lightsource is redrawn, which means repositioning and resizing of the shadow, and the twilight.

window.onresize = Lightsource.redraw;
All Lightsources are redrawn, in our example just one.

Is my Browser a fast Ray-tracing Numbercruncher?

No. It is just a trick and simple vector-calculations. Especially if the shadow of a higher z-index hits two different z-index levels you can see that it is just a rectangle. Also the calculations are related to z-index 0 so stacked shadows are not correct. So it's just a funny effect.

Just check out with the form or with the buttons.

What can be done

Make an absolute positioned div which can be resized with em-text and scaling images. Take scriptaculous, make a growing Effect.Resize and on the updates increase the elements z-index and call ithe elements shadow.shadowUpdate() method. . What do you see? An element lifting off the screen. (make sure that the lightsource is not too far away.)

Limitations and Todos

Documentation

Not written up to now. RTFS (read the fine source).

Version 0.1

Just little testing...

Download

Comments & Contact

Martin Kufner, mk@kufnerfutures.com