#How can I create a CSS grid that aligns with 1x1 pixel canvas rendering?
42 messages · Page 1 of 1 (latest)
Why is it ineffcient?
the grid or my canvas
sorry for not specifying, but the style is for a div:
<div id="grid"></div>
(i just want to ping you since @ doesnt work)
The grid is inefficient, because each item you put in has to be an element. Elements require far more memory than pixels, and they become part of the DOM, which has to be iterated for rendering and updates.
Also, it's completely redundant. You can set individual pixels in the canvas.
They arent elements, i just have canvas and a div with the style that i send previously
Or if you want event listeners, you can set one on the whole canvas and check coordinates if it's for mouse activity.
Are you referring to CSS grid, or just a grid pattern?
I thought it was CSS grid. That holds elements.
this.
no, its a empty div
with the #grid style, thats what i was trying to tell you
Empty divs are elements. If you intend to have thousands of empty divs, it is far less efficient than thousands of pixels. It could be so bad that the browser may become unresponsive.
what
You mean it's not a CSS grid and you just want the visual appearance of a grid?
its just one, here let me send a snippet:
<div id="canvasDiv">
<canvas style="flex: 1" class="canvas" id="canvas"></canvas>
<div id="grid"></div>
</div>
YES, exactly
OK. That can be efficient, but what size are the boxes? 1px each?
they should be since im drawing them with this code:
this.context.fillRect(x, y, 1, 1);
That is JS canvas and not CSS at all.
however, it doesnt fit when i set grid-size to 1px:
I know, the grid and the the canvas are sepearte things, i just want a grid overlay on top of the canvas
I mentioned earlier that it won't work with borders, margins, or padding.
Are you trying to tell me that I can't create an overlay grid?
what does that mean?
?
Borders take space and cannot be narrower than 1px. It is possible to have a visible grid if the size is substantially larger than 1px per cell, but at 1px, the border is the size of a cell and will cover half of all the cells.
I mentioned margins and padding because they also take space.
setting the grid strenght/thickness to 0.1px makes it thinner tho
This is not safe. The lines may become invisible depending on the monitor and zoom level.
oh okay
But if you want to do that, you still need to do math to account fo the thickness of the borders. Otherwise, everything will be out of alignment.
yes, that is my issue, can you help me with that
I tried to copy what the team in this repo did, however, I failed, maybe it helps:
I still don't know why you need a CSS overlay. If you already drew something with the canvas, you can draw the lines too.
no, that will cause bugs/glitches and i just dont think thats the right way to do it, an overlay is perfectly suited for the job
So, can you please help me?
There must be something fundamentally wrong in what you tried if this causes bugs and glitches. The grid must be drawn last. If it is last in the draw order, it cannot interfere with anything else. It will simply go on top. Also, you must redraw when there is an update.
No, im just familiar with canvas and thin lines, plus I will need to redraw which is slower than an overlay that just stays consistent
In most web based games, the entire canvas is completely erased and redrawn 60 times per second. Being slower is not a problem.