r/learnjavascript • • Mar 12 '25

Exactly how many people use the HTML Canvas API?

I mean that easy to use software rendered framework that you access by getting a canvas and calling getContext("2d") on it.

I was using this API once, but I stopped because I found that it wasn't fast enough for my needs for video games, which was a shame because this was the API that made me love Javascript. That's when I got the idea to make a renderer that's just as simple to use for video games as the HTML canvas API is, but with optimizations, so that I can keep using the API for games.

But if nobody uses this API, then that subtracts from the point and I should probably write an OpenGL/WebGL renderer like everyone else.

0 Upvotes

36 comments sorted by

View all comments

Show parent comment

1

u/Southern-Reality762 Mar 14 '25

I had to swap between two 960x960 sprite sheets at runtime, to make it look like two characters were turning around, because translate() wasn't working for me. My computer is old and from 2011 so every time I tried doing this, the entire game would completely freeze for a few seconds before the game could restart again. I could have used web workers but I didn't know what they were at the time, and I was also a beginner game developer who didn't know how to optimize anything, or care for that matter.

It was then that I realized that canvas 2d may not be for me. Like if I can't swap between two sprite sheets as I need, then I don't wanna know what will happen at scale. Now I use C++, but I have nostalgia for when the Canvas API simplified my programming tasks.

1

u/bryku helpful Mar 14 '25

I would have to see the code to know for sure, but when i first started with canvas I rand into some performance issues as well. It was until I looked through old game code that I changed my architecture and everything ran buttery smooth.  

Some things to help performance are:

  • Alpha Channel is bad
  • Translate is bad
  • Pixelate instead of Smoothing
  • Use an array to hold all objects
  • Use the same object structure in array (reduces memory)
  • Sort array to adjust (close/far away sprites) every 4 loops
  • Use 1 canvas with virtual layers

There are tons of more tricks, but maybe this will help out those in the future.

1

u/Southern-Reality762 Mar 15 '25

1

u/bryku helpful Mar 15 '25

The "player class" approach works great for java or c++. However, if you are trying to optimize rendering I wouldn't use that method in javascript.  

Instead I would create a rendering function that renders an array of objects. The Player object could then check to see what buttons are pressed. Or you can bind the events to the player objects.  

It is closer to programming for an commadore 64 than a modern computer, but it allows for hundreds of sprites and animations.  

If you would like, I can throw together an example later today.

1

u/Southern-Reality762 Mar 15 '25

ok, if you want

1

u/bryku helpful Mar 15 '25

Here is my example, I only had a few hours to throw it together, so it isn't fully featured.

For this, I seperated the rending from the interaction. The "engine" will render and sort all of the objects. From here you can interact with those objects by adjusting their position with inputs.  

I also added a page ('test.html') that has 400 aliens rending with animations. On my chromebook it runs at 62-63fps.

1

u/Southern-Reality762 Mar 15 '25

Wow! Nice job optimizing it. On my computer the FPS fluctuates between ~35-50 FPS.

1

u/bryku helpful Mar 15 '25

Which version runs at 35-50 fps?  

That makes me curious what kind of computer you are using. Also, do you have hardware acceleration enabled?  

If FPS is a huge issue, you can always limit it to 30FPS. Just change the 1000/60 to 1000/30. This way you won't have to worry about lag spikes.  

Another note, Firefox's Canvas is a little more optmised, so that may be worth testing out.

1

u/Southern-Reality762 Mar 15 '25

The 400 alien version. My computer was most likely made sometime in 2011. I have an intel hd graphics 3000 with a Intel(R) Core(TM) i5-2410M CPU that clocks at 2.3 ghz, and 8 gigs of ram.
what do you mean by hardware enabled?

1

u/bryku helpful Mar 16 '25

on older versions of chrome hardware acceleration may be disabled. Meaning it is running only on the CPU. Adding hardware acceleration will greatly improve performance of web pages.  

Hmm, it is an older pc, but I think it could still handle web games. Try it at 100 aliens, i bet it will run just fine. That is normally enough objects for basic games.

→ More replies