123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- var Definable = require("./Definable");
- var zrUtil = require("../../core/util");
- var logError = require("../../core/log");
- var colorTool = require("../../tool/color");
- function GradientManager(zrId, svgRoot) {
- Definable.call(this, zrId, svgRoot, ['linearGradient', 'radialGradient'], '__gradient_in_use__');
- }
- zrUtil.inherits(GradientManager, Definable);
- GradientManager.prototype.addWithoutUpdate = function (svgElement, displayable) {
- if (displayable && displayable.style) {
- var that = this;
- zrUtil.each(['fill', 'stroke'], function (fillOrStroke) {
- if (displayable.style[fillOrStroke] && (displayable.style[fillOrStroke].type === 'linear' || displayable.style[fillOrStroke].type === 'radial')) {
- var gradient = displayable.style[fillOrStroke];
- var defs = that.getDefs(true);
- var dom;
- if (gradient._dom) {
-
- dom = gradient._dom;
- if (!defs.contains(gradient._dom)) {
-
- that.addDom(dom);
- }
- } else {
-
- dom = that.add(gradient);
- }
- that.markUsed(displayable);
- var id = dom.getAttribute('id');
- svgElement.setAttribute(fillOrStroke, 'url(#' + id + ')');
- }
- });
- }
- };
- GradientManager.prototype.add = function (gradient) {
- var dom;
- if (gradient.type === 'linear') {
- dom = this.createElement('linearGradient');
- } else if (gradient.type === 'radial') {
- dom = this.createElement('radialGradient');
- } else {
- logError('Illegal gradient type.');
- return null;
- }
-
-
-
-
- gradient.id = gradient.id || this.nextId++;
- dom.setAttribute('id', 'zr' + this._zrId + '-gradient-' + gradient.id);
- this.updateDom(gradient, dom);
- this.addDom(dom);
- return dom;
- };
- GradientManager.prototype.update = function (gradient) {
- var that = this;
- Definable.prototype.update.call(this, gradient, function () {
- var type = gradient.type;
- var tagName = gradient._dom.tagName;
- if (type === 'linear' && tagName === 'linearGradient' || type === 'radial' && tagName === 'radialGradient') {
-
- that.updateDom(gradient, gradient._dom);
- } else {
-
- that.removeDom(gradient);
- that.add(gradient);
- }
- });
- };
- GradientManager.prototype.updateDom = function (gradient, dom) {
- if (gradient.type === 'linear') {
- dom.setAttribute('x1', gradient.x);
- dom.setAttribute('y1', gradient.y);
- dom.setAttribute('x2', gradient.x2);
- dom.setAttribute('y2', gradient.y2);
- } else if (gradient.type === 'radial') {
- dom.setAttribute('cx', gradient.x);
- dom.setAttribute('cy', gradient.y);
- dom.setAttribute('r', gradient.r);
- } else {
- logError('Illegal gradient type.');
- return;
- }
- if (gradient.global) {
-
- dom.setAttribute('gradientUnits', 'userSpaceOnUse');
- } else {
-
- dom.setAttribute('gradientUnits', 'objectBoundingBox');
- }
- dom.innerHTML = '';
- var colors = gradient.colorStops;
- for (var i = 0, len = colors.length; i < len; ++i) {
- var stop = this.createElement('stop');
- stop.setAttribute('offset', colors[i].offset * 100 + '%');
- var color = colors[i].color;
- if (color.indexOf('rgba') > -1) {
-
- var opacity = colorTool.parse(color)[3];
- var hex = colorTool.toHex(color);
-
-
-
-
- stop.setAttribute('stop-color', '#' + hex);
- stop.setAttribute('stop-opacity', opacity);
- } else {
- stop.setAttribute('stop-color', colors[i].color);
- }
- dom.appendChild(stop);
- }
-
- gradient._dom = dom;
- };
- GradientManager.prototype.markUsed = function (displayable) {
- if (displayable.style) {
- var gradient = displayable.style.fill;
- if (gradient && gradient._dom) {
- Definable.prototype.markUsed.call(this, gradient._dom);
- }
- gradient = displayable.style.stroke;
- if (gradient && gradient._dom) {
- Definable.prototype.markUsed.call(this, gradient._dom);
- }
- }
- };
- var _default = GradientManager;
- module.exports = _default;
|