News for hackers. Stuff that matters.
Re:Tools we used to create a hit HTML5 game on Steam by dom96 on Tuesday November 19, 2019 @12:31PM (#21575624)
What I'm curious about, as somebody who's writing their own game right now which targets HTML5 (among other things), is how you were able to achieve such great performance. I've given your game demo a quick look and it plays incredibly smoothly, do you have any tips on what to avoid for best rendering performance?
Re:Tools we used to create a hit HTML5 game on Steam by danShumway on Tuesday November 19, 2019 @02:44PM (#21577389)
Not OP, but a few tips from my experience:

- Use `requestAnimationFrame`, don't use timers. JS timers are really bad, and they won't sync with monitor refresh rates.

- If possible, avoid lots of canvas calls. For example, if you want to draw something simple like a bunch of boxes on the screen, grab the pixel array from the canvas and manipulate it directly, don't call `ctx.rect` in a for loop.

I once stuck in a quick dozen-line block of code before a demo that just drew a couple hundred tiny pixel stars in the background and occasionally randomized their position. Went back and profiled later and found out it was taking something like 30% of my render time each frame, and refactoring just that one block of code got rid of a bit of stuttering on old hardware.

- On that note, cache stuff in memory. My game is tile-based. At the start of a level, I iterate over the tiles on an offscreen canvas and generate a single, giant image for the entire map. Then for subsequent frames I can render that with a single sprite call, rather than by iterating over every tile.

Caching visual effects is huge. In general, don't be afraid to stick generated effects in RAM, because every call you make to Canvas has an overhead.

- Also on that note, don't be afraid to use multiple canvases. If you have an overlay or a visual effect on top of your main game, and you can just update that without needing to redraw all of your sprites/tiles, that can sometimes be a big performance benefit.

Re:Tools we used to create a hit HTML5 game on Steam by dom96 on Wednesday November 20, 2019 @06:40PM (#21589849)
Thank you for your advice!

> If possible, avoid lots of canvas calls. For example, if you want to draw something simple like a bunch of boxes on the screen, grab the pixel array from the canvas and manipulate it directly, don't call `ctx.rect` in a for loop.

I think this is likely what's causing me the most trouble. The problem is that my game needs to draw many hundreds of circles, each of different colors/sizes. Perhaps I need to pre-render these to an off-screen canvas and then do some blitting.

I'm also tempted now to try what someone else suggested and target WebGL instead (likely via Pixi.js).

Re:Tools we used to create a hit HTML5 game on Steam by plopz on Tuesday November 19, 2019 @03:06PM (#21577661)
Is requestAnimationFrame still capped at 60fps? My monitor runs at 165hz and the last time I tried requestAnimationFrame it would only do 30 or 60.
Re:Tools we used to create a hit HTML5 game on Steam by enoj on Tuesday November 19, 2019 @03:51PM (#21578141)
No, for a project I am working on using three.js with requestAnimationFrame, it is capped at the refresh rate the monitor is running on.

So my gaming monitor running 144hz at 1080p runs the application at 144 FPS according to my FPS counter from stats.js[0]

On my laptop running at 60hz it's capped at 60 FPS. There is a huge difference in the "smooth-ness" as well as reduced input lag on the 144hz vs 60hz monitors. So much that I currently dislike working on the app on <100hz monitors.

[0] - https://github.com/mrdoob/stats.js/

Re:Tools we used to create a hit HTML5 game on Steam by plopz on Tuesday November 19, 2019 @07:38PM (#21580009)
That's good to know, might have to try it again. Especially now that shared buffers and atomics are available in web workers.
Re:Tools we used to create a hit HTML5 game on Steam by danShumway on Tuesday November 19, 2019 @03:12PM (#21577743)
I don't know the answer to that. I imagine different browsers/OSes will have different caps.

However, assuming the cap is an issue, I still strongly doubt that with `setInterval` you'll be able to get the precision you need to target specific monitor refresh rates, even if you can figure out a way to detect what the refresh rate is. Maybe if you're targeting Electron, but even there I kind of doubt it. Even with frame caps, `requestAnimationFrame` is still likely the best bet (opinion me).

The reality of games on the web is that timing in general, whether it be for update logic, rendering, or audio is kind of a massive pain, and you'll always be accepting some compromises. To me, those compromises aren't big enough to outweigh some of the other substantial benefits, but I don't want to pretend those compromises don't exist.

Re:Tools we used to create a hit HTML5 game on Steam by modeless on Tuesday November 19, 2019 @11:17PM (#21581047)
Are you on Linux with an Nvidia GPU? I fixed that bug recently. https://crbug.com/535392 On other platforms requestAnimationFrame has been synced to the monitor for a long time, in Chrome at least.
Re:Tools we used to create a hit HTML5 game on Steam by hoorayimhelping on Tuesday November 19, 2019 @03:50PM (#21578126)
This little demo runs at 144hz on my gaming monitor: http://hoorayimhelping.github.io/Arty/
Re:Tools we used to create a hit HTML5 game on Steam by chrisweekly on Tuesday November 19, 2019 @02:52PM (#21577476)
(Not OP w the Q you answered)

Thanks! Great tips. Do you write about this stuff elsewhere?

Re:Tools we used to create a hit HTML5 game on Steam by danShumway on Tuesday November 19, 2019 @03:06PM (#21577658)
I don't have any articles specifically about canvas performance, but I'm pretty open about development so I probably will at some point in the future. If I do they'll be posted at https://loop-thesis.com/blog/ (https://loop-thesis.com/rss.xml)
Re:Tools we used to create a hit HTML5 game on Steam by chrisweekly on Tuesday November 19, 2019 @07:34PM (#21579985)
Cool, thanks for your response. Checked out your "reset hard" prototype demo; it looks great! Reminds me a bit of Bret Victor's talk on inventing from first principles, and a game called "Braid" that involves actively playing with time as a 1st-class property of gameplay. You should post a "show hn" on it! :)
Re:Tools we used to create a hit HTML5 game on Steam by riadd on Tuesday November 19, 2019 @02:02PM (#21576848)
Thanks! We made extensive use of Canvas caching and only redrawing if things changed.

https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/...

Re:Tools we used to create a hit HTML5 game on Steam by komali2 on Tuesday November 19, 2019 @02:39PM (#21577316)
Whenever I try to use Canvas caching I run into an issue I don't yet fully understand that causes my images to come out of cache blocky and terrible looking. Something about a max canvas size? I don't quite get it. Happens whenever I use fabric.js caching.
Re:Tools we used to create a hit HTML5 game on Steam by lazerwalker on Tuesday November 19, 2019 @02:30PM (#21577179)
I'm guessing that means you rolled your own canvas API code, rather than using a library like PixiJS. Is that something you'd consider open-sourcing, or is it so tightly integrated with game-specific logic that it wouldn't make sense?
Re:Tools we used to create a hit HTML5 game on Steam by riadd on Tuesday November 19, 2019 @02:36PM (#21577262)
Yes, no helper libraries and I coded quite close to the metal.It's probably even a stretch to call it an engine. It's just built around the specific game mechanics. It wouldn't make sense to open source that part unfortunately.
Re:Tools we used to create a hit HTML5 game on Steam by dyarosla on Tuesday November 19, 2019 @09:47PM (#21580690)
I don’t know how this wasn’t mentioned yet but 100% use a Webgl framework, even for 2D games. Three.js is my personal favorite. Nowadays most all browsers and devices support it and I’m sure I don’t have to touch on the magnitudes of difference in speed for rendering anything on the GPU vs the CPU?

If you plan on having any amount of medium to large numbers of things (heck I’d say any amount) you’d be doing it wrong to be using the 2D canvas API.

Re:Tools we used to create a hit HTML5 game on Steam by ttty on Thursday November 21, 2019 @07:12AM (#21593325)
You can use pixi.js for rendering. For example my game https://bitplanets.com can run at 100fps on a mobile device (on phones that support that refresh rate, like one plus 7t)