DoubleArrowAnnotation.js 7.8 KB

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