Skip to content

Commit

Permalink
Use one SVG image and track loaded state w/ flag
Browse files Browse the repository at this point in the history
Instead of creating a new image every time `setSVG` is called and
checking whether that image is complete with `loaded`, reuse the same
image, override its `src`, & use `onload` instead of `addEventListener`
to replace the previous event listener. This is necessary to avoid race
conditions with the `_svgImageLoaded` flag.
  • Loading branch information
adroitwhiz committed Jun 10, 2021
1 parent 801b0da commit 7af5b3e
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/SVGSkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ class SVGSkin extends Skin {
this._renderer = renderer;

/** @type {HTMLImageElement} */
this._svgImage = null;
this._svgImage = document.createElement('img');

/** @type {boolean} */
this._svgImageLoaded = false;

/** @type {Array<number>} */
this._size = [0, 0];
Expand Down Expand Up @@ -165,7 +168,7 @@ class SVGSkin extends Skin {
// Can't use bitwise stuff here because we need to handle negative exponents
const mipScale = Math.pow(2, mipLevel - INDEX_OFFSET);

if (this._svgImage.complete && !this._scaledMIPs[mipLevel]) {
if (this._svgImageLoaded && !this._scaledMIPs[mipLevel]) {
this._scaledMIPs[mipLevel] = this.createMIP(mipScale);
}

Expand All @@ -191,10 +194,10 @@ class SVGSkin extends Skin {
*/
setSVG (svgData, rotationCenter) {
const svgTag = loadSvgString(svgData);
this._svgImage = document.createElement('img');
const svgText = serializeSvgToString(svgTag, true /* shouldInjectFonts */);
this._svgImageLoaded = false;

this._svgImage.addEventListener('load', () => {
this._svgImage.onload = () => {
const {x, y, width, height} = svgTag.viewBox.baseVal;
this._size[0] = width;
this._size[1] = height;
Expand All @@ -218,8 +221,10 @@ class SVGSkin extends Skin {
this._rotationCenter[0] = rotationCenter[0] - x;
this._rotationCenter[1] = rotationCenter[1] - y;

this._svgImageLoaded = true;

this.emit(Skin.Events.WasAltered);
});
};

this._svgImage.src = `data:image/svg+xml;utf8,${encodeURIComponent(svgText)}`;
}
Expand Down

0 comments on commit 7af5b3e

Please sign in to comment.