Hexagonal Grids (2013)
A surprise to see my page on HN! For those of you curious about the tech:<p>Diagrams are in SVG. Canvas would be faster but SVG is easier for me to work with, especially for attaching mouse events to each hexagon. It also automatically scales to high dpi displays. With SVG and HTML accessed the same way, I can use the same code for updating text&#x2F;samples as I do for updating diagrams. This includes interpolating values as noted by anchpop.<p>I don&#x27;t use a build step for the JS code. It&#x27;s just &lt;script&gt; tags, like it&#x27;s 1999! You can View Source and see them all. I realize it&#x27;d be better if I minified bundled etc. but I&#x27;m lazy and this was easier. The page is implemented differently from a &quot;single page app&quot;. For that, you might want something like JSX so that you can stay on the JS side, and output HTML+CSS as needed. For this kind of page, I mostly write HTML+CSS, and I tap into JS when needed using Vue.js. For example, I want the text &quot;w = 2 * size&quot; to be next to the rest of the text for that paragraph, instead of in a separate JS file:<p><pre><code>       &lt;p&gt;
          Next we want to put several hexagons together.
          In the {{layout}} orientation, a hexagon 
          has width
          &lt;code v-if=&quot;layout === &#x27;flat&#x27;&quot;&gt;w = 2 * size&lt;&#x2F;code&gt;
          &lt;code v-else=&quot;&quot;&gt;w = sqrt(3) * size&lt;&#x2F;code&gt;
          …
        &lt;&#x2F;p&gt;
</code></pre>
I do use a build step for the HTML+SVG. Although the page works without this step (see pre-index.html), the user experience is a little bit better if I generate it on the server. I use cheesy pre-rendering for this: &quot;$(CHROME) --headless --disable-gpu --dump-dom&quot;. It means more bytes :( but having all the HTML&#x2F;SVG load at once is better for linking to a section, hitting the back button, etc. You don&#x27;t lose the scroll position. Bonus: the page loads if you have Javascript turned off!<p>The static SVG on the server doesn&#x27;t include the interactive parts. I use InteractionObserver to replace the static diagram with an interactive one as soon as you scroll to that section of the page.<p>I said I don&#x27;t have a JS build step, but that&#x27;s not quite true. The core hexagon algorithms were originally implemented in Haxe, compiled to Javascript. At the time (2013), I was curious about Haxe as an alternative to Javascript. Because I was either bored or crazy or both, in 2015 I implemented a code generator that used Haxe macros to parse the algorithms in Haxe-ish syntax[1] and then generate C++, Javascript, Python, and other output. The Javascript output from <i>that</i> is what now powers the page. Well, it&#x27;s even more convoluted than that. The Haxe code generates Typescript[2], which I turn into Javascript[3]. I don&#x27;t know what I was thinking! Well, I do — I wanted my readers to be able to use my unit-tested hexagon algorithms even if they were using a different programming language.<p>I wrote [4] describing the process of making an interactive page like this with D3.js. Check out [5] for a collection of interactive explanations from other people.<p>It&#x27;s unlikely that you will print the page, but if you do, it will show endnotes for all the links so that you can see the URLs.[6]<p>[1] <a href="https:&#x2F;&#x2F;www.redblobgames.com&#x2F;grids&#x2F;hexagons&#x2F;codegen&#x2F;Codegen.hx" rel="nofollow">https:&#x2F;&#x2F;www.redblobgames.com&#x2F;grids&#x2F;hexagons&#x2F;codegen&#x2F;Codegen....</a>
[2] <a href="https:&#x2F;&#x2F;www.redblobgames.com&#x2F;grids&#x2F;hexagons&#x2F;codegen&#x2F;output&#x2F;lib.ts" rel="nofollow">https:&#x2F;&#x2F;www.redblobgames.com&#x2F;grids&#x2F;hexagons&#x2F;codegen&#x2F;output&#x2F;l...</a>
[3] <a href="https:&#x2F;&#x2F;www.redblobgames.com&#x2F;grids&#x2F;hexagons&#x2F;codegen&#x2F;output&#x2F;lib.js" rel="nofollow">https:&#x2F;&#x2F;www.redblobgames.com&#x2F;grids&#x2F;hexagons&#x2F;codegen&#x2F;output&#x2F;l...</a>
[4] <a href="https:&#x2F;&#x2F;www.redblobgames.com&#x2F;making-of&#x2F;line-drawing&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.redblobgames.com&#x2F;making-of&#x2F;line-drawing&#x2F;</a>
[5] <a href="https:&#x2F;&#x2F;explorabl.es&#x2F;" rel="nofollow">https:&#x2F;&#x2F;explorabl.es&#x2F;</a>
[6] <a href="https:&#x2F;&#x2F;simblob.blogspot.com&#x2F;2018&#x2F;12&#x2F;printing-my-pages.html" rel="nofollow">https:&#x2F;&#x2F;simblob.blogspot.com&#x2F;2018&#x2F;12&#x2F;printing-my-pages.html</a>