Image.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // wrapped by build app
  2. define("dojox/drawing/stencil/Image", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.drawing.stencil.Image");
  4. dojox.drawing.stencil.Image = dojox.drawing.util.oo.declare(
  5. // summary:
  6. // Creates an dojox.gfx Image based on the data
  7. // provided.
  8. //
  9. dojox.drawing.stencil._Base,
  10. function(options){
  11. // summary:
  12. // constructor
  13. },
  14. {
  15. type:"dojox.drawing.stencil.Image",
  16. anchorType: "group",
  17. baseRender:true,
  18. /*=====
  19. StencilData: {
  20. // summary:
  21. // The data used to create the dojox.gfx Shape
  22. // x: Number
  23. // Left point x
  24. // y: Number
  25. // Top point y
  26. // width: ? Number
  27. // Optional width of Image. If not provided, it is obtained
  28. // height: ? Number
  29. // Optional height of Image. If not provided, it is obtained
  30. // src: String
  31. // The location of the source image
  32. },
  33. StencilPoints: [
  34. // summary:
  35. // An Array of dojox.__StencilPoint objects that describe the Stencil
  36. // 0: Object
  37. // Top left point
  38. // 1: Object
  39. // Top right point
  40. // 2: Object
  41. // Bottom right point
  42. // 3: Object
  43. // Bottom left point
  44. ],
  45. =====*/
  46. dataToPoints: function(/*Object*/o){
  47. //summary:
  48. // Converts data to points.
  49. o = o || this.data;
  50. this.points = [
  51. {x:o.x, y:o.y}, // TL
  52. {x:o.x + o.width, y:o.y}, // TR
  53. {x:o.x + o.width, y:o.y + o.height}, // BR
  54. {x:o.x, y:o.y + o.height} // BL
  55. ];
  56. return this.points;
  57. },
  58. pointsToData: function(/*Array*/p){
  59. // summary:
  60. // Converts points to data
  61. p = p || this.points;
  62. var s = p[0];
  63. var e = p[2];
  64. this.data = {
  65. x: s.x,
  66. y: s.y,
  67. width: e.x-s.x,
  68. height: e.y-s.y,
  69. src: this.src || this.data.src
  70. };
  71. return this.data;
  72. },
  73. _createHilite: function(){
  74. // summary:
  75. // Create the hit and highlight area
  76. // for the Image.
  77. this.remove(this.hit);
  78. this.hit = this.container.createRect(this.data)
  79. .setStroke(this.style.current)
  80. .setFill(this.style.current.fill);
  81. this._setNodeAtts(this.hit);
  82. },
  83. _create: function(/*String*/shp, /*StencilData*/d, /*Object*/sty){
  84. // summary:
  85. // Creates a dojox.gfx.shape based on passed arguments.
  86. // Can be called many times by implementation to create
  87. // multiple shapes in one stencil.
  88. //
  89. this.remove(this[shp]);
  90. var s = this.container.getParent();
  91. this[shp] = s.createImage(d)
  92. this.container.add(this[shp]);
  93. this._setNodeAtts(this[shp]);
  94. },
  95. render: function(dbg){
  96. // summary:
  97. // Renders the 'hit' object (the shape used for an expanded
  98. // hit area and for highlighting) and the'shape' (the actual
  99. // display object). Image is slightly different than other
  100. // implementations. Instead of calling render twice, it calls
  101. // _createHilite for the 'hit'
  102. //
  103. if(this.data.width == "auto" || isNaN(this.data.width)){
  104. this.getImageSize(true);
  105. console.warn("Image size not provided. Acquiring...")
  106. return;
  107. }
  108. this.onBeforeRender(this);
  109. this.renderHit && this._createHilite();
  110. this._create("shape", this.data, this.style.current);
  111. },
  112. getImageSize: function(render){
  113. // summary:
  114. // Internal. If no image size is passed in with the data
  115. // create a dom node, insert and image, gets its dimensions
  116. // record them - then destroy everything.
  117. //
  118. if(this._gettingSize){ return; } // IE gets it twice (will need to mod if src changes)
  119. this._gettingSize = true;
  120. var img = dojo.create("img", {src:this.data.src}, dojo.body());
  121. var err = dojo.connect(img, "error", this, function(){
  122. dojo.disconnect(c);
  123. dojo.disconnect(err);
  124. console.error("Error loading image:", this.data.src)
  125. console.warn("Error image:", this.data)
  126. });
  127. var c = dojo.connect(img, "load", this, function(){
  128. var dim = dojo.marginBox(img);
  129. this.setData({
  130. x:this.data.x,
  131. y:this.data.y,
  132. src:this.data.src,
  133. width:dim.w,
  134. height:dim.h
  135. });
  136. dojo.disconnect(c);
  137. dojo.destroy(img);
  138. render && this.render(true);
  139. });
  140. }
  141. }
  142. );
  143. dojox.drawing.register({
  144. name:"dojox.drawing.stencil.Image"
  145. }, "stencil");
  146. });