K
K
KlassT2017-06-05 00:30:04
JavaScript
KlassT, 2017-06-05 00:30:04

How to create a grid in canvas?

How to create a grid in canvas for chess?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Victor L, 2017-06-05
@KlassT

<canvas id="myCanvas" width="560" height="560" style="border:2px solid #d3d3d3;"></canvas>

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
 
for(i=0;i<8;i++){
  for(j=0;j<8;j++){
  ctx.moveTo(0,70*j);
  ctx.lineTo(560,70*j);
   ctx.stroke();
 
ctx.moveTo(70*i,0);
ctx.lineTo(70*i,560);
ctx.stroke();
var left = 0;
for(var a=0;a<8;a++) {
    for(var b=0; b<8;b+=2) {
      startX = b * 70;
      if(a%2==0) startX = (b+1) * 70;
      ctx.fillRect(startX + left,(a*70) ,70,70);
  }}
}}

chessboard.js -

O
Oleg, 2017-06-05
@politon

Explore examples of ready-made solutions https://codepen.io/search/pens?q=chess&limit=all&o...

D
Denis Bukreev, 2017-06-05
@denisbookreev

draw squares alternating white with black
8x8
what's so difficult?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question