zrender.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. var useCoarsePointer = opts.useCoarsePointer;
  75. var usePointerSize = (useCoarsePointer == null || useCoarsePointer === 'auto')
  76. ? env.touchEventsSupported
  77. : !!useCoarsePointer;
  78. var defaultPointerSize = 44;
  79. var pointerSize;
  80. if (usePointerSize) {
  81. pointerSize = zrUtil.retrieve2(opts.pointerSize, defaultPointerSize);
  82. }
  83. this.handler = new Handler(storage, painter, handerProxy, painter.root, pointerSize);
  84. this.animation = new Animation({
  85. stage: {
  86. update: ssrMode ? null : function () { return _this._flush(true); }
  87. }
  88. });
  89. if (!ssrMode) {
  90. this.animation.start();
  91. }
  92. }
  93. ZRender.prototype.add = function (el) {
  94. if (!el) {
  95. return;
  96. }
  97. this.storage.addRoot(el);
  98. el.addSelfToZr(this);
  99. this.refresh();
  100. };
  101. ZRender.prototype.remove = function (el) {
  102. if (!el) {
  103. return;
  104. }
  105. this.storage.delRoot(el);
  106. el.removeSelfFromZr(this);
  107. this.refresh();
  108. };
  109. ZRender.prototype.configLayer = function (zLevel, config) {
  110. if (this.painter.configLayer) {
  111. this.painter.configLayer(zLevel, config);
  112. }
  113. this.refresh();
  114. };
  115. ZRender.prototype.setBackgroundColor = function (backgroundColor) {
  116. if (this.painter.setBackgroundColor) {
  117. this.painter.setBackgroundColor(backgroundColor);
  118. }
  119. this.refresh();
  120. this._backgroundColor = backgroundColor;
  121. this._darkMode = isDarkMode(backgroundColor);
  122. };
  123. ZRender.prototype.getBackgroundColor = function () {
  124. return this._backgroundColor;
  125. };
  126. ZRender.prototype.setDarkMode = function (darkMode) {
  127. this._darkMode = darkMode;
  128. };
  129. ZRender.prototype.isDarkMode = function () {
  130. return this._darkMode;
  131. };
  132. ZRender.prototype.refreshImmediately = function (fromInside) {
  133. if (!fromInside) {
  134. this.animation.update(true);
  135. }
  136. this._needsRefresh = false;
  137. this.painter.refresh();
  138. this._needsRefresh = false;
  139. };
  140. ZRender.prototype.refresh = function () {
  141. this._needsRefresh = true;
  142. this.animation.start();
  143. };
  144. ZRender.prototype.flush = function () {
  145. this._flush(false);
  146. };
  147. ZRender.prototype._flush = function (fromInside) {
  148. var triggerRendered;
  149. var start = getTime();
  150. if (this._needsRefresh) {
  151. triggerRendered = true;
  152. this.refreshImmediately(fromInside);
  153. }
  154. if (this._needsRefreshHover) {
  155. triggerRendered = true;
  156. this.refreshHoverImmediately();
  157. }
  158. var end = getTime();
  159. if (triggerRendered) {
  160. this._stillFrameAccum = 0;
  161. this.trigger('rendered', {
  162. elapsedTime: end - start
  163. });
  164. }
  165. else if (this._sleepAfterStill > 0) {
  166. this._stillFrameAccum++;
  167. if (this._stillFrameAccum > this._sleepAfterStill) {
  168. this.animation.stop();
  169. }
  170. }
  171. };
  172. ZRender.prototype.setSleepAfterStill = function (stillFramesCount) {
  173. this._sleepAfterStill = stillFramesCount;
  174. };
  175. ZRender.prototype.wakeUp = function () {
  176. this.animation.start();
  177. this._stillFrameAccum = 0;
  178. };
  179. ZRender.prototype.refreshHover = function () {
  180. this._needsRefreshHover = true;
  181. };
  182. ZRender.prototype.refreshHoverImmediately = function () {
  183. this._needsRefreshHover = false;
  184. if (this.painter.refreshHover && this.painter.getType() === 'canvas') {
  185. this.painter.refreshHover();
  186. }
  187. };
  188. ZRender.prototype.resize = function (opts) {
  189. opts = opts || {};
  190. this.painter.resize(opts.width, opts.height);
  191. this.handler.resize();
  192. };
  193. ZRender.prototype.clearAnimation = function () {
  194. this.animation.clear();
  195. };
  196. ZRender.prototype.getWidth = function () {
  197. return this.painter.getWidth();
  198. };
  199. ZRender.prototype.getHeight = function () {
  200. return this.painter.getHeight();
  201. };
  202. ZRender.prototype.setCursorStyle = function (cursorStyle) {
  203. this.handler.setCursorStyle(cursorStyle);
  204. };
  205. ZRender.prototype.findHover = function (x, y) {
  206. return this.handler.findHover(x, y);
  207. };
  208. ZRender.prototype.on = function (eventName, eventHandler, context) {
  209. this.handler.on(eventName, eventHandler, context);
  210. return this;
  211. };
  212. ZRender.prototype.off = function (eventName, eventHandler) {
  213. this.handler.off(eventName, eventHandler);
  214. };
  215. ZRender.prototype.trigger = function (eventName, event) {
  216. this.handler.trigger(eventName, event);
  217. };
  218. ZRender.prototype.clear = function () {
  219. var roots = this.storage.getRoots();
  220. for (var i = 0; i < roots.length; i++) {
  221. if (roots[i] instanceof Group) {
  222. roots[i].removeSelfFromZr(this);
  223. }
  224. }
  225. this.storage.delAllRoots();
  226. this.painter.clear();
  227. };
  228. ZRender.prototype.dispose = function () {
  229. this.animation.stop();
  230. this.clear();
  231. this.storage.dispose();
  232. this.painter.dispose();
  233. this.handler.dispose();
  234. this.animation =
  235. this.storage =
  236. this.painter =
  237. this.handler = null;
  238. delInstance(this.id);
  239. };
  240. return ZRender;
  241. }());
  242. export function init(dom, opts) {
  243. var zr = new ZRender(zrUtil.guid(), dom, opts);
  244. instances[zr.id] = zr;
  245. return zr;
  246. }
  247. export function dispose(zr) {
  248. zr.dispose();
  249. }
  250. export function disposeAll() {
  251. for (var key in instances) {
  252. if (instances.hasOwnProperty(key)) {
  253. instances[key].dispose();
  254. }
  255. }
  256. instances = {};
  257. }
  258. export function getInstance(id) {
  259. return instances[id];
  260. }
  261. export function registerPainter(name, Ctor) {
  262. painterCtors[name] = Ctor;
  263. }
  264. export var version = '5.4.3';
  265. ;