Rewrite to allow targeting to a specific DOM element
[raphael] / plugins / raphael.shadow.js
1 /*!
2  * Raphael Shadow plugin 0.3
3  *
4  * Copyright (c) 2008 - 2009 Dmitry Baranovskiy (http://raphaeljs.com)
5  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
6  */
7 Raphael.shadow = function (x, y, w, h, options) {
8     // options format: {
9     //     size: 0..1, shadow size
10     //     color: "#000", placeholder colour
11     //     stroke: "#000", placeholder stroke colour
12     //     shadow: "#000", shadow colour
13     //     target: "someID" | htmlElement
14     //     r: 5, radius of placeholder rounded corners
15     // }
16     options = options || {};
17     var t = ~~(size * .3 + .5),
18         size = (options.size || 1) * 10,
19         color = options.color || "#fff",
20         stroke = options.stroke || color,
21         shadowColor = options.shadow || "#000",
22         target = options.target || null,
23         R = options.r == null ? 3 : options.r,
24         s = size,
25         b = size * 2,
26         r = b + s,
27         rg = this.format("r{0}-{0}", shadowColor),
28         rect = "rect",
29         none = "none",
30         res,
31         set;
32
33         if (target) {
34             res = this(target, w + (x = s) * 2, h + (y = t) + b);
35         } else {
36             res = this(x - s, y - t, w + (x = s) * 2, h + (y = t) + b);
37         }
38
39         set = res.set(
40             res.rect(x - s, y - t, b + s, h + y + b).attr({stroke: none, fill: this.format("180-{0}-{0}", shadowColor), opacity: 0, "clip-rect": [x - s + 1, y - t + r, b, h + y + b - r * 2 + .9]}),
41             res.rect(x + w - b, y - t, b + s, h + y + b).attr({stroke: none, fill: this.format("0-{0}-{0}", shadowColor), opacity: 0, "clip-rect": [x + w - s + 1, y - t + r, b, h + y + b - r * 2]}),
42             res.rect(x + b - 1, y + h - s, w + b, b + s).attr({stroke: none, fill: this.format("270-{0}-{0}", shadowColor), opacity: 0, "clip-rect": [x + b, y + h - s, w + b - r * 2, b + s]}),
43             res.rect(x + s - 1, y - t, w + b, b + s).attr({stroke: none, fill: this.format("90-{0}-{0}", shadowColor), opacity: 0, "clip-rect": [x + b, y - t, w + b - r * 2, s + t + 1]}),
44             res.circle(x + b, y + h - s, r).attr({stroke: none, fill: rg, opacity: 0, "clip-rect": [x - s, y + h - s + .999, r, r]}),
45             res.circle(x + w - b, y + h - s, r).attr({stroke: none, fill: rg, opacity: 0, "clip-rect": [x + w - b, y + h - s, r, r]}),
46             res.circle(x + b, y - t + r, r).attr({stroke: none, fill: rg, opacity: 0, "clip-rect": [x - s, y - t, r, r]}),
47             res.circle(x + w - b, y - t + r, r).attr({stroke: none, fill: rg, opacity: 0, "clip-rect": [x + w - b, y - t, r, r]}),
48             res.rect(x, y, w, h, R).attr({fill: color, stroke: stroke})
49         );
50
51     return set[0].paper;
52 };