UNPKG

scrawl-canvas

Version:
634 lines (478 loc) 37.6 kB
<!DOCTYPE html> <html> <head> <title>Demo Snippets 006</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> <link rel="stylesheet" media="all" href="../../docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <ul id="jump_to"> <li> <a class="large" href="javascript:void(0);">Jump To &hellip;</a> <a class="small" href="javascript:void(0);">+</a> <div id="jump_wrapper"> <div id="jump_page_wrapper"> <div id="jump_page"> <a class="source" href="../modules/canvas-minimap.html"> ./demo/modules/canvas-minimap.js </a> <a class="source" href="../modules/canvas-scene-editor.html"> ./demo/modules/canvas-scene-editor.js </a> <a class="source" href="../modules/dom-entity-editor.html"> ./demo/modules/dom-entity-editor.js </a> <a class="source" href="../modules/entity-copy-paste.html"> ./demo/modules/entity-copy-paste.js </a> <a class="source" href="../modules/entity-manipulation-gui.html"> ./demo/modules/entity-manipulation-gui.js </a> <a class="source" href="../modules/entity-navigation.html"> ./demo/modules/entity-navigation.js </a> <a class="source" href="../modules/entity-ring-builder.html"> ./demo/modules/entity-ring-builder.js </a> <a class="source" href="../modules/london-crime-graphic.html"> ./demo/modules/london-crime-graphic.js </a> <a class="source" href="../modules/london-crime-lines.html"> ./demo/modules/london-crime-lines.js </a> <a class="source" href="../modules/london-crime-stacked-bars.html"> ./demo/modules/london-crime-stacked-bars.js </a> <a class="source" href="../modules/lottie-loader.html"> ./demo/modules/lottie-loader.js </a> <a class="source" href="../modules/simple-chart-frame-tests.html"> ./demo/modules/simple-chart-frame-tests.js </a> <a class="source" href="../modules/simple-chart-frame.html"> ./demo/modules/simple-chart-frame.js </a> <a class="source" href="../modules/simple-graph-lines.html"> ./demo/modules/simple-graph-lines.js </a> <a class="source" href="../modules/simple-graph-stacked-bars.html"> ./demo/modules/simple-graph-stacked-bars.js </a> <a class="source" href="../modules/wikipedia-views-spiral-chart.html"> ./demo/modules/wikipedia-views-spiral-chart.js </a> <a class="source" href="animated-highlight-gradient-text-snippet.html"> ./demo/snippets/animated-highlight-gradient-text-snippet.js </a> <a class="source" href="animated-hover-gradient-snippet.html"> ./demo/snippets/animated-hover-gradient-snippet.js </a> <a class="source" href="animated-word-gradient-snippet.html"> ./demo/snippets/animated-word-gradient-snippet.js </a> <a class="source" href="before-after-slider-infographic.html"> ./demo/snippets/before-after-slider-infographic.js </a> <a class="source" href="bubbles-text-snippet.html"> ./demo/snippets/bubbles-text-snippet.js </a> <a class="source" href="green-box-snippet.html"> ./demo/snippets/green-box-snippet.js </a> <a class="source" href="jazzy-button-snippet.html"> ./demo/snippets/jazzy-button-snippet.js </a> <a class="source" href="page-performance-snippet-test.html"> ./demo/snippets/page-performance-snippet-test.js </a> <a class="source" href="page-performance-snippet.html"> ./demo/snippets/page-performance-snippet.js </a> <a class="source" href="pan-image-snippet.html"> ./demo/snippets/pan-image-snippet.js </a> <a class="source" href="placeholder-effect-snippet.html"> ./demo/snippets/placeholder-effect-snippet.js </a> <a class="source" href="ripple-effect-snippet.html"> ./demo/snippets/ripple-effect-snippet.js </a> <a class="source" href="risograph-text-gradient-snippet.html"> ./demo/snippets/risograph-text-gradient-snippet.js </a> <a class="source" href="spotlight-text-snippet-test.html"> ./demo/snippets/spotlight-text-snippet-test.js </a> <a class="source" href="spotlight-text-snippet.html"> ./demo/snippets/spotlight-text-snippet.js </a> <a class="source" href="swirling-stripes-text-snippet.html"> ./demo/snippets/swirling-stripes-text-snippet.js </a> <a class="source" href="text-snippet-helper.html"> ./demo/snippets/text-snippet-helper.js </a> <a class="source" href="word-highlighter-snippet.html"> ./demo/snippets/word-highlighter-snippet.js </a> <a class="source" href="worley-text-gradient-snippet.html"> ./demo/snippets/worley-text-gradient-snippet.js </a> </div> </div> </li> </ul> <ul class="sections"> <li id="section-1"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-1">&#x00a7;</a> </div> <h1 id="demo-snippets-006">Demo Snippets 006</h1> <p>Editable header text colorizer and animation effect snippets</p> <p>Related files:</p> <ul> <li><a href="../snippets-006.html">Editable header text colorizer and animation effect snippets</a></li> <li><a href="./animated-highlight-gradient-text-snippet.html">Animated highlight gradient text snippet</a></li> <li><a href="./bubbles-text-snippet.html">Bubbles text snippet</a></li> <li><a href="./risograph-text-gradient-snippet.html">Risograph text gradient snippet</a></li> <li><a href="./swirling-stripes-text-snippet.html">Swirling stripes text snippet</a></li> <li><a href="./worley-text-gradient-snippet.html">Worley text gradient snippet</a></li> </ul> </div> </li> <li id="section-2"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-2">&#x00a7;</a> </div> <h3 id="text-snippet-helper-function">Text snippet helper function</h3> <p><strong>Purpose</strong> - Used by text snippets to handle some boilerplate and common functionality:</p> <ul> <li>Handles most work related to matching the canvas font to the HTML element’s font</li> <li>Retrieves the <code>data-</code> attributes which handle canvas text alignment with the HTML element’s text layout</li> <li>For editable HTML elements, handles user updates to the text</li> <li>Canvas initialization, including accessibility</li> <li>Phrase entity initialisation, and updates - including adapting the HTML element to accommodate the phrase’s dimensions</li> <li>Animation boilerplate code, including animation accessibility - create and manage the <code>play|pause</code> <code>button</code> element created and added to the HTML element</li> </ul> <p><strong>Function input</strong> - The Scrawl-canvas snippet object, alongside the SC object (both arguments required).</p> <p><strong>Function output</strong> - A Javascript object with the following <code>key:value</code> attributes:</p> <ul> <li><p><code>canvas</code> - SC snippet’s Canvas wrapper</p> </li> <li><p><code>group</code> - (string) Canvas wrapper’s base Cell’s name value</p> </li> <li><p><code>animation</code> - SC snippet’s animation object</p> </li> <li><p><code>wrapper</code> - SC snippet’s Element artefact wrapper</p> </li> <li><p><code>dataset</code> - HTML element’s <code>dataset</code> object</p> </li> <li><p><code>compStyles</code> - HTML element’s <code>elementComputedStyles</code> object</p> </li> <li><p><code>name</code> - (string) SC snippet’s Element artefact wrapper’s name value</p> </li> <li><p><code>width</code> - (number px) initial HTML element width value</p> </li> <li><p><code>height</code> - (number px) initial HTML element height value</p> </li> <li><p><code>lineHeight</code> - (number px) initial HTML element line height value</p> </li> <li><p><code>fontSize</code> - (number px) initial HTML element font size value</p> </li> <li><p><code>yOffset</code> - user-defined canvas font vertical positioning correction value</p> </li> <li><p><code>textGroup</code> - the non-cell group used to manage and update the two Phrase entitys</p> </li> <li><p><code>initCanvas</code> - function to invoke to initialize the Canvas wrapper</p> </li> <li><p><code>initPhrase</code> - function to invoke to initialize the Phrase entitys</p> </li> <li><p><code>processText</code> - function to handle <code>contenteditable</code> element text updates</p> </li> <li><p><code>responsiveFunctions</code> - Array to hold snippet-specific functions relating to responsiveness</p> </li> <li><p><code>animationFunctions</code> - Array to hold snippet-specific functions relating to running animations</p> </li> <li><p><code>animationStartFunctions</code> - Array to hold snippet-specific functions relating to <code>animation.run()</code></p> </li> <li><p><code>animationEndFunctions</code> - Array to hold snippet-specific functions relating to <code>animation.halt()</code></p> </li> <li><p><code>contrastMoreActions</code> - Array to hold a <code>prefers-contrast</code> accessibility change function</p> </li> <li><p><code>contrastOtherActions</code> - Array to hold a <code>prefers-contrast</code> accessibility change function</p> </li> <li><p><code>colorSchemeDarkActions</code> - Array to hold a <code>prefers-color-scheme</code> accessibility change function</p> </li> <li><p><code>colorSchemeLightActions</code> - Array to hold a <code>prefers-color-scheme</code> accessibility change function</p> </li> <li><p><code>eternalTweens</code> - Array to hold sets of continuously-running tweens created by the snippet</p> </li> <li><p><code>additionalDemolishActions</code> - Array to hold snippet-specific functions relating to snippet demolition</p> </li> </ul> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> <span class="hljs-title function_">getSnippetData</span> = (<span class="hljs-params">snippet, scrawl</span>) =&gt; { <span class="hljs-keyword">const</span> canvas = snippet.<span class="hljs-property">canvas</span>, group = canvas.<span class="hljs-property">base</span>.<span class="hljs-property">name</span>, animation = snippet.<span class="hljs-property">animation</span>, wrapper = snippet.<span class="hljs-property">element</span>, demolishAction = snippet.<span class="hljs-property">demolish</span>, el = wrapper.<span class="hljs-property">domElement</span>, compStyles = wrapper.<span class="hljs-property">elementComputedStyles</span>, name = wrapper.<span class="hljs-property">name</span>, dataset = el.<span class="hljs-property">dataset</span>; <span class="hljs-keyword">const</span> {fontStyle, fontVariant, fontWeight, fontSize, fontFamily, lineHeight, color, width, height, backgroundColor, textAlign, letterSpacing} = compStyles; <span class="hljs-keyword">let</span> yOffset = <span class="hljs-number">0</span>, lineheightAdjuster = <span class="hljs-number">1</span>, underlinePosition = <span class="hljs-number">0.8</span>, underlineWidth = <span class="hljs-number">0.05</span>, noUnderlineGlyphs = <span class="hljs-string">&#x27;&#x27;</span>, isAnimated = <span class="hljs-literal">false</span>; <span class="hljs-keyword">if</span> (dataset.<span class="hljs-property">yOffset</span>) yOffset = <span class="hljs-built_in">parseFloat</span>(dataset.<span class="hljs-property">yOffset</span>); <span class="hljs-keyword">else</span> { <span class="hljs-keyword">const</span> s = compStyles.<span class="hljs-title function_">getPropertyValue</span>(<span class="hljs-string">&#x27;--data-y-offset&#x27;</span>); <span class="hljs-keyword">if</span> (s) yOffset = <span class="hljs-built_in">parseFloat</span>(s); } <span class="hljs-keyword">if</span> (dataset.<span class="hljs-property">lineheightAdjuster</span>) lineheightAdjuster = <span class="hljs-built_in">parseFloat</span>(dataset.<span class="hljs-property">lineheightAdjuster</span>); <span class="hljs-keyword">else</span> { <span class="hljs-keyword">const</span> s = compStyles.<span class="hljs-title function_">getPropertyValue</span>(<span class="hljs-string">&#x27;--data-lineheight-adjuster&#x27;</span>); <span class="hljs-keyword">if</span> (s) lineheightAdjuster = <span class="hljs-built_in">parseFloat</span>(s); } <span class="hljs-keyword">if</span> (dataset.<span class="hljs-property">underlinePosition</span>) underlinePosition = <span class="hljs-built_in">parseFloat</span>(dataset.<span class="hljs-property">underlinePosition</span>); <span class="hljs-keyword">else</span> { <span class="hljs-keyword">const</span> s = compStyles.<span class="hljs-title function_">getPropertyValue</span>(<span class="hljs-string">&#x27;--data-underline-position&#x27;</span>); <span class="hljs-keyword">if</span> (s) underlinePosition = <span class="hljs-built_in">parseFloat</span>(s); } <span class="hljs-keyword">if</span> (dataset.<span class="hljs-property">underlineWidth</span>) underlineWidth = <span class="hljs-built_in">parseFloat</span>(dataset.<span class="hljs-property">underlineWidth</span>); <span class="hljs-keyword">else</span> { <span class="hljs-keyword">const</span> s = compStyles.<span class="hljs-title function_">getPropertyValue</span>(<span class="hljs-string">&#x27;--data-underline-width&#x27;</span>); <span class="hljs-keyword">if</span> (s) underlineWidth = <span class="hljs-built_in">parseFloat</span>(s); } <span class="hljs-keyword">if</span> (dataset.<span class="hljs-property">noUnderlineGlyphs</span>) noUnderlineGlyphs = dataset.<span class="hljs-property">noUnderlineGlyphs</span>; <span class="hljs-keyword">else</span> { <span class="hljs-keyword">const</span> s = compStyles.<span class="hljs-title function_">getPropertyValue</span>(<span class="hljs-string">&#x27;--data-no-underline-glyphs&#x27;</span>); <span class="hljs-keyword">if</span> (s) noUnderlineGlyphs = s; } <span class="hljs-keyword">if</span> (dataset.<span class="hljs-property">isAnimated</span>) isAnimated = dataset.<span class="hljs-property">isAnimated</span>; <span class="hljs-keyword">else</span> { <span class="hljs-keyword">const</span> s = compStyles.<span class="hljs-title function_">getPropertyValue</span>(<span class="hljs-string">&#x27;--data-is-animated&#x27;</span>); <span class="hljs-keyword">if</span> (s) isAnimated = s; } <span class="hljs-keyword">const</span> additionalDemolishActions = []; <span class="hljs-keyword">const</span> responsiveFunctions = []; <span class="hljs-keyword">const</span> animationFunctions = []; <span class="hljs-keyword">const</span> animationStartFunctions = []; <span class="hljs-keyword">const</span> animationEndFunctions = []; <span class="hljs-keyword">const</span> contrastMoreActions = []; <span class="hljs-keyword">const</span> contrastOtherActions = []; <span class="hljs-keyword">const</span> colorSchemeDarkActions = []; <span class="hljs-keyword">const</span> colorSchemeLightActions = []; <span class="hljs-keyword">const</span> eternalTweens = []; <span class="hljs-keyword">const</span> <span class="hljs-title function_">initCanvas</span> = (<span class="hljs-params"></span>) =&gt; { el.<span class="hljs-property">style</span>.<span class="hljs-property">color</span> = <span class="hljs-string">&#x27;transparent&#x27;</span>; el.<span class="hljs-property">style</span>.<span class="hljs-property">overflow</span> = <span class="hljs-string">&#x27;hidden&#x27;</span>; canvas.<span class="hljs-title function_">setColorSchemeDarkAction</span>(<span class="hljs-function">() =&gt;</span> colorSchemeDarkActions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =&gt;</span> <span class="hljs-title function_">a</span>(compStyles))); canvas.<span class="hljs-title function_">setColorSchemeLightAction</span>(<span class="hljs-function">() =&gt;</span> colorSchemeLightActions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =&gt;</span> <span class="hljs-title function_">a</span>(compStyles))); canvas.<span class="hljs-title function_">setMoreContrastAction</span>(<span class="hljs-function">() =&gt;</span> contrastMoreActions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =&gt;</span> <span class="hljs-title function_">a</span>(compStyles))); canvas.<span class="hljs-title function_">setOtherContrastAction</span>(<span class="hljs-function">() =&gt;</span> contrastOtherActions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =&gt;</span> <span class="hljs-title function_">a</span>(compStyles))); }; <span class="hljs-keyword">const</span> <span class="hljs-title function_">addTextNode</span> = (<span class="hljs-params"></span>) =&gt; { <span class="hljs-keyword">const</span> shy = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">createTextNode</span>(<span class="hljs-string">&#x27;!&#x27;</span>); el.<span class="hljs-title function_">appendChild</span>(shy); }; <span class="hljs-keyword">const</span> <span class="hljs-title function_">processText</span> = t =&gt; { t = t.<span class="hljs-title function_">replace</span>(<span class="hljs-regexp">/&lt;canvas.*&lt;\/canvas&gt;/gi</span>, <span class="hljs-string">&#x27;&#x27;</span>); t = t.<span class="hljs-title function_">replace</span>(<span class="hljs-regexp">/&lt;button.*&lt;\/button&gt;/gi</span>, <span class="hljs-string">&#x27;&#x27;</span>); <span class="hljs-keyword">if</span> (!t.<span class="hljs-property">length</span>) { <span class="hljs-title function_">addTextNode</span>(); t = <span class="hljs-string">&#x27;!&#x27;</span>; } <span class="hljs-keyword">return</span> t; } <span class="hljs-keyword">if</span> (el.<span class="hljs-title function_">getAttribute</span>(<span class="hljs-string">&#x27;contenteditable&#x27;</span>)) { <span class="hljs-keyword">const</span> <span class="hljs-title function_">updateText</span> = (<span class="hljs-params">e</span>) =&gt; { textGroup.<span class="hljs-title function_">setArtefacts</span>({ <span class="hljs-attr">text</span>: <span class="hljs-title function_">processText</span>(el.<span class="hljs-property">innerHTML</span>) }); } <span class="hljs-keyword">const</span> <span class="hljs-title function_">focusText</span> = (<span class="hljs-params">e</span>) =&gt; { el.<span class="hljs-property">style</span>.<span class="hljs-property">color</span> = <span class="hljs-string">&#x27;gray&#x27;</span>; } <span class="hljs-keyword">const</span> <span class="hljs-title function_">blurText</span> = (<span class="hljs-params">e</span>) =&gt; { el.<span class="hljs-property">style</span>.<span class="hljs-property">color</span> = <span class="hljs-string">&#x27;transparent&#x27;</span>; } scrawl.<span class="hljs-title function_">addNativeListener</span>(<span class="hljs-string">&#x27;input&#x27;</span>, updateText, el); scrawl.<span class="hljs-title function_">addNativeListener</span>(<span class="hljs-string">&#x27;focus&#x27;</span>, focusText, el); scrawl.<span class="hljs-title function_">addNativeListener</span>(<span class="hljs-string">&#x27;blur&#x27;</span>, blurText, el); additionalDemolishActions.<span class="hljs-title function_">push</span>(<span class="hljs-function">() =&gt;</span> { scrawl.<span class="hljs-title function_">removeNativeListener</span>(<span class="hljs-string">&#x27;input&#x27;</span>, updateText, el); scrawl.<span class="hljs-title function_">removeNativeListener</span>(<span class="hljs-string">&#x27;focus&#x27;</span>, focusText, el); scrawl.<span class="hljs-title function_">removeNativeListener</span>(<span class="hljs-string">&#x27;blur&#x27;</span>, blurText, el); }); } <span class="hljs-keyword">const</span> <span class="hljs-title function_">initPhrase</span> = (<span class="hljs-params">phrase</span>) =&gt; { <span class="hljs-keyword">const</span> localLineHeight = <span class="hljs-built_in">parseFloat</span>(lineHeight), localFontSize = <span class="hljs-built_in">parseFloat</span>(fontSize); phrase.<span class="hljs-title function_">set</span>({ <span class="hljs-attr">style</span>: fontStyle, <span class="hljs-attr">variant</span>: fontVariant, <span class="hljs-attr">weight</span>: fontWeight, <span class="hljs-attr">size</span>: fontSize, <span class="hljs-attr">family</span>: fontFamily, <span class="hljs-attr">lineHeight</span>: (localLineHeight / localFontSize) * lineheightAdjuster, <span class="hljs-attr">width</span>: <span class="hljs-string">&#x27;100%&#x27;</span>, <span class="hljs-attr">text</span>: <span class="hljs-title function_">processText</span>(el.<span class="hljs-property">innerHTML</span>), <span class="hljs-attr">startY</span>: <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">round</span>(localFontSize * yOffset), underlinePosition, <span class="hljs-attr">underlineWidth</span>: <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">round</span>(localFontSize * underlineWidth) || <span class="hljs-number">1</span>, noUnderlineGlyphs, <span class="hljs-attr">underlineStyle</span>: <span class="hljs-string">&#x27;black&#x27;</span>, <span class="hljs-attr">justify</span>: textAlign || <span class="hljs-string">&#x27;left&#x27;</span>, <span class="hljs-attr">letterSpacing</span>: letterSpacing || <span class="hljs-number">0</span>, <span class="hljs-attr">exposeText</span>: <span class="hljs-literal">false</span>, }); };</pre></div></div> </li> <li id="section-3"> <div class="annotation"> <div class="sswrap "> <a class="ss" href="#section-3">&#x00a7;</a> </div> <p>This is a specific fix to handle cases where, because of differences in the way SC and the browser calculate how to fit words on a line, SC phrases may break the text onto an additional line not required by the DOM element</p> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">let</span> localWidth = width; <span class="hljs-keyword">let</span> localHeight = <span class="hljs-built_in">parseFloat</span>(height); <span class="hljs-keyword">const</span> <span class="hljs-title function_">textHeightAction</span> = (<span class="hljs-params"></span>) =&gt; { <span class="hljs-keyword">if</span> (textGroup.<span class="hljs-property">artefacts</span>.<span class="hljs-property">length</span>) { <span class="hljs-keyword">const</span> art = textGroup.<span class="hljs-title function_">getArtefact</span>(textGroup.<span class="hljs-property">artefacts</span>[<span class="hljs-number">0</span>]); <span class="hljs-keyword">const</span> headerHeight = art.<span class="hljs-title function_">get</span>(<span class="hljs-string">&#x27;height&#x27;</span>); <span class="hljs-keyword">if</span> (headerHeight !== localHeight) { el.<span class="hljs-property">style</span>.<span class="hljs-property">height</span> = <span class="hljs-string">`<span class="hljs-subst">${headerHeight}</span>px`</span>; localHeight = headerHeight; } } }; <span class="hljs-keyword">const</span> <span class="hljs-title function_">updatePhrase</span> = (<span class="hljs-params"></span>) =&gt; { <span class="hljs-keyword">if</span> (textGroup.<span class="hljs-property">artefacts</span>.<span class="hljs-property">length</span> &amp;&amp; compStyles.<span class="hljs-property">width</span> !== localWidth) { localWidth = compStyles.<span class="hljs-property">width</span>; <span class="hljs-keyword">const</span> {fontStyle, fontVariant, fontWeight, fontSize, fontFamily, lineHeight, textAlign, letterSpacing} = compStyles; <span class="hljs-keyword">const</span> localLineHeight = <span class="hljs-built_in">parseFloat</span>(lineHeight), localFontSize = <span class="hljs-built_in">parseFloat</span>(fontSize); textGroup.<span class="hljs-title function_">setArtefacts</span>({ <span class="hljs-attr">style</span>: fontStyle, <span class="hljs-attr">variant</span>: fontVariant, <span class="hljs-attr">weight</span>: fontWeight, <span class="hljs-attr">size</span>: fontSize, <span class="hljs-attr">family</span>: fontFamily, <span class="hljs-attr">lineHeight</span>: (localLineHeight / localFontSize) * lineheightAdjuster, <span class="hljs-attr">startY</span>: <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">round</span>(localFontSize * yOffset), <span class="hljs-attr">underlineWidth</span>: <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">round</span>(localFontSize * underlineWidth) || <span class="hljs-number">1</span>, <span class="hljs-attr">justify</span>: textAlign || <span class="hljs-string">&#x27;left&#x27;</span>, <span class="hljs-attr">letterSpacing</span>: letterSpacing || <span class="hljs-number">0</span>, }); responsiveFunctions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">r</span> =&gt;</span> <span class="hljs-title function_">r</span>(compStyles)); } }; <span class="hljs-keyword">const</span> textGroup = scrawl.<span class="hljs-title function_">makeGroup</span>({ <span class="hljs-attr">name</span>: <span class="hljs-string">`<span class="hljs-subst">${name}</span>-text-group`</span>, }); <span class="hljs-keyword">let</span> doAnimation = <span class="hljs-literal">false</span>; <span class="hljs-keyword">const</span> <span class="hljs-title function_">commenceActions</span> = (<span class="hljs-params"></span>) =&gt; { <span class="hljs-title function_">updatePhrase</span>(); <span class="hljs-title function_">textHeightAction</span>(); <span class="hljs-keyword">if</span> (doAnimation) { animationFunctions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">f</span> =&gt;</span> <span class="hljs-title function_">f</span>(compStyles)); } } animation.<span class="hljs-title function_">set</span>({ <span class="hljs-attr">commence</span>: commenceActions, }); <span class="hljs-keyword">if</span> (isAnimated) { doAnimation = <span class="hljs-literal">true</span>; <span class="hljs-keyword">if</span> (<span class="hljs-string">&#x27;static&#x27;</span> === compStyles.<span class="hljs-property">position</span>) el.<span class="hljs-property">style</span>.<span class="hljs-property">position</span> = <span class="hljs-string">&#x27;relative&#x27;</span>; <span class="hljs-keyword">const</span> control = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">createElement</span>(<span class="hljs-string">&#x27;button&#x27;</span>); control.<span class="hljs-property">style</span>.<span class="hljs-property">position</span> = <span class="hljs-string">&#x27;absolute&#x27;</span>; control.<span class="hljs-property">style</span>.<span class="hljs-property">fontSize</span> = <span class="hljs-string">&#x27;12px&#x27;</span>; control.<span class="hljs-property">style</span>.<span class="hljs-property">display</span> = <span class="hljs-string">&#x27;block&#x27;</span>; control.<span class="hljs-property">style</span>.<span class="hljs-property">top</span> = <span class="hljs-string">&#x27;0&#x27;</span>; control.<span class="hljs-property">style</span>.<span class="hljs-property">right</span> = <span class="hljs-string">&#x27;0&#x27;</span>; control.<span class="hljs-property">textContent</span> = <span class="hljs-string">&#x27;Halt&#x27;</span>; control.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;contenteditable&#x27;</span>, <span class="hljs-string">&#x27;false&#x27;</span>); <span class="hljs-keyword">const</span> <span class="hljs-title function_">controlClick</span> = (<span class="hljs-params">e</span>) =&gt; { doAnimation = !doAnimation; <span class="hljs-keyword">if</span> (doAnimation) { control.<span class="hljs-property">textContent</span> = <span class="hljs-string">&#x27;Halt&#x27;</span>; animationStartFunctions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =&gt;</span> <span class="hljs-title function_">a</span>()); eternalTweens.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">t</span> =&gt;</span> t.<span class="hljs-title function_">resume</span>()); } <span class="hljs-keyword">else</span> { control.<span class="hljs-property">textContent</span> = <span class="hljs-string">&#x27;Play&#x27;</span>; animationEndFunctions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =&gt;</span> <span class="hljs-title function_">a</span>()); eternalTweens.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">t</span> =&gt;</span> t.<span class="hljs-title function_">halt</span>()); } }; scrawl.<span class="hljs-title function_">addNativeListener</span>(<span class="hljs-string">&#x27;click&#x27;</span>, controlClick, control); el.<span class="hljs-title function_">appendChild</span>(control); canvas.<span class="hljs-title function_">setReduceMotionAction</span>(<span class="hljs-function">() =&gt;</span> { <span class="hljs-keyword">if</span> (doAnimation) { doAnimation = <span class="hljs-literal">false</span>; control.<span class="hljs-property">textContent</span> = <span class="hljs-string">&#x27;Play&#x27;</span>; animationEndFunctions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =&gt;</span> <span class="hljs-title function_">a</span>()); eternalTweens.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">t</span> =&gt;</span> t.<span class="hljs-title function_">halt</span>()); } }); canvas.<span class="hljs-title function_">setNoPreferenceMotionAction</span>(<span class="hljs-function">() =&gt;</span> { <span class="hljs-keyword">if</span> (!doAnimation) { doAnimation = <span class="hljs-literal">true</span>; control.<span class="hljs-property">textContent</span> = <span class="hljs-string">&#x27;Halt&#x27;</span>; animationStartFunctions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =&gt;</span> <span class="hljs-title function_">a</span>()); eternalTweens.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">t</span> =&gt;</span> t.<span class="hljs-title function_">resume</span>()); } }); additionalDemolishActions.<span class="hljs-title function_">push</span>(<span class="hljs-function">() =&gt;</span> { scrawl.<span class="hljs-title function_">removeNativeListener</span>(<span class="hljs-string">&#x27;click&#x27;</span>, controlClick, control); }); } snippet.<span class="hljs-property">demolish</span> = <span class="hljs-function">() =&gt;</span> { eternalTweens.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">t</span> =&gt;</span> t.<span class="hljs-title function_">kill</span>()); textGroup.<span class="hljs-title function_">kill</span>(); additionalDemolishActions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">f</span> =&gt;</span> <span class="hljs-title function_">f</span>()); <span class="hljs-title function_">demolishAction</span>(); }; <span class="hljs-keyword">return</span> { canvas, group, animation, wrapper, dataset, compStyles, name, <span class="hljs-attr">width</span>: <span class="hljs-built_in">parseFloat</span>(width), <span class="hljs-attr">height</span>: localHeight, <span class="hljs-attr">lineHeight</span>: <span class="hljs-built_in">parseFloat</span>(lineHeight), <span class="hljs-attr">fontSize</span>: <span class="hljs-built_in">parseFloat</span>(fontSize), yOffset, textGroup, initCanvas, initPhrase, processText, responsiveFunctions, animationFunctions, animationStartFunctions, animationEndFunctions, contrastMoreActions, contrastOtherActions, colorSchemeDarkActions, colorSchemeLightActions, eternalTweens, additionalDemolishActions, }; }</pre></div></div> </li> </ul> </div> </body> </html>