zrender.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 handlerProxy = (!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, handlerProxy, 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 (this._disposed || !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 (this._disposed || !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._disposed) {
  111. return;
  112. }
  113. if (this.painter.configLayer) {
  114. this.painter.configLayer(zLevel, config);
  115. }
  116. this.refresh();
  117. };
  118. ZRender.prototype.setBackgroundColor = function (backgroundColor) {
  119. if (this._disposed) {
  120. return;
  121. }
  122. if (this.painter.setBackgroundColor) {
  123. this.painter.setBackgroundColor(backgroundColor);
  124. }
  125. this.refresh();
  126. this._backgroundColor = backgroundColor;
  127. this._darkMode = isDarkMode(backgroundColor);
  128. };
  129. ZRender.prototype.getBackgroundColor = function () {
  130. return this._backgroundColor;
  131. };
  132. ZRender.prototype.setDarkMode = function (darkMode) {
  133. this._darkMode = darkMode;
  134. };
  135. ZRender.prototype.isDarkMode = function () {
  136. return this._darkMode;
  137. };
  138. ZRender.prototype.refreshImmediately = function (fromInside) {
  139. if (this._disposed) {
  140. return;
  141. }
  142. if (!fromInside) {
  143. this.animation.update(true);
  144. }
  145. this._needsRefresh = false;
  146. this.painter.refresh();
  147. this._needsRefresh = false;
  148. };
  149. ZRender.prototype.refresh = function () {
  150. if (this._disposed) {
  151. return;
  152. }
  153. this._needsRefresh = true;
  154. this.animation.start();
  155. };
  156. ZRender.prototype.flush = function () {
  157. if (this._disposed) {
  158. return;
  159. }
  160. this._flush(false);
  161. };
  162. ZRender.prototype._flush = function (fromInside) {
  163. var triggerRendered;
  164. var start = getTime();
  165. if (this._needsRefresh) {
  166. triggerRendered = true;
  167. this.refreshImmediately(fromInside);
  168. }
  169. if (this._needsRefreshHover) {
  170. triggerRendered = true;
  171. this.refreshHoverImmediately();
  172. }
  173. var end = getTime();
  174. if (triggerRendered) {
  175. this._stillFrameAccum = 0;
  176. this.trigger('rendered', {
  177. elapsedTime: end - start
  178. });
  179. }
  180. else if (this._sleepAfterStill > 0) {
  181. this._stillFrameAccum++;
  182. if (this._stillFrameAccum > this._sleepAfterStill) {
  183. this.animation.stop();
  184. }
  185. }
  186. };
  187. ZRender.prototype.setSleepAfterStill = function (stillFramesCount) {
  188. this._sleepAfterStill = stillFramesCount;
  189. };
  190. ZRender.prototype.wakeUp = function () {
  191. if (this._disposed) {
  192. return;
  193. }
  194. this.animation.start();
  195. this._stillFrameAccum = 0;
  196. };
  197. ZRender.prototype.refreshHover = function () {
  198. this._needsRefreshHover = true;
  199. };
  200. ZRender.prototype.refreshHoverImmediately = function () {
  201. if (this._disposed) {
  202. return;
  203. }
  204. this._needsRefreshHover = false;
  205. if (this.painter.refreshHover && this.painter.getType() === 'canvas') {
  206. this.painter.refreshHover();
  207. }
  208. };
  209. ZRender.prototype.resize = function (opts) {
  210. if (this._disposed) {
  211. return;
  212. }
  213. opts = opts || {};
  214. this.painter.resize(opts.width, opts.height);
  215. this.handler.resize();
  216. };
  217. ZRender.prototype.clearAnimation = function () {
  218. if (this._disposed) {
  219. return;
  220. }
  221. this.animation.clear();
  222. };
  223. ZRender.prototype.getWidth = function () {
  224. if (this._disposed) {
  225. return;
  226. }
  227. return this.painter.getWidth();
  228. };
  229. ZRender.prototype.getHeight = function () {
  230. if (this._disposed) {
  231. return;
  232. }
  233. return this.painter.getHeight();
  234. };
  235. ZRender.prototype.setCursorStyle = function (cursorStyle) {
  236. if (this._disposed) {
  237. return;
  238. }
  239. this.handler.setCursorStyle(cursorStyle);
  240. };
  241. ZRender.prototype.findHover = function (x, y) {
  242. if (this._disposed) {
  243. return;
  244. }
  245. return this.handler.findHover(x, y);
  246. };
  247. ZRender.prototype.on = function (eventName, eventHandler, context) {
  248. if (!this._disposed) {
  249. this.handler.on(eventName, eventHandler, context);
  250. }
  251. return this;
  252. };
  253. ZRender.prototype.off = function (eventName, eventHandler) {
  254. if (this._disposed) {
  255. return;
  256. }
  257. this.handler.off(eventName, eventHandler);
  258. };
  259. ZRender.prototype.trigger = function (eventName, event) {
  260. if (this._disposed) {
  261. return;
  262. }
  263. this.handler.trigger(eventName, event);
  264. };
  265. ZRender.prototype.clear = function () {
  266. if (this._disposed) {
  267. return;
  268. }
  269. var roots = this.storage.getRoots();
  270. for (var i = 0; i < roots.length; i++) {
  271. if (roots[i] instanceof Group) {
  272. roots[i].removeSelfFromZr(this);
  273. }
  274. }
  275. this.storage.delAllRoots();
  276. this.painter.clear();
  277. };
  278. ZRender.prototype.dispose = function () {
  279. if (this._disposed) {
  280. return;
  281. }
  282. this.animation.stop();
  283. this.clear();
  284. this.storage.dispose();
  285. this.painter.dispose();
  286. this.handler.dispose();
  287. this.animation =
  288. this.storage =
  289. this.painter =
  290. this.handler = null;
  291. this._disposed = true;
  292. delInstance(this.id);
  293. };
  294. return ZRender;
  295. }());
  296. export function init(dom, opts) {
  297. var zr = new ZRender(zrUtil.guid(), dom, opts);
  298. instances[zr.id] = zr;
  299. return zr;
  300. }
  301. export function dispose(zr) {
  302. zr.dispose();
  303. }
  304. export function disposeAll() {
  305. for (var key in instances) {
  306. if (instances.hasOwnProperty(key)) {
  307. instances[key].dispose();
  308. }
  309. }
  310. instances = {};
  311. }
  312. export function getInstance(id) {
  313. return instances[id];
  314. }
  315. export function registerPainter(name, Ctor) {
  316. painterCtors[name] = Ctor;
  317. }
  318. var ssrDataGetter;
  319. export function getElementSSRData(el) {
  320. if (typeof ssrDataGetter === 'function') {
  321. return ssrDataGetter(el);
  322. }
  323. }
  324. export function registerSSRDataGetter(getter) {
  325. ssrDataGetter = getter;
  326. }
  327. export var version = '5.6.0';
  328. ;