Skip to content

Instantly share code, notes, and snippets.

@JossWhittle
Created August 6, 2012 14:41
Show Gist options
  • Save JossWhittle/3274919 to your computer and use it in GitHub Desktop.
Save JossWhittle/3274919 to your computer and use it in GitHub Desktop.
Colour Bar using HTML Canvas and Javascript
canvas { position: fixed; top: 0; left: 0; }
<canvas id="colbar"></canvas>
// Calls the function
colourification('colbar',7,8);
// Worker function, call initially and setsup resize event
function colourification(id,h,n) {
colourify(id,h,n);
window.addEventListener('resize', function () {
colourify(id,h,n);
}, false);
}
// Work the magic *cracks knuckles*
function colourify(id,h,n) {
var colours = ['#641717','#173F64','#2B6417','#644F17','#175C64'];
var w = window.innerWidth,
c = document.getElementById(id),
ctx = c.getContext('2d');
c.width = w;
c.height = h;
var x = 0;
for (var i = 0; i < n; i++) {
x = i * (w / n);
ctx.beginPath();
ctx.fillStyle=colours[i % colours.length];
ctx.fillRect(x,0,(w / n)+1,h);
ctx.closePath();
ctx.fill();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment