Image.js 4.3 KB

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