Last active
October 6, 2016 19:29
-
-
Save herrernst/1a66146d154ac4461fc39797e941b1b0 to your computer and use it in GitHub Desktop.
draw grayscale ramp with html/css
This file contains 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
<!doctype html> | |
<title>draw grayscale ramp</title> | |
<meta name="viewport" content="width=device-width"> | |
<style> | |
.ramp { | |
height: 300px; | |
position: relative; | |
} | |
.ramp > div { | |
position: absolute; | |
height: 100%; | |
} | |
</style> | |
<body> | |
<label for="widthRGB">rgb: width of one jump</label><input id="widthRGB" value="1" /> | |
<button id="drawRGB">draw</button> | |
<label for="widthAlpha">alpha: number of jumps</label><input id="widthAlpha" value="1024" /> | |
<button id="drawAlpha">draw</button> | |
<div class="ramp"></div> | |
<script> | |
function drawRampRGB(w, el) { | |
for(var i=0; i<1024; i++) { | |
var r = document.createElement("div"); | |
r.style.width = w + "px"; | |
r.style.left = i*w + "px"; | |
r.style.backgroundColor = "rgb(" + i + ", " + i + ", " + i + ")"; | |
el.appendChild(r); | |
} | |
} | |
function drawRampAlpha(c, el) { | |
for(var i=0; i<c; i++) { | |
var r = document.createElement("div"); | |
r.style.width = "1px"; | |
r.style.left = i + "px"; | |
r.style.backgroundColor = "rgba(0, 0, 0, " + (i/c) + ")"; | |
el.appendChild(r); | |
} | |
} | |
var ramp = document.querySelector(".ramp"); | |
document.querySelector("#drawRGB").addEventListener("click", function () { | |
ramp.innerHTML = ""; | |
drawRampRGB(parseInt(document.querySelector("#widthRGB").value, 10), ramp); | |
}); | |
document.querySelector("#drawAlpha").addEventListener("click", function () { | |
ramp.innerHTML = ""; | |
drawRampAlpha(parseInt(document.querySelector("#widthAlpha").value, 10), ramp); | |
}); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment