Skip to content

Commit 38e2676

Browse files
committed
Remove SvgRenderer._draw call from setSVG
1 parent bc893f8 commit 38e2676

File tree

1 file changed

+28
-39
lines changed

1 file changed

+28
-39
lines changed

src/SVGSkin.js

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class SVGSkin extends Skin {
2222
/** @type {SvgRenderer} */
2323
this._svgRenderer = new SvgRenderer();
2424

25+
/** @type {boolean} */
26+
this._svgDirty = false;
27+
2528
/** @type {WebGLTexture} */
2629
this._texture = null;
2730

@@ -66,7 +69,9 @@ class SVGSkin extends Skin {
6669
*/
6770
// eslint-disable-next-line no-unused-vars
6871
getTexture (scale) {
69-
if (!this._svgRenderer.canvas.width || !this._svgRenderer.canvas.height) {
72+
if (this._svgRenderer.canvas.width === 0 ||
73+
this._svgRenderer.canvas.height === 0 ||
74+
!this._svgRenderer.loaded) {
7075
return super.getTexture();
7176
}
7277

@@ -77,20 +82,22 @@ class SVGSkin extends Skin {
7782
while ((newScale < this._maxTextureScale) && (requestedScale >= 1.5 * newScale)) {
7883
newScale *= 2;
7984
}
80-
if (this._textureScale !== newScale) {
85+
if (this._svgDirty || this._textureScale !== newScale) {
8186
this._textureScale = newScale;
82-
this._svgRenderer._draw(this._textureScale, () => {
83-
if (this._textureScale === newScale) {
84-
const canvas = this._svgRenderer.canvas;
85-
const context = canvas.getContext('2d');
86-
const textureData = context.getImageData(0, 0, canvas.width, canvas.height);
87-
88-
const gl = this._renderer.gl;
89-
gl.bindTexture(gl.TEXTURE_2D, this._texture);
90-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureData);
91-
this._silhouette.update(textureData);
92-
}
93-
});
87+
this._svgRenderer.draw(this._textureScale);
88+
// Pull out the ImageData from the canvas. ImageData speeds up
89+
// updating Silhouette and is better handled by more browsers in
90+
// regards to memory.
91+
const canvas = this._svgRenderer.canvas;
92+
const context = canvas.getContext('2d');
93+
const textureData = context.getImageData(0, 0, canvas.width, canvas.height);
94+
95+
const gl = this._renderer.gl;
96+
gl.bindTexture(gl.TEXTURE_2D, this._texture);
97+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureData);
98+
this._silhouette.update(textureData);
99+
100+
this._svgDirty = false;
94101
}
95102

96103
return this._texture;
@@ -104,39 +111,19 @@ class SVGSkin extends Skin {
104111
* @fires Skin.event:WasAltered
105112
*/
106113
setSVG (svgData, rotationCenter) {
107-
this._svgRenderer.fromString(svgData, 1, () => {
114+
this._svgRenderer.loadSVG(svgData, false, () => {
108115
const gl = this._renderer.gl;
109-
this._textureScale = this._maxTextureScale = 1;
110-
111-
// Pull out the ImageData from the canvas. ImageData speeds up
112-
// updating Silhouette and is better handled by more browsers in
113-
// regards to memory.
114-
const canvas = this._svgRenderer.canvas;
115-
116-
if (!canvas.width || !canvas.height) {
117-
super.setEmptyImageData();
118-
return;
119-
}
120116

121-
const context = canvas.getContext('2d');
122-
const textureData = context.getImageData(0, 0, canvas.width, canvas.height);
123-
124-
if (this._texture) {
125-
gl.bindTexture(gl.TEXTURE_2D, this._texture);
126-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureData);
127-
this._silhouette.update(textureData);
128-
} else {
117+
if (this._texture === null) {
129118
// TODO: mipmaps?
130119
const textureOptions = {
131-
auto: true,
132-
wrap: gl.CLAMP_TO_EDGE,
133-
src: textureData
120+
auto: false,
121+
wrap: gl.CLAMP_TO_EDGE
134122
};
135-
136123
this._texture = twgl.createTexture(gl, textureOptions);
137-
this._silhouette.update(textureData);
138124
}
139125

126+
this._maxTextureScale = 1;
140127
const maxDimension = Math.max(this._svgRenderer.canvas.width, this._svgRenderer.canvas.height);
141128
let testScale = 2;
142129
for (testScale; maxDimension * testScale <= MAX_TEXTURE_DIMENSION; testScale *= 2) {
@@ -146,6 +133,8 @@ class SVGSkin extends Skin {
146133
if (typeof rotationCenter === 'undefined') rotationCenter = this.calculateRotationCenter();
147134
this.setRotationCenter.apply(this, rotationCenter);
148135
this.emit(Skin.Events.WasAltered);
136+
137+
this._svgDirty = true;
149138
});
150139
}
151140

0 commit comments

Comments
 (0)