function checkerboard(){
document.getElementById('result').style.display = "block";
//set width and height variables
var width = parseInt(document.getElementById('width').value),
height = parseInt(document.getElementById('height').value);
//set canvas variables
var canvas = document.getElementById('checkerboard'),
ctx = canvas.getContext('2d');
//set canvas height and width
canvas.width = width;
canvas.height = height;
//set number of columns and rows based on height and width divided by 20
var rows = Math.ceil(height/20),
cols = Math.ceil(width/20);
//check to make sure the user entered at least a 2 in both of the input fields
if(width < 40 || height < 40){
document.getElementById('status').innerHTML = "Please select a width and height greater than '39' to generate a checkerboard.";
return;
}
//loop to render checkerboard
var i = 0,
ii = 0;
while(ii < rows){
//if even number row, start with red square, otherwise start with black
if (ii % 2 == 0)
while(i < cols){
//create red square
ctx.fillStyle = "rgb(255,0,0)";
ctx.fillRect(i*20, ii*20, 20, 20);
i++;
if(i < cols){
//create black square
ctx.fillStyle = "rgb(0, 0, 0)";
ctx.fillRect(i*20, ii*20, 20, 20);
i++;
}
}
else
while(i < cols){
//create black square
ctx.fillStyle = "rgb(0, 0, 0)";
ctx.fillRect(i*20, ii*20, 20, 20);
i++;
if(i < cols){
//create red square
ctx.fillStyle = "rgb(255,0,0)";
ctx.fillRect(i*20, ii*20, 20, 20);
i++;
}
}
ii++;
i...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.