Painter.js
4.43 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
var zrLog = require("../core/log");
var vmlCore = require("./core");
var _util = require("../core/util");
var each = _util.each;
/**
* VML Painter.
*
* @module zrender/vml/Painter
*/
function parseInt10(val) {
return parseInt(val, 10);
}
/**
* @alias module:zrender/vml/Painter
*/
function VMLPainter(root, storage) {
vmlCore.initVML();
this.root = root;
this.storage = storage;
var vmlViewport = document.createElement('div');
var vmlRoot = document.createElement('div');
vmlViewport.style.cssText = 'display:inline-block;overflow:hidden;position:relative;width:300px;height:150px;';
vmlRoot.style.cssText = 'position:absolute;left:0;top:0;';
root.appendChild(vmlViewport);
this._vmlRoot = vmlRoot;
this._vmlViewport = vmlViewport;
this.resize(); // Modify storage
var oldDelFromStorage = storage.delFromStorage;
var oldAddToStorage = storage.addToStorage;
storage.delFromStorage = function (el) {
oldDelFromStorage.call(storage, el);
if (el) {
el.onRemove && el.onRemove(vmlRoot);
}
};
storage.addToStorage = function (el) {
// Displayable already has a vml node
el.onAdd && el.onAdd(vmlRoot);
oldAddToStorage.call(storage, el);
};
this._firstPaint = true;
}
VMLPainter.prototype = {
constructor: VMLPainter,
getType: function () {
return 'vml';
},
/**
* @return {HTMLDivElement}
*/
getViewportRoot: function () {
return this._vmlViewport;
},
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, true);
this._paintList(list);
},
_paintList: function (list) {
var vmlRoot = this._vmlRoot;
for (var i = 0; i < list.length; i++) {
var el = list[i];
if (el.invisible || el.ignore) {
if (!el.__alreadyNotVisible) {
el.onRemove(vmlRoot);
} // Set as already invisible
el.__alreadyNotVisible = true;
} else {
if (el.__alreadyNotVisible) {
el.onAdd(vmlRoot);
}
el.__alreadyNotVisible = false;
if (el.__dirty) {
el.beforeBrush && el.beforeBrush();
(el.brushVML || el.brush).call(el, vmlRoot);
el.afterBrush && el.afterBrush();
}
}
el.__dirty = false;
}
if (this._firstPaint) {
// Detached from document at first time
// to avoid page refreshing too many times
// FIXME 如果每次都先 removeChild 可能会导致一些填充和描边的效果改变
this._vmlViewport.appendChild(vmlRoot);
this._firstPaint = false;
}
},
resize: function (width, height) {
var width = width == null ? this._getWidth() : width;
var height = height == null ? this._getHeight() : height;
if (this._width != width || this._height != height) {
this._width = width;
this._height = height;
var vmlViewportStyle = this._vmlViewport.style;
vmlViewportStyle.width = width + 'px';
vmlViewportStyle.height = height + 'px';
}
},
dispose: function () {
this.root.innerHTML = '';
this._vmlRoot = this._vmlViewport = this.storage = null;
},
getWidth: function () {
return this._width;
},
getHeight: function () {
return this._height;
},
clear: function () {
if (this._vmlViewport) {
this.root.removeChild(this._vmlViewport);
}
},
_getWidth: function () {
var root = this.root;
var stl = root.currentStyle;
return (root.clientWidth || parseInt10(stl.width)) - parseInt10(stl.paddingLeft) - parseInt10(stl.paddingRight) | 0;
},
_getHeight: function () {
var root = this.root;
var stl = root.currentStyle;
return (root.clientHeight || parseInt10(stl.height)) - parseInt10(stl.paddingTop) - parseInt10(stl.paddingBottom) | 0;
}
}; // Not supported methods
function createMethodNotSupport(method) {
return function () {
zrLog('In IE8.0 VML mode painter not support method "' + method + '"');
};
} // Unsupported methods
each(['getLayer', 'insertLayer', 'eachLayer', 'eachBuiltinLayer', 'eachOtherLayer', 'getLayers', 'modLayer', 'delLayer', 'clearLayer', 'toDataURL', 'pathToImage'], function (name) {
VMLPainter.prototype[name] = createMethodNotSupport(name);
});
var _default = VMLPainter;
module.exports = _default;