Hi all, I’m trying to implement a fade effect. The basic idea is to draw something on the canvas that fades overtime. I draw multiple times, and call fade after every draw, to create an effect where old drawings eventually disappear. My problem is that after the first “fade”, the drawing function stops showing anything in the canvas, even when messages in the console will indicate that is executing. Here is a simplified version of my sketch:
var q = function(sketch) {
var backColor = sketch.color(255,255,255);
var colorStep = 5;
sketch.setup = function() {
console.log("In setup");
sketch.createCanvas(500,500);
sketch.background(backColor);
sketch.strokeWeight(0.8);
sketch.frameRate(10);
sketch.smooth();
}
sketch.draw = function() {
randomLines();
fadeImage();
}
function randomLines() {
sketch.stroke(sketch.random(200), sketch.random(200), sketch.random(200));
sketch.line(sketch.random(200), sketch.random(200), 300+sketch.random(100), 300+sketch.random(100));
}
function fadeImage() {
sketch.loadPixels();
for (var i = 0; i < sketch.pixels.length; i+=4) {
var r = sketch.pixels[i];
var g = sketch.pixels[i+1];
var b = sketch.pixels[i+2];
if (r < sketch.red(backColor)) {
r += colorStep;
// console.log("adjusted red");
}
if (g < sketch.blue(backColor)) {
g += colorStep;
// console.log("adjusted green");
}
if (b < sketch.green(backColor)) {
b += colorStep;
// console.log("adjusted blue");
}
sketch.pixels[i] = r;
sketch.pixels[i+1] = g;
sketch.pixels[i+2] = b;
}
sketch.updatePixels();
}
}
var fadeSample = new p5(q, 'fadeSampleCanvas');
and the html file:
<!doctype html>
<html lang="en">
<head>
<!-- Processing p5.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
</head>
<body>
<div class="container">
<div id='fadeSample'>
<h2>Fade Sample</h2>
<div id='fadeSampleCanvas' style="border:1px solid black; align-content: center">
</div>
<div>
<p>Fading sample</p>
</div>
</div>
</div>
<!-- sketches-->
<script src="fadeSample.js"></script>
</body>
</html>
I only get 1 line that fades overtime, when I should get multiple lines with different degrees of fading. This works in P3.
Any ideas?
Thanks.