Painter.js
10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
var _core = require("./core");
var createElement = _core.createElement;
var util = require("../core/util");
var zrLog = require("../core/log");
var Path = require("../graphic/Path");
var ZImage = require("../graphic/Image");
var ZText = require("../graphic/Text");
var arrayDiff = require("../core/arrayDiff2");
var GradientManager = require("./helper/GradientManager");
var ClippathManager = require("./helper/ClippathManager");
var ShadowManager = require("./helper/ShadowManager");
var _graphic = require("./graphic");
var svgPath = _graphic.path;
var svgImage = _graphic.image;
var svgText = _graphic.text;
/**
* SVG Painter
* @module zrender/svg/Painter
*/
function parseInt10(val) {
return parseInt(val, 10);
}
function getSvgProxy(el) {
if (el instanceof Path) {
return svgPath;
} else if (el instanceof ZImage) {
return svgImage;
} else if (el instanceof ZText) {
return svgText;
} else {
return svgPath;
}
}
function checkParentAvailable(parent, child) {
return child && parent && child.parentNode !== parent;
}
function insertAfter(parent, child, prevSibling) {
if (checkParentAvailable(parent, child) && prevSibling) {
var nextSibling = prevSibling.nextSibling;
nextSibling ? parent.insertBefore(child, nextSibling) : parent.appendChild(child);
}
}
function prepend(parent, child) {
if (checkParentAvailable(parent, child)) {
var firstChild = parent.firstChild;
firstChild ? parent.insertBefore(child, firstChild) : parent.appendChild(child);
}
}
function append(parent, child) {
if (checkParentAvailable(parent, child)) {
parent.appendChild(child);
}
}
function remove(parent, child) {
if (child && parent && child.parentNode === parent) {
parent.removeChild(child);
}
}
function getTextSvgElement(displayable) {
return displayable.__textSvgEl;
}
function getSvgElement(displayable) {
return displayable.__svgEl;
}
/**
* @alias module:zrender/svg/Painter
* @constructor
* @param {HTMLElement} root 绘图容器
* @param {module:zrender/Storage} storage
* @param {Object} opts
*/
var SVGPainter = function (root, storage, opts, zrId) {
this.root = root;
this.storage = storage;
this._opts = opts = util.extend({}, opts || {});
var svgRoot = createElement('svg');
svgRoot.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
svgRoot.setAttribute('version', '1.1');
svgRoot.setAttribute('baseProfile', 'full');
svgRoot.style.cssText = 'user-select:none;position:absolute;left:0;top:0;';
this.gradientManager = new GradientManager(zrId, svgRoot);
this.clipPathManager = new ClippathManager(zrId, svgRoot);
this.shadowManager = new ShadowManager(zrId, svgRoot);
var viewport = document.createElement('div');
viewport.style.cssText = 'overflow:hidden;position:relative';
this._svgRoot = svgRoot;
this._viewport = viewport;
root.appendChild(viewport);
viewport.appendChild(svgRoot);
this.resize(opts.width, opts.height);
this._visibleList = [];
};
SVGPainter.prototype = {
constructor: SVGPainter,
getType: function () {
return 'svg';
},
getViewportRoot: function () {
return this._viewport;
},
getViewportRootOffset: function () {
var viewportRoot = this.getViewportRoot();
if (viewportRoot) {
return {
offsetLeft: viewportRoot.offsetLeft || 0,
offsetTop: viewportRoot.offsetTop || 0
};
}
},
refresh: function () {
var list = this.storage.getDisplayList(true);
this._paintList(list);
},
setBackgroundColor: function (backgroundColor) {
// TODO gradient
this._viewport.style.background = backgroundColor;
},
_paintList: function (list) {
this.gradientManager.markAllUnused();
this.clipPathManager.markAllUnused();
this.shadowManager.markAllUnused();
var svgRoot = this._svgRoot;
var visibleList = this._visibleList;
var listLen = list.length;
var newVisibleList = [];
var i;
for (i = 0; i < listLen; i++) {
var displayable = list[i];
var svgProxy = getSvgProxy(displayable);
var svgElement = getSvgElement(displayable) || getTextSvgElement(displayable);
if (!displayable.invisible) {
if (displayable.__dirty) {
svgProxy && svgProxy.brush(displayable); // Update clipPath
this.clipPathManager.update(displayable); // Update gradient and shadow
if (displayable.style) {
this.gradientManager.update(displayable.style.fill);
this.gradientManager.update(displayable.style.stroke);
this.shadowManager.update(svgElement, displayable);
}
displayable.__dirty = false;
}
newVisibleList.push(displayable);
}
}
var diff = arrayDiff(visibleList, newVisibleList);
var prevSvgElement; // First do remove, in case element moved to the head and do remove
// after add
for (i = 0; i < diff.length; i++) {
var item = diff[i];
if (item.removed) {
for (var k = 0; k < item.count; k++) {
var displayable = visibleList[item.indices[k]];
var svgElement = getSvgElement(displayable);
var textSvgElement = getTextSvgElement(displayable);
remove(svgRoot, svgElement);
remove(svgRoot, textSvgElement);
}
}
}
for (i = 0; i < diff.length; i++) {
var item = diff[i];
if (item.added) {
for (var k = 0; k < item.count; k++) {
var displayable = newVisibleList[item.indices[k]];
var svgElement = getSvgElement(displayable);
var textSvgElement = getTextSvgElement(displayable);
prevSvgElement ? insertAfter(svgRoot, svgElement, prevSvgElement) : prepend(svgRoot, svgElement);
if (svgElement) {
insertAfter(svgRoot, textSvgElement, svgElement);
} else if (prevSvgElement) {
insertAfter(svgRoot, textSvgElement, prevSvgElement);
} else {
prepend(svgRoot, textSvgElement);
} // Insert text
insertAfter(svgRoot, textSvgElement, svgElement);
prevSvgElement = textSvgElement || svgElement || prevSvgElement;
this.gradientManager.addWithoutUpdate(svgElement, displayable);
this.shadowManager.addWithoutUpdate(prevSvgElement, displayable);
this.clipPathManager.markUsed(displayable);
}
} else if (!item.removed) {
for (var k = 0; k < item.count; k++) {
var displayable = newVisibleList[item.indices[k]];
prevSvgElement = svgElement = getTextSvgElement(displayable) || getSvgElement(displayable) || prevSvgElement;
this.gradientManager.markUsed(displayable);
this.gradientManager.addWithoutUpdate(svgElement, displayable);
this.shadowManager.markUsed(displayable);
this.shadowManager.addWithoutUpdate(svgElement, displayable);
this.clipPathManager.markUsed(displayable);
}
}
}
this.gradientManager.removeUnused();
this.clipPathManager.removeUnused();
this.shadowManager.removeUnused();
this._visibleList = newVisibleList;
},
_getDefs: function (isForceCreating) {
var svgRoot = this._svgRoot;
var defs = this._svgRoot.getElementsByTagName('defs');
if (defs.length === 0) {
// Not exist
if (isForceCreating) {
var defs = svgRoot.insertBefore(createElement('defs'), // Create new tag
svgRoot.firstChild // Insert in the front of svg
);
if (!defs.contains) {
// IE doesn't support contains method
defs.contains = function (el) {
var children = defs.children;
if (!children) {
return false;
}
for (var i = children.length - 1; i >= 0; --i) {
if (children[i] === el) {
return true;
}
}
return false;
};
}
return defs;
} else {
return null;
}
} else {
return defs[0];
}
},
resize: function (width, height) {
var viewport = this._viewport; // FIXME Why ?
viewport.style.display = 'none'; // Save input w/h
var opts = this._opts;
width != null && (opts.width = width);
height != null && (opts.height = height);
width = this._getSize(0);
height = this._getSize(1);
viewport.style.display = '';
if (this._width !== width || this._height !== height) {
this._width = width;
this._height = height;
var viewportStyle = viewport.style;
viewportStyle.width = width + 'px';
viewportStyle.height = height + 'px';
var svgRoot = this._svgRoot; // Set width by 'svgRoot.width = width' is invalid
svgRoot.setAttribute('width', width);
svgRoot.setAttribute('height', height);
}
},
/**
* 获取绘图区域宽度
*/
getWidth: function () {
return this._width;
},
/**
* 获取绘图区域高度
*/
getHeight: function () {
return this._height;
},
_getSize: function (whIdx) {
var opts = this._opts;
var wh = ['width', 'height'][whIdx];
var cwh = ['clientWidth', 'clientHeight'][whIdx];
var plt = ['paddingLeft', 'paddingTop'][whIdx];
var prb = ['paddingRight', 'paddingBottom'][whIdx];
if (opts[wh] != null && opts[wh] !== 'auto') {
return parseFloat(opts[wh]);
}
var root = this.root; // IE8 does not support getComputedStyle, but it use VML.
var stl = document.defaultView.getComputedStyle(root);
return (root[cwh] || parseInt10(stl[wh]) || parseInt10(root.style[wh])) - (parseInt10(stl[plt]) || 0) - (parseInt10(stl[prb]) || 0) | 0;
},
dispose: function () {
this.root.innerHTML = '';
this._svgRoot = this._viewport = this.storage = null;
},
clear: function () {
if (this._viewport) {
this.root.removeChild(this._viewport);
}
},
pathToDataUrl: function () {
this.refresh();
var html = this._svgRoot.outerHTML;
return 'data:image/svg+xml;charset=UTF-8,' + html;
}
}; // Not supported methods
function createMethodNotSupport(method) {
return function () {
zrLog('In SVG mode painter not support method "' + method + '"');
};
} // Unsuppoted methods
util.each(['getLayer', 'insertLayer', 'eachLayer', 'eachBuiltinLayer', 'eachOtherLayer', 'getLayers', 'modLayer', 'delLayer', 'clearLayer', 'toDataURL', 'pathToImage'], function (name) {
SVGPainter.prototype[name] = createMethodNotSupport(name);
});
var _default = SVGPainter;
module.exports = _default;