Path.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // wrapped by build app
  2. define("dojox/drawing/stencil/Path", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.drawing.stencil.Path");
  4. dojox.drawing.stencil.Path = dojox.drawing.util.oo.declare(
  5. // summary:
  6. // Creates a dojox.gfx Path based on points provided.
  7. //
  8. dojox.drawing.stencil._Base,
  9. function(options){
  10. dojo.disconnect(this._postRenderCon);
  11. },
  12. {
  13. type:"dojox.drawing.stencil.Path",
  14. closePath: true,
  15. baseRender:true,
  16. closeRadius:10,
  17. closeColor:{r:255,g:255,b:0,a:.5},
  18. /*=====
  19. StencilData: {
  20. // NOT SUPPORTED FOR PATH
  21. },
  22. StencilPoints: [
  23. // summary:
  24. // An Array of StencilPoint objects that describe the Stencil
  25. // 0: Object
  26. // First point
  27. // [1, 2, 3...] more points
  28. ],
  29. =====*/
  30. _create: function(/*String*/shp, /*Object*/sty){
  31. // summary:
  32. // Creates a dojox.gfx.shape based on passed arguments.
  33. // Can be called many times by implementation to create
  34. // multiple shapes in one stencil.
  35. //
  36. this.remove(this[shp]);
  37. if(!this.points.length){ return; }
  38. if(dojox.gfx.renderer=="svg"){
  39. // NOTE:
  40. // In order to avoid the Safari d="" errors,
  41. // we'll need to build a string and set that.
  42. var strAr = [];
  43. dojo.forEach(this.points, function(o, i){
  44. if(!o.skip){
  45. if(i==0){
  46. strAr.push("M " + o.x +" "+ o.y);
  47. }else{
  48. var cmd = (o.t || "") + " ";
  49. if(o.x===undefined){// Z + undefined works here, but checking anyway
  50. strAr.push(cmd);
  51. }else{
  52. strAr.push(cmd + o.x +" "+ o.y);
  53. }
  54. }
  55. }
  56. }, this);
  57. if(this.closePath){
  58. strAr.push("Z");
  59. }
  60. this.stringPath = strAr.join(" ");
  61. this[shp] = this.container.createPath(strAr.join(" ")).setStroke(sty);
  62. this.closePath && this[shp].setFill(sty.fill);
  63. }else{
  64. // Leaving this code for VML. It seems slightly faster but times vary.
  65. this[shp] = this.container.createPath({}).setStroke(sty);
  66. this.closePath && this[shp].setFill(sty.fill);
  67. dojo.forEach(this.points, function(o, i){
  68. if(!o.skip){
  69. if(i==0 || o.t=="M"){
  70. this[shp].moveTo(o.x, o.y);
  71. }else if(o.t=="Z"){
  72. this.closePath && this[shp].closePath();
  73. }else{
  74. this[shp].lineTo(o.x, o.y);
  75. }
  76. }
  77. }, this);
  78. this.closePath && this[shp].closePath();
  79. }
  80. this._setNodeAtts(this[shp]);
  81. },
  82. render: function(){
  83. // summary:
  84. // Renders the 'hit' object (the shape used for an expanded
  85. // hit area and for highlighting) and the'shape' (the actual
  86. // display object).
  87. //
  88. this.onBeforeRender(this);
  89. this.renderHit && this._create("hit", this.style.currentHit);
  90. this._create("shape", this.style.current);
  91. //console.log("path render")
  92. //console.log("---------------------rend hit", this.renderHit, this.id)
  93. },
  94. getBounds: function(/* ? Boolean*/absolute){
  95. // summary:
  96. // Overwriting _Base.getBounds. Not sure how absolute should
  97. // work for a path.
  98. var minx = 10000, miny = 10000, maxx = 0, maxy = 0;
  99. dojo.forEach(this.points, function(p){
  100. if(p.x!==undefined && !isNaN(p.x)){
  101. minx = Math.min(minx, p.x);
  102. miny = Math.min(miny, p.y);
  103. maxx = Math.max(maxx, p.x);
  104. maxy = Math.max(maxy, p.y);
  105. }
  106. });
  107. return {
  108. x1:minx,
  109. y1:miny,
  110. x2:maxx,
  111. y2:maxy,
  112. x:minx,
  113. y:miny,
  114. w:maxx-minx,
  115. h:maxy-miny
  116. };
  117. },
  118. checkClosePoint: function(/*Object*/firstPt, /*Object*/currPt, /*Boolean*/remove){
  119. // summary:
  120. // Checks if points are close enough to indicate that
  121. // path should be close. Provides a visual cue.
  122. // description:
  123. // Not actually used in stencil.path - this is used for
  124. // drawable tools that extend it. Note that those tools
  125. // need to remove the shape created: this.closeGuide, or
  126. // add arg: remove
  127. //
  128. var dist = this.util.distance(firstPt.x, firstPt.y, currPt.x, currPt.y);
  129. if(this.points.length>1){
  130. if(dist<this.closeRadius && !this.closeGuide && !remove){
  131. var c = {
  132. cx:firstPt.x,
  133. cy:firstPt.y,
  134. rx:this.closeRadius,
  135. ry:this.closeRadius
  136. }
  137. this.closeGuide = this.container.createEllipse(c)
  138. .setFill(this.closeColor);
  139. }else if(remove || dist > this.closeRadius && this.closeGuide){
  140. this.remove(this.closeGuide);
  141. this.closeGuide = null;
  142. }
  143. }
  144. // return if we are within close distance
  145. return dist < this.closeRadius; // Boolean
  146. }
  147. }
  148. );
  149. dojox.drawing.register({
  150. name:"dojox.drawing.stencil.Path"
  151. }, "stencil");
  152. });