Grid with Single Input

Asked

Viewed 129 times

1

Hello, what is the best way to make a grid for Javascript and HTML? I need that I can draw or change the color of the area of each white space. I did it this way but he doesn’t answer me because when I try to change the color of a single square he ends up changing either the whole row or the whole column.

for(var i = 0;i < linha;i++) {
    for (var n = 0;n < coluna;n++) {
        grid_x[i] = i * 40;
        grid_y[n] = n * 40;
        res_x[i] = i * 40;
        res_y[n] = n * 40;
        c[i] = false * i
        dRs(grid_x[i],grid_y[n],grid_w,grid_h,1,"#747474",c[i])
    }
}
function dRs(x,y,w,h,l,s,c) {
    context.beginPath();
    context.lineWidth= l;
    context.strokeStyle= s;
    context.rect(x,y,w,h);      
    context.stroke();
}
  • What do you mean grid? table? What makes the function dRs()?

  • Sorry I forgot to explain in the topic. Drs() draws squares with Strokes, part c[i] is not part of the code. By grid I mean this: http://simcity1re.6te.net/ but without the drawing functions, this is a very old version of what I’m doing.

  • And what are the parameters of this function? It’s not very clear what you want to do. You want to change the color inside the for or later?

  • I want to change the color of each square separately per click. Example: if I click the middle square it will be black without turning any other square into black too.

  • I prefer pure JS.

  • Take a look here: http://jsfiddle.net/Kj8N7/

  • Sérgio, I wanted to thank you and ask one more thing I understood much of the code but I didn’t know what you did (apply,Concat and foreach) you can recommend me a site that shows me this type of function (I don’t know if it would be a function) Javascript standard?

Show 2 more comments

1 answer

1

Deny if the intention is to create a game I suggest you look for an engine to help you because there are several other issues that not only changes color or perform an event.

Jsgamesoup is very simple and full of examples. can be an initial starting point.

Here too is a simple example of how to solve your question :

grid mudacolor

Remembering that one works with events, onclick, and for each change it is necessary to carry out the "painting" again of the grids.

engine jsgamesoup

Browser other questions tagged

You are not signed in. Login or sign up in order to post.