Optimizing HTML5 Games: 10 Years of Learnings

Rate this content
Bookmark

The open source PlayCanvas game engine is built specifically for the browser, incorporating 10 years of learnings about optimization. In this talk, you will discover the secret sauce that enables PlayCanvas to generate games with lightning fast load times and rock solid frame rates.

This talk has been presented at JS GameDev Summit 2022, check out the latest edition of this Tech Conference.

FAQ

PlayCanvas powers Snap Games on Snapchat, which has had over 200 million players. It is also used by game developers worldwide to create various types of games, including casual games, .io games, and FPS games.

PlayCanvas is an open-source game engine written in JavaScript and based on WebGL. It includes a browser-based, real-time collaborative visual editor built in the cloud.

Performance optimization is crucial for HTML5 games to ensure fast load times and smooth frame rates. Long load times can cause users to leave, and poor frame rates can affect the gameplay experience.

Chrome DevTools provides advanced tools for optimizing load times, including the Networking and Performance tabs. These tools help identify large resources and redundant data that can be optimized.

Texture data can be optimized using hardware texture compression formats like DXT, PVR, and ETC. Additionally, the Basis texture format can be used to compress textures further and reduce GPU memory usage.

GLTF is designed to be the JPEG of 3D, providing a standard format for 3D models. It reduces file size, improves parse times, and lowers peak memory usage compared to JSON, making it ideal for HTML5 games.

Techniques to improve frame rates include using hierarchical profiling, adjusting rendering resolution based on device GPU, limiting graphical complexity, and reducing the number of draw calls through texture atlasing and batching.

You can use user agent sniffing and properties like window width and device pixel ratio to infer device capabilities. For Android, Chrome reports the GPU family, allowing for more precise optimizations based on hardware.

A common mistake is rendering games at full device resolution without considering the device's GPU capabilities. Using overly complex shaders and not optimizing draw calls can also negatively impact performance.

Testing on different devices ensures that the game performs well across a range of hardware, from high-end to budget devices. This helps in identifying and fixing performance issues early in the development cycle.

Will Eastcott
Will Eastcott
33 min
08 Apr, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

PlayCanvas is an open-source game engine used by game developers worldwide. Optimization is crucial for HTML5 games, focusing on load times and frame rate. Texture and mesh optimization can significantly reduce download sizes. GLTF and GLB formats offer smaller file sizes and faster parsing times. Compressing game resources and using efficient file formats can improve load times. Framerate optimization and resolution scaling are important for better performance. Managing draw calls and using batching techniques can optimize performance. Browser DevTools, such as Chrome and Firefox, are useful for debugging and profiling. Detecting device performance and optimizing based on specific devices can improve game performance. Apple is making progress with WebGPU implementation. HTML5 games can be shipped to the App Store using Cordova.

1. Introduction to PlayCanvas and Game Optimization

Short description:

Hi, my name's Will Eastcott. I'm the creator of PlayCanvas. Today, I'll be talking about optimizing HTML5 games based on 10 years of experience with the PlayCanvas game engine. PlayCanvas is an open-source game engine written in JavaScript and based on WebGL. It includes a browser-based visual editor for real-time collaborative game development. It powers Snap Games in Snapchat and is used by game developers worldwide for various types of games. My journey in game optimization began with Renderware, a game engine used in the PlayStation 2 generation. HTML5 game developers now have powerful hardware and built-in tools, but optimization is still crucial, focusing on load times and frame rate optimization.

♪ Hi, my name's Will Eastcott. I'm the creator of PlayCanvas. And I'm going to be talking to you today about optimizing HTML5 games based on 10 years of learnings working on the PlayCanvas game engine.

So to begin with, I want to just start by explaining a little bit about what PlayCanvas is. It's an open-source game engine. It's written in JavaScript. It's based on WebGL. And you also get this visual editor that's like browser-based. It's real-time collaborative. It's built in the cloud. So yeah, you can visually build your games in this editor.

So PlayCanvas actually powers Snap Games, which is the gaming platform in Snapchat. It's had over 200 million players. There's a large number of games that you can check out of just about any genre, so I would encourage you to check them out. But PlayCanvas isn't just used by Snapchat-based game developers. It's used by game developers the world over to make all sorts of different types of games, from casual games to .io games. It's actually pretty popular for FPS game developers, and you can see several of those represented there.

Now, my personal journey in working on game optimization started in the early 2000s working for a company called Criterion Software on a game engine called Renderware. Now, Renderware was used to power about a third of the games in the PlayStation 2 generation, and day-to-day I would be working on this type of hardware. So we're talking a T-10,000 PlayStation 2 developer kit. And yeah, if you wanted to do performance analysis on that kind of hardware, you would struggle. And you would often need to go into Sony's HQ and work on special hardware that was developed by then, very expensive hardware. It was incredibly inconvenient.

Fast forward to 2022, and HTML5 game devs are living the dream, right? I mean, we've got incredibly powerful hardware in the palms of our hand. And we've got great tools that are built right into our web browsers. So is optimization still important? Well, spoiler alert, yes it is. Now, performance optimization, in my view, falls into two main areas. There's load times, and there's also frame rate optimization. And let's start talking about load times. Now, this is something that we don't want our end users to see, loading bars.

2. Investigating Load Times and Texture Optimization

Short description:

So why does it matter whether we present our users with loading bars? After 6 seconds of waiting, we tend to lose 40% of our audience. To investigate load times, we can use advanced tools built into the browser, such as Chrome DevTools. By sorting resources based on size, we can identify opportunities for optimization. In HTML5 games, most of the data is texture-based, and large images can cause crashes and long load times. However, hardware texture compression can help by reducing GPU memory usage and eliminating decode costs.

So why does it matter whether we present our users with loading bars? Well, as it turns out, after 6 seconds of waiting, we tend to lose anything like 40% of our audience who just bounce, not prepared to wait for the page to load.

So when we begin an investigation into load time, what kind of tools do we have available to us that can help us investigate optimizing load times? Well, like I said, built right into the browser, you have some pretty advanced tools. In Chrome DevTools, you have a couple of tabs. You have the Networking tab, which shows you what resources My Game is loading, and then you have the Performance tab that shows how My Game is loading those resources. So when you start your investigation, you typically look for the low hanging fruits. If you look in the bottom left of the Browser tab, you'll see the number of requests made by the browser. You'll see the amount of data that's transferred, and you'll see the time it takes to load your game. Now, the first thing that you'll want to do is sort the list of resources based on size, because, obviously, the bigger the resource, the bigger the opportunities for optimization. You can search for duplicates or redundant resources that your game shouldn't really be loading in the first place. And if we look at the biggest file that's being loaded here into our game, it's a 2.2 megabyte jpeg. So we can ask ourselves, hey, can we downsize these resources? Can we optimize them somehow?

Now, as it turns out, in HTML5 games, typically most of the data tends to be texture-based data. And in our example in the previous slide, we had a 2.2 megabyte jpeg. Now, if the browser downloads this file, we need to decompress it into memory. And that is 48 megabytes of data. Then we have to pass it to WebGL to be used as a texture. And a copy of it is made, plus, we have to generate mitmaps, which is another 64 megabytes of data. So together, that's like 112 megabytes of data just for a single jpeg. Now, if you try to load about 10 of these into a browser tab on mobile, I guarantee you you'll crash the tab. So we need some kind of solution around this. Moreover, every jpeg reload, we have to pay, like, a decode cost. It takes time to actually decompress the data from jpeg to an uncompressed format. And in this case, this 2-megabyte texture takes 160 milliseconds, which is just excessive. Because it's going to cause the mainframe to block, and it's going to cause long load times. Luckily, we have hardware texture compression that we can use to load more optimized texture data. So if we take our original 2.2-megabyte jpeg and we crunch that down to some natively supported hardware texture formats like DXT, PVR, and ETC, we find that the sizes are actually larger than the original jpeg. So because the format of this hardware texture compression data is native to the GPU, we can just supply it to the GPU with no decode cost, which is great. Also, the amount of GPU memory used by hardware texture compressed data formats is between 1 5th and 1 10th of the original jpeg. So we're at least sure we're not going to be crashing the browser tab anymore. But we are paying the costs for having to download large images. And that's a problem.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

A Guide to React Rendering Behavior
React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
Top Content
This transcription provides a brief guide to React rendering behavior. It explains the process of rendering, comparing new and old elements, and the importance of pure rendering without side effects. It also covers topics such as batching and double rendering, optimizing rendering and using context and Redux in React. Overall, it offers valuable insights for developers looking to understand and optimize React rendering.
Speeding Up Your React App With Less JavaScript
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Top Content
Watch video: Speeding Up Your React App With Less JavaScript
Mishko, the creator of Angular and AngularJS, discusses the challenges of website performance and JavaScript hydration. He explains the differences between client-side and server-side rendering and introduces Quik as a solution for efficient component hydration. Mishko demonstrates examples of state management and intercommunication using Quik. He highlights the performance benefits of using Quik with React and emphasizes the importance of reducing JavaScript size for better performance. Finally, he mentions the use of QUIC in both MPA and SPA applications for improved startup performance.
React Concurrency, Explained
React Summit 2023React Summit 2023
23 min
React Concurrency, Explained
Top Content
Watch video: React Concurrency, Explained
React 18's concurrent rendering, specifically the useTransition hook, optimizes app performance by allowing non-urgent updates to be processed without freezing the UI. However, there are drawbacks such as longer processing time for non-urgent updates and increased CPU usage. The useTransition hook works similarly to throttling or bouncing, making it useful for addressing performance issues caused by multiple small components. Libraries like React Query may require the use of alternative APIs to handle urgent and non-urgent updates effectively.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Today's Talk discusses the future of performance tooling, focusing on user-centric, actionable, and contextual approaches. The introduction highlights Adi Osmani's expertise in performance tools and his passion for DevTools features. The Talk explores the integration of user flows into DevTools and Lighthouse, enabling performance measurement and optimization. It also showcases the import/export feature for user flows and the collaboration potential with Lighthouse. The Talk further delves into the use of flows with other tools like web page test and Cypress, offering cross-browser testing capabilities. The actionable aspect emphasizes the importance of metrics like Interaction to Next Paint and Total Blocking Time, as well as the improvements in Lighthouse and performance debugging tools. Lastly, the Talk emphasizes the iterative nature of performance improvement and the user-centric, actionable, and contextual future of performance tooling.
Building Fun Experiments with WebXR & Babylon.js
JS GameDev Summit 2022JS GameDev Summit 2022
33 min
Building Fun Experiments with WebXR & Babylon.js
Top Content
This Talk explores the use of Babylon.js and WebXR to create immersive VR and AR experiences on the web. It showcases various demos, including transforming a 2D game into a 3D and VR experience, VR music composition, AR demos, and exploring a virtual museum. The speaker emphasizes the potential of web development in the metaverse and mentions the use of WebXR in Microsoft products. The limitations of WebXR on Safari iOS are discussed, along with the simplicity and features of Babylon.js. Contact information is provided for further inquiries.
Power Fixing React Performance Woes
React Advanced Conference 2023React Advanced Conference 2023
22 min
Power Fixing React Performance Woes
Top Content
Watch video: Power Fixing React Performance Woes
This Talk discusses various strategies to improve React performance, including lazy loading iframes, analyzing and optimizing bundles, fixing barrel exports and tree shaking, removing dead code, and caching expensive computations. The speaker shares their experience in identifying and addressing performance issues in a real-world application. They also highlight the importance of regularly auditing webpack and bundle analyzers, using tools like Knip to find unused code, and contributing improvements to open source libraries.

Workshops on related topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
Make a Game With PlayCanvas in 2 Hours
JSNation 2023JSNation 2023
116 min
Make a Game With PlayCanvas in 2 Hours
Top Content
Featured WorkshopFree
Steven Yau
Steven Yau
In this workshop, we’ll build a game using the PlayCanvas WebGL engine from start to finish. From development to publishing, we’ll cover the most crucial features such as scripting, UI creation and much more.
Table of the content:- Introduction- Intro to PlayCanvas- What we will be building- Adding a character model and animation- Making the character move with scripts- 'Fake' running- Adding obstacles- Detecting collisions- Adding a score counter- Game over and restarting- Wrap up!- Questions
Workshop levelFamiliarity with game engines and game development aspects is recommended, but not required.
Building WebApps That Light Up the Internet with QwikCity
JSNation 2023JSNation 2023
170 min
Building WebApps That Light Up the Internet with QwikCity
Featured WorkshopFree
Miško Hevery
Miško Hevery
Building instant-on web applications at scale have been elusive. Real-world sites need tracking, analytics, and complex user interfaces and interactions. We always start with the best intentions but end up with a less-than-ideal site.
QwikCity is a new meta-framework that allows you to build large-scale applications with constant startup-up performance. We will look at how to build a QwikCity application and what makes it unique. The workshop will show you how to set up a QwikCitp project. How routing works with layout. The demo application will fetch data and present it to the user in an editable form. And finally, how one can use authentication. All of the basic parts for any large-scale applications.
Along the way, we will also look at what makes Qwik unique, and how resumability enables constant startup performance no matter the application complexity.
Next.js 13: Data Fetching Strategies
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
Top Content
WorkshopFree
Alice De Mauro
Alice De Mauro
- Introduction- Prerequisites for the workshop- Fetching strategies: fundamentals- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)- Test your build and serve it on Vercel- Future: Server components VS Client components- Workshop easter egg (unrelated to the topic, calling out accessibility)- Wrapping up
PlayCanvas End-to-End : the quick version
JS GameDev Summit 2022JS GameDev Summit 2022
121 min
PlayCanvas End-to-End : the quick version
Top Content
WorkshopFree
João Ruschel
João Ruschel
In this workshop, we’ll build a complete game using the PlayCanvas engine while learning the best practices for project management. From development to publishing, we’ll cover the most crucial features such as asset management, scripting, audio, debugging, and much more.
React Performance Debugging
React Advanced Conference 2023React Advanced Conference 2023
148 min
React Performance Debugging
Workshop
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)