Random squares
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jscolor/2.0.4/jscolor.js"></script>
<p>
Beads in row:
<input type="number" id="inputCols" min="2" max="60" value="12">
</p>
<p>
Gradient rows count:
<input type="number" id="inputRows" min="2" max="60" value="12">
</p>
<p>
First bead color:
<input class="jscolor" value="FF44CC" id="firstColor">
</p>
<p>
Second bead color:
<input class="jscolor" value="00FFAA" id="secondColor">
</p>
<p>
Render type:
</p>
<span>
<input id="Radio1" name="algorithm" type="radio" value="random" checked="checked">
<label for="Radio1">Random</label>
</span>
<span>
<input id="Radio2" name="algorithm" type="radio" value="regular" >
<label for="Radio2">Regular</label>
</span>
<span>
<input id="Radio3" name="algorithm" type="radio" value="dither" >
<label for="Radio3">Dither</label>
</span>
<p>
<button onclick="run()">Render!</button>
<br>
<br>
</p>
CSS
.cell {
float: left;
font-size: 10px;
}
.cell::before {
counter-increment: section;
/* Increment the value of section counter by 1 */
content: counter(section);
/* Display the value of section counter */
}
.container {
border: 0px solid #000;
counter-reset: section;
}
button {
margin-left: 0;
}
input,
button {
font-size: 1em;
width: 4.5em;
padding: 3px;
margin: 2px 0px;
border: 1px solid #ddd;
border-radius: 5px;
text-align: center;
}
input[type=number] {
width: 2.5em;
}
input:focus {
outline: none;
}
span {
/* style this span element so we can display nicely, this stlying is not neccessary */
margin: 10px 0;
display: block;
width: 100%;
float: left;
}
input[type="radio"],
input[type="checkbox"] {
/* hide the inputs */
opacity: 0;
}
/* style your lables/button */
input[type="radio"] + label,
input[type="checkbox"] + label {
/* keep pointer so that you get the little hand showing when you are on a button */
cursor: pointer;
/* the following are the styles */
padding: 4px 10px;
border: 1px solid #ccc;
background: #efefef;
color: #aaa;
border-radius: 3px;
text-shadow: 1px 1px 0 rgba(0, 0, 0, 0);
}
input[type="radio"]:checked + label,
input[type="checkbox"]:checked + label {
/* style for the checked/selected state */
background: #777;
border: 1px solid #444;
text-shadow: 1px 1px 0 rgba(0, 0, 0, 0.4);
color: white;
}
JavaScript
function run() {
var o = document.getElementById("randomBlock");
if (o) {
o.parentNode.removeChild(o);
}
var rows = parseInt(document.getElementById('inputRows').value);
var cols = parseInt(document.getElementById('inputCols').value);
var algorithm = document.querySelector('input[name="algorithm"]:checked').value;
var size = 20;
var container = document.createElement("div");
container.id = "randomBlock";
container.className = "container";
container.style.width = (cols * size) + "px";
container.style.height = (rows * size) + "px";
if (algorithm == 'dither') {
var colors = dither(rows, cols);
for (var row = 0; row < rows; row++) {
for (var col = 0; col < cols; col++) {
o = document.createElement("div");
o.className = "cell";
o.style.height = size + "px";
o.style.width = size + "px";
o.style.backgroundColor = colors[row][col];
container.appendChild(o);
}
}
}
if (algorithm == 'regular') {
for (var row = 0; row < rows; row++) {
var bucket = createBucket(row, rows, cols);
for (var col = 0; col < cols; col++) {
o = document.createElement("div");
o.className = "cell";
o.style.height = size + "px";
o.style.width = size + "px";
o.style.backgroundColor = chooseColor(bucket, col);
container.appendChild(o);
}
}
}
if (algorithm == 'random') {
for (var row = 0; row < rows; row++) {
var bucket = createRandomBucket(row, rows, cols);
for (var col = 0; col < cols; col++) {
o = document.createElement("div");
o.className = "cell";
o.style.height = size + "px";
o.style.width = size + "px";
o.style.backgroundColor = chooseColor(bucket, col);
container.appendChild(o);
}
}
}
document.body.appendChild(container);
}
function createBucket(row, rows, cols) {
var colorPercent = transform(row, rows);
colPercent = 100 / cols;
...