From 959c635f58f7245d093468b2c98a8db29c768788 Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford Date: Tue, 14 Feb 2017 15:59:45 -0800 Subject: [PATCH] SVG: compensate for viewbox offset In Scratch 2.0, an SVG costume's origin is stored relative to that SVG's viewbox. In order to use the origin values correctly, then, we need to compensate for the viewbox offset. --- src/SVGSkin.js | 10 ++++++++++ src/svg-quirks-mode/svg-renderer.js | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/src/SVGSkin.js b/src/SVGSkin.js index 4d68ca0..86a3c1c 100644 --- a/src/SVGSkin.js +++ b/src/SVGSkin.js @@ -40,6 +40,16 @@ class SVGSkin extends Skin { 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]); + } + /** * @param {[number,number]} scale - The scaling factors to be used. * @return {WebGLTexture} The GL texture representation of this skin when drawing at the given scale. diff --git a/src/svg-quirks-mode/svg-renderer.js b/src/svg-quirks-mode/svg-renderer.js index b6c133b..6c5ff4f 100644 --- a/src/svg-quirks-mode/svg-renderer.js +++ b/src/svg-quirks-mode/svg-renderer.js @@ -80,6 +80,13 @@ class SvgRenderer { return [this._measurements.width, this._measurements.height]; } + /** + * @return {[number,number]} the offset (upper left corner) of the SVG's view box. + */ + get viewOffset () { + return [this._measurements.x, this._measurements.y]; + } + /** * Transforms an SVG's text elements for Scratch 2.0 quirks. * These quirks include: