Created
August 6, 2012 14:41
-
-
Save JossWhittle/3274919 to your computer and use it in GitHub Desktop.
Colour Bar using HTML Canvas and Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
canvas { position: fixed; top: 0; left: 0; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<canvas id="colbar"></canvas> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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