123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- /*!
- * ZRender, a high performance 2d drawing library.
- *
- * Copyright (c) 2013, Baidu Inc.
- * All rights reserved.
- *
- * LICENSE
- * https://github.com/ecomfe/zrender/blob/master/LICENSE.txt
- */
- import env from './core/env.js';
- import * as zrUtil from './core/util.js';
- import Handler from './Handler.js';
- import Storage from './Storage.js';
- import Animation, { getTime } from './animation/Animation.js';
- import HandlerProxy from './dom/HandlerProxy.js';
- import { lum } from './tool/color.js';
- import { DARK_MODE_THRESHOLD } from './config.js';
- import Group from './graphic/Group.js';
- var painterCtors = {};
- var instances = {};
- function delInstance(id) {
- delete instances[id];
- }
- function isDarkMode(backgroundColor) {
- if (!backgroundColor) {
- return false;
- }
- if (typeof backgroundColor === 'string') {
- return lum(backgroundColor, 1) < DARK_MODE_THRESHOLD;
- }
- else if (backgroundColor.colorStops) {
- var colorStops = backgroundColor.colorStops;
- var totalLum = 0;
- var len = colorStops.length;
- for (var i = 0; i < len; i++) {
- totalLum += lum(colorStops[i].color, 1);
- }
- totalLum /= len;
- return totalLum < DARK_MODE_THRESHOLD;
- }
- return false;
- }
- var ZRender = (function () {
- function ZRender(id, dom, opts) {
- var _this = this;
- this._sleepAfterStill = 10;
- this._stillFrameAccum = 0;
- this._needsRefresh = true;
- this._needsRefreshHover = true;
- this._darkMode = false;
- opts = opts || {};
- this.dom = dom;
- this.id = id;
- var storage = new Storage();
- var rendererType = opts.renderer || 'canvas';
- if (!painterCtors[rendererType]) {
- rendererType = zrUtil.keys(painterCtors)[0];
- }
- if (process.env.NODE_ENV !== 'production') {
- if (!painterCtors[rendererType]) {
- throw new Error("Renderer '" + rendererType + "' is not imported. Please import it first.");
- }
- }
- opts.useDirtyRect = opts.useDirtyRect == null
- ? false
- : opts.useDirtyRect;
- var painter = new painterCtors[rendererType](dom, storage, opts, id);
- var ssrMode = opts.ssr || painter.ssrOnly;
- this.storage = storage;
- this.painter = painter;
- var handlerProxy = (!env.node && !env.worker && !ssrMode)
- ? new HandlerProxy(painter.getViewportRoot(), painter.root)
- : null;
- var useCoarsePointer = opts.useCoarsePointer;
- var usePointerSize = (useCoarsePointer == null || useCoarsePointer === 'auto')
- ? env.touchEventsSupported
- : !!useCoarsePointer;
- var defaultPointerSize = 44;
- var pointerSize;
- if (usePointerSize) {
- pointerSize = zrUtil.retrieve2(opts.pointerSize, defaultPointerSize);
- }
- this.handler = new Handler(storage, painter, handlerProxy, painter.root, pointerSize);
- this.animation = new Animation({
- stage: {
- update: ssrMode ? null : function () { return _this._flush(true); }
- }
- });
- if (!ssrMode) {
- this.animation.start();
- }
- }
- ZRender.prototype.add = function (el) {
- if (this._disposed || !el) {
- return;
- }
- this.storage.addRoot(el);
- el.addSelfToZr(this);
- this.refresh();
- };
- ZRender.prototype.remove = function (el) {
- if (this._disposed || !el) {
- return;
- }
- this.storage.delRoot(el);
- el.removeSelfFromZr(this);
- this.refresh();
- };
- ZRender.prototype.configLayer = function (zLevel, config) {
- if (this._disposed) {
- return;
- }
- if (this.painter.configLayer) {
- this.painter.configLayer(zLevel, config);
- }
- this.refresh();
- };
- ZRender.prototype.setBackgroundColor = function (backgroundColor) {
- if (this._disposed) {
- return;
- }
- if (this.painter.setBackgroundColor) {
- this.painter.setBackgroundColor(backgroundColor);
- }
- this.refresh();
- this._backgroundColor = backgroundColor;
- this._darkMode = isDarkMode(backgroundColor);
- };
- ZRender.prototype.getBackgroundColor = function () {
- return this._backgroundColor;
- };
- ZRender.prototype.setDarkMode = function (darkMode) {
- this._darkMode = darkMode;
- };
- ZRender.prototype.isDarkMode = function () {
- return this._darkMode;
- };
- ZRender.prototype.refreshImmediately = function (fromInside) {
- if (this._disposed) {
- return;
- }
- if (!fromInside) {
- this.animation.update(true);
- }
- this._needsRefresh = false;
- this.painter.refresh();
- this._needsRefresh = false;
- };
- ZRender.prototype.refresh = function () {
- if (this._disposed) {
- return;
- }
- this._needsRefresh = true;
- this.animation.start();
- };
- ZRender.prototype.flush = function () {
- if (this._disposed) {
- return;
- }
- this._flush(false);
- };
- ZRender.prototype._flush = function (fromInside) {
- var triggerRendered;
- var start = getTime();
- if (this._needsRefresh) {
- triggerRendered = true;
- this.refreshImmediately(fromInside);
- }
- if (this._needsRefreshHover) {
- triggerRendered = true;
- this.refreshHoverImmediately();
- }
- var end = getTime();
- if (triggerRendered) {
- this._stillFrameAccum = 0;
- this.trigger('rendered', {
- elapsedTime: end - start
- });
- }
- else if (this._sleepAfterStill > 0) {
- this._stillFrameAccum++;
- if (this._stillFrameAccum > this._sleepAfterStill) {
- this.animation.stop();
- }
- }
- };
- ZRender.prototype.setSleepAfterStill = function (stillFramesCount) {
- this._sleepAfterStill = stillFramesCount;
- };
- ZRender.prototype.wakeUp = function () {
- if (this._disposed) {
- return;
- }
- this.animation.start();
- this._stillFrameAccum = 0;
- };
- ZRender.prototype.refreshHover = function () {
- this._needsRefreshHover = true;
- };
- ZRender.prototype.refreshHoverImmediately = function () {
- if (this._disposed) {
- return;
- }
- this._needsRefreshHover = false;
- if (this.painter.refreshHover && this.painter.getType() === 'canvas') {
- this.painter.refreshHover();
- }
- };
- ZRender.prototype.resize = function (opts) {
- if (this._disposed) {
- return;
- }
- opts = opts || {};
- this.painter.resize(opts.width, opts.height);
- this.handler.resize();
- };
- ZRender.prototype.clearAnimation = function () {
- if (this._disposed) {
- return;
- }
- this.animation.clear();
- };
- ZRender.prototype.getWidth = function () {
- if (this._disposed) {
- return;
- }
- return this.painter.getWidth();
- };
- ZRender.prototype.getHeight = function () {
- if (this._disposed) {
- return;
- }
- return this.painter.getHeight();
- };
- ZRender.prototype.setCursorStyle = function (cursorStyle) {
- if (this._disposed) {
- return;
- }
- this.handler.setCursorStyle(cursorStyle);
- };
- ZRender.prototype.findHover = function (x, y) {
- if (this._disposed) {
- return;
- }
- return this.handler.findHover(x, y);
- };
- ZRender.prototype.on = function (eventName, eventHandler, context) {
- if (!this._disposed) {
- this.handler.on(eventName, eventHandler, context);
- }
- return this;
- };
- ZRender.prototype.off = function (eventName, eventHandler) {
- if (this._disposed) {
- return;
- }
- this.handler.off(eventName, eventHandler);
- };
- ZRender.prototype.trigger = function (eventName, event) {
- if (this._disposed) {
- return;
- }
- this.handler.trigger(eventName, event);
- };
- ZRender.prototype.clear = function () {
- if (this._disposed) {
- return;
- }
- var roots = this.storage.getRoots();
- for (var i = 0; i < roots.length; i++) {
- if (roots[i] instanceof Group) {
- roots[i].removeSelfFromZr(this);
- }
- }
- this.storage.delAllRoots();
- this.painter.clear();
- };
- ZRender.prototype.dispose = function () {
- if (this._disposed) {
- return;
- }
- this.animation.stop();
- this.clear();
- this.storage.dispose();
- this.painter.dispose();
- this.handler.dispose();
- this.animation =
- this.storage =
- this.painter =
- this.handler = null;
- this._disposed = true;
- delInstance(this.id);
- };
- return ZRender;
- }());
- export function init(dom, opts) {
- var zr = new ZRender(zrUtil.guid(), dom, opts);
- instances[zr.id] = zr;
- return zr;
- }
- export function dispose(zr) {
- zr.dispose();
- }
- export function disposeAll() {
- for (var key in instances) {
- if (instances.hasOwnProperty(key)) {
- instances[key].dispose();
- }
- }
- instances = {};
- }
- export function getInstance(id) {
- return instances[id];
- }
- export function registerPainter(name, Ctor) {
- painterCtors[name] = Ctor;
- }
- var ssrDataGetter;
- export function getElementSSRData(el) {
- if (typeof ssrDataGetter === 'function') {
- return ssrDataGetter(el);
- }
- }
- export function registerSSRDataGetter(getter) {
- ssrDataGetter = getter;
- }
- export var version = '5.6.0';
- ;
|