zrender.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*!
  2. * ZRender, a high performance 2d drawing library.
  3. *
  4. * Copyright (c) 2013, Baidu Inc.
  5. * All rights reserved.
  6. *
  7. * LICENSE
  8. * https://github.com/ecomfe/zrender/blob/master/LICENSE.txt
  9. */
  10. import env from './core/env.js';
  11. import * as zrUtil from './core/util.js';
  12. import Handler from './Handler.js';
  13. import Storage from './Storage.js';
  14. import Animation, { getTime } from './animation/Animation.js';
  15. import HandlerProxy from './dom/HandlerProxy.js';
  16. import { lum } from './tool/color.js';
  17. import { DARK_MODE_THRESHOLD } from './config.js';
  18. import Group from './graphic/Group.js';
  19. var painterCtors = {};
  20. var instances = {};
  21. function delInstance(id) {
  22. delete instances[id];
  23. }
  24. function isDarkMode(backgroundColor) {
  25. if (!backgroundColor) {
  26. return false;
  27. }
  28. if (typeof backgroundColor === 'string') {
  29. return lum(backgroundColor, 1) < DARK_MODE_THRESHOLD;
  30. }
  31. else if (backgroundColor.colorStops) {
  32. var colorStops = backgroundColor.colorStops;
  33. var totalLum = 0;
  34. var len = colorStops.length;
  35. for (var i = 0; i < len; i++) {
  36. totalLum += lum(colorStops[i].color, 1);
  37. }
  38. totalLum /= len;
  39. return totalLum < DARK_MODE_THRESHOLD;
  40. }
  41. return false;
  42. }
  43. var ZRender = (function () {
  44. function ZRender(id, dom, opts) {
  45. var _this = this;
  46. this._sleepAfterStill = 10;
  47. this._stillFrameAccum = 0;
  48. this._needsRefresh = true;
  49. this._needsRefreshHover = true;
  50. this._darkMode = false;
  51. opts = opts || {};
  52. this.dom = dom;
  53. this.id = id;
  54. var storage = new Storage();
  55. var rendererType = opts.renderer || 'canvas';
  56. if (!painterCtors[rendererType]) {
  57. rendererType = zrUtil.keys(painterCtors)[0];
  58. }
  59. if (process.env.NODE_ENV !== 'production') {
  60. if (!painterCtors[rendererType]) {
  61. throw new Error("Renderer '" + rendererType + "' is not imported. Please import it first.");
  62. }
  63. }
  64. opts.useDirtyRect = opts.useDirtyRect == null
  65. ? false
  66. : opts.useDirtyRect;
  67. var painter = new painterCtors[rendererType](dom, storage, opts, id);
  68. var ssrMode = opts.ssr || painter.ssrOnly;
  69. this.storage = storage;
  70. this.painter = painter;
  71. var handerProxy = (!env.node && !env.worker && !ssrMode)
  72. ? new HandlerProxy(painter.getViewportRoot(), painter.root)
  73. : null;
  74. this.handler = new Handler(storage, painter, handerProxy, painter.root);
  75. this.animation = new Animation({
  76. stage: {
  77. update: ssrMode ? null : function () { return _this._flush(true); }
  78. }
  79. });
  80. if (!ssrMode) {
  81. this.animation.start();
  82. }
  83. }
  84. ZRender.prototype.add = function (el) {
  85. if (!el) {
  86. return;
  87. }
  88. this.storage.addRoot(el);
  89. el.addSelfToZr(this);
  90. this.refresh();
  91. };
  92. ZRender.prototype.remove = function (el) {
  93. if (!el) {
  94. return;
  95. }
  96. this.storage.delRoot(el);
  97. el.removeSelfFromZr(this);
  98. this.refresh();
  99. };
  100. ZRender.prototype.configLayer = function (zLevel, config) {
  101. if (this.painter.configLayer) {
  102. this.painter.configLayer(zLevel, config);
  103. }
  104. this.refresh();
  105. };
  106. ZRender.prototype.setBackgroundColor = function (backgroundColor) {
  107. if (this.painter.setBackgroundColor) {
  108. this.painter.setBackgroundColor(backgroundColor);
  109. }
  110. this.refresh();
  111. this._backgroundColor = backgroundColor;
  112. this._darkMode = isDarkMode(backgroundColor);
  113. };
  114. ZRender.prototype.getBackgroundColor = function () {
  115. return this._backgroundColor;
  116. };
  117. ZRender.prototype.setDarkMode = function (darkMode) {
  118. this._darkMode = darkMode;
  119. };
  120. ZRender.prototype.isDarkMode = function () {
  121. return this._darkMode;
  122. };
  123. ZRender.prototype.refreshImmediately = function (fromInside) {
  124. if (!fromInside) {
  125. this.animation.update(true);
  126. }
  127. this._needsRefresh = false;
  128. this.painter.refresh();
  129. this._needsRefresh = false;
  130. };
  131. ZRender.prototype.refresh = function () {
  132. this._needsRefresh = true;
  133. this.animation.start();
  134. };
  135. ZRender.prototype.flush = function () {
  136. this._flush(false);
  137. };
  138. ZRender.prototype._flush = function (fromInside) {
  139. var triggerRendered;
  140. var start = getTime();
  141. if (this._needsRefresh) {
  142. triggerRendered = true;
  143. this.refreshImmediately(fromInside);
  144. }
  145. if (this._needsRefreshHover) {
  146. triggerRendered = true;
  147. this.refreshHoverImmediately();
  148. }
  149. var end = getTime();
  150. if (triggerRendered) {
  151. this._stillFrameAccum = 0;
  152. this.trigger('rendered', {
  153. elapsedTime: end - start
  154. });
  155. }
  156. else if (this._sleepAfterStill > 0) {
  157. this._stillFrameAccum++;
  158. if (this._stillFrameAccum > this._sleepAfterStill) {
  159. this.animation.stop();
  160. }
  161. }
  162. };
  163. ZRender.prototype.setSleepAfterStill = function (stillFramesCount) {
  164. this._sleepAfterStill = stillFramesCount;
  165. };
  166. ZRender.prototype.wakeUp = function () {
  167. this.animation.start();
  168. this._stillFrameAccum = 0;
  169. };
  170. ZRender.prototype.refreshHover = function () {
  171. this._needsRefreshHover = true;
  172. };
  173. ZRender.prototype.refreshHoverImmediately = function () {
  174. this._needsRefreshHover = false;
  175. if (this.painter.refreshHover && this.painter.getType() === 'canvas') {
  176. this.painter.refreshHover();
  177. }
  178. };
  179. ZRender.prototype.resize = function (opts) {
  180. opts = opts || {};
  181. this.painter.resize(opts.width, opts.height);
  182. this.handler.resize();
  183. };
  184. ZRender.prototype.clearAnimation = function () {
  185. this.animation.clear();
  186. };
  187. ZRender.prototype.getWidth = function () {
  188. return this.painter.getWidth();
  189. };
  190. ZRender.prototype.getHeight = function () {
  191. return this.painter.getHeight();
  192. };
  193. ZRender.prototype.setCursorStyle = function (cursorStyle) {
  194. this.handler.setCursorStyle(cursorStyle);
  195. };
  196. ZRender.prototype.findHover = function (x, y) {
  197. return this.handler.findHover(x, y);
  198. };
  199. ZRender.prototype.on = function (eventName, eventHandler, context) {
  200. this.handler.on(eventName, eventHandler, context);
  201. return this;
  202. };
  203. ZRender.prototype.off = function (eventName, eventHandler) {
  204. this.handler.off(eventName, eventHandler);
  205. };
  206. ZRender.prototype.trigger = function (eventName, event) {
  207. this.handler.trigger(eventName, event);
  208. };
  209. ZRender.prototype.clear = function () {
  210. var roots = this.storage.getRoots();
  211. for (var i = 0; i < roots.length; i++) {
  212. if (roots[i] instanceof Group) {
  213. roots[i].removeSelfFromZr(this);
  214. }
  215. }
  216. this.storage.delAllRoots();
  217. this.painter.clear();
  218. };
  219. ZRender.prototype.dispose = function () {
  220. this.animation.stop();
  221. this.clear();
  222. this.storage.dispose();
  223. this.painter.dispose();
  224. this.handler.dispose();
  225. this.animation =
  226. this.storage =
  227. this.painter =
  228. this.handler = null;
  229. delInstance(this.id);
  230. };
  231. return ZRender;
  232. }());
  233. export function init(dom, opts) {
  234. var zr = new ZRender(zrUtil.guid(), dom, opts);
  235. instances[zr.id] = zr;
  236. return zr;
  237. }
  238. export function dispose(zr) {
  239. zr.dispose();
  240. }
  241. export function disposeAll() {
  242. for (var key in instances) {
  243. if (instances.hasOwnProperty(key)) {
  244. instances[key].dispose();
  245. }
  246. }
  247. instances = {};
  248. }
  249. export function getInstance(id) {
  250. return instances[id];
  251. }
  252. export function registerPainter(name, Ctor) {
  253. painterCtors[name] = Ctor;
  254. }
  255. export var version = '5.3.2';
  256. ;