Compare commits

...

1 Commits

Author SHA1 Message Date
Christopher Willis-Ford
0ec6f4f6f0 expose the async nature of createSVGSkin and updateSVGSkin 2020-10-13 17:57:27 -07:00
2 changed files with 14 additions and 6 deletions

View File

@@ -350,12 +350,13 @@ class RenderWebGL extends EventEmitter {
* @param {!string} svgData - new SVG to use. * @param {!string} svgData - new SVG to use.
* @param {?Array<number>} rotationCenter Optional: rotation center of the skin. If not supplied, the center of the * @param {?Array<number>} rotationCenter Optional: rotation center of the skin. If not supplied, the center of the
* skin will be used * skin will be used
* @param {function} [onSkinReady] - Optional: if present, will be called after the skin loads
* @returns {!int} the ID for the new skin. * @returns {!int} the ID for the new skin.
*/ */
createSVGSkin (svgData, rotationCenter) { createSVGSkin (svgData, rotationCenter, onSkinReady) {
const skinId = this._nextSkinId++; const skinId = this._nextSkinId++;
const newSkin = new SVGSkin(skinId, this); const newSkin = new SVGSkin(skinId, this);
newSkin.setSVG(svgData, rotationCenter); newSkin.setSVG(svgData, rotationCenter, onSkinReady);
this._allSkins[skinId] = newSkin; this._allSkins[skinId] = newSkin;
return skinId; return skinId;
} }
@@ -393,6 +394,7 @@ class RenderWebGL extends EventEmitter {
* @param {!string} svgData - new SVG to use. * @param {!string} svgData - new SVG to use.
* @param {?Array<number>} rotationCenter Optional: rotation center of the skin. If not supplied, the center of the * @param {?Array<number>} rotationCenter Optional: rotation center of the skin. If not supplied, the center of the
* skin will be used * skin will be used
* @returns {Promise} - a promise which resolves after the skin has updated
*/ */
updateSVGSkin (skinId, svgData, rotationCenter) { updateSVGSkin (skinId, svgData, rotationCenter) {
if (this._allSkins[skinId] instanceof SVGSkin) { if (this._allSkins[skinId] instanceof SVGSkin) {
@@ -401,8 +403,9 @@ class RenderWebGL extends EventEmitter {
} }
const newSkin = new SVGSkin(skinId, this); const newSkin = new SVGSkin(skinId, this);
newSkin.setSVG(svgData, rotationCenter); return newSkin.setSVG(svgData, rotationCenter).then(() => {
this._reskin(skinId, newSkin); this._reskin(skinId, newSkin);
});
} }
/** /**

View File

@@ -140,9 +140,11 @@ class SVGSkin extends Skin {
* Set the contents of this skin to a snapshot of the provided SVG data. * Set the contents of this skin to a snapshot of the provided SVG data.
* @param {string} svgData - new SVG to use. * @param {string} svgData - new SVG to use.
* @param {Array<number>} [rotationCenter] - Optional rotation center for the SVG. * @param {Array<number>} [rotationCenter] - Optional rotation center for the SVG.
* @param {function} [onSkinReady] - Optional: if present, will be called after the skin loads
* @returns {Promise} - a promise which resolves after the SVG is loaded and ready
*/ */
setSVG (svgData, rotationCenter) { setSVG (svgData, rotationCenter, onSkinReady) {
this._svgRenderer.loadSVG(svgData, false, () => { return this._svgRenderer.loadSVG(svgData, false, () => {
const svgSize = this._svgRenderer.size; const svgSize = this._svgRenderer.size;
if (svgSize[0] === 0 || svgSize[1] === 0) { if (svgSize[0] === 0 || svgSize[1] === 0) {
super.setEmptyImageData(); super.setEmptyImageData();
@@ -163,6 +165,9 @@ class SVGSkin extends Skin {
this._rotationCenter[1] = rotationCenter[1] - viewOffset[1]; this._rotationCenter[1] = rotationCenter[1] - viewOffset[1];
this.emit(Skin.Events.WasAltered); this.emit(Skin.Events.WasAltered);
if (onSkinReady) {
onSkinReady();
}
}); });
} }