BoxShadow.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.drawing.annotations.BoxShadow"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.drawing.annotations.BoxShadow"] = true;
  8. dojo.provide("dojox.drawing.annotations.BoxShadow");
  9. dojox.drawing.annotations.BoxShadow = dojox.drawing.util.oo.declare(
  10. // summary:
  11. // Creates a box shadow under solid objects. Can change the
  12. // shadow direction, color, size, and intensity. Can center
  13. // the shadow and make it a Glow.
  14. // description:
  15. // This is a psuedo shadow, created by duplicating the
  16. // original stencil and increasing the line weight while
  17. // reducing the opacity. Therefore it will not work with
  18. // text. Also won't look very good if the Stencil has no
  19. // fill or is transparent. Can't do knockouts or inner
  20. // shadows. Currently can't do paths - while doable, it
  21. // will most likely choke IE into certain death.
  22. //
  23. function(/*Object*/options){
  24. this.stencil = options.stencil;
  25. this.util = options.stencil.util;
  26. this.mouse = options.stencil.mouse;
  27. this.style = options.stencil.style;
  28. var shadowDefaults = {
  29. // summary:
  30. // When passing a shadow object into a stencil, that shadow
  31. // object will be mixed in with these defaults.
  32. //
  33. // size: Number, mult: Number
  34. // These two props work together. Both affect the size and quality
  35. // of the shadow. size affects the actual size and mult affects the
  36. // lineWidths that overlap to make the shadow. Generally you want a
  37. // bigger 'size' than 'mult'. The defaults are good for a shadow, but
  38. // you will want to increase them when making a glow.
  39. // TODO: Make this more clear or use other properties.
  40. size:6,
  41. mult:4,
  42. // alpha: Float
  43. // Affects the alpha of the shadow. Because this is multiple shapes
  44. // overlapped, you want much less than you may think. .1 is pretty
  45. // dark and . is black. Higher numbers also give a sharper edge.
  46. alpha:.05,
  47. // place: String
  48. // Tells the position of the shadow:
  49. // B: bottom
  50. // T: top
  51. // L: left
  52. // R: right
  53. // C: center, or a glow
  54. // Can be used in combinations such as BR, BL, L, T, etc. 'C' should
  55. // be used by itself.
  56. place:"BR",
  57. // color: String
  58. // The color of the shadow or glow.
  59. color:"#646464"
  60. }
  61. delete options.stencil;
  62. this.options = dojo.mixin(shadowDefaults, options);
  63. this.options.color = new dojo.Color(this.options.color)
  64. this.options.color.a = this.options.alpha;
  65. switch(this.stencil.shortType){
  66. case "image":
  67. case "rect":
  68. this.method = "createForRect"; break;
  69. case "ellipse":
  70. this.method = "createForEllipse"; break;
  71. case "line":
  72. this.method = "createForLine"; break;
  73. case "path":
  74. this.method = "createForPath"; break;
  75. // path is a bit of a hassle. Plus I think in IE it would be
  76. //slower than the political process. Maybe TODO.
  77. case "vector":
  78. this.method = "createForZArrow"; break;
  79. default:
  80. console.warn("A shadow cannot be made for Stencil type ", this.stencil.type);
  81. }
  82. if(this.method){
  83. this.render();
  84. this.stencil.connectMult([
  85. [this.stencil, "onTransform", this, "onTransform"],
  86. this.method=="createForZArrow"?[this.stencil, "render", this, "render"]:[this.stencil, "render", this, "onRender"],
  87. [this.stencil, "onDelete", this, "destroy"]
  88. ]);
  89. }
  90. },
  91. {
  92. showing:true,
  93. render: function(){
  94. if(this.container){
  95. this.container.removeShape();
  96. }
  97. this.container = this.stencil.container.createGroup();
  98. this.container.moveToBack();
  99. var o = this.options,
  100. size = o.size,
  101. mult = o.mult,
  102. d = this.method == "createForPath"
  103. ? this.stencil.points
  104. : this.stencil.data,
  105. r = d.r || 1,
  106. p = o.place,
  107. c = o.color;
  108. this[this.method](o, size, mult, d, r, p, c);
  109. },
  110. hide: function(){
  111. if(this.showing){
  112. this.showing = false;
  113. this.container.removeShape();
  114. }
  115. },
  116. show: function(){
  117. if(!this.showing){
  118. this.showing = true;
  119. this.stencil.container.add(this.container);
  120. }
  121. },
  122. createForPath: function(o, size, mult, pts, r, p, c){
  123. var sh = size * mult / 4,
  124. shy = /B/.test(p) ? sh : /T/.test(p) ? sh*-1 : 0,
  125. shx = /R/.test(p) ? sh : /L/.test(p) ? sh*-1 : 0;
  126. var closePath = true;
  127. for(var i=1;i<=size;i++){
  128. var lineWidth = i * mult;
  129. //var rect = this.container.createLine({x1:d.x1+shx, y1:d.y1+shy, x2:d.x2+shx, y2:d.y2+shy})
  130. // .setStroke({width:lineWidth, color:c, cap:"round"})
  131. if(dojox.gfx.renderer=="svg"){
  132. var strAr = [];
  133. dojo.forEach(pts, function(o, i){
  134. if(i==0){
  135. strAr.push("M " + (o.x+shx) +" "+ (o.y+shy));
  136. }else{
  137. var cmd = o.t || "L ";
  138. strAr.push(cmd + (o.x+shx) +" "+ (o.y+shy)); // Z + undefined works here
  139. }
  140. }, this);
  141. if(closePath){
  142. strAr.push("Z");
  143. }
  144. this.container.createPath(strAr.join(", ")).setStroke({width:lineWidth, color:c, cap:"round"})
  145. }else{
  146. // Leaving this code for VML. It seems slightly faster but times vary.
  147. var pth = this.container.createPath({}).setStroke({width:lineWidth, color:c, cap:"round"})
  148. dojo.forEach(this.points, function(o, i){
  149. if(i==0 || o.t=="M"){
  150. pth.moveTo(o.x+shx, o.y+shy);
  151. }else if(o.t=="Z"){
  152. closePath && pth.closePath();
  153. }else{
  154. pth.lineTo(o.x+shx, o.y+shy);
  155. }
  156. }, this);
  157. closePath && pth.closePath();
  158. }
  159. }
  160. },
  161. createForLine: function(o, size, mult, d, r, p, c){
  162. var sh = size * mult / 4,
  163. shy = /B/.test(p) ? sh : /T/.test(p) ? sh*-1 : 0,
  164. shx = /R/.test(p) ? sh : /L/.test(p) ? sh*-1 : 0;
  165. for(var i=1;i<=size;i++){
  166. var lineWidth = i * mult;
  167. this.container.createLine({x1:d.x1+shx, y1:d.y1+shy, x2:d.x2+shx, y2:d.y2+shy})
  168. .setStroke({width:lineWidth, color:c, cap:"round"})
  169. }
  170. },
  171. createForEllipse: function(o, size, mult, d, r, p, c){
  172. var sh = size * mult / 8,
  173. shy = /B/.test(p) ? sh : /T/.test(p) ? sh*-1 : 0,
  174. shx = /R/.test(p) ? sh*.8 : /L/.test(p) ? sh*-.8 : 0;
  175. for(var i=1;i<=size;i++){
  176. var lineWidth = i * mult;
  177. this.container.createEllipse({cx:d.cx+shx, cy:d.cy+shy, rx:d.rx-sh, ry:d.ry-sh, r:r})
  178. .setStroke({width:lineWidth, color:c})
  179. }
  180. },
  181. createForRect: function(o, size, mult, d, r, p, c){
  182. var sh = size * mult / 2,
  183. shy = /B/.test(p) ? sh : /T/.test(p) ? 0 : sh /2,
  184. shx = /R/.test(p) ? sh : /L/.test(p) ? 0 : sh /2;
  185. for(var i=1;i<=size;i++){
  186. var lineWidth = i * mult;
  187. this.container.createRect({x:d.x+shx, y:d.y+shy, width:d.width-sh, height:d.height-sh, r:r})
  188. .setStroke({width:lineWidth, color:c})
  189. }
  190. },
  191. arrowPoints: function(){
  192. // summary:
  193. // Creates data used to draw arrow head.
  194. //
  195. var d = this.stencil.data;
  196. var radius = this.stencil.getRadius();
  197. var angle = this.style.zAngle + 30;
  198. var pt = this.util.pointOnCircle(d.x1, d.y1, radius*.75, angle);
  199. var obj = {
  200. start:{
  201. x:d.x1,
  202. y:d.y1
  203. },
  204. x:pt.x,
  205. y:pt.y
  206. }
  207. var angle = this.util.angle(obj);
  208. var lineLength = this.util.length(obj);
  209. var al = this.style.arrows.length;
  210. var aw = this.style.arrows.width/3;
  211. if(lineLength<al){
  212. al = lineLength/2;
  213. }
  214. var p1 = this.util.pointOnCircle(obj.x, obj.y, -al, angle-aw);
  215. var p2 = this.util.pointOnCircle(obj.x, obj.y, -al, angle+aw);
  216. return [
  217. {x:obj.x, y:obj.y},
  218. p1,
  219. p2
  220. ];
  221. },
  222. createForZArrow: function(o, size, mult, pts, r, p, c){
  223. if(this.stencil.data.cosphi<1 || !this.stencil.points[0]){ return; }
  224. var sh = size * mult / 4,
  225. shy = /B/.test(p) ? sh : /T/.test(p) ? sh*-1 : 0,
  226. shx = /R/.test(p) ? sh : /L/.test(p) ? sh*-1 : 0;
  227. var closePath = true;
  228. for(var i=1;i<=size;i++){
  229. var lineWidth = i * mult;
  230. pts = this.arrowPoints();
  231. if(!pts){ return; }
  232. if(dojox.gfx.renderer=="svg"){
  233. var strAr = [];
  234. dojo.forEach(pts, function(o, i){
  235. if(i==0){
  236. strAr.push("M " + (o.x+shx) +" "+ (o.y+shy));
  237. }else{
  238. var cmd = o.t || "L ";
  239. strAr.push(cmd + (o.x+shx) +" "+ (o.y+shy)); // Z + undefined works here
  240. }
  241. }, this);
  242. if(closePath){
  243. strAr.push("Z");
  244. }
  245. this.container.createPath(strAr.join(", ")).setStroke({width:lineWidth, color:c, cap:"round"}).setFill(c);
  246. }else{
  247. // Leaving this code for VML. It seems slightly faster but times vary.
  248. var pth = this.container.createPath({}).setStroke({width:lineWidth, color:c, cap:"round"})
  249. dojo.forEach(pts, function(o, i){
  250. if(i==0 || o.t=="M"){
  251. pth.moveTo(o.x+shx, o.y+shy);
  252. }else if(o.t=="Z"){
  253. closePath && pth.closePath();
  254. }else{
  255. pth.lineTo(o.x+shx, o.y+shy);
  256. }
  257. }, this);
  258. closePath && pth.closePath();
  259. }
  260. var sp = this.stencil.points;
  261. this.container.createLine({x1:sp[0].x, y1:sp[0].y, x2:pts[0].x, y2:pts[0].y})
  262. .setStroke({width:lineWidth, color:c, cap:"round"});
  263. }
  264. },
  265. onTransform: function(){
  266. this.render();
  267. },
  268. onRender: function(){
  269. this.container.moveToBack();
  270. },
  271. destroy: function(){
  272. if(this.container){
  273. this.container.removeShape();
  274. }
  275. }
  276. }
  277. );
  278. }