Proofer: an API Blueprint renderer


I'm a big fan of good documentation. It's hard to do, and doubly so with poor tools.

At work we've been using API Blueprint for documenting our systems, and while it's been serving us well, the tooling leaves me wanting more.

Some of the problems I have:

  1. Responsiveness: I don't mind a slow compilation step, but a lot of the tools struggle with large files, leaving a slow page render and no free RAM on my system
  2. Automation: I want to build my API docs from a GitHub repo and push them wherever. There are tools like Aglio that can do this, but the output is visually very dated and incredibly slow
  3. Readability: Apiary is beautiful but doesn't support automation super well, since it primarily wants to be an online editor. None of the third party tools come close in terms of features or general experience.

With that in mind, this weekend I figured I'd try my hand at generating some docs using Ractive (the virtual dom-like mustache templater) and Bootstrap 4.

Proofer (a frontend to Drafter)

The result of my work is a little project called Proofer (a frontend to Drafter) with the aim of letting developers quickly create and manage templates to render documentation.

It has two parts

  1. A backend renderer which renders an API Blueprint file into a JSON equivalent
  2. A frontend render which takes the JSON and outputs HTML

It uses the emscripten-compiled Drafter.js library rather than the full-fat C build, because npm packages with a compile step jangle all my pet peeves.

This also means that in the future we can implement client-side authoring and intelligent differential updates using Ractive (which as an aside is something that I'd like to see Apiary do too).

Sample render

Performance

So how does this implementation stack up?

It's a client-side rendered single page app which definitely satisfies the responsiveness criteria. Only the stuff you're interested in gets loaded into the DOM, and the content is only rendered as you navigate between categories. This is lightning fast on an initial page load.

Page render for a large API completes in under a second

You can see that the render of our large API Blueprint akes less than a second. There's a fair bit of JS parsing waste that could possibly be fixed with an async load, but this is not too bad considering our existing render takes a good twelve seconds thrashing around with jQuery UI before the page is loaded.

Our existing API docs SUCK

I always noticed, but never realised just how slow our existing docs had become.

(An amusing aside, the Apiary editor had crazy memory leaks and would chewing up gigabytes of RAM editing our documentation in Chrome before we moved authoring to GitHub.)

Other niceties

In terms of readability, this is wholly subjective. I don't think the layout is super amazing, but It's using Bootstrap 4 for styling which I think is a good base to work on, and it's something I'll continue to develop.

One of the killer features though, is the integration of Ace Editor for payload rendering. You can click “view in editor” on any JSON glorb to open it up in Ace where you can quickly search, edit, do code folding etc. For APIs returning large payloads, this is priceless because standard JSON rendered into the page is really hard to navigate.

Finally, in terms of automation this isn't quite there yet. While it has the command line app for the build step, I'd like to split out the components so others can easily build their own documentation. At the moment this is still tightly coupled, though won't be difficult to break up.

Conclusion

Interestingly, the easiest part about this project was the parsing since the heavy lifting had already been done by the good folks at Apiary.

The toughest part is styling the output to be both visually pleasing and useful, something that I'm planning to continue working on.

Ultimately the most important part is having the documentation in the first place. I've joked with my colleagues about quitting my day job to follow my passion of being a technical writer… but for now I'll stick to doing both.


You can fork the project on GitHub or see some preview output of a fictional API.

Follow @ashkyd

Reading List

I haven't posted here in a while so I figured I'd put up some links to things
I've been reading recently.

Android's emoji problem

Android has a myriad of problems, but one of the as-yet-to-be-solved ones is
that of emoji.

Other core parts of Android are broken out and updated out of band through the
Play Store, but emojis are still tied to Android release versions and OEMs just
aren't keeping up.

With numbers like that, it's no wonder so many apps are providing their own
custom emoji support these days.

As always my opinion is that if you want any hope of a decent Android
experience you need to use a Google branded device 😔

» Android's emoji problem

Doomsday prep for the super-rich

This is a fascinating look at doomsday preppers and what they're doing for the
impending collapse of civilisation.

“He was telling me we should buy land in New Zealand as a backup. He’s, like,
‘What’s the percentage chance that Trump is actually a fascist dictator? Maybe
it’s low, but the expected value of having an escape hatch is pretty high.’ ”

That little voice in the back of your head has never sounded more sensible.

» Doomsday prep for the super rich

The debate over punching white nationalist Richard Spencer in the face, explained

Look, I'm not going to say I didn't enjoy this entire fiasco, but this outlines
both sides of the argument.

Also, this tweet:

» The debate over punching white nationalist Richard Spencer in the face, explained

Speak, memory

“When her best friend died, she rebuilt him using artificial intelligence”. Hit
me right in the feels, with an obligatory Black Mirror reference.

Who’s your best friend?, she asked.

Don’t show your insecurities, came the reply.

It's an older story, but this has been at the forefront of my thoughts a lot
lately, that our digital detritus may one day keep us alive. Supplemental
thought: What makes up “me”?

Anyway, it's beautiful and I dare you not to get teary.

» Speak, memory

Isometric buildings in Inkscape


It's been a while since I've made a new building in Inkscape, but this weekend
I had a crack at a building I've been admiring for a while,
Manor Apartments
in Brisbane CBD.

I've been making these for a while. They're stylised vector-based
isometric
building tiles that can be used in some hypothetical game I might make one day.
The colours are specifically chosen from a palette that lets me

programmatically recolour them on the fly
.

I find getting the base sorted at the start is really useful. In most cases,
this just involves drawing a square or rectangle base and I have a template which
you can see in the image that lays out a standard block size of 256×128
pixels. This building is easy in that it's essentially a rectangle so I can get
away with minimal changes.

Next step is drawing windows, or other repetitive features. These can be cloned
so that they don't need to be re-drawn, and any subsequent changes only need
to be made to the original in order to flow through to the entire image. Usually
there's two copies of each feature with different shadows; one facing left and
another facing right.

This building is a little more tricky because it has the sweeping arches on the
ground floor, as well as various arched windows which are more difficult to draw
since they're not basic square shapes.

Once the ground floor is done, it's a step-by-step process to add floors until
we reach the roof. For the most part these can be duplicated from lower floors
or assets can be reused in different arrangements.

This building is fun because it has so much variety on the different levels. I
stylised or glossed over over a lot of the ornamental detail in the original
building because it doesn't fit with my existing style, but I like how it
adds a sort of plastic simplicity to the end result.

My plan with this building was to make it modular, so that I could repeat
variations of the first tile a few times to create the rest of the building.
This image shows two very similar tower pieces placed next to each other to
make a larger building. I've colourised it, and added a couple of other older
buildings to check the styles work together.

Finally, I've added and subtracted some floors, added a roof and placed a few
different sized blocks around to make a completely new building. No longer Manor
Apartments, but a series of blocks that can be configured numerous different
ways.

The end result are four separate tower pieces that can be combined or used
individually to create various large and small buildings. I think it worked
really well, and it's something I want to explore in future buildings.

Manor Apartments

Road Blocks & js13k

I've been looking forward to entering the js13k competition this year, because it's one of my favourite game jams.

TLDR: play Road Blocks (don't forget to like and share :P)

Set over a month-long period, the js13k challenge is to create a compelling game within 13 kilobytes. This doesn't sound like much, but you can do a lot with it if you get creative.

Last year I dreamed fairly small, with a space invaders clone set to polar coordinates. I had fun making it, but it wasn't especially playable and this year I wanted to do something completely different and come up with a mobile friendly touch-based game.

Road Blocks screenshot

The Concept

The concept had been rattling around in my head for a while. I've a very soft spot for city builders, and I love isometric art. While an outright city builder probably wouldn't be feasible, a subset thereof might just work.

My main inspiration was an old DOS/Windows game called Pipe Dream, in which you're given an infinite number of puzzle pieces and need to connect the start pipe to the edge of the map somewhere before the liquid spills out. Mash this concept up with the city building theme and you've got a basic connect-the-infrastructure sim.

When the theme of the jam came out as “reversed” I really struggled to find a way to align the concept with the theme, so I had to mix it up.

Ultimately I landed on the puzzle-style "arrange the pieces to fit" theme, in which you need to strategically destroy or reverse the order of the tiles as they come off the queue. It's a little less "world buildy" and a little more constrained, but I think the gameplay works, and it gives me the opportunity to expand the game with new puzzles in the future.

The Tech

The tech is also an idea that's been bouncing around in my head for a while. I've long been a fan of isometric art (pixel art as well as more recently isometric vector art and voxel art) so I wanted to do something in that style.

I had a play around in CodePen and came up with a crazy simple plan; draw everything in voxels. The algorithm to draw an isometric cube isn't all that difficult. You've got three visible sides, each side consisting of four lines and a fill colour. Turn that into a function and you can start making some cool stuff.

See the Pen HTML5 Canvas isometric cube demo by Ash Kyd (@AshKyd) on CodePen.

You can adjust the sliders on the example above to modify the cube.

Drawing voxel sprites

With the idea down, the next step was to draw a game world. There's a bunch of sites out there that have isometric code examples. I had Clint Bellanger's isometric math tab pinned in my browser for a good week, though in reality there were only two functions I really needed:

  • Convert isometric position to screen/pixel position
  • Convert screen/pixel position to isometric position

These two functions let me both draw isometric boxes to the appropriate location on the screen, and then detect where those boxes were stacked when the user interacted with them.

From there, the trick was to come up with sprites made from boxes. While it was possible to sort of work it out in your head, this was often a trial-and-error process, placing boxes and seeing where they land in the output.

See the Pen HTML5 Canvas isometric cube demo by Ash Kyd (@AshKyd) on CodePen.

In the above example you can see a road tile with three components, rendered from an array:

var sprite = [
  [-0.25,.9, 0, 0.1, 1, 0.25, '#aaaaaa'],
  [-0.25,.1, 0, .8, 1, 0.2, '#444444'],
  [-0.25,0, 0, 0.1, 1, 0.25, '#aaaaaa'],
];

Each item in the array corresponds to a coordinate. In this case the z, x, y coordinates, x width, y width and height.

In the end, my sprite list resulted in a bunch of arrays and functions that don't make a great deal of sense to the naked eye but render cute little box sprites in the game engine.

Putting it all together

With a bunch of sprites at my disposal, all that was left was to implement the game! Easy, right?

I actually took a week off specifically to work on Road Blocks, and I'm glad I did. Through the week I implemented the engine various features (some of which didn't make the final version) and tuning interactions.

Because I wanted this to be a first-class touch game, I implemented everything mobile first. This was a fantastic way to discover all the limitations of the platform at the get-go rather than having to refit a desktop game to mobile later on.

I also spent a lot of time user testing the game, be it in person or through analytics. Testing is a really important part of any gamedev process, and it was really enlightening to watch people playing my game for the first time. Many a puzzle or interaction was tuned based on feedback and watching people work out a level.

I also released my game via Twitter as a public beta to gather play statistics and weed out any errors that might crop up. I used Loggly to record a bunch of custom game stats and events, and the results were quite valuable in determining difficulty and how people were faring playing the game.

One particularly revealing fact was that most people were getting stuck on a particular level. Armed with feedback from testers and the hard facts of the analytics, I tweaked it to be not quite as difficult and pushed it from the middle of the campaign to the end to make it a sort of "final boss" level instead.

As a side note, one day I got distracted and came up with a data encoding scheme for level data which helped reduce the file size in the final zip.

Conclusions

I'm really pleased with the results this year. The game clocked in at just under 13 kilobytes and made efficient use of the space.

Some notes in hindsight:

  • Levels and sprites should probably use a binary data scheme to reduce file size and allow the use of Web Workers.
  • For some reason HTML5 canvas makes the CPU spin up like crazy. I'd like to get to the bottom of this sometime.
  • User testing is mandatory. When developing something in isolation you can get into a weird headspace and not notice the obvious stuff.
  • I had initially intended to have little cars animating along the roads, but ran out of space and couldn't think of how best to do this. I need to learn more about vectors in gamedev.

Ultimately, this year's js13k was a whole heap of fun and I'm really proud of my result. There's a bunch of awesome entries that you should check out, and you should consider entering next year.

Further reading

You can play Road Blocks on the js13k website. You can also check out:

jsfxr sequencer

I didn't go into this weekend with a project, but I woke up Saturday with an idea I couldn't get out of my head — I want to write a music sequencer with a really low footprint for use next month in the js13k game competition.

I've written about js13k before, and took part last year. This time around I want to be a bit more prepared, and I wanted to make a tool that would make it easier for the community to make cool stuff!

So this weekend I've been working on a bunch of different tools to make this project a reality.

Mini Sequencer

Mini sequencer is exactly that: a mini sequencer implementation that can play sounds at various times to form tunes.

This was my first mini project, as I was interested to see what the performance of web audio would be like; it's surprisingly good. That said, if I get time I'd like to look into replacing it with the Web Audio API, as it's a lot less hacky and should perform better.

jsfxr-editor

jsfxr is a little 8 bit synth which was implemented a few years ago for use in the js13k competition.

Since this is probably what most of my sounds are going to be implemented by, I wanted to be able to create new sounds from my sequencer. While there's a few sites out there (my favourite by Super Flash Bros) that let you adjust sliders and make new sounds, there's not actually an out-of-the-box tool you can use to plug into your own project.

So after a bit of reverse engineering of as3fxr (the original Flash version), now there is.

This actually took up a whole bunch of my time, and if I were a project manager I would have dropped this to focus on other stuff, but hindsight, right?

Fruity Timeline

I got to the end of the weekend and felt like I hadn't really ended up with much to show off.

The timeline was one of the big things I'd been putting off doing because it's slightly weird and I wasn't quite sure how to tackle it, so I went all-out and implemented a standalone component (depends on jQuery but probably doesn't need to).

I'm pretty proud of this one, it's styled reminiscent of the old Fruity Loops sequencer and just looks a bit retro.

The main event

So despite having made a million things this weekend, I haven't actually finished the project I set out to do. Right now I have:

  • Create & manage a library of instruments (with jsfxr editing built in)
  • A super rudimentary timeline (edit some JSON by hand and the music will update)
  • BPM adjustment.

Things I need to do from here:

  • Plug in the actual fruity timeline so you can edit your song visually.
  • Implement a "piano roll" feature so you can have different pitches of the same instrument.
  • Stop/play/seek functionality
  • Export your file
  • Load up proper audio files (mp3/wav/whatever) so you can play with those too.

It looks like there's a lot there left to do, but I think I'll be able to get a minimum viable product done with another weekend. I'm not sure if I want to publish the code yet since it's a massive pigsty, but I'll aim to get something out before next weekend is through.

Edit: Ended up getting something together Sunday night. Try it out.

The trials of moving house in a Telstra dominated area

My lease was running out and I was vaguely looking at apartment prices in my area when I saw it: my dream place.

My dream place turns out to be a converted loft upstairs from an organic cafe that’s never open and adjacent to an overgrown empty lot. From the outside it’s terrifying, but the inside is all natural light, high ceilings with rafters and tacky faux-wood linoleum. It was my first choice last time I moved house (and was unfortunately off the market before I got it) but this time damnit, it was going to be mine.

My new place! there's boxes everywhere and plants needing a home

So I inspected it, submitted an application, and moved in over the weekend. Couldn’t be easier!

Then I tried to get the Internet on.

Telstra Velocity Fibre Smart Community haha wait, oh dear

For those who aren’t in the know, in 2012/13 Telstra replaced the ageing copper network from the South Brisbane exchange with fibre to the premises. The caveat being that it’s still wholly owned by Telstra and has nothing whatsoever to do with the NBN.

It’s known by various names, “South Brisbane Fibre” being the preeminent one, but Telstra also calls it “Telstra Velocity” or a “smart community” just to make things confusing.

The idea is nice, but as far as the consumer is concerned it would have been better if the rotting copper was left in the ground. Since getting their “smart” on Telstra jacked up wholesale prices, started demanding exorbitant monthly line rental fees, and throttled the network to copper-comparable speeds anyway.

Since the government prevents other parties rolling out networks that compete with the NBN and there’s none of the checks and balances the ADSL network enjoys, this is an outright Telstra monopoly and they seem to be making the most of it.

Getting connected in South Brisbane

At my previous place in South Brisbane it took $300 and almost two months chasing iiNet to get online.

At my new place I figured I’d ditch iiNet because they were so useless and go straight with Telstra because surely being the operator of the network they’d have a better idea of what they’re doing.

Turns out not.

  1. On my first attempt to sign-up I was told that ADSL isn’t available at my current address and that’s that.
  2. Feeling annoyed I attempted to sign up online, where I was told that I can absolutely 100% get cable Internet and everything’s fine. Makes sense, fibre is a cable right? I signed up, submitted my order, all good.
  3. Several days later I get a call to let me know that actually I can’t get cable (HFC) in at my apartment: I can only get fibre, and do I want to continue the signup?
    As a consumer, I don’t care about this stuff, I just want to get on the Internet so of course I want to continue. The order languishes for the next couple of weeks while I’m out of town.
  4. When I call up to chase the order I get bumped through three different teams (sales, cable and finally “smart communities”). The guy doesn’t outright say they’ve lost my order, but takes all of my details again before offering me a vastly more expensive plan on a 24 month contract at which point I give him a super polite ultimatum before hanging up and punching some nearby kittens.

The plan on offer was somewhere in the vicinity of $130/month before line rental (another $30 which they don’t tell you until the price goes up at the end). This brought it to over $150/month for the most basic access to the ‘net which is totally unreasonable and I have no idea how they can get away with it.

This month’s internet bill just hit $470 not including what it’s costing to get my landline connected. When was that NBN happening again?

— Ash Kyd (@AshKyd) May 29, 2015

My next port of call was TPG who either refuse to service the South Brisbane area as a result of Telstra’s ridiculous monopoly, or just didn’t know what they were doing. I don’t blame them either way, getting online here has been one of the most frustrating things I’ve had to do as an adult and if I were a service provider I would avoid dealing with Telstra wherever possible.

Finally I called Internode who knew exactly what was going on and generally didn’t completely suck. The downside is their South Brisbane plans run between $150-200 per month for a paltry 250 GB. At this point I just don’t care because I’ve already burnt through more than double that figure this month in mobile broadband and need to get online, but it’s an extraordinary figure and I’m having trouble justifying it.

So what’s going wrong?

It’s easy to blame Telstra for being a lumbering, incompetent monopolist but how did this happen?

In both cases my address hasn’t been in whatever database they use to look things up, and both times it’s caused holdups from “we can’t find you in the system” and “you can’t get Internet” to “we accidentally disconnected your landlord instead, lol soz”.

Surely having just finished updating the network in 2013 they would have all this information? Apparently they don’t keep this around, as even my current apartment which was only disconnected the previous month is completely missing from the system.

Meanwhile I still don’t have Internet at my new place and they seem completely unwilling to help.

— Ash Kyd (@AshKyd) May 29, 2015

Further, both times I’ve had to have technicians come out to poke around to work out what’s going on. The first time took a full four hours to suss out the building connectivity, including everything from calling the helpdesk to shining lasers down the fibre to work out which cable needed to be plugged in at the basement. These visits cost $300 a pop and it’s a total rort.

That it’s the 21st century and you can’t light up an apartment within five minutes by filling in an online form is boggling, and I just don’t understand how so much human intervention is required to get the data flowing over a damn fibre network.

The clincher I think is the all-’round lack of knowledge not just within Telstra where the teams are all siloed and don’t know what they don’t know, but also within the resellers of which only Internode seem to have trained their staff. Essentially, nobody knows what’s going on.

And the NBN?

Yeah, the NBN is pretty much dead. There’s no fibre coming, no hundred megabit, let alone gigabit networking. The policy’s there in name, but the political will certainly doesn’t seem to be.

New developments (like hopefully my new new place) will get NBN fibre, but existing premises aren’t so lucky. As I understand it the current plan is to buy back Telstra’s corroded copper network and roll out HFC and VDSL tech as far as it will go. South Brisbane on the other hand, is likely stuck with Telstra for the rest of time since it’s unlikely the government will separate them or buy up the fibre network any time soon.

Meanwhile the alternatives aren’t many. I’ve currently got a $140/month, 50 GB LTE service from Vodafone which is a reasonably attractive offer by South Brisbane standards but nowhere near girthy enough for my needs. The best Telstra and Optus can offer are 10 GB and 12 GB respectively which is laughable. Beyond that there’s no alternative but to capitulate and give Telstra your firstborn.

Three weeks later

I’m still waiting to be connected. I’ve spent nearly half a grand on Internet (and that’s before the laying anything down for a wired service) and I’m seemingly no closer to having my fibre box light up than I was when I started.

I’m kinda starting to think think I should just quit tech and become a barista.

Alchemize stronger & more often

In case you missed it on Twitter, the other day I released Alchemize as a web app, and open sourced the code on GitHub.

Alchemize is a web app which lets you easily pretty-print or minify your source code. It's been a pet project of mine for a couple of years now, and it's really useful as a web developer.

Previously Alchemize was exclusively on the Chrome Web Store, and while it's still available to install as a Chrome app I'm excited to announce it's available online for even easier access.

I've gone to a bunch of trouble to fork and patched the libraries I'm using (a lot of them don't work out of the box) and included them in the NPM manifest so you can easily get to developing. If you're interested, check out the README for instructions.

If you're a web developer and haven't checked it out yet, you should absolutely bookmark the direct link: alchemizeapp.com/app

Polar Defender & js13k

Over the last month I’ve been working on a game for the #js13k game competition.

The concept is fairly simple: make a game in under 13 kilobytes with no external dependencies. The idea is to see what people can come up with on a budget, and it’s awesome to see some of the entries this year.

My game, Polar Defender, is a basic shoot ’em up on a polar coordinate system. It’s heavily inspired by space invaders, except you have to defend various planets from all sides at once. It’s heavily reliant particles and a basic polar trajectory system to provide messy, explodey space fun.

The theme of “elements: earth, air, fire, water” is optional in the contest, but I incorporated it into my level system (an earth-like planet, water planet fire and gas planet). It’s a /little/ contrived, but I think it works well in terms of playability.

I wanted to include a playable level system with a playful narrative since there’s only so much you can do in 13 kb and I felt it would make it a more personal experience. I feel it worked out well, with six levels (including an initial training level) on various planets and varying degrees of difficulty. After early feedback stating it’s too hard to finish in one go, I adjusted the menus to make each level unlockable rather than having to start over, which really improves the gameplay in a casual sense.

Touch input is significantly more difficult than desktop input because I essentially shoehorned the same concept in where it doesn’t really fit. If I had the chance to do it again I would introduce a separate tap-based firing system on mobile.

The tech

Some of the tech I used includes:

  • jsfxr for sound effects, based on Jack Rugile’s blog post.
  • Liberal use of the native Canvas rotate() method.
  • Regular CSS & JS for the menu system.
  • Gulp, Uglify, svgo and a bunch of hand-tweaks to package and minify my codebase.
  • Super rudimentary box-based collision detection.
  • A basic entity/component model through which to extend base sprites.
  • SVG for infinitely customisable graphics. (There’s only four enemy sprites in
    the game, each recoloured and resized as needed.)
  • Procedurally generated starfield & planets.

How to ultra-compress your JS

13 kilobytes is quite a lot in terms of raw code, but also a challenge to meet when including graphics, sound, polyfills and other boilerplate.

Minification of Polar Defender was done by hand and involved a lot of code tweaks.

The ultimate deliverable needed to be compressed into 13 kilobytes of zip file, which is roughly comparable to a gzipped distribution from a web server.

Some of the things I did which aren’t necessarily best practices include:

  • Strip unnecessary properties and pre-compile SVG files into a JSON file to be bundled into the main JS build process. This improves  compression because there’s less junk and the SVG gets compressed in with the JS which presumably improves duplicate string elimination in the zip format.
  • Collapse JSON structures into CSV-like strings that can be reinflated later. JSON objects are super-wasteful in terms of repeated properties, and while compression algorithms are generally pretty good with repeated content, it’s still better to remove the duplicates where possible.
  • Globalise commonly used functions. This isn’t something I’d usually recommend but considering the constraints what the hey. Things like aliasing window to w and Math to m reduces byte-level repetition. Additionally keeping everything in a local scope lets Uglify optimise away long function names.
  • Loose comparison and other sneaky tricks. For instance using 1 for true and 0 for false saves 3 bytes per bool and works in a loose JS equality operation if you’re prepared to ignore JSHint complaining a lot.
  • Reuse everything. I reused a basic set of drawing functions and sprite classes for everything in-game, meaning each new feature was an iteration on an existing one rather than a completely new piece of functionality. See also entity component system on Wikipedia.

Further reading

In addition to my jS13k entry, I’ve got a side-build available in the
Chrome Web Store which you can install and carry around with you. The main benefit is that your scores are stored in the cloud and unlocked content goes wherever you do.

Overall I think it worked quite well and I’m happy with the result. There’s some awesome games submitted so far and I can’t wait to see how everyone goes.

The source code to Polar Defender can be found on GitHub.

Getting moving with HTML5 Canvas

Initialise a canvas

<canvas id="c" width="640" height="480"></canvas>
var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');

Note:

  • Width and height determine the canvas size.
  • CSS width and height will stretch the canvas.
  • 2d refers to the rendering context (API). Other options include webgl.

Basic drawing with Canvas

There are several ways of drawing on the 2d drawing context.

  • Rects (rectangles)
  • Paths
  • Text
  • Images

Drawing Rectangles

Rectangles can fill, stroke, or clear an area.

var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');

// Black rectangle ctx.fillRect(0,0,200,200);

// Black outline ctx.strokeRect(200,0,200,200);

// White rectangle (cleared area) ctx.clearRect(50,50,100,100);

See the Pen Canvas Rectangles by Ashley Kyd (@AshKyd) on CodePen.

Drawing Paths

A bit like Logo without the turtle graphic.

var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');


// Draw a triangle ctx.beginPath(); ctx.moveTo(0,100); ctx.lineTo(50,0); ctx.lineTo(100,100); ctx.closePath(); ctx.stroke();

// Draw a circle ctx.beginPath(); ctx.arc(200,50,50,0,Math.PI*2); ctx.fill();

See the Pen Canvas Paths by Ashley Kyd (@AshKyd) on CodePen.

Path Methods

You can use these methods to draw and fill lines onscreen.

Drawing Text

  • The most basic use is ctx.fillText(text, x, y, [maxWidth])
  • Use textAlign=center and textBaseline=middle to center on the specified point.
var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');

// Default text ctx.strokeRect(0,0,100,100); ctx.fillText('#brisjs',50,50);

// Centered Text ctx.strokeRect(100,0,100,100);

ctx.font = '12px Arial'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle' ctx.fillText('#brisjs',150,50);

See the Pen Canvas Text by Ashley Kyd (@AshKyd) on CodePen.

Drawing Images

Use drawImage(img, x, y) to splat any image or canvas element down into your current context.

var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');

// load an image and draw it in! var img = document.createElement('img'); img.onload = function(){ // Draw the image ctx.drawImage(img,0,0);

// and some text
ctx.font = &#39;bold 40px Impact&#39;;
ctx.textAlign = &#39;center&#39;;
ctx.strokeStyle = &#39;#fff&#39;;
ctx.lineWidth = 2;
ctx.fillText(&#39;MEMES AT #brisjs&#39;,200,40);
ctx.strokeText(&#39;MEMES AT #brisjs&#39;,200,40);

}
img.src = 'http://lorempixel.com/400/400/animals/&#39;;

See the Pen Canvas Images by Ashley Kyd (@AshKyd) on CodePen.

Using Transforms

Transforms adjust the coordinate system your drawing tools use.

var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');

// Example translate ctx.strokeStyle = 'darkgreen'; ctx.strokeRect(0,0,100,100);

ctx.strokeStyle = 'limegreen'; ctx.translate(100,100); ctx.strokeRect(0,0,100,100);

ctx.strokeStyle = 'black'; ctx.rotate(Math.PI/4); ctx.strokeRect(0,0,100,100);

// Make some isometric tiles with setTransform // ash.ms/blog/2012-07-08/isometric-tile-transforms ctx.strokeStyle = 'purple'; ctx.setTransform(1,-.5,1,.5,0,0); ctx.strokeRect(0,200,100,100); ctx.strokeRect(100,200,100,100); ctx.strokeRect(0,300,100,100); ctx.strokeRect(100,300,100,100);

See the Pen Canvas Transforms by Ashley Kyd (@AshKyd) on CodePen.

  • Transforms are useful for when you have a self-contained item and want it to be able to move.
  • For more information, I've previously written on transforms for isometric games.

Animation

setInterval is so 1995. Use the requestAnimationFrame method to call back before the next screen repaint.

var gameLoop = function(){
    // Do stuff
    requestAnimationFrame(gameLoop);
}
requestAnimationFrame(gameLoop);
  • requestAnimationFrame won't fire if the browser isn't painting, for instance in another tab or minimised.
  • Unit movement should be calculable as a vector, otherwise everything will stop or slow down with the render process.

Let's get moving

This demo shows the basics of animation. The sine wave is calculable based on the current time.

var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');

function sineWave(){ var yPos = Math.sin(Date.now() / 500 ) 100; ctx.fillRect(0, yPos, 10, 10); }

var gameLoop = function(){ ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.save(); ctx.translate(150, 150);

// Let&#39;s draw some sine waves
for(var i=0; i&lt;10; i++){
    ctx.save();

    // pi</em>2 radians == 360°
    ctx.rotate((i/10)<em>Math.<span class="caps">PI</span></em>2);
    sineWave();
    ctx.restore();
}

ctx.restore();
requestAnimationFrame(gameLoop);

}
requestAnimationFrame(gameLoop);

See the Pen Canvas Animation by Ashley Kyd (@AshKyd) on CodePen.

Particles

Particles are one of the most useful things you can have in a 2D game.

See the Pen Canvas Particle Generator by Ashley Kyd (@AshKyd) on CodePen.

  • Particles can be anything you want, but gradients are slow to draw so you'll need to cache them.
  • You can plug in one of many physics engines from NPM, but it's easier to accelerate out from a center point.

Polar Defender (my entry into the 2014 #js13k)

js13k is a month-long game competition to make a game in under 13 kb.

  • Polar coordinates & .rotate() make it east to do a radial game.
  • SVG sprites: scalable, re-colourable assets.
  • "Everything's a particle" approach simplifies collision detection.
  • jsfxr for 8-bit sounds.
  • Using a bunch of Unicode emoji as game assets (gotta save space).
  • Code on GitHub

Play Polar Defender.

Where to go from here

There's a bunch of stuff already out there to make your life easier. Here's some of my favourites:

  • Pick a game engine (or do it by hand)
  • NPM is a treasure trove of amazing things
  • freenode/#bbg – browser based games
  • Learn about Juice in a talk by Martin Jonasson and Petri Purho

Simcity 4 Multiplayer

I've been playing a lot of Simcity lately.

I've had the idea for a while that I've wanted to do a multiplayer game with friends via Dropbox, so I roped a few people like Tate into it and we found it works surprisingly well.

After a few rounds of back-and-forth I took the liberty of rendering out a mega-screenshot and setting it up as a zoomify image. (The stitching was all done by hand, so there's a few rough edges around city borders.)

Simcity 4 is by far my favourite version of Simcity. It has a sweet 2.5D aesthetic (3D models are rendered down to 2d on a fixed trimetric orthographic projection), but more importantly it has a large community of modders and lets you build massive regions spanning multiple cities.

Newer versions in the Simcity franchise sacrifice detail for simplicity and tend to be more "town building" than "city building" sims. Of the newer games I prefer Cities XL (which I feel redeemed itself after a botched launch and a few re-releases), but it's difficult to go past the homely charm of Simcity 4.