cache svg renderer size and view offset
This commit is contained in:
@@ -30,6 +30,24 @@ class SVGSkin extends Skin {
|
||||
|
||||
/** @type {Number} */
|
||||
this._maxTextureScale = 0;
|
||||
|
||||
/**
|
||||
* The natural size, in Scratch units, of this skin.
|
||||
* @type {Array<number>}
|
||||
*/
|
||||
this.size = [0, 0];
|
||||
|
||||
/**
|
||||
* The viewbox offset of the svg.
|
||||
* @type {Array<number>}
|
||||
*/
|
||||
this._viewOffset = [0, 0];
|
||||
|
||||
/**
|
||||
* The rotation center before offset by _viewOffset.
|
||||
* @type {Array<number>}
|
||||
*/
|
||||
this._rawRotationCenter = [NaN, NaN];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,21 +61,17 @@ class SVGSkin extends Skin {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {Array<number>} the natural size, in Scratch units, of this skin.
|
||||
*/
|
||||
get size () {
|
||||
return this._svgRenderer.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the origin, in object space, about which this Skin should rotate.
|
||||
* @param {number} x - The x coordinate of the new rotation center.
|
||||
* @param {number} y - The y coordinate of the new rotation center.
|
||||
*/
|
||||
setRotationCenter (x, y) {
|
||||
const viewOffset = this._svgRenderer.viewOffset;
|
||||
super.setRotationCenter(x - viewOffset[0], y - viewOffset[1]);
|
||||
if (x !== this._rawRotationCenter[0] || y !== this._rawRotationCenter[1]) {
|
||||
this._rawRotationCenter[0] = x;
|
||||
this._rawRotationCenter[1] = y;
|
||||
super.setRotationCenter(x - this._viewOffset[0], y - this._viewOffset[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,7 +148,11 @@ class SVGSkin extends Skin {
|
||||
}
|
||||
|
||||
if (typeof rotationCenter === 'undefined') rotationCenter = this.calculateRotationCenter();
|
||||
this.setRotationCenter.apply(this, rotationCenter);
|
||||
this.size = this._svgRenderer.size;
|
||||
this._viewOffset = this._svgRenderer.viewOffset;
|
||||
// Reset rawRotationCenter when we update viewOffset.
|
||||
this._rawRotationCenter = [NaN, NaN];
|
||||
this.setRotationCenter(rotationCenter[0], rotationCenter[1]);
|
||||
this.emit(Skin.Events.WasAltered);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user