UNPKG

scrawl-canvas

Version:
518 lines (353 loc) 28.1 kB
<!DOCTYPE html> <html> <head> <title>Demo Modules 001</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="canvas-minimap.html"> ./demo/modules/canvas-minimap.js </a> <a class="source" href="canvas-scene-editor.html"> ./demo/modules/canvas-scene-editor.js </a> <a class="source" href="dom-entity-editor.html"> ./demo/modules/dom-entity-editor.js </a> <a class="source" href="entity-copy-paste.html"> ./demo/modules/entity-copy-paste.js </a> <a class="source" href="entity-manipulation-gui.html"> ./demo/modules/entity-manipulation-gui.js </a> <a class="source" href="entity-navigation.html"> ./demo/modules/entity-navigation.js </a> <a class="source" href="entity-ring-builder.html"> ./demo/modules/entity-ring-builder.js </a> <a class="source" href="london-crime-graphic.html"> ./demo/modules/london-crime-graphic.js </a> <a class="source" href="london-crime-lines.html"> ./demo/modules/london-crime-lines.js </a> <a class="source" href="london-crime-stacked-bars.html"> ./demo/modules/london-crime-stacked-bars.js </a> <a class="source" href="lottie-loader.html"> ./demo/modules/lottie-loader.js </a> <a class="source" href="simple-chart-frame-tests.html"> ./demo/modules/simple-chart-frame-tests.js </a> <a class="source" href="simple-chart-frame.html"> ./demo/modules/simple-chart-frame.js </a> <a class="source" href="simple-graph-lines.html"> ./demo/modules/simple-graph-lines.js </a> <a class="source" href="simple-graph-stacked-bars.html"> ./demo/modules/simple-graph-stacked-bars.js </a> <a class="source" href="wikipedia-views-spiral-chart.html"> ./demo/modules/wikipedia-views-spiral-chart.js </a> <a class="source" href="../snippets/animated-highlight-gradient-text-snippet.html"> ./demo/snippets/animated-highlight-gradient-text-snippet.js </a> <a class="source" href="../snippets/animated-hover-gradient-snippet.html"> ./demo/snippets/animated-hover-gradient-snippet.js </a> <a class="source" href="../snippets/animated-word-gradient-snippet.html"> ./demo/snippets/animated-word-gradient-snippet.js </a> <a class="source" href="../snippets/before-after-slider-infographic.html"> ./demo/snippets/before-after-slider-infographic.js </a> <a class="source" href="../snippets/bubbles-text-snippet.html"> ./demo/snippets/bubbles-text-snippet.js </a> <a class="source" href="../snippets/green-box-snippet.html"> ./demo/snippets/green-box-snippet.js </a> <a class="source" href="../snippets/jazzy-button-snippet.html"> ./demo/snippets/jazzy-button-snippet.js </a> <a class="source" href="../snippets/page-performance-snippet-test.html"> ./demo/snippets/page-performance-snippet-test.js </a> <a class="source" href="../snippets/page-performance-snippet.html"> ./demo/snippets/page-performance-snippet.js </a> <a class="source" href="../snippets/pan-image-snippet.html"> ./demo/snippets/pan-image-snippet.js </a> <a class="source" href="../snippets/placeholder-effect-snippet.html"> ./demo/snippets/placeholder-effect-snippet.js </a> <a class="source" href="../snippets/ripple-effect-snippet.html"> ./demo/snippets/ripple-effect-snippet.js </a> <a class="source" href="../snippets/risograph-text-gradient-snippet.html"> ./demo/snippets/risograph-text-gradient-snippet.js </a> <a class="source" href="../snippets/spotlight-text-snippet-test.html"> ./demo/snippets/spotlight-text-snippet-test.js </a> <a class="source" href="../snippets/spotlight-text-snippet.html"> ./demo/snippets/spotlight-text-snippet.js </a> <a class="source" href="../snippets/swirling-stripes-text-snippet.html"> ./demo/snippets/swirling-stripes-text-snippet.js </a> <a class="source" href="../snippets/text-snippet-helper.html"> ./demo/snippets/text-snippet-helper.js </a> <a class="source" href="../snippets/word-highlighter-snippet.html"> ./demo/snippets/word-highlighter-snippet.js </a> <a class="source" href="../snippets/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-modules-001">Demo Modules 001</h1> <p>Scrawl-canvas modularized code - London crime charts</p> <p>Related files:</p> <ul> <li><a href="../modules-001.html">London crime charts - main module</a></li> <li><a href="./london-crime-graphic.html">London crime graphic module</a></li> <li><a href="./london-crime-lines.html">London crime lines module</a></li> <li><a href="./london-crime-stacked-bars.html">London crime stacked bar module</a></li> <li><a href="./simple-chart-frame.html">Simple chart frame module</a></li> <li><a href="./simple-chart-frame-tests.html">Simple chart frame tests module</a></li> <li><a href="./simple-graph-lines.html">Simple graph lines module</a></li> <li><a href="./simple-graph-stacked-bars.html">Simple graph stacked bars module</a></li> </ul> </div> <div class="content"><div class='highlight'><pre> <span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> scrawl <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./source/scrawl.js&#x27;</span>; <span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> frame <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./simple-chart-frame.js&#x27;</span>; <span class="hljs-keyword">let</span> testParent, testButton, testListeners, buttonListener, frameNamespace, frameBackground; <span class="hljs-keyword">const</span> activateButton = <span class="hljs-keyword">function</span> (<span class="hljs-params">frameName, canvas, host, button, background</span>) { frameNamespace = frameName; testButton = button; testParent = host; frameBackground = background; buttonListener = scrawl.<span class="hljs-title function_">addListener</span>(<span class="hljs-string">&#x27;up&#x27;</span>, <span class="hljs-keyword">function</span> (<span class="hljs-params">e</span>) { <span class="hljs-keyword">if</span> (e) { e.<span class="hljs-title function_">preventDefault</span>(); e.<span class="hljs-title function_">stopPropagation</span>(); <span class="hljs-keyword">let</span> val = <span class="hljs-built_in">parseInt</span>(testButton.<span class="hljs-property">value</span>, <span class="hljs-number">10</span>); <span class="hljs-keyword">if</span> (val) { <span class="hljs-title function_">killTests</span>(); testButton.<span class="hljs-property">value</span> = <span class="hljs-string">&#x27;0&#x27;</span>; testButton.<span class="hljs-property">textContent</span> = <span class="hljs-string">&#x27;Show tests&#x27;</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-title function_">buildTests</span>(canvas, host); testButton.<span class="hljs-property">value</span> = <span class="hljs-string">&#x27;1&#x27;</span>; testButton.<span class="hljs-property">textContent</span> = <span class="hljs-string">&#x27;Hide tests&#x27;</span>; } } }, testButton); }; <span class="hljs-keyword">const</span> removeButton = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) { <span class="hljs-title function_">buttonListener</span>(); <span class="hljs-keyword">if</span> (<span class="hljs-built_in">parseInt</span>(testButton.<span class="hljs-property">value</span>, <span class="hljs-number">10</span>)) <span class="hljs-title function_">killTests</span>(); testButton.<span class="hljs-title function_">remove</span>(); testParent.<span class="hljs-title function_">remove</span>(); }; <span class="hljs-keyword">const</span> buildTests = <span class="hljs-keyword">function</span> (<span class="hljs-params">canvas, host</span>) { testParent.<span class="hljs-property">innerHTML</span> = <span class="hljs-string">` &lt;div&gt; &lt;button id=&quot;build-button&quot; class=&quot;controls&quot;&gt;Build graph&lt;/button&gt; &lt;button id=&quot;kill-button&quot; class=&quot;controls&quot;&gt;Kill graph&lt;/button&gt; &lt;button id=&quot;show-button&quot; class=&quot;controls&quot;&gt;Show graph&lt;/button&gt; &lt;button id=&quot;hide-button&quot; class=&quot;controls&quot;&gt;Hide graph&lt;/button&gt; &lt;/div&gt; &lt;p&gt; Title: &lt;input type=&quot;text&quot; value=&quot;&quot; id=&quot;title-input&quot; class=&quot;controls&quot;/&gt; Subtitle: &lt;input type=&quot;text&quot; value=&quot;&quot; id=&quot;subtitle-input&quot; class=&quot;controls&quot;/&gt; &lt;/p&gt; &lt;p&gt; Y-axis - top: &lt;input type=&quot;number&quot; value=&quot;&quot; id=&quot;y-top-input&quot; class=&quot;controls&quot;/&gt; bottom: &lt;input type=&quot;number&quot; value=&quot;&quot; id=&quot;y-bottom-input&quot; class=&quot;controls&quot;/&gt; &lt;/p&gt; &lt;p&gt; X-axis - left: &lt;input type=&quot;text&quot; value=&quot;&quot; id=&quot;x-left-input&quot; class=&quot;controls&quot;/&gt; right: &lt;input type=&quot;text&quot; value=&quot;&quot; id=&quot;x-right-input&quot; class=&quot;controls&quot;/&gt; &lt;/p&gt; &lt;p&gt;Background: &lt;select class=&quot;controls&quot; id=&quot;crime-types&quot;&gt; &lt;option value=&quot;Burglary&quot;&gt;Burglary&lt;/option&gt; &lt;option value=&quot;Criminal Damage&quot;&gt;Criminal Damage&lt;/option&gt; &lt;option value=&quot;Drugs&quot;&gt;Drugs&lt;/option&gt; &lt;option value=&quot;Fraud or Forgery&quot;&gt;Fraud or Forgery&lt;/option&gt; &lt;option value=&quot;Other Notifiable Offences&quot;&gt;Other Notifiable Offences&lt;/option&gt; &lt;option value=&quot;Robbery&quot;&gt;Robbery&lt;/option&gt; &lt;option value=&quot;Sexual Offences&quot;&gt;Sexual Offences&lt;/option&gt; &lt;option value=&quot;Theft and Handling&quot;&gt;Theft and Handling&lt;/option&gt; &lt;option value=&quot;Violence Against the Person&quot;&gt;Violence Against the Person&lt;/option&gt; &lt;/select&gt; &lt;/p&gt; &lt;p&gt; &lt;button id=&quot;kill-tests&quot; class=&quot;controls&quot;&gt;Remove tests&lt;/button&gt; &lt;/p&gt;`</span>; <span class="hljs-keyword">let</span> buildButton = testParent.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#build-button&#x27;</span>), killButton = testParent.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#kill-button&#x27;</span>), showButton = testParent.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#show-button&#x27;</span>), hideButton = testParent.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#hide-button&#x27;</span>), titleInput = testParent.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#title-input&#x27;</span>), subtitleInput = testParent.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#subtitle-input&#x27;</span>), yTop = testParent.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#y-top-input&#x27;</span>), yBottom = testParent.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#y-bottom-input&#x27;</span>), xLeft = testParent.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#x-left-input&#x27;</span>), xRight = testParent.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#x-right-input&#x27;</span>), crimeTypes = testParent.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#crime-types&#x27;</span>), killTests = testParent.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#kill-tests&#x27;</span>); <span class="hljs-keyword">let</span> currentBackground, currentTitle, currentSubtitle, currentYTop, currentYBottom, currentXLeft, currentXRight; <span class="hljs-keyword">let</span> <span class="hljs-title function_">setControlsToDefaults</span> = (<span class="hljs-params"></span>) =&gt; { crimeTypes.<span class="hljs-property">value</span> = currentBackground = frameBackground; titleInput.<span class="hljs-property">value</span> = currentTitle = <span class="hljs-string">&#x27;No title&#x27;</span>; subtitleInput.<span class="hljs-property">value</span> = currentSubtitle = <span class="hljs-string">&#x27;No data selected&#x27;</span>; yTop.<span class="hljs-property">value</span> = currentYTop = <span class="hljs-string">&#x27;0&#x27;</span>; yBottom.<span class="hljs-property">value</span> = currentYBottom = <span class="hljs-string">&#x27;0&#x27;</span>; xLeft.<span class="hljs-property">value</span> = currentXLeft = <span class="hljs-string">&#x27;0&#x27;</span>; xRight.<span class="hljs-property">value</span> = currentXRight = <span class="hljs-string">&#x27;0&#x27;</span>; buildButton.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>); showButton.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>); hideButton.<span class="hljs-title function_">removeAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>); titleInput.<span class="hljs-title function_">removeAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>); subtitleInput.<span class="hljs-title function_">removeAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>); crimeTypes.<span class="hljs-title function_">removeAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>); killButton.<span class="hljs-title function_">removeAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>); yTop.<span class="hljs-title function_">removeAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>); yBottom.<span class="hljs-title function_">removeAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>); xLeft.<span class="hljs-title function_">removeAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>); xRight.<span class="hljs-title function_">removeAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>); }; <span class="hljs-title function_">setControlsToDefaults</span>(); <span class="hljs-keyword">let</span> <span class="hljs-title function_">setNumbersHelper</span> = (<span class="hljs-params">val, current, fn</span>) =&gt; { val = (val) ? val : <span class="hljs-number">0</span>; <span class="hljs-keyword">if</span> (current !== val) { current = val; <span class="hljs-title function_">fn</span>(<span class="hljs-built_in">parseFloat</span>(val).<span class="hljs-title function_">toLocaleString</span>()); } }; testListeners = scrawl.<span class="hljs-title function_">addNativeListener</span>([<span class="hljs-string">&#x27;click&#x27;</span>, <span class="hljs-string">&#x27;input&#x27;</span>, <span class="hljs-string">&#x27;change&#x27;</span>], <span class="hljs-keyword">function</span> (<span class="hljs-params">e</span>) { <span class="hljs-keyword">if</span> (e) { e.<span class="hljs-title function_">stopPropagation</span>(); e.<span class="hljs-title function_">preventDefault</span>(); <span class="hljs-keyword">if</span> (e.<span class="hljs-property">target</span>) { <span class="hljs-keyword">let</span> target = e.<span class="hljs-property">target</span>.<span class="hljs-property">id</span>, val = e.<span class="hljs-property">target</span>.<span class="hljs-property">value</span>; <span class="hljs-keyword">switch</span> (target) { <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;build-button&#x27;</span> : frame.<span class="hljs-title function_">build</span>(frameNamespace, canvas, frameBackground); frame.<span class="hljs-title function_">show</span>(); <span class="hljs-title function_">setControlsToDefaults</span>(); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;kill-button&#x27;</span> : frame.<span class="hljs-title function_">kill</span>(); killButton.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>); showButton.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>); hideButton.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>); titleInput.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>); subtitleInput.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>); crimeTypes.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>); yTop.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>); yBottom.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>); xLeft.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>); xRight.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>); buildButton.<span class="hljs-title function_">removeAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;show-button&#x27;</span> : frame.<span class="hljs-title function_">show</span>(); showButton.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>); hideButton.<span class="hljs-title function_">removeAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;hide-button&#x27;</span> : frame.<span class="hljs-title function_">hide</span>(); hideButton.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>); showButton.<span class="hljs-title function_">removeAttribute</span>(<span class="hljs-string">&#x27;disabled&#x27;</span>); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;title-input&#x27;</span> : <span class="hljs-keyword">if</span> (currentTitle !== val) { currentTitle = val; frame.<span class="hljs-title function_">updateTitle</span>(val); } <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;subtitle-input&#x27;</span> : <span class="hljs-keyword">if</span> (currentSubtitle !== val) { currentSubtitle = val; frame.<span class="hljs-title function_">updateSubtitle</span>(val); } <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;y-top-input&#x27;</span> : <span class="hljs-title function_">setNumbersHelper</span>(val, currentYTop, frame.<span class="hljs-property">updateYTop</span>); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;y-bottom-input&#x27;</span> : <span class="hljs-title function_">setNumbersHelper</span>(val, currentYBottom, frame.<span class="hljs-property">updateYBottom</span>); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;x-left-input&#x27;</span> : <span class="hljs-keyword">if</span> (currentXLeft !== val) { currentXLeft = val; frame.<span class="hljs-title function_">updateXLeft</span>(val); } <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;x-right-input&#x27;</span> : <span class="hljs-keyword">if</span> (currentXRight !== val) { currentXRight = val; frame.<span class="hljs-title function_">updateXRight</span>(val); } <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;crime-types&#x27;</span> : <span class="hljs-keyword">if</span> (currentBackground !== val) { currentBackground = val; frame.<span class="hljs-title function_">updateBackground</span>(val); } <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;kill-tests&#x27;</span> : <span class="hljs-title function_">removeButton</span>(); <span class="hljs-keyword">break</span>; } } } }, <span class="hljs-string">&#x27;.controls&#x27;</span>); }; <span class="hljs-keyword">const</span> killTests = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) { <span class="hljs-title function_">testListeners</span>(); [...testParent.<span class="hljs-property">childNodes</span>].<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">el</span> =&gt;</span> el.<span class="hljs-title function_">remove</span>()); }; <span class="hljs-keyword">export</span> { activateButton, removeButton, };</pre></div></div> </li> </ul> </div> </body> </html>