Sunday, 28 July 2024
Next.js Server Actions - how do I send complex data
Saturday, 30 September 2023
Frenzy.js - the saga of flood-fill
It may surprise you to know that yes, I am back working on Frenzy.js after a multi-year hiatus. It's a bit surreal working on an "old-skool" (pre-hooks) React app but the end is actually in sight. As of September 2023 I have (by my reckoning) about 80% of the game done;
- Basic geometry (it's all scaled 2x)
- Levels with increasing numbers of Leptons with increasing speed
- Reliable collision detection
- High-score table that persists to a cookie
- Mostly-reliable calculation of the area to be filled
- Accurate emulation of the game state, particularly when the game "pauses"
The big-ticket items I still need to complete are:
- Implement "chasers" on higher levels
- Fine-tune the filled-area calculation (it still gets it wrong sometimes)
- Animated flood-fill
- Player-start, player-death and Lepton-death animations
- (unsure) Sound.
It all comes flooding back
To remind you (if you were an 80s Acorn kid) or (far more likely) educate you on what I mean by "flood-fill", here's Frenzy doing its thing in an emulator; I've just completed drawing the long vertical line, and now the game is flood-filling the smaller area:
I wanted to replicate this distinctive style of flood-fill exactly in my browser-based version, and it's been quite the labour of love. My first attempt (that actually worked there were many iterations that did not) was so comically slow that I almost gave up on the whole idea. Then I took a concrete pill and decided that if I couldn't get a multiple-GHz multi-cored MONSTER of a machine to replicate a single-cored 2MHz (optimistically) 8-bit grot-box from the early 1980s, I may as well just give up...
The basic concept for this is:
Given a polygonal area A that needs to be flood-filled; Determine bottom-rightmost inner point P within A. The "frontier pixels" is now the array [P] On each game update "tick": Expand each of the "frontier pixels" to the N,S,E and W; but Discard an expansion if it hits a boundary of A Also discard it if the pixel has already been filled The new "frontier pixels" is all the undiscarded pixels Stop when the "frontier pixels" array is emptyI got this to be pretty efficient using a bit-field "sparse array" to quickly check for already-filled pixels. In the browser, I could perform the per-tick operations in less than 0.1 milliseconds for any size of A. Not too surprising given the entire game area is only 240x180 pixels, and the maximum possible polygonal area could only ever be half that big: 21,600 pixels.
The problem now became efficiently shifting the big pile'o'filled-pixels from the algorithm onto the HTML5 canvas that is the main gameplay area. I'm using the excellent React Konva library as a nice abstraction over the canvas, but the principal problem is that a canvas doesn't expose per-pixel operations in its API, and nor does Konva. The Konva team has done an admirable job making their code as performant as possible, but my first cut (instantiating a pile of tiny 1x1 Rects on each tick) simply couldn't cope once the number of pixels got significant:
This has led me down a quite-interesting rabbit-hole at the intersection of HTML5 Canvas, React, React-Konva, and general "performance" stuff which is familiar-yet-different. There's an interesting benchmark set up for this, and the results are all over the shop depending on browser and platform. Mobile results are predictably terrible but I'm deliberately not targeting them. This was a game for a "desktop" (before we called them that) and it needs keyboard input. I contemplated some kind of gestural control but it's just not good enough I think, so I'd rather omit it.
What I need to do, is find a way to automagically go from a big dumb pile of individual filled pixels into a suitable collection of optimally-shaped polygons, implemented as Konva Lines.
The baseline
In code, what I first naïvely had was:
type Point = [number, number] // get the latest flood fill result as array of points const filledPixels:Array<Point> = toPointArray(sparseMap); // Simplified a little - we use some extra options // on Rect for performance... return filledPixels.map((fp) => <Rect x={fp[0]} y={fp[1]} width={1} height={1} lineCap="square" fillColor="red" /> );With the above code, the worst-case render time while filling the worst-case shape (a box 120x180px) was 123ms. Unacceptable. What I want is:
// Konva just wants a flat list of x1,y1,x2,y2,x3,y3 type Poly = Array<number>; // get the latest flood fill result as array of polys const polys:Array<number> = toOptimalPolyArray(sparseMap); // far-fewer, much-larger polygons return polys.map((poly) => <Line points={poly} lineCap="square" fillColor="red" closed /> );
So how the hell do I write toOptimalPolyArray()?
Optimisation step 1: RLE FTW
My Googling for "pixel-to-polygon" and "pixel vectorisation" failed me, so I just went from first principles and tried a Run-Length-Encoding on each line of the area to be filled. As a first cut, this should dramatically reduce the number of Konva objects required. Here's the worst-case render time while filling the worst-case shape (a box 120x180px): 4.4ms
Optimisation step 2: Boxy, but good
I'd consider this to be a kind of half-vectorisation. Each row of the area is optimally vectorised into a line with a start and end point. The next step would be to iterate over the lines, and simply merge lines that are "stacked" directly on top of each other. Given the nature of the shapes being filled is typically highly rectilinear, this felt like it would "win" quite often. Worst-case render time now became: 1.9ms
Optimisation step 3: Know your enemy
I felt there was still one more optimisation possible, and that is to exploit the fact that the game always picks the bottom-right-hand corner in which to start filling. Thus there is a very heavy bias towards the fill at any instant looking something like this:
---------------- | | | | | P| | LL| | LLL| | LLLL| | LLLLL| | LLLLLL| | LLLLLLL| | LLLLLLLL| | LLLLLLLLL| | LLLLLLLLLL| | LLLLLLLLLLL| | LLLLLLLLLLLL| | LLLLLLLLLLLLL| |SSSSSSSSSSSSSS| |SSSSSSSSSSSSSS| |SSSSSSSSSSSSSS| ----------------where
- P is an unoptimised pixel
- L is a part line, that can be fairly efficiently represented by my "half-vectorisation", and
- S is an optimal block from the "stacked vectorisation" approach
Monday, 27 February 2023
Stepping up, and back, with the new Next.js "app" directory
I'm toying around with a new web-based side project and I thought it was time to give the latest Next.js version a spin. Although I've used Create-React-App (generally hosted on Netlify) more recently, I've dabbled with Next.js in one capacity or another since 2018, and this time some server-side requirements made it a better choice.
The killer feature of the 2023 "beta version" of Next.js (which I assume will eventually be named Next.js 14) is the app directory, which takes Next's already-excellent filesystem-based routing (i.e. if you create a file called bar.tsx in a directory called foo, you'll find it served up at /foo/bar without writing a line of code) and amps it up. A lot.
I won't try and reiterate their excellent documentation, but their nested layouts feature is looking like an absolute winner from where I'm sitting, and I'd like to explain why by taking you back in time. I've done this before when talking about React-related stuff when I joked that the HTML <img> tag was like a proto-React component. And I still stand by that comparison; I think this instant familiarity is one of the fundamental reasons why React has "won" the webapp developer mindshare battle.
Let me take you back to 1998. The Web is pretty raw, pretty wild, and mostly static pages. My Dad's website is absolutely no exception. I've meticulously hand-coded it in vi as a series of stand-alone HTML pages which get FTP'ed into position on his ISP's web server. Although I'm dimly aware of CSS, it's mainly still used for small hacks like removing the underlines from links (I still remember being shown the way to do this with an inline style tag and thinking it would never take off) - and I'm certainly not writing a separate .css file to be included by every HTML file. As a result, everything is styled "inline" so-to-speak, but not even in the CSS way; just mountains of widths and heights and font faces all over the place. It sucked, but HTML was getting better all the time so we just put up with it, used all that it offered, and automated what we could. Which was exactly what I did. If you dare to inspect the source of the above Wayback Machine page, you'll see that it uses HTML frames (ugh), which was a primitive way of maintaining a certain amount of UI consistency while navigating around the site.
The other thing I did to improve UI consistency, was a primitive form of templating. Probably more akin to concatenation, but I definitely had a header.htm which was crammed together with (for-example) order-body.htm to end up with order.htm using a DOS batch file that I ran to "pre-process" everything prior to doing an FTP upload - a monthly occurrence as my Dad liked to keep his "new arrivals" page genuinely fresh. Now header.htm definitely wasn't valid HTML as it would have had unclosed tags galore, but it was re-used for several pages that needed to look the same, and that made me feel efficient.
And this brings me to Next.js and the nesting layouts functionality I mentioned before. To achieve what took me a pile of HTML frames, some malformed HTML documents and a hacky batch file, all I have to do is add a layout.tsx and put all the pages that should use that UI alongside it. I can add a layout.tsx in any subdirectory and it will apply from there "down". Consistency via convention over configuration, while still nodding to the hierarchical filesystem structures we've been using since Before The Web. It's just really well thought-out, and a telling example of how much thought is going into Next.js right now. I am on board, and will be digging deeper this year for sure.
Sunday, 12 June 2022
Introducing ... Cardle!
Yes, it's yet-another Wordle clone, this time about cars:
https://www.cardle.xyz
Like so many other fans of Wordle, I'd been wanting to try doing a nice self-contained client-side game like this, and after trying the Australian Rules player version of Wordle, Worpel (named after a player), I saw a pattern that I could use. Worpel uses "attributes" of an AFL player like their height, playing position, and team, and uses the Wordle "yellow tile" convention to show if you're close in a certain attribute. For example, if the team is not correct, but it is from the correct Australian state. Or if the player's height is within 3 centimetres of the target player's.
After a bit of head-scratching I came up with the 5 categories that I figured would be challenging but with enough possibilities for the "yellow tile" to be helpful. There's no point having a category that can only be right (green tile) or wrong (black tile). The least-useful is probably the "model name" category but of course that is vital to the game, and having played the game literally hundreds of times now, it has on occasion proved useful to know that a certain character appears in the target car's name (obviously cars like the Mazda 6 are hugely helpful here!)
It has been a while since I last did a publicly-visible web side-project, and I wanted to see what the landscape was like in 2022. The last time I published a dynamic website it was on the Heroku platform, which is still pretty good, but I think there are better options these days. After a bit of a look around I settled on Netlify, and so far they've delivered admirably - fast, easy-to-configure and free!
There has been some criticism bandied about for create-react-app recently, saying it's a bad starting point, but for me it was a no-brainer. I figure not having to know how to optimally configure webpack just leaves me more brain-space to devote to making the game a bit better. So without any further ado, I'd like to showcase some of my favourite bits of the app.
Tile reveal animation
Wordle is outstanding in its subtle but highly-effective animations that give it a really polished feel, but I really didn't want to have to use a Javascript animation library to get a few slick-looking animations. The few libraries I've tried in the past have been quite heavy in both bundle-size and intrusiveness into the code. I had a feeling I could get what I wanted with a suitable CSS keyframes animation of a couple of attributes, and after some experimenting, I was happy with this:
@keyframes fade-in { from { opacity: 0; transform:scale(0.5) } 50% { transform:scale(1.2); opacity: 0.5; } to { opacity: 1; transform:scale(1.0) } }
I really like the "over-bulge" effect before it settles back down to the correct size. The pure-CSS solution for a gradual left-to-right "reveal" once a guess has been entered worked out even better I think. Certainly a lot less fiddly than doing it in Javascript:
.BoxRow :nth-child(1) { animation: fade-in 200ms; } .BoxRow :nth-child(2) { animation: fade-in 400ms; } .BoxRow :nth-child(3) { animation: fade-in 600ms; } .BoxRow :nth-child(4) { animation: fade-in 800ms; } .BoxRow :nth-child(5) { animation: fade-in 1000ms; }Those different times are the amount of time the animation should take to run - giving it the "sweeping" effect I was after:
Mobile-first
As developers we get far too used to working on our own, fully up-to-date, desktop, browser of choice. But a game like this is far more likely to be played on a mobile device. So I made a concerted effort to test as I went both with my desktop Chrome browser simulating various mobile screens and on my actual iPhone 8. Using an actual device threw up a number of subtle issues that the desktop simulation couldn't possibly hope to replicate (and nor should it try) like the extraordinarily quirky stuff you have to do to share to the clipboard on iOS and subtleties of font sizing. It was worth it when my beta-testing crew complimented me on how well it looked and played on their phones.
Performance
The site gets 98 for mobile performance (on slow 4G) and 100 for desktop from PageSpeed, which I'm pretty chuffed about. I spent a lot of time messing around with Google Fonts and then FontSource trying to get a custom sans-serif font to be performant, before just giving up and going with "whatever you've got", i.e.:
font-family: 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;... sometimes it's just not worth the fight.
The other "trick" I did was relocating a ton of CSS from the various ComponentName.css files in src right into a <style> block right in the head of index.html. This allows the browser to get busy rendering the header (which I also pulled out of React-land), bringing the "first contentful paint" time down appreciably. Obviously this is not something you'd want to be doing while you're in "active development" mode, but it doesn't have to be a nightmare - for example, in this project I made good use of CSS Variables for the first time, and I define a whole bunch of them in that style block and then reference them in ComponentName.css to ensure consistency.
Saturday, 26 March 2022
Things people have trouble with in React / Javascript, part 1: Too much React state
I've been on a bit of a Stack Overflow rampage recently, most commonly swooping in and answering questions/fixing problems people are having with their React apps. I'm going to spend my next few blog posts going over what seems to be giving people the most trouble in 2022.
Episode 1: In which things are overstated
This problem typically manifests itself as a question like "why doesn't my UI update" or the inverse "my UI is always updating" (which is almost always related to useEffect - see later in this series). With the useState hook, state management becomes so easy, it's tempting to just scatter state everywhere inside a component instead of thinking about whether it belongs there, or indeed if it's needed at all.
If I see more than 3 useState hooks in one component, I get nervous, and start looking for ways to:
- Derive the state rather than store it
- Push it up
- Pull it down
What do I mean? Well, I see people doing this:
const [cars, setCars] = useState([]); const [preferredColor, setPreferredColor] = useState(undefined); const [preferredMaker, setPreferredMaker] = useState(undefined); // DON'T DO THIS: const [filteredCars, setFilteredCars] = useState([]); ...Obviously I've left out tons of code where the list of cars is fetched and the UI is wired up, but honestly, you can already see the trouble brewing. The cars list and the filteredCars list are both held as React state. But filteredCars shouldn't be - it's the result of applying the user's selections (preferred color and maker) and so can be trivially calculated at render time. As soon as you realise this, all kinds of UI problems with staleness, flicker, and lag just melt away:
const [cars, setCars] = useState([]); const [preferredColor, setPreferredColor] = useState(undefined); const [preferredMaker, setPreferredMaker= = useState(undefined); // Derive the visible list based on what we have and what we know const filteredCars = filterCars(cars, preferredColor, preferredMaker); ...
I think some people have trouble with their mental model around functional components, and are afraid to have a "naked const" sitting there in their function - somehow it's a hack or a lesser variable than one that useState handed you. Quite the reverse I think.
Another argument might be that it's "inefficient" to derive the data on each and every render. To that, I counter that if you are maintaining the cars and filteredCars lists properly (and this is certainly not guaranteed), then the number of renders should be exactly the same, and thus the amount of work being done is the same. However there's a strong chance that deriving-on-the-fly will actually save you unnecessary renders. I might keep using this car-filtering analogy through this series to explain why this is.
Saturday, 29 January 2022
2022 To-Do List
There's just never enough time and/or motivation to do all the things I've got on the back-burner, but some of these have been around for just too long. Hopefully listing them here (and ideally ticking them off with suitable links) will be motivating...
- The openHAB Broadlink Binding still hasn't been merged into the openHAB codebase. Every week it becomes harder - with such a busy upstream repository, with very exacting code-review standards, it's like nailing jelly to the wall ... of a moving train.
- frenzy.js my browser-based retro 8-bit arcade game has been rotting for years now. It needs a full React upgrade and some deep thought (or reading-up, or both) about some of the basic game algorithms (like collision-detection, for example)
- Tons of openHAB mini-projects to automate things around the house - blinds, heating vents, all sorts of stuff
Wish me luck...
Thursday, 24 June 2021
How do I find all the HTML elements that aren't getting my preferred font?
A quickie script that saved a lot of manually combing through the DOM
Someone noticed that certain elements in our React app were not getting the desired font-face, instead getting plain-old Arial. I wanted to be able to programmatically sniff them out in the browser, so here's what I came up with, mainly thanks to an answer on Stack Overflow for finding all elements on a page, and the MDN documentation for the getComputedStyle function which browsers helpfully expose.
Whack this in your browser's Javascript console and you should be able to hover on any element that is listed to see it on the page:
// Start where React attaches to the DOM const reactRoot = document.getElementById("root"); // Get all elements below that const kids = reactRoot.getElementsByTagName("*"); for (var e of kids) { if (window.getComputedStyle(e)["font-family"] === "Arial") { console.log(e); // Allows it to be hovered in console } }
In case you were wondering, the culprit here was a button that didn't have its font-family set - and Chrome (perhaps others) will use its default (user-agent stylesheet) font style for that in preference to what you have set on the body, which you might be forgiven for assuming gets cascaded down.
Thursday, 31 December 2020
Quick 2020 A-Z Wrapup
So 2020 was a thing, huh?
In between all the incredibly bad stuff, there were a few cool techy things worth remembering:
- Apple released their M1 Silicon which is ushering in a new level of performance and power-efficiency
- OpenHAB Version 3.0 has rolled out and fixed a lot of quirks and clunky behaviours, improving the WAF of this automation platform still further
- Tesla shipped its millionth electric car and became the world's most valuable carmaker
- Zeit rebranded as Vercel and continued to build Next.js as the best framework for React apps
Stay safe!
Saturday, 28 December 2019
SOTSOG 2019
It's been quite a while since I last did a SOTSOG awards ceremony but there have definitely been some standouts for Standing On The Shoulders Of Giants this year. So without any further ado:
React.js
I wouldn't consider building a front-end with anything else. Hooks have been a game-changer, taking the declarative approach to the next level.
Apollo GraphQL
Version 2.x of the reference GraphQL client library brought vast improvements, and the React Hooks support fits in beautifully. Combine it with top-notch documentation and you've got a stone-cold winner for efficient and elegant data communications.
Next.js and now.sh
The dynamic duo from Zeit have almost-instantly become my go-to for insanely-quick prototyping and deployment. The built-in Lambda deployment system is ridiculously good. Just try it - it's free.
Honourable Mentions
Typescript and ES6
For making JavaScript feel like a grown-up language. Real compile-time type safety and protection from the dreaded undefined is not a function, the functional goodness of map(), reduce() and Promises without pain.
Visual Studio Code
Two Microsoft products getting a nod?!? Amazing to see - a stunning turnaround this decade from Redmond. VSCode has simply exploded onto most developers' launch bars thanks to its speed, flexibility, incredible rate of feature development and enormous plugin community. At this price point, it's very hard to look further for an IDE.
Saturday, 27 July 2019
React: 24 years' experience ;-)
I wrote my first HTML page in 1995. Unfortunately I don't have a copy, but given the era you can be pretty confident it would have consisted of a wall of Times New Roman text in sizes h1, h2 and p broken up with some <hr>s, a repeated background image in a tiled style, and, down at the bottom of the page, an animated Under Construction GIF.
My mind turns to that first img tag I placed on the page. I would have Yahoo-ed or AltaVista-ed (we're well before the Googles, remember) for a reference on HTML syntax, and awkwardly vi-ed this line into existence:
<img src="images/undercon.gif" width="240" height="80">
Undoubtedly I would have forgotten a double-quote or two, got the filename wrong and/or messed up the sizing at first, but eventually, my frustrated bash of the [F5] key would have shown the desired result in Netscape Navigator, and delivered me a nice little dopamine hit to boot. I learnt something, I built something, and it worked. Addictive.
24 years later while reflecting on how far we've come in many areas, I've just realised one of the many reasons why I enjoy developing React.js apps so much. Here's a random snippet of JSX, the XML-like syntax extension that is a key part of React:
<CancelButton bg="red" color="white" width="6em" />
Seem a little familiar?
One of the most successful parts of the React world is the "component model" which strives to break pages down into small, composable, testable elements, combined with a "declarative style" where, to quote React demigod Dan Abramov:
"we can describe the process at all points in time simultaneously"
If you've written any HTML before, writing JSX feels totally frictionless, because the <img> tag has been encapsulating complex behaviour (namely, converting a simple string URL into a web request, dealing with errors, and rendering the successfully-fetched data as a graphical image with the desired attributes) in a declarative way, since 1993.
And it turns out to have been another web god, Marc Andreessen, who originally suggested the basic form of the <img> tag. Truly we are standing on the shoulders of giants.
Friday, 24 August 2018
Building a retro game with React.js. Part 5 - Fill 'er up
The answer was to challenge my own comfort zone. Allow myself to quote, myself:
I could do that with an HTML canvas, but I'm (possibly/probably wrongly) not going to use a canvas. I'm drawing divs dammit!
After all, if ever there was a time and place to learn how to do something completely new, isn't it a personal "fun" project? After a quick scan around the canvas-in-React landscape I settled upon React-konva, which has turned out to be completely awesome for my needs, as evidenced by the amount of code changes actually needed to go from my div-based way of drawing a line to the Konva way:
The old way:
const Line = styled('div')` position: absolute; border: 1px solid ${FrenzyColors.CYAN}; box-sizing: border-box; padding: 0; `; renderLine = (line, lineIndex) => { const [top, bottom] = minMax(line[2], line[4]); const [left, right] = minMax(line[1], line[3]); const height = (bottom - top) + 1; const width = (right - left) + 1; return <Line key={`line-${lineIndex}`} style={{ top: `${top}px`, left: `${left}px`, width: `${width}px`, height: `${height}px` }} />; }
The new way:
import { Line } from 'react-konva'; renderLine = (line, lineIndex) => { return ( <Line key={`line-${lineIndex}`} points={line.slice(1)} stroke={FrenzyColors.CYAN} strokeWidth={2} /> ); }
... and the jewel in the crown? Here's how simple it is to draw a closed polygon with the correct fill colour; by total fluke, my "model" for lines and polygons is virtually a one-to-one match with the Konva Line object, making this amazingly straightforward:
renderFilledArea = (filledPolygon) => { const { polygonLines, color } = filledPolygon; const flatPointsList = polygonLines.reduce((acc, l) => { const firstPair = l.slice(1, 3); return acc.concat(firstPair); }, []); return ( <Line points={flatPointsList} stroke={FrenzyColors.CYAN} strokeWidth={2} closed fill={color} /> ); }
Time Tracking
Probably at around 20 hours of work now.Monday, 16 July 2018
Building a retro game with React.js. Part 4 - Drawing the line somewhere
Bugs Galore
It was at this point where I started being plagued by off-by-one errors; it seemed everywhere I turned I was encountering little one-pixel gaps when drawing lines, because:- My on-screen lines are actually 2px wide
- My line-drawing function was doing an incorrect length calculation (had to do (right - left) + 1)
- I was not updating my position at the right time, so was storing my "old" position as the current line's end point; and;
- I was naively using setState and expecting the new this.state to be visible immediately
My solution to almost all of these problems (with the exception of the UI line-drawing function) was to write a heap of unit tests; these generally flushed things out pretty quickly.
Writing the line-drawing function was a weird experience. Virtually every software development "environment" I've ever used before, from BBC Basic on my Acorn Electron on, has had a function like drawLine(startX, startY, endX, endY);. And I could do that with an HTML canvas, but I'm (possibly/probably wrongly) not going to use a canvas. I'm drawing divs dammit! Here's what my function looks like:
renderLine = (line, lineIndex) => { const [top, bottom] = minMax(line[2], line[4]); const [left, right] = minMax(line[1], line[3]); const height = (bottom - top) + 1; const width = (right - left) + 1; return <Line key={`line-${lineIndex}`} style={{ top: `${top}px`, left: `${left}px`, width: `${width}px`, height: `${height}px` }} />; }Where minMax is a simple function that returns [min, max] of its inputs, and Line is a React-Emotion styled div:
const Line = styled('div')` position: absolute; border: 1px solid ${FrenzyColors.CYAN}; box-sizing: border-box; padding: 0; `;Notice that I resisted the temptation to pass the top, left etc into the Line wrapper. The reason for this is that doing so results in a whole new CSS class being created, and getting applied to the line, every time one of these computed values changes. This seems wasteful when most of the line's attributes remain the same! So I use an inline style to position the very-thin divs where I need it.
Time Tracking
Up to about 12 hours by my rough estimate.Friday, 15 June 2018
Building a retro game with React.js. Part 3 - I Like To Move It
Again, starting with the easy stuff, I wanted the four directional keys to move the Player around. But in Frenzy, you can only move (as opposed to draw) along the boundaries of the game area and on lines you have already drawn. So if we look at my first iteration of the code in GameArea to handle a request to move the Player left, it's something like this:
update = () => { if (this.keyListener.isDown(this.keyListener.LEFT)) { this.moveLeft(); } }; moveLeft = () => { if (this.canMove(Direction.LEFT)) { this.setState({ playerX : this.state.playerX -1 }); } }I ended up bundling quite a lot of smarts into the Direction enumeration in order to make the logic less "iffy" and more declarative. That one Direction.LEFT key encapsulates everything that is needed to check whether a) the player is on a line that has the correct orientation (horizontal) and b) there is room on that line to go further to the left.
A line looks like this:
[Orientation.HORIZONTAL, 0, 0, 478, 0], // startX, startY, endX, endYand Direction looks like this:
export const Direction = { LEFT: { orientation: Orientation.HORIZONTAL, primaryCoord: (x, y) => y, lineToPrimaryCoord: (line) => line[2], secondaryCoord: (x, y) => x, testSecondary: (c, line) => c > Math.min(line[1], line[3]) }, ... }
My test for whether I can move in a certain direction is:
static canPlayerMoveOnExistingLine = (playerX, playerY, direction, lines) => { const candidates = lines.filter(line => { return (line[0] === direction.orientation) }); const pri = direction.primaryCoord(playerX, playerY); const primaryLines = candidates.filter(candidateLine => { return direction.lineToPrimaryCoord(candidateLine) === pri; }); if (primaryLines.length > 0) { const sec = direction.secondaryCoord(playerX, playerY); const found = primaryLines.find(line => { return direction.testSecondary(sec, line); }); return typeof found !== 'undefined'; } return false; }Declared static for ease of testing - easy and well worth doing for something like this where actually moving the player around is time-consuming and tedious. It's working well as it stands, although as we all know, naming things is hard. It's pretty easy to follow the process though. At this point I'm holding a lines array in this.state and doing filter and find operations on it as you can see above. We'll have to wait and see whether that will be fast enough. It may well be a good idea to keep a currentLine in state, as most of the time it will be unchanged from the last player movement. Next up, it's time to start drawing some new lines on the screen!
Kudos
I am starting to build up some tremendous respect for the original author of this game; although often dismissed as "very simple" there are some tricky little elements to coding this game and I'm only just scratching the surface. To achieve the necessary performance on an 8-bit, 1MHz processor with RAM measured in the handfuls of kilobytes is super impressive. Assembly language would have been necessary for speed, making the development and debugging a real pain. I haven't even started thinking about how to do the "fill" operation once a line has been drawn and it encloses some arbitrary space, but I suspect the original developer "sniffed" the graphics buffer to see what was at each pixel location - a "luxury" I don't think I'll have!Time Tracking
Up to about 6 hours now.Monday, 11 June 2018
Building a retro game with React.js. Part 2 - Screens
One thing I hadn't thought about was the behaviour of listening to the P key to pause and also resume. The first iteration of the code would flicker madly between the game page and the paused page whenever the P key was pressed - the game loop runs so fast that the app would transition multiple times between the two pages, because the isDown test would simply toggle the state of this.state.paused. I messed around for a while with storing the UNIX time when the key was first pressed, and comparing it to the current time, before realising I really just needed to know what "direction" was being requested, and then waiting for this "request" to finish before completing the transition.
... debounce = (testCondition, key, newState) => { if (testCondition) { if (this.keyListener.isDown(key)) { // No. They are still holding it return true; } this.setState(newState); } return false; }; handleKeysWhilePaused = () => { if (this.debounce(this.state.pauseRequested, P, { pauseRequested: false, paused: true })) { return; } if (this.debounce(this.state.unpauseRequested,P, { unpauseRequested: false, paused: false })) { return; } if (this.keyListener.isDown(Q)) { this.props.onGameExit(); } if (this.keyListener.isDown(P)) { this.setState({unpauseRequested: true}); } } handleGameplayKeys = () => { if (this.keyListener.isDown(P)) { this.setState({pauseRequested: true}); } } update = () => { if (this.state.pauseRequested || this.state.paused) { this.handleKeysWhilePaused(); } else { this.handleGameplayKeys(); } }; ...Effectively I've implemented an isUp(), which is surprisingly not in the react-game-kit library. Oh well, it's all good learning.
Time tracking
I'd estimate the total time spent on the game so far would be 3-4 hours.Friday, 8 June 2018
A New Old Thing; Building a retro game with React.js. Part 1 - Background and Setup
At the risk of being immodest, I'm comfortable with those concepts - Design Patterns from waaaay back and the functional paradigms from my five-year (and counting) love affair with Scala. What I wanted to explore was - what would happen if I built a React app by myself, endeavouring to write the cleanest, purest software based upon the best starting-point we currently have? How productive could I be? How long would it take to build a working full app? How would maintenance go? How quickly could I add a new feature?
As my day job is basically building CRUD apps, I wanted to do something a lot more fun for this side-project. And what could be more fun than a game? (Mental note: ask people working at Electronic Arts...) There's also a pleasing circularity in building a game and documenting how I did it - back in my earliest days of having a computer, aged about 7, I would buy magazines with program listings and laboriously enter them, line-by-line, while marvelling at how anyone could really have been good enough to know how to do this.
The Game
I'll admit, I've never built a non-trivial game before, but I think attempting an 8-bit home computer game I remember fondly from my childhood, on top of 2018's best front-end technologies, should be about the right level of difficulty.The game I'll be replicating is called Frenzy; a Micro Power publication for the BBC B, Acorn Electron and Commodore 64. My machine was the Electron - basically a low-cost little brother to the mighty Beeb; highly limited in RAM and CPU, titles for this platform usually needed substantial trimming from their BBC B donor games, despite using the same BBC BASIC language.
Check out the links above for more details and screenshots, but the game is basically a simplified version of "Qix" or "Kix" where the object is to fill in areas of the screen without being hit by one or more moving enemies.
Just for the hell of it, I'm going to place this game front-and-centre on my homepage at http://www.themillhousegroup.com, which I just nuked for this purpose. The page is now a React app being served off a Play Scala backend as per my new-era architecture, and the key technologies I'm using so far are:
- React via Create-React-App
- react-game-kit for game secret-sauce
- react-emotion for component styling
Initial Development
To develop the game, I decided to start from the start. The welcome page would need to be suitably old-skool but would force me to consider a few things:- What screen size should I be working to?
- Can I get a suitably chunky, monospaced font?
- Press Space to start sounds easy, but how do I make that actually work?
Decisions
The original Frenzy must have operated in the BBC's graphical MODE 1 because it used a whopping 4 colours and the pixels were square. So that means the native resolution was 320x256. While it would be tempting to stick to that screen size and thus have it fit on every smartphone screen, I've decided to double things and target a 640x512 effective canvas.Some searching for 8-bit fonts led me to "Press Start 2P" which, while intended to honour Namco arcade machines, is near enough to the chunky fonts I remember fondly from my childhood home computer that I can't go past it:
As a tiny nod to the present, the "screen" is actually slightly transparent and has a drop shadow - showing how far we've come in 34 years!
The final piece of the welcome screen was achieved by mounting the FrenzyGame component in a React-Game-Kit Loop and using the KeyListener to subscribe to the keys I care about - a quick perusal of the demo game showed me how to use it:
class FrenzyGame extends Component { constructor(props) { super(props); this.keyListener = new KeyListener(); this.state = { gameOver: true }; } componentDidMount() { this.loopSubscriptionId = this.context.loop.subscribe(this.update); this.keyListener.subscribe([ this.keyListener.SPACE ]); } componentWillUnmount() { this.context.loop.unsubscribe(this.loopSubscriptionId); this.keyListener.unsubscribe(); } update() { if (this.state.gameOver) { if (this.keyListener.isDown(this.keyListener.SPACE)) { this.setState({ gameOver: false }); } } }; ... render() { return this.state.gameOver ? this.renderWelcomeScreen() : this.renderGame(); } }
Sunday, 29 October 2017
Stack Evolution part 2
Javascript | JQuery, Moment.js, etc | |
"Presentation" | JSON/AJAX | HTML/LESS |
Controllers | Play (Reactive, Async) | |
Services | Play - RESTful API calls | Mondrian |
Persistence | MongoDB (via ReactiveMongo) |
I am simply delighted with the performance, scalability, maintainability and reliability of the entire stack from the Controllers layer down - i.e. Scala, Play and Mongo. (Incidentally, I've been running these apps on Heroku with MongoDB provided by MLab, and they have been similarly excellent). So that will not be changing any time soon.
What is no longer tenable is the mixture of HTML (including form submissions), LESS and per-page Javascript. At the top of the table, (i.e. front-end technologies), there is just too much awesomeness happening in this space to ignore. To me, React.js is the current culmination of the best thinking in the front-end world. The way every concept is the most reduced-down thing that could work (as opposed to the competition's kitchen-sink approach) really makes it a pleasure to learn and use.
Currently I'm absolutely loving Create-React-App as a brilliant bootstrapper that continues to add value even once you're up and running. It's got finely-honed and sensible defaults for things like Webpack, is upgradeable in-place, is beautifully documented and is almost psychic in always offering good suggestions or output as to what it's just done, or what can be done next. I currently have no plans to "eject" Create-React-App from any of the front-end projects I'm working on - it's just too useful to keep around.
Into this mix I've also added React Cosmos - this is a "component showcase" system that allows a super-rapid way to see all of the possible "states" of a given React component. The React props that a component needs are specified in fixture files, and Cosmos supplies a nice web UI to browse around and check that changes made to a component are working well and looking good in all of its potential states. It works excellently with the hot-reloading facilities of Create-React-App and really helps nail down component interfaces.
Another element I'm using to try and keep front-end complexity in check is Styled Components. Go have a read of their Github page for the full run-down but basically, I can get the best of both worlds with global CSS used where appropriate, keeping it DRY, together with individual components that won't mess with each other. It also massively helps in stopping the "mental CSS selector" problems during refactoring as observed by Ryan Florence. Extremely cool.
So to summarise, here's my 2017-and-beyond software stack:
Javascript | React.js (with Cosmos) | |
"Presentation" | JSON/AJAX | JSX/CSS/Styled Components |
Controllers | Play (Reactive, Async) | |
Services | Play - RESTful API calls | Mondrian |
Persistence | MongoDB (via ReactiveMongo) |
Saturday, 8 July 2017
The CRAP Stack, Part 3 - Front-End Routes with a Play server
http://myapp.com/foo/barwhere there is no explicit entry for GET /foo/bar in Play's routes and nor is there a physical asset located in /public/foo/bar for the Assets controller to return to the client, as we set up in the last instalment:
# Last of all, fall through to the React app GET / controllers.Assets.at(path="/public",file="index.html") GET /*file controllers.Assets.at(path="/public",file)What we'd like is for the React application at index.html to be served up, so that it can then consume/inspect/route from the original URL via the Window.location API.
As it stands, the last line of routes will match, the Assets controller will fail to find the resource, and your configured "client error handler" will be called to deal with the 404. This is not what we want for a "front-end route"!
We want requests that don't correspond to a physical asset to be considered a request for a virtual asset - and hence given to the React app. And after a bit of fiddling around, I've come up with a FrontEndServingController that gives me the most efficient possible way of dealing with this. The Gist is available for your copy-paste-and-improve pleasure, but the key points are:
The fall-through cases at the bottom of routes become:
GET / controllers.FrontEndServingController.index GET /*file controllers.FrontEndServingController.frontEndPath(file)Those methods in FrontEndServingController just being:
val index = serve(indexFile) def frontEndPath(path: String) = serve(path) private def serve(path: String) = { if (physicalAssets.contains(path)) { logger.debug(s"Serving physical resource: '$path'") assets.at(publicDirectory, path, true) } else { logger.debug(s"Serving virtual resource: '$path'") // It's some kind of "virtual resource" - // a front-end "route" most likely assets.at(publicDirectory, indexFile, true) } }
We're still using Play's excellent built-in AssetsController to do the hard work of caching, ETags, GZipping (all the classic webserver jobs) - we have injected it as assets using Dependency Injection - composition FTW. That true argument tells it to use "aggressive caching" which is ideal for this scenario where the bundle files we're serving up already have a cache-busting filename.
And now the "clever" bit being a recursive scan of the /public directory when we start up, assembling a definitive (and immutable!) Set[String] of what's actually a physical asset path:
lazy val physicalAssets:Set[String] = { val startingDirectory = new File(physicalPublicDirectory) deepList(startingDirectory) } private def deepList(f: File): Set[String] = { val these = f.listFiles.toSet val inHere = these.filter(_.isFile).map { f => f.getPath.replace(physicalPublicDirectory, "") } val belowHere = these.filter(_.isDirectory).flatMap(deepList) inHere ++ belowHere }
Wednesday, 17 May 2017
A Top Shelf Web Stack - Scala version - Play 2.5 + create-react-app
A fantastic example is create-react-app, by Dan (Redux) Abramov et al, which makes it ludicrously-simple to get going with a React.JS front-end application that incorporates all of the current best-practices and configuration. I came across a very fine article that discussed hosting a create-react-app front-end on a Ruby-on-Rails server (in turn on Heroku) and figured it would be a good exercise to do a version with the Play Framework 2.5 (Scala) on the back end. This version will have a lot fewer animated GIFs and general hilarity, but hopefully it is still a worthwhile exercise!
I won't go into setting up a simple Play app as complete instructions for both beginners and experts are provided at the Play website, and it can be as simple as typing:
% sbt new % sbt runOnce you've got your Play app all happy, getting the front-end going is as simple as running two commands in your project's root directory:
% npm install -g create-react-app % create-react-app clientto create a React application in the client directory. You can check it works with:
% cd client % npm startGreat. Now's a good time to commit your new client directory to Git, although you'll definitely want to add client/node_modules to your .gitignore file first. Let's modify the backend to have a tiny little JSON endpoint, call it from React when the app mounts, and display the content. First, we just add one line to our package.json so that backend data requests get proxied through the front-end server, making everything just work with no CORS concerns:
"private": true, "proxy": "http://localhost:9000/", "dependencies": { "react": "^15.5.4", "react-dom": "^15.5.4" },Make sure you kill-and-restart your React app after adding that line. Next, let's whip up a Play endpoint that returns some JSON: In conf/routes:
GET /dummy-json controllers.DummyController.dummyJsonIn app/controllers/DummyController.scala:
class DummyController extends Controller { val logger = Logger("DummyController") def dummyJson = Action { logger.info("Handling request for dummy JSON") Ok(Json.obj( "foo" -> "foo", "bar" -> "bar", "bazzes" -> Seq("baz1", "baz2", "baz3") ) ) }Check that's all good by hitting http://localhost:9000/dummy-json directly with your browser. Now we put our front-end hat on and get the React app to fetch the JSON when it mounts:
class App extends Component { componentDidMount() { console.log('Mounted'); fetch('/dummy-json',{ accept: 'application/json'}) .then(response => response.json() ) .then(json => console.log(json) ) .catch(error => console.error(error)); } ... }Setting the accept header is not strictly necessary but it helps create-react-app to know that this request should be proxied. Plus it's generally good form too. Now when your app hot-reloads, watch your browser's Network tab. You'll see the request go out on port 3000, the server log the request and respond on 9000, and the response arrive back on port 3000. Let's finish off the local-development part of this little demo by wiring that response into our app's state so that we can render appropriately:
class App extends Component {
constructor() {
super();
this.state = {};
}
componentDidMount() {
console.log('Mounted');
this.fetch('/dummy-json').then( result => {
this.setState({
result
});
});
}
fetch (endpoint) {
return new Promise((resolve, reject) => {
window.fetch(endpoint, { accept: 'application/json'})
.then(response => response.json())
.then(json => resolve(json))
.catch(error => reject(error))
})
}
render() {
let { result } = this.state;
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
{result ? (
<h3>{result.foo}</h3>
) : (
<h3>Loading...</h3>
)
}
</div>
<p className="App-intro">
To get started, edit src/App.js
and save to reload.
</p>
</div>
);
}
}
So easy!
In the next installment, we'll consider deployment to Heroku.
Friday, 28 October 2016
Configuring Jacoco4sbt for a Play application
One such plugin is Jacoco4sbt, which wires the JaCoCo code-coverage tool into SBT (the build system for Play apps). Configuration is pretty straightforward, and the generated HTML report is a nice way to target untested corners of your code. The only downside (which I've finally got around to looking at fixing) was that by default, a lot of framework-generated code is included in your coverage stats.
So without further ado, here's a stanza you can add to your Play app's build.sbt to whittle down your coverage report to code you actually wrote:
jacoco.settings jacoco.excludes in jacoco.Config := Seq( "views*", "*Routes*", "controllers*routes*", "controllers*Reverse*", "controllers*javascript*", "controller*ref*", "assets*" )I'll be putting this into all my Play projects from now on. Hope it helps someone.
Wednesday, 29 June 2016
ReactJS - Early Thoughts
I have always been comfortable with HTML, and first applied a CSS rule (inline) waaay back in the Netscape 3.0 days:
<a href="..." style="text-decoration: none;">Link</a>I'm pretty sure there were HTML frames involved there too. Good times. Anyway, the Javascript world had always been a little too wild-and-crazy for me to get deeply into; browser support sucked, the "standard library" kinda sucked, and people had holy wars about how to structure even the simplest code.
Up to about 2015, I'd been happy enough with the basic tools - JQuery for DOM-smashing and AJAX, Underscore/Lodash for collection-manipulation, and bringing in Bootstrap's JS library for a little extra polish. The whole thing based on full HTML being rendered from a traditional multi-tiered server, ideally written in Scala. I got a lot done this way.
I had a couple of brushes with Angular (1.x) along the way and didn't really see the point; the Angular code was always still layered on top of "perfectly-good" HTML from the server. It was certainly better-structured than the usual JQuery mess but the hundreds of extra kilobytes to be downloaded just didn't seem to justify themselves.
Now this year, I've been working with a Real™ Front End project - that is, one that stands alone and consumes JSON from a back-end. This is using Webpack, Babel, ES6, ReactJS and Redux as its principal technologies. After 6 weeks of working on this, here are some of my first thoughts:
- Good ES6 goes a long way to making Javascript feel grown-up
- Bad The whole Webpack-Babel-bundling thing feels really rough - so much configuration, so hard to follow
- Good React-Hot-Reloading is super, super-nice. Automatic browser reloads that keep state are truly magic
- Bad You can still completely, silently toast a React app with a misplaced comma ...
- Good ... but ESLint will probably tell you where you messed up
- Bad It's still Javascript, I miss strong typing ...
- Good ... but React PropTypes can partly help to ensure arguments are there and (roughly) the right type
- Good Redux is a really neat way of compartmentalising state and state transitions - super-important in front-ends
- Good The React Way of flowing props down through components really helps with code structure
So yep, there were more Goods than Bads - I'm liking React, and I'm finally feeling like large apps can be built with JavaScript, get that complete separation between back- and front-ends, and be maintainable and practical for multiple people to work on concurrently. Stay tuned for more!