UNPKG

scratch-render

Version:
1,184 lines (236 loc) • 21.1 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>SvgRenderer - Documentation</title> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc.css"> </head> <body> <input type="checkbox" id="nav-trigger" class="nav-trigger" /> <label for="nav-trigger" class="navicon-button x"> <div class="navicon"></div> </label> <label for="nav-trigger" class="overlay"></label> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BitmapSkin.html">BitmapSkin</a><ul class='methods'><li data-type='method'><a href="BitmapSkin.html#._getBitmapSize">_getBitmapSize</a></li><li data-type='method'><a href="BitmapSkin.html#calculateRotationCenter">calculateRotationCenter</a></li><li data-type='method'><a href="BitmapSkin.html#dispose">dispose</a></li><li data-type='method'><a href="BitmapSkin.html#getTexture">getTexture</a></li><li data-type='method'><a href="BitmapSkin.html#getUniforms">getUniforms</a></li><li data-type='method'><a href="BitmapSkin.html#setBitmap">setBitmap</a></li><li data-type='method'><a href="BitmapSkin.html#setRotationCenter">setRotationCenter</a></li></ul></li><li><a href="Drawable.html">Drawable</a><ul class='methods'><li data-type='method'><a href="Drawable.html#.color3bToID">color3bToID</a></li><li data-type='method'><a href="Drawable.html#.color4fFromID">color4fFromID</a></li><li data-type='method'><a href="Drawable.html#_calculateTransform">_calculateTransform</a></li><li data-type='method'><a href="Drawable.html#_skinWasAltered">_skinWasAltered</a></li><li data-type='method'><a href="Drawable.html#dispose">dispose</a></li><li data-type='method'><a href="Drawable.html#getAABB">getAABB</a></li><li data-type='method'><a href="Drawable.html#getBounds">getBounds</a></li><li data-type='method'><a href="Drawable.html#getEnabledEffects">getEnabledEffects</a></li><li data-type='method'><a href="Drawable.html#getFastBounds">getFastBounds</a></li><li data-type='method'><a href="Drawable.html#getUniforms">getUniforms</a></li><li data-type='method'><a href="Drawable.html#getVisible">getVisible</a></li><li data-type='method'><a href="Drawable.html#needsConvexHullPoints">needsConvexHullPoints</a></li><li data-type='method'><a href="Drawable.html#setConvexHullDirty">setConvexHullDirty</a></li><li data-type='method'><a href="Drawable.html#setConvexHullPoints">setConvexHullPoints</a></li><li data-type='method'><a href="Drawable.html#setTransformDirty">setTransformDirty</a></li><li data-type='method'><a href="Drawable.html#updateProperties">updateProperties</a></li></ul></li><li><a href="PenSkin.html">PenSkin</a><ul class='methods'><li data-type='method'><a href="PenSkin.html#_setAttributes">_setAttributes</a></li><li data-type='method'><a href="PenSkin.html#_setCanvasSize">_setCanvasSize</a></li><li data-type='method'><a href="PenSkin.html#calculateRotationCenter">calculateRotationCenter</a></li><li data-type='method'><a href="PenSkin.html#clear">clear</a></li><li data-type='method'><a href="PenSkin.html#dispose">dispose</a></li><li data-type='method'><a href="PenSkin.html#drawLine">drawLine</a></li><li data-type='method'><a href="PenSkin.html#drawPoint">drawPoint</a></li><li data-type='method'><a href="PenSkin.html#drawStamp">drawStamp</a></li><li data-type='method'><a href="PenSkin.html#getTexture">getTexture</a></li><li data-type='method'><a href="PenSkin.html#getUniforms">getUniforms</a></li><li data-type='method'><a href="PenSkin.html#onNativeSizeChanged">onNativeSizeChanged</a></li><li data-type='method'><a href="PenSkin.html#setRotationCenter">setRotationCenter</a></li></ul></li><li><a href="Rectangle.html">Rectangle</a><ul class='methods'><li data-type='method'><a href="Rectangle.html#clamp">clamp</a></li><li data-type='method'><a href="Rectangle.html#contains">contains</a></li><li data-type='method'><a href="Rectangle.html#initFromBounds">initFromBounds</a></li><li data-type='method'><a href="Rectangle.html#initFromPointsAABB">initFromPointsAABB</a></li><li data-type='method'><a href="Rectangle.html#intersects">intersects</a></li><li data-type='method'><a href="Rectangle.html#snapToInt">snapToInt</a></li></ul></li><li><a href="RenderWebGL.html">RenderWebGL</a><ul class='methods'><li data-type='method'><a href="RenderWebGL.html#_createGeometry">_createGeometry</a></li><li data-type='method'><a href="RenderWebGL.html#_drawThese">_drawThese</a></li><li data-type='method'><a href="RenderWebGL.html#_filterCandidatesTouching">_filterCandidatesTouching</a></li><li data-type='method'><a href="RenderWebGL.html#_getConvexHullPointsForDrawable">_getConvexHullPointsForDrawable</a></li><li data-type='method'><a href="RenderWebGL.html#_setNativeSize">_setNativeSize</a></li><li data-type='method'><a href="RenderWebGL.html#_touchingBounds">_touchingBounds</a></li><li data-type='method'><a href="RenderWebGL.html#createBitmapSkin">createBitmapSkin</a></li><li data-type='method'><a href="RenderWebGL.html#createDrawable">createDrawable</a></li><li data-type='method'><a href="RenderWebGL.html#createPenSkin">createPenSkin</a></li><li data-type='method'><a href="RenderWebGL.html#createSkinFromURL">createSkinFromURL</a></li><li data-type='method'><a href="RenderWebGL.html#createSVGSkin">createSVGSkin</a></li><li data-type='method'><a href="RenderWebGL.html#destroyDrawable">destroyDrawable</a></li><li data-type='method'><a href="RenderWebGL.html#destroySkin">destroySkin</a></li><li data-type='method'><a href="RenderWebGL.html#draw">draw</a></li><li data-type='method'><a href="RenderWebGL.html#extractDrawable">extractDrawable</a></li><li data-type='method'><a href="RenderWebGL.html#getBounds">getBounds</a></li><li data-type='method'><a href="RenderWebGL.html#getFencedPositionOfDrawable">getFencedPositionOfDrawable</a></li><li data-type='method'><a href="RenderWebGL.html#getNativeSize">getNativeSize</a></li><li data-type='method'><a href="RenderWebGL.html#getSkinSize">getSkinSize</a></li><li data-type='method'><a href="RenderWebGL.html#isTouchingColor">isTouchingColor</a></li><li data-type='method'><a href="RenderWebGL.html#isTouchingDrawables">isTouchingDrawables</a></li><li data-type='method'><a href="RenderWebGL.html#onNativeSizeChanged">onNativeSizeChanged</a></li><li data-type='method'><a href="RenderWebGL.html#penClear">penClear</a></li><li data-type='method'><a href="RenderWebGL.html#penLine">penLine</a></li><li data-type='method'><a href="RenderWebGL.html#penPoint">penPoint</a></li><li data-type='method'><a href="RenderWebGL.html#penStamp">penStamp</a></li><li data-type='method'><a href="RenderWebGL.html#pick">pick</a></li><li data-type='method'><a href="RenderWebGL.html#resize">resize</a></li><li data-type='method'><a href="RenderWebGL.html#setBackgroundColor">setBackgroundColor</a></li><li data-type='method'><a href="RenderWebGL.html#setDebugCanvas">setDebugCanvas</a></li><li data-type='method'><a href="RenderWebGL.html#setDrawableOrder">setDrawableOrder</a></li><li data-type='method'><a href="RenderWebGL.html#setStageSize">setStageSize</a></li><li data-type='method'><a href="RenderWebGL.html#updateDrawableProperties">updateDrawableProperties</a></li></ul></li><li><a href="ShaderManager.html">ShaderManager</a><ul class='methods'><li data-type='method'><a href="ShaderManager.html#_buildShader">_buildShader</a></li><li data-type='method'><a href="ShaderManager.html#getShader">getShader</a></li></ul></li><li><a href="Skin.html">Skin</a><ul class='methods'><li data-type='method'><a href="Skin.html#calculateRotationCenter">calculateRotationCenter</a></li><li data-type='method'><a href="Skin.html#dispose">dispose</a></li><li data-type='method'><a href="Skin.html#getTexture">getTexture</a></li><li data-type='method'><a href="Skin.html#getUniforms">getUniforms</a></li><li data-type='method'><a href="Skin.html#setRotationCenter">setRotationCenter</a></li></ul></li><li><a href="SvgRenderer.html">SvgRenderer</a><ul class='methods'><li data-type='method'><a href="SvgRenderer.html#_createSVGElement">_createSVGElement</a></li><li data-type='method'><a href="SvgRenderer.html#_draw">_draw</a></li><li data-type='method'><a href="SvgRenderer.html#_toString">_toString</a></li><li data-type='method'><a href="SvgRenderer.html#_transformMeasurements">_transformMeasurements</a></li><li data-type='method'><a href="SvgRenderer.html#_transformText">_transformText</a></li><li data-type='method'><a href="SvgRenderer.html#fromString">fromString</a></li><li data-type='method'><a href="SvgRenderer.html#getDrawRatio">getDrawRatio</a></li></ul></li><li><a href="SVGSkin.html">SVGSkin</a><ul class='methods'><li data-type='method'><a href="SVGSkin.html#calculateRotationCenter">calculateRotationCenter</a></li><li data-type='method'><a href="SVGSkin.html#dispose">dispose</a></li><li data-type='method'><a href="SVGSkin.html#getTexture">getTexture</a></li><li data-type='method'><a href="SVGSkin.html#getUniforms">getUniforms</a></li><li data-type='method'><a href="SVGSkin.html#setRotationCenter">setRotationCenter</a></li><li data-type='method'><a href="SVGSkin.html#setSVG">setSVG</a></li></ul></li></ul><h3>Modules</h3><ul><li><a href="module-RenderConstants.html">RenderConstants</a></li></ul><h3>Events</h3><ul><li><a href="RenderWebGL.html#event:event:NativeSizeChanged">event:NativeSizeChanged</a></li><li><a href="Skin.html#.event:event:WasAltered">event:WasAltered</a></li></ul><h3>Global</h3><ul><li><a href="global.html#FENCE_WIDTH">FENCE_WIDTH</a></li></ul> </nav> <div id="main"> <h1 class="page-title">SvgRenderer</h1> <section> <header> <h2> SvgRenderer </h2> <div class="class-description"><p>Main quirks-mode SVG rendering code.</p></div> </header> <article> <div class="container-overview"> <h2>Constructor</h2> <h4 class="name" id="SvgRenderer"><span class="type-signature"></span>new SvgRenderer<span class="signature">(canvas<span class="signature-attributes">opt</span>)</span><span class="type-signature"></span></h4> <dl class="details"> </dl> <div class="description"> <p>Create a quirks-mode SVG renderer for a particular canvas.</p> </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Attributes</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>canvas</code></td> <td class="type"> <span class="param-type">HTMLCanvasElement</span> </td> <td class="attributes"> &lt;optional><br> </td> <td class="description last"><p>An optional canvas element to draw to. If this is not provided, the renderer will create a new canvas.</p></td> </tr> </tbody> </table> </div> <h3 class="subsection-title">Members</h3> <h4 class="name" id="canvas"><span class="type-signature"></span>canvas<span class="type-signature"></span></h4> <dl class="details"> </dl> <h4 class="name" id="size"><span class="type-signature"></span>size<span class="type-signature"></span></h4> <dl class="details"> </dl> <h4 class="name" id="viewOffset"><span class="type-signature"></span>viewOffset<span class="type-signature"></span></h4> <dl class="details"> </dl> <h3 class="subsection-title">Methods</h3> <h4 class="name" id="_createSVGElement"><span class="type-signature"></span>_createSVGElement<span class="signature">(tagName)</span><span class="type-signature"> &rarr; (non-null) {DOMElement}</span></h4> <dl class="details"> </dl> <div class="description"> <p>Helper to create an SVG element with the correct NS.</p> </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>tagName</code></td> <td class="type"> <span class="param-type">string</span> </td> <td class="description last"><p>Tag name for the element.</p></td> </tr> </tbody> </table> <h5>Returns:</h5> <div class="param-desc"> <p>Element created.</p> </div> <dl class="param-type"> <dt> Type </dt> <dd> <span class="param-type">DOMElement</span> </dd> </dl> <h4 class="name" id="_draw"><span class="type-signature"></span>_draw<span class="signature">()</span><span class="type-signature"></span></h4> <dl class="details"> </dl> <div class="description"> <p>Draw the SVG to a canvas.</p> </div> <h4 class="name" id="_toString"><span class="type-signature"></span>_toString<span class="signature">()</span><span class="type-signature"> &rarr; {string}</span></h4> <dl class="details"> </dl> <div class="description"> <p>Serialize the active SVG DOM to a string.</p> </div> <h5>Returns:</h5> <div class="param-desc"> <p>String representing current SVG data.</p> </div> <dl class="param-type"> <dt> Type </dt> <dd> <span class="param-type">string</span> </dd> </dl> <h4 class="name" id="_transformMeasurements"><span class="type-signature"></span>_transformMeasurements<span class="signature">()</span><span class="type-signature"></span></h4> <dl class="details"> </dl> <div class="description"> <p>Transform the measurements of the SVG. In Scratch 2.0, SVGs are drawn without respect to the width, height, and viewBox attribute on the tag. The exporter does output these properties - but they appear to be incorrect often. To address the incorrect measurements, we append the DOM to the document, and then use SVG's native <code>getBBox</code> to find the real drawn dimensions. This ensures things drawn in negative dimensions, outside the given viewBox, etc., are all eventually drawn to the canvas. I tried to do this several other ways: stripping the width/height/viewBox attributes and then drawing (Firefox won't draw anything), or inflating them and then measuring a canvas. But this seems to be a natural and performant way.</p> </div> <h4 class="name" id="_transformText"><span class="type-signature"></span>_transformText<span class="signature">()</span><span class="type-signature"></span></h4> <dl class="details"> </dl> <div class="description"> <p>Transforms an SVG's text elements for Scratch 2.0 quirks. These quirks include:</p> <ol> <li><code>x</code> and <code>y</code> properties are removed/ignored.</li> <li>Alignment is set to <code>text-before-edge</code>.</li> <li>Line-breaks are converted to explicit <tspan> elements.</li> <li>Any required fonts are injected.</li> </ol> </div> <h4 class="name" id="fromString"><span class="type-signature"></span>fromString<span class="signature">(svgString, onFinish<span class="signature-attributes">opt</span>)</span><span class="type-signature"></span></h4> <dl class="details"> </dl> <div class="description"> <p>Load an SVG from a string and draw it. This will be parsed and transformed, and finally drawn. When drawing is finished, the <code>onFinish</code> callback is called.</p> </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Attributes</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>svgString</code></td> <td class="type"> <span class="param-type">string</span> </td> <td class="attributes"> </td> <td class="description last"><p>String of SVG data to draw in quirks-mode.</p></td> </tr> <tr> <td class="name"><code>onFinish</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="attributes"> &lt;optional><br> </td> <td class="description last"><p>Optional callback for when drawing finished.</p></td> </tr> </tbody> </table> <h4 class="name" id="getDrawRatio"><span class="type-signature"></span>getDrawRatio<span class="signature">()</span><span class="type-signature"> &rarr; {number}</span></h4> <dl class="details"> </dl> <div class="description"> <p>Get the drawing ratio, adjusted for HiDPI screens.</p> </div> <h5>Returns:</h5> <div class="param-desc"> <p>Scale ratio to draw to canvases with.</p> </div> <dl class="param-type"> <dt> Type </dt> <dd> <span class="param-type">number</span> </dd> </dl> </article> </section> </div> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> using the <a href="https://github.com/clenemt/docdash">docdash</a> theme. </footer> <script>prettyPrint();</script> <script src="scripts/linenumber.js"></script> </body> </html>