canvas.width = canvas.width;
That's worse than a code smell, that reeks. Adding a new function to clear the state is nice, but what on earth is going on? canvas.width = canvas.width;
That's worse than a code smell, that reeks. Adding a new function to clear the state is nice, but what on earth is going on?[1] https://stackoverflow.com/questions/17522134/how-does-canvas...
[2] https://stackoverflow.com/questions/2142535/how-to-clear-the...
Yikes on a bike. Shit like this is how we end up with `typeof null === 'object'` forever baked into the spec.
> typeof document.all
< "undefined"
> document.all.constructor
< HTMLAllCollectionMy hot take: Software that doesn't take this approach isn't truly serious in the same way as software that does. Personally I'm glad for this level of discipline because at some point, there has to be a "bottom"... software that we have to rely on in order to make our own code work. Not everything gets to enforce deprecation/removal cycles, sometimes it just has to Always, Always Work.
... of course, that would require extending the API with an actual blanking function...
> Whenever the width and height content attributes are set, removed, changed, or redundantly set to the value they already have, then the user agent must perform the action from the row of the following table that corresponds to the canvas element's context mode.
> | 2d | Follow the steps to set bitmap dimensions to the numeric values of the width and height content attributes. |
> When the user agent is to set bitmap dimensions to width and height, it must run these steps: > 1. Reset the rendering context to its default state. > 2. Resize the output bitmap to the new width and height.
There's opportunity to reuse the previous surface and just clear the contents, but I'm not sure if this is done. (But the reset of state and clear are both mandatory for `.width=.width`)
location.href = location.href;One more: If you set an image's src attribute to the same thing, it'll still reload the image.
const image = new Image("/img.png");
document.body.append(image);
image.src = image.src;It's consistent and so your code can know if it sets the size things will reset. It doesn't have to special case when you happen to set it to the same size.
Welcome to webdev.
canvas.width ^= 0;
It's shorter and easier to type, and just as "clear".I simply fillRect with the background color in a method defined "clear".
2D games will forever be playable. A single round of SNES Super Ghouls & Ghosts is enough to humble even the top esports players of the day ;)