-
Notifications
You must be signed in to change notification settings - Fork 87
Clean up SVG renderer code #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fsih
merged 3 commits into
scratchfoundation:develop
from
adroitwhiz:subpixel-v3-cleanups
Feb 19, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,41 @@ class SvgRenderer { | |
* @constructor | ||
*/ | ||
constructor (canvas) { | ||
/** | ||
* The canvas that this SVG renderer will render to. | ||
* @type {HTMLCanvasElement} | ||
* @private | ||
*/ | ||
this._canvas = canvas || document.createElement('canvas'); | ||
this._context = this._canvas.getContext('2d'); | ||
|
||
/** | ||
* A measured SVG "viewbox" | ||
* @typedef {object} SvgRenderer#SvgMeasurements | ||
* @property {number} x - The left edge of the SVG viewbox. | ||
* @property {number} y - The top edge of the SVG viewbox. | ||
* @property {number} width - The width of the SVG viewbox. | ||
* @property {number} height - The height of the SVG viewbox. | ||
*/ | ||
|
||
/** | ||
* The measurement box of the currently loaded SVG. | ||
* @type {SvgRenderer#SvgMeasurements} | ||
* @private | ||
*/ | ||
this._measurements = {x: 0, y: 0, width: 0, height: 0}; | ||
|
||
/** | ||
* The `<img>` element with the contents of the currently loaded SVG. | ||
* @type {?HTMLImageElement} | ||
* @private | ||
*/ | ||
this._cachedImage = null; | ||
|
||
/** | ||
* True if this renderer's current SVG is loaded and can be rendered to the canvas. | ||
* @type {boolean} | ||
*/ | ||
this.loaded = false; | ||
} | ||
|
||
|
@@ -30,22 +61,6 @@ class SvgRenderer { | |
return this._canvas; | ||
} | ||
|
||
/** | ||
* Load an SVG from a string and draw it. | ||
* This will be parsed and transformed, and finally drawn. | ||
* When drawing is finished, the `onFinish` callback is called. | ||
* @param {string} svgString String of SVG data to draw in quirks-mode. | ||
* @param {number} [scale] - Optionally, also scale the image by this factor. | ||
* @param {Function} [onFinish] Optional callback for when drawing finished. | ||
* @deprecated Use the `loadSVG` method and public `draw` method instead. | ||
*/ | ||
fromString (svgString, scale, onFinish) { | ||
this.loadSVG(svgString, false, () => { | ||
this.draw(scale); | ||
if (onFinish) onFinish(); | ||
}); | ||
} | ||
|
||
/** | ||
* Load an SVG from a string and measure it. | ||
* @param {string} svgString String of SVG data to draw in quirks-mode. | ||
|
@@ -438,25 +453,6 @@ class SvgRenderer { | |
this._drawFromImage(scale); | ||
} | ||
|
||
/** | ||
* Asynchronously draw the (possibly non-loaded) SVG to a canvas. | ||
* @param {number} [scale] - Optionally, also scale the image by this factor. | ||
* @param {Function} [onFinish] - An optional callback to call when the draw operation is complete. | ||
* @deprecated Use the `loadSVG` and public `draw` method instead. | ||
*/ | ||
_draw (scale, onFinish) { | ||
// Convert the SVG text to an Image, and then draw it to the canvas. | ||
if (this._cachedImage === null) { | ||
this._createSVGImage(() => { | ||
this._drawFromImage(scale); | ||
onFinish(); | ||
}); | ||
} else { | ||
this._drawFromImage(scale); | ||
onFinish(); | ||
} | ||
} | ||
|
||
/** | ||
* Draw to the canvas from a loaded image element. | ||
* @param {number} [scale] - Optionally, also scale the image by this factor. | ||
|
@@ -478,13 +474,8 @@ class SvgRenderer { | |
this._cachedImage.naturalHeight <= 0 | ||
) return; | ||
this._context.clearRect(0, 0, this._canvas.width, this._canvas.height); | ||
this._context.scale(ratio, ratio); | ||
this._context.setTransform(ratio, 0, 0, ratio, 0, 0); | ||
this._context.drawImage(this._cachedImage, 0, 0); | ||
// Reset the canvas transform after drawing. | ||
this._context.setTransform(1, 0, 0, 1, 0, 0); | ||
// Set the CSS style of the canvas to the actual measurements. | ||
this._canvas.style.width = bbox.width; | ||
this._canvas.style.height = bbox.height; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copying this comment over from the other PR: this was added way back in scratchfoundation/scratch-render@b35f684 and we were unable to determine why, exactly. |
||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea what changed here--doesn't seem to be missing a newline