The Goal
An often seen effect in illustrations and other graphic works is a gradient between two colors, which is provided with a grain/noise and thus gets a very special effect. (especially example 3.)
My research has shown many solutions how to achieve this effect in software like Illustrator, but I would like to recreate it with p5js or the vanilla js canvas.
My attempt
I have already tried to create a gradient and set a random noise with a specific color using the pixel array on top of it. Which only partially leads to the desired effect:
function draw() {
setGradient(0, 0, width, height, color(0, 0, 0), color(255, 255 ,255));
setNoise();
}
function setNoise() {
loadPixels();
for (let x = 0; x < width; x++ ) {
for (let y = 0; y < height; y++ ) {
if (random(1) > 0.9) {
const index = (x + y * width) * 4;
pixels[index] = 255;
pixels[index + 1] = 255;
pixels[index + 2] = 255;
pixels[index + 3] = 255;
}
}
}
updatePixels();
}
function setGradient(x, y, w, h, c1, c2) {
noFill();
for (let i = y; i <= y + h; i++) {
let inter = map(i, y, y + h, 0, 1);
let c = lerpColor(c1, c2, inter);
stroke(c);
line(x, i, x + w, i);
}
}
what do you think? is this the right approach or is there a better / simpler solution?