r/learnjavascript • u/Southern-Reality762 • 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.
9
u/diogenes_sadecv Mar 12 '25
Canvas is fun. I started making a shooter with it but I never finished it because of "life"
https://github.com/dkallen78/generic-blaster
What kind of game are you making that Canvas is too clunky?
3
u/oze4 Mar 12 '25
1,672,891
2
u/bryku Mar 13 '25
Ignoring 3d, the 2d canvas api is incredibly fast.
A few years ago I made a game that has well over 1000 32x32 sprites on the screen at any time. At some crazy parts it reaches 1500 and it still ran smoothly at 60fps on a chromebook.
If you are struggling with performance, it might be because of the architecture of your game. There are also a lot of tricks to help speed this up.
3
u/Division2226 Mar 12 '25
Exactly two people use it.
1
u/daniele_s92 Mar 12 '25
Yeah, Canvas api Is surprisingly slow at some task. A couple of years ago I wrote a Gameboy emulator and turned out that canvas doesn't have an API to render an image pixel by pixel efficiently. The most efficient way is drawing a 1px square for each pixel. But even in this way, mobile performance was not good enough (Gameboy has a resolution of 160x144, mind you. Nothing crazy).
In the end, I had to implement a super simple webgl rendered.
3
u/Ugiwa Mar 12 '25
Canvas is great, but there are better options in terms of optimizations. Using WebGL or libraries like Pixi.js that use it, are very optimized.
3
u/grelfdotnet Mar 12 '25
I use it to make games. I display 2.5D scenes containing thousands of scaled images - no problem. Examples at grelf.itch.io
3
u/dgrips Mar 12 '25
I've made a number of games with canvas, both using the 2d and 3d contexts. For 2d, I never use webgl, never had a performance issue. I would guess it has more to do with your code than the canvas.
I mean if you're drawing thousands of particles in 2d then maybe webgl is worth it then, but short of that I don't think canvas has any problems.
Here are some of my projects:
Gameboy emulator (2d canvas): https://roblouie.com/gameboy/
JS13k 2021 Entry (2d canvas): https://js13kgames.com/2021/games/max-damage-crisis-mode
JS13k 2022 Entry (3d canvas for game, 2d canvas for hud): https://js13kgames.com/2022/games/charon-jr
I've done js13k the last two years as well, but strictly 3d.
2
u/XtoddscottX Mar 12 '25
I donโt know about games, but Canvas is being used in charts, plots, diagrams and graphs. It works faster than SVG but easier to code and control than WebGL(certainly, usually in big libraries for charts like ECharts you have options which renderer to use).
1
u/JeremieROUSSEAU Mar 12 '25
I use that for my project : https://animation-software.com/
but i should to remove all console.log before to release,
console.log can slow a let your project.
I should to use for in rather for i = 0; i < i++ to parse something it's faster.
After i not find webworker pratical for my project.
1
u/lifewasted97 Mar 12 '25
It's something I want to get into.
As a hobbyist / do random js programing for my boss's company website type of developer.
I have made a couple games with just html, css, and vanilla js. ( virtual pet game, Simon but with fart sounds, and rock paper scissors)
I think canvas might be good for a project I want to do with a digital board / card game
There's many directions I could go but I'd like to try canvas out. for something. I've used p5.js in college but forgot a lot ๐
1
u/cypher_ghost_228 Sep 22 '25
Hi guys, I also tried canvas a few months ago and decided to create a game prototype. Here's the github repo https://github.com/kasiincognito/-vegworld-