JSFiddle - React, Tailwind, and code Playground
HTML
<div id="bg-view" style="width: 200px; height: 200px"></div>
<canvas id="canvas-view" width="200" height="200"></canvas>
<div id="console"></div>
CSS
#bg-view {
background: -webkit-canvas(myscreen);
border: 2px solid blue;
}
#canvas-view {
border: 2px solid green;
margin-top: 10px;
}
JavaScript
(function (id) {
var canvas = document.getElementById(id),
ctW = canvas.clientWidth,
ctH = canvas.clientHeight,
ctx = document.getCSSCanvasContext('2d', 'myscreen', ctW, ctH)
profile(id, function () {
drawCheckers(ctx, ctW, ctH)
})
}('bg-view'));
(function (id) {
var canvas = document.getElementById(id),
ctW = canvas.width,
ctH = canvas.height,
ctx = canvas.getContext('2d')
profile(id, function () {
drawCheckers(ctx, ctW, ctH)
})
}('canvas-view'));
function profile(id, fn) {
var TIMES = 10000
var start = Date.now()
for (var i = 0; i < TIMES; i++) {
fn && fn()
}
document.getElementById('console').innerHTML += '<br />' +
id + ': ' + (Date.now() - start);
}
function drawCheckers(ctx, w, h) {
var RED = 'red', WHITE = 'white'
var LINES = 8, ROWS = 8
var sqW = w / ROWS,
sqH = h / LINES
var toggle = false
ctx.clearRect(0, 0, w, h)
ctx.fillStyle = WHITE
ctx.fillRect(0, 0, w, h)
ctx.fillStyle = RED
for (var i = 0; i < ROWS; i++) {
for (var j = 0; j < LINES; j++) {
if (toggle) {
ctx.fillRect(i * sqW, j * sqH, sqW, sqH)
}
toggle = !toggle
}
toggle = !toggle
}
}