DoubleArrowAnnotation.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.sketch.DoubleArrowAnnotation"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.sketch.DoubleArrowAnnotation"] = true;
  8. dojo.provide("dojox.sketch.DoubleArrowAnnotation");
  9. dojo.require("dojox.sketch.Annotation");
  10. dojo.require("dojox.sketch.Anchor");
  11. (function(){
  12. var ta=dojox.sketch;
  13. ta.DoubleArrowAnnotation=function(figure, id){
  14. ta.Annotation.call(this, figure, id);
  15. this.transform={ dx:0, dy:0 };
  16. this.start={x:0, y:0};
  17. this.control={x:100, y:-50};
  18. this.end={x:200, y:0};
  19. this.textPosition={ x:0, y:0 };
  20. this.textOffset=6;
  21. this.textYOffset=10;
  22. this.textAlign="middle";
  23. this.startRotation=0;
  24. this.endRotation=0;
  25. // this.property('label',this.id);
  26. this.labelShape=null;
  27. this.pathShape=null;
  28. this.startArrow=null;
  29. this.startArrowGroup=null;
  30. this.endArrow=null;
  31. this.endArrowGroup=null;
  32. this.anchors.start=new ta.Anchor(this, "start");
  33. this.anchors.control=new ta.Anchor(this, "control");
  34. this.anchors.end=new ta.Anchor(this, "end");
  35. };
  36. ta.DoubleArrowAnnotation.prototype=new ta.Annotation;
  37. var p=ta.DoubleArrowAnnotation.prototype;
  38. p.constructor=ta.DoubleArrowAnnotation;
  39. p.type=function(){ return 'DoubleArrow'; };
  40. p.getType=function(){ return ta.DoubleArrowAnnotation; };
  41. p._rot=function(){
  42. // arrowhead rotation
  43. var opp=this.control.y-this.start.y;
  44. var adj=this.control.x-this.start.x;
  45. this.startRotation=Math.atan2(opp,adj);
  46. opp=this.end.y-this.control.y;
  47. adj=this.end.x-this.control.x;
  48. this.endRotation=Math.atan2(opp,adj);
  49. };
  50. p._pos=function(){
  51. // text position
  52. var offset=this.textOffset;
  53. // figure out the pull of the curve and place accordingly
  54. if(this.control.y<this.end.y){ offset*=-1; }
  55. else { offset+=this.textYOffset; }
  56. var ab={
  57. x:((this.control.x-this.start.x)*.5)+this.start.x,
  58. y:((this.control.y-this.start.y)*.5)+this.start.y
  59. };
  60. var bc={
  61. x:((this.end.x-this.control.x)*.5)+this.control.x,
  62. y:((this.end.y-this.control.y)*.5)+this.control.y
  63. };
  64. this.textPosition={
  65. x:((bc.x-ab.x)*.5)+ab.x,
  66. y:(((bc.y-ab.y)*.5)+ab.y)+offset
  67. };
  68. };
  69. p.apply=function(obj){
  70. if(!obj){ return; }
  71. if(obj.documentElement){ obj=obj.documentElement; }
  72. this.readCommonAttrs(obj);
  73. for(var i=0; i<obj.childNodes.length; i++){
  74. var c=obj.childNodes[i];
  75. if(c.localName=="text"){ this.property('label',c.childNodes.length?c.childNodes[0].nodeValue:''); }
  76. else if(c.localName=="path"){
  77. // the line
  78. var d=c.getAttribute('d').split(" ");
  79. var s=d[0].split(",");
  80. this.start.x=parseFloat(s[0].substr(1),10);
  81. this.start.y=parseFloat(s[1],10);
  82. s=d[1].split(",");
  83. this.control.x=parseFloat(s[0].substr(1),10);
  84. this.control.y=parseFloat(s[1],10);
  85. s=d[2].split(",");
  86. this.end.x=parseFloat(s[0],10);
  87. this.end.y=parseFloat(s[1],10);
  88. var stroke=this.property('stroke');
  89. var style=c.getAttribute('style');
  90. var m=style.match(/stroke:([^;]+);/);
  91. if(m){
  92. stroke.color=m[1];
  93. this.property('fill',m[1]);
  94. }
  95. m=style.match(/stroke-width:([^;]+);/);
  96. if(m){
  97. stroke.width=m[1];
  98. }
  99. this.property('stroke',stroke);
  100. }
  101. }
  102. };
  103. p.initialize=function(obj){
  104. // create, based on passed DOM node if available.
  105. var font=(ta.Annotation.labelFont)?ta.Annotation.labelFont:{family:"Times", size:"16px"};
  106. this.apply(obj);
  107. // calculate the other positions
  108. this._rot();
  109. this._pos();
  110. // rotation matrix
  111. var rot=this.startRotation;
  112. var startRot=dojox.gfx.matrix.rotate(rot);
  113. rot=this.endRotation;
  114. var endRot=dojox.gfx.matrix.rotateAt(rot, this.end.x, this.end.y);
  115. // draw the shapes
  116. this.shape=this.figure.group.createGroup();
  117. this.shape.getEventSource().setAttribute("id", this.id);
  118. //if(this.transform.dx||this.transform.dy){ this.shape.setTransform(this.transform); }
  119. this.pathShape=this.shape.createPath("M"+this.start.x+" "+this.start.y+"Q"+this.control.x+" "+this.control.y+" "+this.end.x+" "+this.end.y + " l0,0")
  120. //.setStroke(this.property('stroke'));
  121. this.startArrowGroup=this.shape.createGroup().setTransform({ dx:this.start.x, dy:this.start.y });
  122. this.startArrowGroup.applyTransform(startRot);
  123. this.startArrow=this.startArrowGroup.createPath();//"M0,0 l20,-5 -3,5 3,5 Z").setFill(this.property('fill'));
  124. this.endArrowGroup=this.shape.createGroup().setTransform(endRot);
  125. this.endArrow=this.endArrowGroup.createPath();//("M" + this.end.x + "," + this.end.y + " l-20,-5 3,5 -3,5 Z").setFill(this.property('fill'));
  126. this.labelShape=this.shape.createText({
  127. x:this.textPosition.x,
  128. y:this.textPosition.y,
  129. text:this.property('label'),
  130. align:this.textAlign
  131. })
  132. //.setFont(font)
  133. .setFill(this.property('fill'));
  134. this.labelShape.getEventSource().setAttribute('id',this.id+"-labelShape");
  135. this.draw();
  136. };
  137. p.destroy=function(){
  138. if(!this.shape){ return; }
  139. this.startArrowGroup.remove(this.startArrow);
  140. this.endArrowGroup.remove(this.endArrow);
  141. this.shape.remove(this.startArrowGroup);
  142. this.shape.remove(this.endArrowGroup);
  143. this.shape.remove(this.pathShape);
  144. this.shape.remove(this.labelShape);
  145. this.figure.group.remove(this.shape);
  146. this.shape=this.pathShape=this.labelShape=this.startArrowGroup=this.startArrow=this.endArrowGroup=this.endArrow=null;
  147. };
  148. p.draw=function(obj){
  149. this.apply(obj);
  150. this._rot();
  151. this._pos();
  152. // rotation matrix
  153. var rot=this.startRotation;
  154. var startRot=dojox.gfx.matrix.rotate(rot);
  155. rot=this.endRotation;
  156. var endRot=dojox.gfx.matrix.rotateAt(rot, this.end.x, this.end.y);
  157. this.shape.setTransform(this.transform);
  158. this.pathShape.setShape("M"+this.start.x+" "+this.start.y+" Q"+this.control.x+" "+this.control.y+" "+this.end.x+" "+this.end.y + " l0,0")
  159. //.setStroke(this.property('stroke'));
  160. this.startArrowGroup.setTransform({ dx:this.start.x, dy:this.start.y }).applyTransform(startRot);
  161. this.startArrow.setFill(this.property('fill'));
  162. this.endArrowGroup.setTransform(endRot);
  163. this.endArrow.setFill(this.property('fill'));
  164. this.labelShape.setShape({
  165. x:this.textPosition.x,
  166. y:this.textPosition.y,
  167. text:this.property('label')
  168. })
  169. .setFill(this.property('fill'));
  170. this.zoom();
  171. };
  172. p.zoom=function(pct){
  173. if(this.startArrow){
  174. pct = pct || this.figure.zoomFactor;
  175. ta.Annotation.prototype.zoom.call(this,pct);
  176. var l=pct>1?20:Math.floor(20/pct), w=pct>1?5:Math.floor(5/pct),h=pct>1?3:Math.floor(3/pct);
  177. this.startArrow.setShape("M0,0 l"+l+",-"+w+" -"+h+","+w+" "+h+","+w+" Z");//.setFill(this.property('fill'));
  178. this.endArrow.setShape("M" + this.end.x + "," + this.end.y + " l-"+l+",-"+w+" "+h+","+w+" -"+h+","+w+" Z");
  179. }
  180. };
  181. p.getBBox=function(){
  182. var x=Math.min(this.start.x, this.control.x, this.end.x);
  183. var y=Math.min(this.start.y, this.control.y, this.end.y);
  184. var w=Math.max(this.start.x, this.control.x, this.end.x)-x;
  185. var h=Math.max(this.start.y, this.control.y, this.end.y)-y;
  186. return { x:x, y:y, width:w, height:h };
  187. };
  188. p.serialize=function(){
  189. var s=this.property('stroke');
  190. return '<g '+this.writeCommonAttrs()+'>'
  191. + '<path style="stroke:'+s.color+';stroke-width:'+s.width+';fill:none;" d="'
  192. + "M"+this.start.x+","+this.start.y+" "
  193. + "Q"+this.control.x+","+this.control.y+" "
  194. + this.end.x+","+this.end.y
  195. + '" />'
  196. + '<g transform="translate(' + this.start.x + "," + this.start.y + ") "
  197. + 'rotate(' + (Math.round((this.startRotation*(180/Math.PI))*Math.pow(10,4))/Math.pow(10,4)) + ')">'
  198. + '<path style="fill:'+s.color+';" d="M0,0 l20,-5, -3,5, 3,5 Z" />'
  199. + '</g>'
  200. + '<g transform="rotate('
  201. + (Math.round((this.endRotation*(180/Math.PI))*Math.pow(10,4))/Math.pow(10,4))
  202. + ", "+this.end.x+", "+this.end.y
  203. + ')">'
  204. + '<path style="fill:'+s.color+';" d="M'+this.end.x+","+this.end.y+' l-20,-5, 3,5, -3,5 Z" />'
  205. + '</g>'
  206. + '<text style="fill:'+s.color+';text-anchor:'+this.textAlign+'" font-weight="bold" '
  207. + 'x="' + this.textPosition.x + '" '
  208. + 'y="' + this.textPosition.y + '">'
  209. + this.property('label')
  210. + '</text>'
  211. + '</g>';
  212. };
  213. ta.Annotation.register("DoubleArrow");
  214. })();
  215. }