Displayable.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. var zrUtil = require("../core/util");
  2. var Style = require("./Style");
  3. var Element = require("../Element");
  4. var RectText = require("./mixin/RectText");
  5. /**
  6. * 可绘制的图形基类
  7. * Base class of all displayable graphic objects
  8. * @module zrender/graphic/Displayable
  9. */
  10. /**
  11. * @alias module:zrender/graphic/Displayable
  12. * @extends module:zrender/Element
  13. * @extends module:zrender/graphic/mixin/RectText
  14. */
  15. function Displayable(opts) {
  16. opts = opts || {};
  17. Element.call(this, opts); // Extend properties
  18. for (var name in opts) {
  19. if (opts.hasOwnProperty(name) && name !== 'style') {
  20. this[name] = opts[name];
  21. }
  22. }
  23. /**
  24. * @type {module:zrender/graphic/Style}
  25. */
  26. this.style = new Style(opts.style, this);
  27. this._rect = null; // Shapes for cascade clipping.
  28. this.__clipPaths = []; // FIXME Stateful must be mixined after style is setted
  29. // Stateful.call(this, opts);
  30. }
  31. Displayable.prototype = {
  32. constructor: Displayable,
  33. type: 'displayable',
  34. /**
  35. * Displayable 是否为脏,Painter 中会根据该标记判断是否需要是否需要重新绘制
  36. * Dirty flag. From which painter will determine if this displayable object needs brush
  37. * @name module:zrender/graphic/Displayable#__dirty
  38. * @type {boolean}
  39. */
  40. __dirty: true,
  41. /**
  42. * 图形是否可见,为true时不绘制图形,但是仍能触发鼠标事件
  43. * If ignore drawing of the displayable object. Mouse event will still be triggered
  44. * @name module:/zrender/graphic/Displayable#invisible
  45. * @type {boolean}
  46. * @default false
  47. */
  48. invisible: false,
  49. /**
  50. * @name module:/zrender/graphic/Displayable#z
  51. * @type {number}
  52. * @default 0
  53. */
  54. z: 0,
  55. /**
  56. * @name module:/zrender/graphic/Displayable#z
  57. * @type {number}
  58. * @default 0
  59. */
  60. z2: 0,
  61. /**
  62. * z层level,决定绘画在哪层canvas中
  63. * @name module:/zrender/graphic/Displayable#zlevel
  64. * @type {number}
  65. * @default 0
  66. */
  67. zlevel: 0,
  68. /**
  69. * 是否可拖拽
  70. * @name module:/zrender/graphic/Displayable#draggable
  71. * @type {boolean}
  72. * @default false
  73. */
  74. draggable: false,
  75. /**
  76. * 是否正在拖拽
  77. * @name module:/zrender/graphic/Displayable#draggable
  78. * @type {boolean}
  79. * @default false
  80. */
  81. dragging: false,
  82. /**
  83. * 是否相应鼠标事件
  84. * @name module:/zrender/graphic/Displayable#silent
  85. * @type {boolean}
  86. * @default false
  87. */
  88. silent: false,
  89. /**
  90. * If enable culling
  91. * @type {boolean}
  92. * @default false
  93. */
  94. culling: false,
  95. /**
  96. * Mouse cursor when hovered
  97. * @name module:/zrender/graphic/Displayable#cursor
  98. * @type {string}
  99. */
  100. cursor: 'pointer',
  101. /**
  102. * If hover area is bounding rect
  103. * @name module:/zrender/graphic/Displayable#rectHover
  104. * @type {string}
  105. */
  106. rectHover: false,
  107. /**
  108. * Render the element progressively when the value >= 0,
  109. * usefull for large data.
  110. * @type {number}
  111. */
  112. progressive: -1,
  113. beforeBrush: function (ctx) {},
  114. afterBrush: function (ctx) {},
  115. /**
  116. * 图形绘制方法
  117. * @param {CanvasRenderingContext2D} ctx
  118. */
  119. // Interface
  120. brush: function (ctx, prevEl) {},
  121. /**
  122. * 获取最小包围盒
  123. * @return {module:zrender/core/BoundingRect}
  124. */
  125. // Interface
  126. getBoundingRect: function () {},
  127. /**
  128. * 判断坐标 x, y 是否在图形上
  129. * If displayable element contain coord x, y
  130. * @param {number} x
  131. * @param {number} y
  132. * @return {boolean}
  133. */
  134. contain: function (x, y) {
  135. return this.rectContain(x, y);
  136. },
  137. /**
  138. * @param {Function} cb
  139. * @param {} context
  140. */
  141. traverse: function (cb, context) {
  142. cb.call(context, this);
  143. },
  144. /**
  145. * 判断坐标 x, y 是否在图形的包围盒上
  146. * If bounding rect of element contain coord x, y
  147. * @param {number} x
  148. * @param {number} y
  149. * @return {boolean}
  150. */
  151. rectContain: function (x, y) {
  152. var coord = this.transformCoordToLocal(x, y);
  153. var rect = this.getBoundingRect();
  154. return rect.contain(coord[0], coord[1]);
  155. },
  156. /**
  157. * 标记图形元素为脏,并且在下一帧重绘
  158. * Mark displayable element dirty and refresh next frame
  159. */
  160. dirty: function () {
  161. this.__dirty = true;
  162. this._rect = null;
  163. this.__zr && this.__zr.refresh();
  164. },
  165. /**
  166. * 图形是否会触发事件
  167. * If displayable object binded any event
  168. * @return {boolean}
  169. */
  170. // TODO, 通过 bind 绑定的事件
  171. // isSilent: function () {
  172. // return !(
  173. // this.hoverable || this.draggable
  174. // || this.onmousemove || this.onmouseover || this.onmouseout
  175. // || this.onmousedown || this.onmouseup || this.onclick
  176. // || this.ondragenter || this.ondragover || this.ondragleave
  177. // || this.ondrop
  178. // );
  179. // },
  180. /**
  181. * Alias for animate('style')
  182. * @param {boolean} loop
  183. */
  184. animateStyle: function (loop) {
  185. return this.animate('style', loop);
  186. },
  187. attrKV: function (key, value) {
  188. if (key !== 'style') {
  189. Element.prototype.attrKV.call(this, key, value);
  190. } else {
  191. this.style.set(value);
  192. }
  193. },
  194. /**
  195. * @param {Object|string} key
  196. * @param {*} value
  197. */
  198. setStyle: function (key, value) {
  199. this.style.set(key, value);
  200. this.dirty(false);
  201. return this;
  202. },
  203. /**
  204. * Use given style object
  205. * @param {Object} obj
  206. */
  207. useStyle: function (obj) {
  208. this.style = new Style(obj, this);
  209. this.dirty(false);
  210. return this;
  211. }
  212. };
  213. zrUtil.inherits(Displayable, Element);
  214. zrUtil.mixin(Displayable, RectText); // zrUtil.mixin(Displayable, Stateful);
  215. var _default = Displayable;
  216. module.exports = _default;