BoxShadow.js 9.0 KB

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