Icon.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/BaseTypes/Class.js
  7. */
  8. /**
  9. * Class: OpenLayers.Icon
  10. *
  11. * The icon represents a graphical icon on the screen. Typically used in
  12. * conjunction with a <OpenLayers.Marker> to represent markers on a screen.
  13. *
  14. * An icon has a url, size and position. It also contains an offset which
  15. * allows the center point to be represented correctly. This can be
  16. * provided either as a fixed offset or a function provided to calculate
  17. * the desired offset.
  18. *
  19. */
  20. OpenLayers.Icon = OpenLayers.Class({
  21. /**
  22. * Property: url
  23. * {String} image url
  24. */
  25. url: null,
  26. /**
  27. * Property: size
  28. * {<OpenLayers.Size>|Object} An OpenLayers.Size or
  29. * an object with a 'w' and 'h' properties.
  30. */
  31. size: null,
  32. /**
  33. * Property: offset
  34. * {<OpenLayers.Pixel>|Object} distance in pixels to offset the
  35. * image when being rendered. An OpenLayers.Pixel or an object
  36. * with a 'x' and 'y' properties.
  37. */
  38. offset: null,
  39. /**
  40. * Property: calculateOffset
  41. * {Function} Function to calculate the offset (based on the size)
  42. */
  43. calculateOffset: null,
  44. /**
  45. * Property: imageDiv
  46. * {DOMElement}
  47. */
  48. imageDiv: null,
  49. /**
  50. * Property: px
  51. * {<OpenLayers.Pixel>|Object} An OpenLayers.Pixel or an object
  52. * with a 'x' and 'y' properties.
  53. */
  54. px: null,
  55. /**
  56. * Constructor: OpenLayers.Icon
  57. * Creates an icon, which is an image tag in a div.
  58. *
  59. * url - {String}
  60. * size - {<OpenLayers.Size>|Object} An OpenLayers.Size or an
  61. * object with a 'w' and 'h'
  62. * properties.
  63. * offset - {<OpenLayers.Pixel>|Object} An OpenLayers.Pixel or an
  64. * object with a 'x' and 'y'
  65. * properties.
  66. * calculateOffset - {Function}
  67. */
  68. initialize: function(url, size, offset, calculateOffset) {
  69. this.url = url;
  70. this.size = size || {w: 20, h: 20};
  71. this.offset = offset || {x: -(this.size.w/2), y: -(this.size.h/2)};
  72. this.calculateOffset = calculateOffset;
  73. var id = OpenLayers.Util.createUniqueID("OL_Icon_");
  74. this.imageDiv = OpenLayers.Util.createAlphaImageDiv(id);
  75. },
  76. /**
  77. * Method: destroy
  78. * Nullify references and remove event listeners to prevent circular
  79. * references and memory leaks
  80. */
  81. destroy: function() {
  82. // erase any drawn elements
  83. this.erase();
  84. OpenLayers.Event.stopObservingElement(this.imageDiv.firstChild);
  85. this.imageDiv.innerHTML = "";
  86. this.imageDiv = null;
  87. },
  88. /**
  89. * Method: clone
  90. *
  91. * Returns:
  92. * {<OpenLayers.Icon>} A fresh copy of the icon.
  93. */
  94. clone: function() {
  95. return new OpenLayers.Icon(this.url,
  96. this.size,
  97. this.offset,
  98. this.calculateOffset);
  99. },
  100. /**
  101. * Method: setSize
  102. *
  103. * Parameters:
  104. * size - {<OpenLayers.Size>|Object} An OpenLayers.Size or
  105. * an object with a 'w' and 'h' properties.
  106. */
  107. setSize: function(size) {
  108. if (size != null) {
  109. this.size = size;
  110. }
  111. this.draw();
  112. },
  113. /**
  114. * Method: setUrl
  115. *
  116. * Parameters:
  117. * url - {String}
  118. */
  119. setUrl: function(url) {
  120. if (url != null) {
  121. this.url = url;
  122. }
  123. this.draw();
  124. },
  125. /**
  126. * Method: draw
  127. * Move the div to the given pixel.
  128. *
  129. * Parameters:
  130. * px - {<OpenLayers.Pixel>|Object} An OpenLayers.Pixel or an
  131. * object with a 'x' and 'y' properties.
  132. *
  133. * Returns:
  134. * {DOMElement} A new DOM Image of this icon set at the location passed-in
  135. */
  136. draw: function(px) {
  137. OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv,
  138. null,
  139. null,
  140. this.size,
  141. this.url,
  142. "absolute");
  143. this.moveTo(px);
  144. return this.imageDiv;
  145. },
  146. /**
  147. * Method: erase
  148. * Erase the underlying image element.
  149. */
  150. erase: function() {
  151. if (this.imageDiv != null && this.imageDiv.parentNode != null) {
  152. OpenLayers.Element.remove(this.imageDiv);
  153. }
  154. },
  155. /**
  156. * Method: setOpacity
  157. * Change the icon's opacity
  158. *
  159. * Parameters:
  160. * opacity - {float}
  161. */
  162. setOpacity: function(opacity) {
  163. OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv, null, null, null,
  164. null, null, null, null, opacity);
  165. },
  166. /**
  167. * Method: moveTo
  168. * move icon to passed in px.
  169. *
  170. * Parameters:
  171. * px - {<OpenLayers.Pixel>|Object} the pixel position to move to.
  172. * An OpenLayers.Pixel or an object with a 'x' and 'y' properties.
  173. */
  174. moveTo: function (px) {
  175. //if no px passed in, use stored location
  176. if (px != null) {
  177. this.px = px;
  178. }
  179. if (this.imageDiv != null) {
  180. if (this.px == null) {
  181. this.display(false);
  182. } else {
  183. if (this.calculateOffset) {
  184. this.offset = this.calculateOffset(this.size);
  185. }
  186. OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv, null, {
  187. x: this.px.x + this.offset.x,
  188. y: this.px.y + this.offset.y
  189. });
  190. }
  191. }
  192. },
  193. /**
  194. * Method: display
  195. * Hide or show the icon
  196. *
  197. * Parameters:
  198. * display - {Boolean}
  199. */
  200. display: function(display) {
  201. this.imageDiv.style.display = (display) ? "" : "none";
  202. },
  203. /**
  204. * APIMethod: isDrawn
  205. *
  206. * Returns:
  207. * {Boolean} Whether or not the icon is drawn.
  208. */
  209. isDrawn: function() {
  210. // nodeType 11 for ie, whose nodes *always* have a parentNode
  211. // (of type document fragment)
  212. var isDrawn = (this.imageDiv && this.imageDiv.parentNode &&
  213. (this.imageDiv.parentNode.nodeType != 11));
  214. return isDrawn;
  215. },
  216. CLASS_NAME: "OpenLayers.Icon"
  217. });