// Make a container and have the canvas and the overlay inside
// It might help the pixel glitch if the width and height are 100%
var canvas = document.getElementsByTagName("canvas")[0];
var $canvas = $(canvas);
var ctx = canvas.getContext('2d');
var w = 30;
var h = w;
var state = [];
for (var i = 0; i < (20 * 20); i++) {
state.push(0);
}
// Create hover overlay grid
var $overlay = $(".overlay");
for(var i = 0; i < 400; i++){
$overlay.append("<div></div>");
}
function mouseIndex(e) {
var rect = canvas.getBoundingClientRect();
var pos = {
x: e.clientX - rect.left,
y: e.clientY - rect.top
};
pos.x = Math.ceil(pos.x / 30) - 1;
pos.y = Math.ceil(pos.y / 30) - 1;
var index = (pos.y * 20) + pos.x;
return index;
}
function toggleState(index) {
var current = state[index];
switch (current) {
case 0:
state[index] = 1;
break;
case 1:
state[index] = 2;
break;
default:
state[index] = 0;
}
//console.log(state);
redraw();
}
function redraw() {
for (var i = 0; i < state.length; i++) {
var pos = getPosition(i);
var currentState = state[i];
switch (currentState) {
case 1:
ctx.fillStyle = "black";
ctx.rect(pos.x, pos.y, w, h);
ctx.fill();
break;
case 2:
ctx.fillStyle = "crimson";
ctx.rect(pos.x, pos.y, w, h);
ctx.fill();
break;
default:
ctx.clearRect(pos.x, pos.y, w, h);
ctx.fill();
break;
}
ctx.closePath();
ctx.beginPath();
}
}
function getPosition(index) {
var x = index % 20 * 30;
var y = (Math.ceil((index + 1) / 20) - 1) * 30;
return {
x: x,
y: y
}
}
$('.artboard').click(function(e) {
var index = mouseIndex(e);
console.log(index);
toggleState(index);
});
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.