#How can I create a CSS grid that aligns with 1x1 pixel canvas rendering?

42 messages · Page 1 of 1 (latest)

terse anchor
#

This is extremely inefficient and whatever your end goal is can almost certainly be achieved some other way. If you really want a grid with 1x1 pixel size cells, you can just specify that in the grid template. Note that this will only work properly if there are no borders, margins, padding, or gap.

restive venture
#

Why is it ineffcient?

#

the grid or my canvas

#

sorry for not specifying, but the style is for a div:

<div id="grid"></div>

restive venture
terse anchor
# restive venture the grid or my canvas

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.

restive venture
terse anchor
#

Or if you want event listeners, you can set one on the whole canvas and check coordinates if it's for mouse activity.

terse anchor
#

I thought it was CSS grid. That holds elements.

restive venture
#

this.

#

no, its a empty div

#

with the #grid style, thats what i was trying to tell you

terse anchor
# restive venture no, its a empty div

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.

restive venture
#

what

terse anchor
restive venture
#

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>

terse anchor
restive venture
#

they should be since im drawing them with this code:

this.context.fillRect(x, y, 1, 1);
terse anchor
restive venture
#

however, it doesnt fit when i set grid-size to 1px:

restive venture
terse anchor
restive venture
#

Are you trying to tell me that I can't create an overlay grid?

#

what does that mean?

terse anchor
#

I mentioned margins and padding because they also take space.

restive venture
#

setting the grid strenght/thickness to 0.1px makes it thinner tho

terse anchor
restive venture
#

oh okay

terse anchor
#

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.

restive venture
#

yes, that is my issue, can you help me with that

restive venture
terse anchor
#

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.

restive venture
restive venture
terse anchor
restive venture
terse anchor
#

In most web based games, the entire canvas is completely erased and redrawn 60 times per second. Being slower is not a problem.