zrender.js 8.2 KB

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