I made a header generator that creates a linear gradient of two random colors from its top left to its bottom right.
Please tell me about everything I could do better, but especially how to calculate and not hardcode the randomly shifted second array secondColor
, that has the minColorValue
, maxColorValue
and the random value on other positions.
const settings = {
width: 500,
height: 180,
minColorValue: 70, // min rgb value
maxColorValue: 185, // max rgb value
};
let container = document.getElementsByClassName("container")[0];
let firstColor = [0, 0, 0];
let secondColor = [0, 0, 0];
let create = function(width, height) {
let canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
container.appendChild(canvas);
};
let colorPicker = function() {
// possible configurations of which of the rgb values is min, which is max, and which is inbetween
let configurations = [
[0, 1, 2],
[0, 2, 1],
[1, 0, 2],
[1, 2, 0],
[2, 0, 1],
[2, 1, 0],
];
let randomConfig = configurations[random(0, 5)];
firstColor[randomConfig[0]] = settings.minColorValue;
firstColor[randomConfig[1]] = settings.maxColorValue;
firstColor[randomConfig[2]] = random(settings.minColorValue, settings.maxColorValue);
// shift configuration by one or two to never get a different second color
let shiftAmount = random(1, 2);
let secondConfig = [0, 0, 0];
for (let i = 0; i < 3; i++) {
if (i + shiftAmount < 3) {
secondConfig[i] = randomConfig[i + shiftAmount];
} else {
secondConfig[i] = randomConfig[i - 3 + shiftAmount];
}
}
secondColor[secondConfig[0]] = settings.minColorValue;
secondColor[secondConfig[1]] = settings.maxColorValue;
secondColor[secondConfig[2]] = random(settings.minColorValue, settings.maxColorValue);
};
let fill = function() {
let canvas = document.getElementsByTagName("canvas")[0];
let context = canvas.getContext("2d");
let gradient = context.createLinearGradient(0, 0, settings.width, settings.height);
gradient.addColorStop(0, `rgb(${firstColor[0]},${firstColor[1]},${firstColor[2]})`);
gradient.addColorStop(1, `rgb(${secondColor[0]},${secondColor[1]},${secondColor[2]})`);
context.fillStyle = gradient;
context.fillRect(0, 0, settings.width, settings.height);
};
let start = function() {
create(settings.width, settings.height);
colorPicker();
fill();
};
// help function
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
html {
background-color: #1d1f21;
}
canvas {
display: inline-block;
}
.container {
text-align: center;
}
.textbox {
text-align: center;
color: white;
font-family: monospace;
}
<head>
<title>random gradient header</title>
<meta charset="UTF-8">
<link href="style.css" rel="stylesheet">
</head>
<body onload="start()">
<div class="container"></div>
<div class="textbox">Reload to get a different header gradient.</div>
<script src="short.js"></script>
</body>