xxxxxxxxxx
161
// Fire Effect
// The Coding Train / Daniel Shiffman
// https://thecodingtrain.com/CodingChallenges/103-fire-effect.html
// improved on by Frode Austvik
// https://edorfaus.github.io/coding-train-variants/CodingChallenges/CC_103_Flames/P5/
// Edited by Bunny for recording to a file with CCapture and doing thingys with the hue and text and stuff - Note this is all just stuff thrown together, and thus is not very efficient
// dHue is the hue of the flame
// dLum is the brightness of the flame
// dSat is how much the flame changes the colour of the text
// 1 - 100
//const dHue = 57;
const dHue = 55;
const dLum = 100;
const dSat = 100;
// text colour
const sR = 255;
const sG = 166;
const sB = 0;
const tR = 245;
const tG = 245;
const tB = 255;
const txt = "meow";
const w = 355;
const h = 290;
// adjust increment and s based on dSat and the recording framerate, basically the 'resolution' of the fire
const s = 2; // vertical resolution (integer multiple of pixeldensity, 2+, 1 or less is wierd)
const increment = 0.02; // fieryness? (0 - 1, to much or little is wierd )
const blur = 1; // speaks for itself
const rec = false;
const recSet = { format: 'webm', framerate: 30 };
// don't change
if(rec){
var capturer = new CCapture( recSet );
document.onkeydown = function(_e) {
if (_e.key == 's') {
console.log(capturer);
capturer.stop();
capturer.save();
capturer.start();
//rec = false;
//remove();
}
};
}
let r, g, b;
let can;
let buffer1;
let buffer2;
let cooling;
let ystart = 0.0;
let yoff = 0.0;
let col;
let ct;
let frames = 0;
function setup() {
ct = millis();
col = color(tR, tG, tB);
scol = color(sR, sG, sB);
colorMode(HSB, 100);
const dCol = color(dHue/3.60, dSat, dLum);
r = red(dCol)/255;
g = green(dCol)/255;
b = blue(dCol)/255;
colorMode(RGB);
pixelDensity(1);
can=createCanvas(w, h);
buffer1 = createGraphics(w, h);
buffer2 = createGraphics(w, h);
cooling = createImage(w, h);
cooling.loadPixels();
for (let y = 0; y < h; y++) {
yoff += increment;
drawCoolingRow(y);
}
cooling.updatePixels();
if (rec) {
capturer.start();
recorder();
}
can.elt.style.background = "#000000";
}
function cool() {
cooling.copy(0, 1, w, h - 1, 0, 0, w, h - 1);
cooling.loadPixels();
yoff += increment; // Increment yoff to move to the next row
drawCoolingRow(h - 1); // Draw the new last row in the image
cooling.updatePixels();
}
function drawCoolingRow(y) {
let xoff = 0.0; // For every row, start xoff at 0
for (let x = 0; x < w; x++) {
xoff += increment;
const n = noise(xoff, yoff);
const bright = pow(n, 3) * 255;
const index = (x + y * w) * 4;
cooling.pixels[index] = bright;
cooling.pixels[index + 1] = bright;
cooling.pixels[index + 2] = bright;
cooling.pixels[index + 3] = 255;
}
}
function fire() {
buffer1.textSize(130);
buffer1.fill(col);
buffer1.stroke(scol);
buffer1.strokeWeight(3);
buffer1.text(txt, 0, h-5);
buffer1.strokeWeight(0);
}
function recorder(){
requestAnimationFrame(recorder);
d();
capturer.capture(can.elt);
}
function d(){
cool();
can.drawingContext.clearRect(0, 0, w, h);
background(0, 0, 0, 0);
buffer1.loadPixels();
buffer2.loadPixels();
for (let x = 1; x < w - 1; x++) {
for (let y = 1; y < h - 1; y++) {
const index = (x + y * w) * 4;
const m = w*4*s;
const c = cooling.pixels[index];
buffer2.pixels[index] = r*buffer1.pixels[index+m];
buffer2.pixels[index+1] = g*buffer1.pixels[index+m+1];
buffer2.pixels[index+2] = b*buffer1.pixels[index+m+2];
buffer2.pixels[index+3] = buffer1.pixels[index+m+3]-c;
}
}
buffer1.filter(BLUR, blur);
buffer2.updatePixels();
const temp = buffer1;
buffer1 = buffer2;
buffer2 = temp;
image(buffer2, 0, 0);
fire();
//frames+=1;
//console.log(Math.round(frames/(millis()-ct)*100000)/100);
}
var draw;
if(!rec) draw = d;