scrawl-canvas
Version:
Version 8.9.4 - 19 Nov 2022
634 lines (478 loc) • 37.6 kB
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 …</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">§</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">§</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>) => {
<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">''</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">'--data-y-offset'</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">'--data-lineheight-adjuster'</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">'--data-underline-position'</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">'--data-underline-width'</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">'--data-no-underline-glyphs'</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">'--data-is-animated'</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>) => {
el.<span class="hljs-property">style</span>.<span class="hljs-property">color</span> = <span class="hljs-string">'transparent'</span>;
el.<span class="hljs-property">style</span>.<span class="hljs-property">overflow</span> = <span class="hljs-string">'hidden'</span>;
canvas.<span class="hljs-title function_">setColorSchemeDarkAction</span>(<span class="hljs-function">() =></span> colorSchemeDarkActions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =></span> <span class="hljs-title function_">a</span>(compStyles)));
canvas.<span class="hljs-title function_">setColorSchemeLightAction</span>(<span class="hljs-function">() =></span> colorSchemeLightActions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =></span> <span class="hljs-title function_">a</span>(compStyles)));
canvas.<span class="hljs-title function_">setMoreContrastAction</span>(<span class="hljs-function">() =></span> contrastMoreActions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =></span> <span class="hljs-title function_">a</span>(compStyles)));
canvas.<span class="hljs-title function_">setOtherContrastAction</span>(<span class="hljs-function">() =></span> contrastOtherActions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =></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>) => {
<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">'!'</span>);
el.<span class="hljs-title function_">appendChild</span>(shy);
};
<span class="hljs-keyword">const</span> <span class="hljs-title function_">processText</span> = t => {
t = t.<span class="hljs-title function_">replace</span>(<span class="hljs-regexp">/<canvas.*<\/canvas>/gi</span>, <span class="hljs-string">''</span>);
t = t.<span class="hljs-title function_">replace</span>(<span class="hljs-regexp">/<button.*<\/button>/gi</span>, <span class="hljs-string">''</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">'!'</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">'contenteditable'</span>)) {
<span class="hljs-keyword">const</span> <span class="hljs-title function_">updateText</span> = (<span class="hljs-params">e</span>) => {
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>) => {
el.<span class="hljs-property">style</span>.<span class="hljs-property">color</span> = <span class="hljs-string">'gray'</span>;
}
<span class="hljs-keyword">const</span> <span class="hljs-title function_">blurText</span> = (<span class="hljs-params">e</span>) => {
el.<span class="hljs-property">style</span>.<span class="hljs-property">color</span> = <span class="hljs-string">'transparent'</span>;
}
scrawl.<span class="hljs-title function_">addNativeListener</span>(<span class="hljs-string">'input'</span>, updateText, el);
scrawl.<span class="hljs-title function_">addNativeListener</span>(<span class="hljs-string">'focus'</span>, focusText, el);
scrawl.<span class="hljs-title function_">addNativeListener</span>(<span class="hljs-string">'blur'</span>, blurText, el);
additionalDemolishActions.<span class="hljs-title function_">push</span>(<span class="hljs-function">() =></span> {
scrawl.<span class="hljs-title function_">removeNativeListener</span>(<span class="hljs-string">'input'</span>, updateText, el);
scrawl.<span class="hljs-title function_">removeNativeListener</span>(<span class="hljs-string">'focus'</span>, focusText, el);
scrawl.<span class="hljs-title function_">removeNativeListener</span>(<span class="hljs-string">'blur'</span>, blurText, el);
});
}
<span class="hljs-keyword">const</span> <span class="hljs-title function_">initPhrase</span> = (<span class="hljs-params">phrase</span>) => {
<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">'100%'</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">'black'</span>,
<span class="hljs-attr">justify</span>: textAlign || <span class="hljs-string">'left'</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">§</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>) => {
<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">'height'</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>) => {
<span class="hljs-keyword">if</span> (textGroup.<span class="hljs-property">artefacts</span>.<span class="hljs-property">length</span> && 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">'left'</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> =></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>) => {
<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> =></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">'static'</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">'relative'</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">'button'</span>);
control.<span class="hljs-property">style</span>.<span class="hljs-property">position</span> = <span class="hljs-string">'absolute'</span>;
control.<span class="hljs-property">style</span>.<span class="hljs-property">fontSize</span> = <span class="hljs-string">'12px'</span>;
control.<span class="hljs-property">style</span>.<span class="hljs-property">display</span> = <span class="hljs-string">'block'</span>;
control.<span class="hljs-property">style</span>.<span class="hljs-property">top</span> = <span class="hljs-string">'0'</span>;
control.<span class="hljs-property">style</span>.<span class="hljs-property">right</span> = <span class="hljs-string">'0'</span>;
control.<span class="hljs-property">textContent</span> = <span class="hljs-string">'Halt'</span>;
control.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">'contenteditable'</span>, <span class="hljs-string">'false'</span>);
<span class="hljs-keyword">const</span> <span class="hljs-title function_">controlClick</span> = (<span class="hljs-params">e</span>) => {
doAnimation = !doAnimation;
<span class="hljs-keyword">if</span> (doAnimation) {
control.<span class="hljs-property">textContent</span> = <span class="hljs-string">'Halt'</span>;
animationStartFunctions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =></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> =></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">'Play'</span>;
animationEndFunctions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =></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> =></span> t.<span class="hljs-title function_">halt</span>());
}
};
scrawl.<span class="hljs-title function_">addNativeListener</span>(<span class="hljs-string">'click'</span>, controlClick, control);
el.<span class="hljs-title function_">appendChild</span>(control);
canvas.<span class="hljs-title function_">setReduceMotionAction</span>(<span class="hljs-function">() =></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">'Play'</span>;
animationEndFunctions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =></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> =></span> t.<span class="hljs-title function_">halt</span>());
}
});
canvas.<span class="hljs-title function_">setNoPreferenceMotionAction</span>(<span class="hljs-function">() =></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">'Halt'</span>;
animationStartFunctions.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">a</span> =></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> =></span> t.<span class="hljs-title function_">resume</span>());
}
});
additionalDemolishActions.<span class="hljs-title function_">push</span>(<span class="hljs-function">() =></span> {
scrawl.<span class="hljs-title function_">removeNativeListener</span>(<span class="hljs-string">'click'</span>, controlClick, control);
});
}
snippet.<span class="hljs-property">demolish</span> = <span class="hljs-function">() =></span> {
eternalTweens.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">t</span> =></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> =></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>