-
Notifications
You must be signed in to change notification settings - Fork 89
Add new draw
and loadSVG
methods to the SvgRenderer
#107
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
Add new draw
and loadSVG
methods to the SvgRenderer
#107
Conversation
57b3b4f
to
f8125fd
Compare
ea0687d
to
49884ac
Compare
13bfcc6
to
62e588c
Compare
I did some weird Git stuff to squash commits-- the review feedback is addressed in "Reuse _cachedImage" |
62e588c
to
3861bce
Compare
*/ | ||
_draw (scale, onFinish) { | ||
// Convert the SVG text to an Image, and then draw it to the canvas. | ||
if (this._cachedImage) { | ||
this._drawFromImage(scale, onFinish); | ||
if (this._cachedImage === null) { |
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.
This one should stay as this._cachedImage === null
to avoid creating duplicate images
78edfeb
to
3861bce
Compare
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.
LG! Thanks!
Resolves
A step towards resolving scratchfoundation/scratch-render#518
Proposed Changes
This PR adds two public methods and one private method to
SvgRenderer
:loadSVG
, which takes in an SVG string, loads it withfromString
, creates an<img>
that can be rendered to a canvas, then calls the passed callbackdraw
(not_draw
), which synchronously draws a previously loaded SVG to theSvgRenderer.canvas
_createSVGImage
, which handles creation of the<img>
itselfIt also:
loaded
getterproperty, which returns whether or not the<img>
has finished loading and can be rendered.fromString
and_draw
. I checkedscratch-gui
,scratch-paint
, andscratch-vm
, and none of them use these methods--it's onlyscratch-render
that uses them, and with Synchronous SVG mipmaps scratch-render#527 merged, it will stop using them.Reason for Changes
Currently, the
SvgRenderer
's methods conflate:<img>
element<canvas>
at an arbitrary scaleThis can result in unpredictable behavior. For instance, the
_draw
method executes asynchronously the first time it is called after a new SVG has been loaded, as it has not yet created an<img>
from the SVG. However, subsequent calls will be synchronous, as the<img>
has been created and can be synchronously drawn to a<canvas>
.These changes make the synchronous/asynchronous split explicit with new methods that are either always synchronous or always asynchronous.