)
}
}
)
(
}
{
)
)
(
)
(
(
{
}
)
(
)
}
)
)
{
(
(
)
)
}
)
(
}

Diagrams 2

  1. const canvas = document.createElement('canvas')
  2. canvas.width = 400
  3. canvas.height = 400
  4. document.body.append(canvas)
  5. document.body.style.background = 'black'
  6. Object.assign(document.body.style, {
  7.   position: 'absolute',
  8.   left: '50%', 
  9.   top: '50%',
  10.   transform: 'translate(-50%, -50%)'
  11. })
  12.  
  13. const ctx = canvas.getContext('2d')
  14. const width = canvas.width;
  15. const height = canvas.height;
  16.  
  17. // --- Global Variables ---
  18. const num = 4;
  19. const smp = new Array(num);
  20. let isMousePressed = false;
  21. let drag = false;
  22. let mouseX = 0;
  23. let mouseY = 0;
  24.  
  25. let theSize, t;
  26. let col = 0;
  27. const alpha = 255;
  28. let curveRes = 0.01;
  29.  
  30. let pan;
  31. const blank = new Int32Array(300 * 400);
  32.  
  33.  
  34. // ===================================================================
  35. //  CLASS AND FUNCTION DEFINITIONS
  36. //  All classes and functions are defined here, before they are called.
  37. // ===================================================================
  38.  
  39. // --- PModel Class (Shape logic) ---
  40. class PModel {
  41.   constructor(p_col) {
  42.     this.pNum = 9;
  43.     this.theColor = p_col;
  44.     this.pLocX = new Float32Array(this.pNum);
  45.     this.pLocY = new Float32Array(this.pNum);
  46.     this.bLocX = new Float32Array(this.pNum);
  47.     this.bLocY = new Float32Array(this.pNum);
  48.     this.bx = new Float32Array(this.pNum);
  49.     this.by = new Float32Array(this.pNum);
  50.     this.down = new Array(this.pNum).fill(false);
  51.     this.outlineColor = convertPColor(0xff0000);
  52.  
  53.     for (let i = 0; i < this.pNum; i++) {
  54.       this.pLocX[i] = this.bLocX[i] = this.bx[i] = Math.random() *
  55.       300; // Start in main area
  56.       this.pLocY[i] = this.bLocY[i] = this.by[i] = Math.random() * 400;
  57.     }
  58.   }
  59.  
  60.   action(pixels) {
  61.     this.outlineColor = this.theColor;
  62.     for (let i = 0; i < this.pNum; i++) {
  63.       if (Math.floor(Math.random() * 300) === 1) {
  64.         if (this.bLocX[i] < 250) {
  65.           drawText(`E-${Math.floor(Math.random() * 11100)}`, this.bLocX[
  66.             i], this.bLocY[i]);
  67.         }
  68.       }
  69.       if (dist(this.bLocX[i], this.bLocY[i], mouseX, mouseY) < 10 && !
  70.         drag) {
  71.         this.outlineColor = convertPColor(0xff0000);
  72.         if (isMousePressed) {
  73.           this.down[i] = true;
  74.           drag = true;
  75.         }
  76.       } else {
  77.         if (!drag) {
  78.           this.down[i] = false;
  79.         }
  80.       }
  81.  
  82.       if (this.down[i]) {
  83.         if (this.bLocX[i] < 250) {
  84.           drawText(`V-${Math.floor(Math.random() * 1100)}`, this.bLocX[i],
  85.             this.bLocY[i]);
  86.         }
  87.         this.outlineColor = convertPColor(0xff0000);
  88.         this.pLocX[i] = mouseX;
  89.         this.pLocY[i] = mouseY;
  90.       }
  91.  
  92.       this.bLocX[i] -= this.bx[i];
  93.       this.bx[i] = ((this.bLocX[i] - this.pLocX[i]) / 17 + this.bx[i]) /
  94.         1.5;
  95.       this.bLocY[i] -= this.by[i];
  96.       this.by[i] = ((this.bLocY[i] - this.pLocY[i]) / 17 + this.by[i]) /
  97.         1.5;
  98.     }
  99.  
  100.     this.ccc(0, 1, 40, 14);
  101.     this.ccc(0, 2, 40, 14);
  102.     this.ccc(0, 3, 40, 14);
  103.     this.ccc(0, 4, 40, 14);
  104.     this.ccc(0, 5, 40, 14);
  105.     this.ccc(0, 6, 40, 14);
  106.     this.ccc(0, 7, 40, 14);
  107.     this.ccc(0, 8, 40, 14);
  108.     this.ccc(1, 2, 40, 14);
  109.     this.ccc(2, 3, 40, 14);
  110.     this.ccc(3, 4, 40, 14);
  111.     this.ccc(4, 5, 40, 14);
  112.     this.ccc(5, 6, 40, 14);
  113.     this.ccc(6, 7, 40, 14);
  114.     this.ccc(8, 7, 40, 14);
  115.     this.ccc(8, 1, 40, 14);
  116.     this.ccc(1, 3, 80, 0);
  117.     this.ccc(3, 5, 80, 0);
  118.     this.ccc(5, 7, 80, 0);
  119.     this.ccc(7, 1, 80, 0);
  120.  
  121.     for (let i = 0; i < this.pNum; i++) {
  122.       if (this.pLocX[i] < 0) this.pLocX[i] += 5;
  123.       if (this.pLocX[i] > 300) this.pLocX[i] -= 5;
  124.       if (this.pLocY[i] < 0) this.pLocY[i] += 5;
  125.       if (this.pLocY[i] > 400) this.pLocY[i] -= 5;
  126.     }
  127.  
  128.     col = this.outlineColor;
  129.     apixel(pixels, this.bLocX[0], this.bLocY[0]);
  130.  
  131.     curveRes = 0.01;
  132.     curvePoint(pixels, this.bLocX[1], this.bLocY[1], this.bLocX[2], this
  133.       .bLocY[2], this.bLocX[3], this.bLocY[3], this.bLocX[2], this
  134.       .bLocY[2]);
  135.     curvePoint(pixels, this.bLocX[3], this.bLocY[3], this.bLocX[4], this
  136.       .bLocY[4], this.bLocX[5], this.bLocY[5], this.bLocX[4], this
  137.       .bLocY[4]);
  138.     curvePoint(pixels, this.bLocX[5], this.bLocY[5], this.bLocX[6], this
  139.       .bLocY[6], this.bLocX[7], this.bLocY[7], this.bLocX[6], this
  140.       .bLocY[6]);
  141.     curvePoint(pixels, this.bLocX[7], this.bLocY[7], this.bLocX[8], this
  142.       .bLocY[8], this.bLocX[1], this.bLocY[1], this.bLocX[8], this
  143.       .bLocY[8]);
  144.  
  145.     curveRes = 0.02;
  146.     col = this.theColor;
  147.     curvePoint(pixels, this.bLocX[1], this.bLocY[1], this.bLocX[0], this
  148.       .bLocY[0], this.bLocX[3], this.bLocY[3], this.bLocX[0], this
  149.       .bLocY[0]);
  150.     curvePoint(pixels, this.bLocX[3], this.bLocY[3], this.bLocX[0], this
  151.       .bLocY[0], this.bLocX[5], this.bLocY[5], this.bLocX[0], this
  152.       .bLocY[0]);
  153.     curvePoint(pixels, this.bLocX[5], this.bLocY[5], this.bLocX[0], this
  154.       .bLocY[0], this.bLocX[7], this.bLocY[7], this.bLocX[0], this
  155.       .bLocY[0]);
  156.     curvePoint(pixels, this.bLocX[7], this.bLocY[7], this.bLocX[0], this
  157.       .bLocY[0], this.bLocX[1], this.bLocY[1], this.bLocX[0], this
  158.       .bLocY[0]);
  159.   }
  160.  
  161.   ccc(indexA, indexB, clength, off) {
  162.     if (dist(this.pLocX[indexA], this.pLocY[indexA], this.pLocX[indexB],
  163.         this.pLocY[indexB]) > clength) {
  164.       this.pLocX[indexA] += (this.pLocX[indexB] - this.pLocX[indexA]) / 6;
  165.       this.pLocY[indexA] += (this.pLocY[indexB] - this.pLocY[indexA]) / 6;
  166.       this.pLocX[indexB] += (this.pLocX[indexA] - this.pLocX[indexB]) / 6;
  167.       this.pLocY[indexB] += (this.pLocY[indexA] - this.pLocY[indexB]) / 6;
  168.     }
  169.     for (let i = 0; i < this.pNum; i++) {
  170.       if (i !== indexA) {
  171.         if (dist(this.pLocX[indexA], this.pLocY[indexA], this.pLocX[i],
  172.             this.pLocY[i]) < clength - off) {
  173.           this.pLocX[indexA] -= (this.pLocX[i] - this.pLocX[indexA]) / 6;
  174.           this.pLocY[indexA] -= (this.pLocY[i] - this.pLocY[indexA]) / 6;
  175.         }
  176.       }
  177.     }
  178.   }
  179. }
  180.  
  181. // --- Panel Class (UI on the right) ---
  182. class Panel {
  183.   constructor() {
  184.     this.h1 = 80;
  185.     this.h2 = 80;
  186.     this.h3 = 80;
  187.     this.h4 = 80;
  188.     this.dh1 = 80;
  189.     this.dh2 = 80;
  190.     this.dh3 = 80;
  191.     this.dh4 = 80;
  192.     this.numsA = 0;
  193.   }
  194.  
  195.   // Static drawing for panel background (called once in setupPanel)
  196.   drawStatic() {
  197.     ctx.fillStyle = "rgb(115,115,115)";
  198.     ctx.fillRect(300, 0, 100, 400);
  199.     ctx.beginPath();
  200.     ctx.moveTo(300, 300);
  201.     ctx.lineTo(400, 400);
  202.     ctx.strokeStyle = "rgb(155,145,105)";
  203.     ctx.stroke();
  204.     /* ctx.strokeStyle = "red";
  205.       for (let i = 0; i < 40; i++) {
  206.           ctx.beginPath(); ctx.moveTo(101, i * 10); ctx.lineTo(400, 20 + i * 20); ctx.stroke();
  207.       }
  208.       ctx.stroke()*/
  209.  
  210.     ctx.strokeStyle = "white";
  211.     ctx.strokeStyle = "rgb(205,205,205)";
  212.     ctx.strokeRect(309.5, 11.5, 82, 111);
  213.     ctx.strokeRect(359.5, 130.5, 32, 21);
  214.     ctx.strokeRect(359.5, 160.5, 32, 21);
  215.     ctx.strokeRect(359.5, 190.5, 32, 21);
  216.   }
  217.  
  218.   action() {
  219.     ctx.strokeStyle = "rgb(135,125,85)";
  220.     ctx.fillStyle = "rgb(165,165,165)";
  221.     ctx.fillRect(310, 10, 80, 110);
  222.     ctx.beginPath();
  223.     ctx.moveTo(301.5, 0);
  224.     ctx.lineTo(301.5, 400);
  225.     ctx.stroke();
  226.  
  227.  
  228.     if (Math.floor(Math.random() * 60) === 1) this.dh1 = Math.random() *
  229.       75 + 5;
  230.     if (Math.floor(Math.random() * 60) === 1) this.dh2 = Math.random() *
  231.       75 + 5;
  232.     if (Math.floor(Math.random() * 60) === 1) this.dh3 = Math.random() *
  233.       75 + 5;
  234.     if (Math.floor(Math.random() * 60) === 1) this.dh4 = Math.random() *
  235.       75 + 5;
  236.  
  237.     this.h1 += (this.dh1 - this.h1) * 0.09;
  238.     this.h2 += (this.dh2 - this.h2) * 0.09;
  239.     this.h3 += (this.dh3 - this.h3) * 0.09;
  240.     this.h4 += (this.dh4 - this.h4) * 0.09;
  241.  
  242.  
  243.  
  244.     ctx.fillRect(310, 130, 10, this.h1);
  245.     ctx.fillRect(320, 130, 10, this.h2);
  246.     ctx.fillRect(330, 130, 10, this.h3);
  247.     ctx.fillRect(340, 130, 10, this.h4);
  248.  
  249.     ctx.fillRect(360, 130, 30, 20);
  250.     ctx.fillRect(360, 160, 30, 20);
  251.     ctx.fillRect(360, 190, 30, 20);
  252.  
  253.     ctx.strokeRect(310, 130, 10, this.h1);
  254.     ctx.fillRect(320, 130, 10, this.h2);
  255.     ctx.strokeRect(330, 130, 10, this.h3);
  256.     ctx.fillRect(340, 130, 10, this.h4);
  257.  
  258.     ctx.strokeRect(360, 130, 30, 20);
  259.     ctx.fillRect(360, 160, 30, 20);
  260.     ctx.fillRect(360, 190, 30, 20);
  261.  
  262.     if (Math.floor(Math.random() * 10) === 1) this.numsA = Math.floor(Math
  263.       .random() * 10000000);
  264.     drawText(`E-${this.numsA}`, 315, 116, "white");
  265.   }
  266. }
  267.  
  268. // --- Main Setup and Loop ---
  269. function setup() {
  270.   ctx.fillStyle = 'black';
  271.   ctx.fillRect(0, 0, width, height);
  272.   theSize = width * height;
  273.   t = 0;
  274.  
  275.   for (let i = 0; i < num; i++) {
  276.     if (i < num / 2) {
  277.       smp[i] = new PModel(convertPColor(0x330000));
  278.     } else {
  279.       smp[i] = new PModel(convertPColor(0xffffff));
  280.     }
  281.   }
  282.   setupPanel();
  283.   addEventListeners();
  284. }
  285.  
  286. function loop() {
  287.   const imageData = ctx.getImageData(0, 0, width, height);
  288.   const pixels = new Uint32Array(imageData.data.buffer);
  289.  
  290.   if (isMousePressed) {
  291.     for (let i = 0; i < 120000 - 300; i++) {
  292.       pixels[blank[i]] = blendC(pixels[blank[i]] << 8, ~pixels[blank[i +
  293.         300]], 40);
  294.     }
  295.   } else {
  296.     for (let i = 0; i < 120000 - 300; i++) {
  297.       pixels[blank[i]] = blendC(~pixels[blank[i]], pixels[blank[i + 300]] |
  298.         0x111111, 150);
  299.     }
  300.   }
  301.  
  302.   if (t < 12) {
  303.     if (t !== 11) {
  304.       for (let i = 400; i < theSize; i++) {
  305.         pixels[i] = blendC(~pixels[i] & 0xDDDDDD, pixels[i - 400], 20);
  306.       }
  307.     } else {
  308.       for (let i = 400; i < theSize; i++) {
  309.         pixels[i] = blendC(0x000000, pixels[i], 50);
  310.       }
  311.     }
  312.     for (let i = 0; i < 120000; i++) {
  313.       pixels[blank[i]] = 0xFFFFFFFF;
  314.     }
  315.   }
  316.   t++;
  317.  
  318.   for (let i = 0; i < num; i++) {
  319.     smp[i].action(pixels);
  320.   }
  321.  
  322.   for (let a = 0; a < num; a++) {
  323.     for (let b = 0; b < num; b++) {
  324.       if (a === b) continue;
  325.       repel(a, b, 0, 0, 120);
  326.       if (Math.floor(Math.random() * 400) === 1) {
  327.         col = convertPColor(0xFFFFFFFF);
  328.         drawLine(pixels, smp[a].bLocX[0], smp[a].bLocY[0], smp[b].bLocX[0],
  329.           smp[b].bLocY[0]);
  330.       }
  331.     }
  332.   }
  333.  
  334.   ctx.putImageData(imageData, 0, 0);
  335.   pan.action();
  336.   ctx.strokeStyle = "white";
  337.   ctx.strokeRect(0.5, 0.5, 399, 399);
  338.   ctx.fillStyle = 'rgba(110, 50, 0, 0.04)'
  339.   ctx.fillRect(300, 0, 100, 400);
  340.   requestAnimationFrame(loop);
  341. }
  342.  
  343.  
  344. // --- Utility & Drawing Functions ---
  345. function repel(ind, ind2, indexA, indexB, clength) {
  346.   if (dist(smp[ind].pLocX[indexA], smp[ind].pLocY[indexA], smp[ind2].pLocX[
  347.       indexB], smp[ind2].pLocY[indexB]) < clength) {
  348.     smp[ind].pLocX[indexA] -= (smp[ind2].pLocX[indexB] - smp[ind].pLocX[
  349.       indexA]) / 2;
  350.     smp[ind].pLocY[indexA] -= (smp[ind2].pLocY[indexB] - smp[ind].pLocY[
  351.       indexA]) / 2;
  352.   }
  353. }
  354.  
  355. function curvePoint(pixels, x1, y1, x2, y2, x3, y3, x4, y4) {
  356.   for (let a = 0; a < 1; a += curveRes) {
  357.     const b = 1 - a;
  358.     const pre1 = a * a * a;
  359.     const pre2 = 3 * (a * a) * b;
  360.     const pre3 = 3 * a * (b * b);
  361.     const pre4 = b * b * b;
  362.     const x = pre1 * x1 + pre2 * x2 + pre3 * x4 + pre4 * x3;
  363.     const y = pre1 * y1 + pre2 * y2 + pre3 * y4 + pre4 * y3;
  364.     apixel(pixels, x, y);
  365.   }
  366. }
  367.  
  368. function blendC(c1, c2, amount) {
  369.   const r1 = c1 & 0xff;
  370.   const g1 = (c1 >> 8) & 0xff;
  371.   const b1 = (c1 >> 16) & 0xff;
  372.   const r2 = c2 & 0xff;
  373.   const g2 = (c2 >> 8) & 0xff;
  374.   const b2 = (c2 >> 16) & 0xff;
  375.   const r = (((amount * (r1 - r2)) >> 8) + r2);
  376.   const g = (((amount * (g1 - g2)) >> 8) + g2);
  377.   const b = (((amount * (b1 - b2)) >> 8) + b2);
  378.   return (0xff000000 | (b << 16) | (g << 8) | r) >>> 0;
  379. }
  380.  
  381. function apixel(pixels, ax, ay) {
  382.   const x = Math.floor(ax);
  383.   const y = Math.floor(ay);
  384.   if (x <= 0 || x >= width - 2 || y <= 0 || y >= height - 2) return;
  385.   const fx = ax - x;
  386.   const fy = ay - y;
  387.   const nfx = 1 - fx;
  388.   const nfy = 1 - fy;
  389.   const loc = x + y * width;
  390.   const loc2 = loc + width;
  391.   const loc3 = loc + 1;
  392.   const loc4 = loc2 + 1;
  393.   pixels[loc] = blendC(col, pixels[loc], Math.floor(nfx * nfy * alpha));
  394.   pixels[loc3] = blendC(col, pixels[loc3], Math.floor(fx * nfy * alpha));
  395.   pixels[loc2] = blendC(col, pixels[loc2], Math.floor(nfx * fy * alpha));
  396.   pixels[loc4] = blendC(col, pixels[loc4], Math.floor(fx * fy * alpha));
  397. }
  398.  
  399. function drawLine(pixels, x1, y1, x2, y2) {
  400.   const dx = x2 - x1;
  401.   const dy = y2 - y1;
  402.   let n = Math.abs(dx) > Math.abs(dy) ? Math.abs(dx) : Math.abs(dy);
  403.   if (n === 0) return;
  404.   const dt = 1.0 / n;
  405.   const dxdt = dx * dt;
  406.   const dydt = dy * dt;
  407.   let x = x1;
  408.   let y = y1;
  409.   while (n-- >= 0) {
  410.     if (x > 1 && x < width - 1 && y > 1 && y < height - 1) {
  411.       apixel(pixels, x, y);
  412.     }
  413.     x += dxdt;
  414.     y += dydt;
  415.   }
  416. }
  417.  
  418. function setupPanel() {
  419.   pan = new Panel();
  420.   pan.drawStatic(); // Draw the static background of the panel once.
  421.   ctx.font = "12px sans-serif";
  422.   let index = 0;
  423.   for (let j = 0; j < 160000; j += 400) {
  424.     for (let i = 0; i < 300; i++) {
  425.       blank[index++] = i + j;
  426.     }
  427.   }
  428. }
  429.  
  430. function dist(x1, y1, x2, y2) {
  431.   const dx = x1 - x2;
  432.   const dy = y1 - y2;
  433.   return Math.sqrt(dx * dx + dy * dy);
  434. }
  435.  
  436. function convertPColor(pColor) {
  437.   const alpha = (pColor & 0xFF000000) >>> 0;
  438.   const red = (pColor & 0x00FF0000) >> 16;
  439.   const green = (pColor & 0x0000FF00);
  440.   const blue = (pColor & 0x000000FF) << 16;
  441.   return (alpha | blue | green | red) >>> 0;
  442. }
  443.  
  444. function drawText(txt, x, y, color = "white") {
  445.   ctx.fillStyle = color;
  446.   ctx.fillText(txt, x, y);
  447. }
  448.  
  449. function addEventListeners() {
  450.   canvas.addEventListener('mousedown', () => {
  451.     isMousePressed = true;
  452.   });
  453.   canvas.addEventListener('mouseup', () => {
  454.     isMousePressed = false;
  455.     drag = false;
  456.   });
  457.   canvas.addEventListener('mouseout', () => {
  458.     isMousePressed = false;
  459.     drag = false;
  460.   });
  461.   canvas.addEventListener('mousemove', (e) => {
  462.     const rect = canvas.getBoundingClientRect();
  463.     mouseX = e.clientX - rect.left;
  464.     mouseY = e.clientY - rect.top;
  465.   });
  466. }
  467.  
  468. // ===================================================================
  469. //  START THE SKETCH
  470. //  This is the final step, ensuring everything above is defined.
  471. // ===================================================================
  472. setup();
  473. requestAnimationFrame(loop);

This is a port of an old Processing experiment from the early 2000s that I ported to JS with Gemini 2.5 pro…

iwrjgnb

  1. const canvas = document.createElement('canvas')
  2. const c = canvas.getContext('2d')
  3.  
  4. canvas.width = innerWidth * 2
  5. canvas.height = innerHeight * 2
  6. canvas.style.scale = '.5 .5'
  7. canvas.style.transformOrigin = '0 0' 
  8.  
  9. c.fillStyle = 'rgba(255, 236, 168, 1)'
  10. c.fillRect(0, 0, canvas.width, canvas.height)
  11. document.body.append(canvas)
  12.  
  13. let width = 300
  14. let height = 300
  15.  
  16. function cell(ox = 0, oy = 0) {
  17.  
  18.   let ax = ox + width / 2
  19.   let ay = oy + height / 2
  20.   let x, y
  21.   let r = width * Math.random()
  22.   let rx = r
  23.   let ry = r
  24.   if (Math.random() < .5) rx *= Math.random()
  25.   if (Math.random() < .5) ry *= Math.random()
  26.   let t = Math.random() * 7
  27.  
  28.   let ti = Math.random() * .1 - .05
  29.   let te;
  30.   let dr = 0
  31.   if (ti > 0) {
  32.     te = t + Math.random() * 7
  33.     dr = 1
  34.   } else {
  35.     te = t - Math.random() * 7
  36.   }
  37.   let rm = 0
  38.   if (Math.random() < .5) rm = Math.random() * 3
  39.  
  40.   return () => {
  41.     x = ax + rx * Math.cos(t)
  42.     y = ay + ry * Math.sin(t)
  43.     if (rm !== 0) {
  44.       rx -= rm;
  45.       ry -= rm
  46.     }
  47.     t += ti
  48.     if ((t > te && dr === 1) || (t < te && dr === 0)) {
  49.       return
  50.     }
  51.     c.fillStyle = 'black'
  52.     c.fillRect(x, y, 2, 2)
  53.  
  54.     if (Math.random() < .5) {
  55.       cls.push(cl(x, y))
  56.     }
  57.   }
  58. }
  59.  
  60. function cl(x, y) {
  61.   let vy = .2 + Math.random() * 2 - 1
  62.   let col = 'rgba(0, 0, 0, 0.08)'
  63.   if (Math.random() < .1) col = 'rgba(0, 178, 227, .3)'
  64.   if (Math.random() < .01) col = 'rgba(255, 0, 0, .3)'
  65.   let vx = 0
  66.   if (Math.random() < .03) {
  67.     vx = 1
  68.     vy = 0;
  69.     col = 'rgba(255, 255, 255, .2)'
  70.   }
  71.   let dead = false;
  72.   return () => {
  73.     if (dead) return
  74.     y += vy
  75.     x += vx
  76.     if (Math.random() < .01) {
  77.       dead = true
  78.     }
  79.     c.fillStyle = col
  80.     c.fillRect(x, y, 2, 2)
  81.   }
  82. }
  83.  
  84. let NUM = 5
  85. let cs = []
  86. let cls = []
  87. for (let j = 0; j < NUM; j++) {
  88.   let yy = Math.random() * innerHeight
  89.   let xx = Math.random() * innerWidth
  90.   let num = 5 + ~~(Math.random() * Math.random() * 10)
  91.   if (Math.random() < .3) num = Math.random() * 50
  92.   for (let i = 0; i < NUM; i++) {
  93.     for (let k = 0; k < num; k++) {
  94.       cs.push(cell(xx, yy))
  95.     }
  96.   }
  97. }
  98.  
  99. function loop() {
  100.   cs.forEach(f => f())
  101.   cls.forEach(f => f())
  102.   requestAnimationFrame(loop)
  103. }
  104. loop()

Just posting more code from recent shorts… more here

sTwister

  1. const canvas = document.createElement('canvas')
  2. const c = canvas.getContext('2d')
  3.  
  4. canvas.width = innerWidth * 2
  5. canvas.height = innerHeight * 2
  6. canvas.style.scale = '.5 .5'
  7. canvas.style.transformOrigin = '0 0'
  8. canvas.style.filter = 'contrast(1.1)'
  9. document.body.append(canvas)
  10.  
  11.  
  12. const canvas2 = document.createElement('canvas')
  13. const c2 = canvas2.getContext('2d')
  14.  
  15. canvas2.width = innerWidth * 2
  16. canvas2.height = innerHeight * 2
  17.  
  18.  
  19. const r = (n = 1) => Math.random() * n
  20.  
  21. let anc = ~~r(360)
  22.  
  23. function plaint(x, y) {
  24.   let vy = r(-20) - 1
  25.   let vx = 0
  26.   const cl = ~~r(50) + anc;
  27.   let t = r(7)
  28.   let rx = r(1) + .5
  29.   let jag = r(.5)
  30.   let bright = r(100)
  31.   return () => {
  32.     vx = rx * Math.cos(t)
  33.     if (r() < jag) {
  34.       t += r(3)
  35.     }
  36.     x += vx
  37.     y += vy
  38.     c.fillStyle = `hsla(${cl}, 20%, ${bright}%, .5)`
  39.  
  40.     c.fillRect(x, y, 4, 4)
  41.     if (y < 0) {
  42.       y = canvas.height
  43.       vy = r(-20) - 1
  44.     }
  45.   }
  46. }
  47.  
  48. function erase() {
  49.   c.fillStyle = 'black'
  50.   c.fillRect(0, 0, canvas.width, canvas.height)
  51.  
  52. }
  53. erase()
  54.  
  55. const plntNum = 350
  56. let xStep = canvas.width / plntNum
  57.  
  58. const plnt = []
  59. for (let i = 0; i < plntNum; i++) {
  60.   const x = i * xStep
  61.   plnt.push(plaint(x, canvas.height))
  62. }
  63.  
  64. let R = null 
  65.  
  66. function swister(C, x, y, fc, ss, n, slow) {
  67.   const shadesNum = n || 100
  68.   const shades = []
  69.   const rad = R || 200   
  70.  
  71.   for (let i = 0; i < shadesNum; i++) {
  72.     shades.push({
  73.       x,
  74.       y,
  75.       xx: 0,
  76.       yy: 0,
  77.       r: r(rad) + 200,
  78.       t: r(7),
  79.       rinc: r(1) - .1,
  80.       tinc: r(.1),
  81.       cl: fc(),
  82.       a: r(.02) + .01
  83.     }) 
  84.  
  85.   }
  86.  
  87.   return () => {
  88.     for (let i = 0; i < shadesNum; i++) {
  89.       let s = shades[i]
  90.       s.xx = s.x + s.r * Math.cos(s.t)
  91.       s.yy = s.y + s.r * Math.sin(s.t)
  92.       s.r -= s.rinc;
  93.       if (s.r < 200) {
  94.         s.r = r(rad) + 200
  95.         s.tinc = r(.1)
  96.         if (slow) s.tinc *= .1
  97.         if (r() < .5) s.inc *= -1;
  98.  
  99.         s.rinc = r(.1)
  100.       }
  101.       s.t += s.tinc
  102.       C.fillStyle = `rgba(${s.cl}, ${s.cl}, ${s.cl}, ${s.a})`
  103.       C.fillRect(s.xx, s.yy, ss, ss)
  104.     }
  105.   }
  106. }
  107.  
  108. R = r(canvas.width / 5) + 50
  109. let s1x = (canvas.width - R) * r() + R / 2
  110. let s1y = (canvas.height - R) * r() + R / 2
  111. let s = swister(c, s1x, s1y,
  112.   () => r(10), 24
  113. )
  114. let s2 = swister(c2,
  115.   s1x - 50, s1y - 50,
  116.   () => r(200) + 0, 4
  117. )
  118.  
  119. let s3 = swister(c2,
  120.   s1x - 50, s1y - 50,
  121.   () => r(50) + 200, 8, 4, true
  122. )
  123.  
  124.  
  125. R = r(150) + 50
  126. let s2x = (canvas.width - R) * r() + R / 2
  127. let s2y = (canvas.height - R) * r() + R / 2
  128. let sa = swister(c, s2x, s2y,   
  129.   () => r(10), 24
  130. )
  131. let sb = swister(c2,
  132.   s2x - 50, s2y - 50,
  133.   () => r(200) + 0, 4
  134. )
  135.  
  136. let sc = swister(c2,
  137.   s2x - 50, s2y - 50,
  138.   () => r(50) + 200, 8, 4, true
  139. ) 
  140.  
  141. function draw() {
  142.  
  143.   c.globalAlpha = 1;
  144.   c.globalCompositeOperation = 'source-over'  
  145.   plnt.forEach(p => p())
  146.  
  147.   s()
  148.   sa()
  149.   c.globalAlpha = .05;
  150.   c.drawImage(canvas, -10, -10, canvas.width + 10, canvas.height + 10)
  151.   // c.globalCompositeOperation = 'source-atop'
  152.   s2()
  153.   sb()
  154.   s3()
  155.   sc()
  156.   c.globalAlpha = 1
  157.   c.globalCompositeOperation = 'hard-light'
  158.   c.drawImage(canvas2, 0, 0);
  159.   c.globalAlpha = .02; 
  160.  
  161.   c.drawImage(canvas, 0, 0, canvas.width, canvas.height + 10)
  162.   requestAnimationFrame(draw)
  163. }
  164. draw()

Speed-coded this on the train the other day. Might port over to WebGL sometime soon…

T Plant

  1. const { PI } = Math
  2. const canvas = document.createElement('canvas')
  3. const c = canvas.getContext('2d')
  4.  
  5. canvas.width = innerWidth * 2
  6. canvas.height = innerHeight * 2
  7. canvas.style.scale = '.5 .5'
  8. canvas.style.transformOrigin = '0 0'
  9. document.body.append(canvas)
  10. let o = -PI / 2
  11.  
  12. const r = (n = 1) => Math.random() * n
  13.  
  14. function branch(x, y, ang = o, f) {
  15.   let vx, vy
  16.   let life = 0
  17.   let dead
  18.   let vv = 1 + r(2)
  19.   let ll = r(200)
  20.   let a = 1;
  21.   if (f) a = 0
  22.  
  23.   return () => {
  24.     if (dead) return
  25.     if (life > 200 + ll) {
  26.       dead = true;
  27.       let n = 10 + r(20)
  28.       for (let i = 0; i < n; i++) {
  29.  
  30.         let dest = {
  31.           x,
  32.           y: y + r(30) - 15
  33.         }
  34.         bs.push(leafPart(x, y, dest))
  35.       }
  36.     }
  37.  
  38.     life++
  39.     if (life > 10 && a >= 1 && Math.random() < .02 && bs.length < 20) {
  40.       if (r() < .3) {
  41.         dead = true
  42.         bs.push(
  43.           branch(x, y, ang + r(PI / 4)))
  44.         branch(x, y, ang + r(-PI / 4))
  45.         return
  46.       }
  47.       bs.push(
  48.         branch(x, y, o + r(2) - 1))
  49.     }
  50.     vx = Math.cos(ang)
  51.     vy = Math.sin(ang) * vv
  52.     ang += r(.1) - .05
  53.  
  54.     x += vx
  55.     y += vy
  56.     c.fillStyle = `rgba(255, 255, 255, ${a})`
  57.     if (f) {
  58.       a += .01
  59.     }
  60.  
  61.     c.fillRect(x, y, 4, 4)
  62.   }
  63. }
  64.  
  65. function leafPart(x, y, dest) {
  66.   let ang = r(7)
  67.   let rr = r(1) + .5
  68.   let vv = -r(3)
  69.   let life = 0
  70.   let maxLife = 40 + r(200)
  71.   let vx
  72.   let vy
  73.   let CC = 190 + r(30)
  74.  
  75.   return () => {
  76.     life++
  77.     if (life < maxLife) {
  78.       vx = Math.cos(ang) * rr
  79.       vy = Math.sin(ang) * rr + vv
  80.       x += vx
  81.       y += vy
  82.       ang += r(.4) - .2
  83.       c.fillStyle = 'rgba(255, 255, 255, .1)'
  84.     } else {
  85.       c.fillStyle = `rgba(255, ${CC}, ${CC}, .25)`
  86.       x += (dest.x - x) / 42
  87.       y += (dest.y - y) / 12
  88.     }
  89.  
  90.     c.fillRect(x, y, 4, 4)
  91.   }
  92. }
  93. let bs = []
  94. let lf = []
  95.  
  96. bs.push(
  97.   branch(innerWidth, innerHeight * 1.9, o, true)
  98. )
  99.  
  100. function loop() {
  101.   bs.forEach(b => b())
  102.   requestAnimationFrame(loop)
  103. }
  104. loop()

Been pretty consistently posting speed-coded youtube shorts. This is a recent one. Magic numbers, nonsense variable names, and strange noops 😀

See the short here…

crystl

  1. const canvas = document.createElement('canvas')
  2. const c = canvas.getContext('2d')
  3.  
  4. let w;
  5. let h;
  6.  
  7. onresize = e => {
  8.   w = innerWidth * 2
  9.   h = innerHeight * 2
  10.   canvas.width = w
  11.   canvas.height = h
  12.   canvas.style.width = (w / 2) + 'px'
  13.   canvas.style.height = (h / 2) + 'px'  
  14.   c.fillStyle = '#000'
  15.   c.fillRect(0, 0, w, h) 
  16. }
  17. onresize()
  18.  
  19. document.body.append(canvas)
  20. canvas.style.filter = 'contrast(2) brightness(1.2)'
  21.  
  22. class Crystal {
  23.   constructor() {
  24.     this.init()
  25.   }
  26.   init() {
  27.     this.x = w / 2
  28.     this.y = h * .7
  29.     this.rad = 250
  30.  
  31.     this.sides = 5
  32.     // + ~~(Math.random() * 5)
  33.     this.shrinkTime = 400 + Math.random() * 100 
  34.     this.t = Math.random() * 7
  35.     this.st = this.t;
  36.     this.step = (Math.PI * 2) / this.sides
  37.     this.time = 0
  38.     this.shrinkSpeed = 1.3 - Math.random() * .6
  39.     this.a = 0
  40.     this.ad = .05
  41.   }
  42.  
  43.   draw() { 
  44.  
  45.     this.a += (this.ad - this.a) / 150
  46.     c.lineWidth = 5
  47.     c.strokeStyle = `rgba(255, 255, 255, ${this.a})`
  48.     c.fillStyle = `rgba(0, 111, 122, ${this.a / 2})`
  49.     c.beginPath()
  50.  
  51.     for (let j = 0; j < 2; j++) {
  52.       this.time++
  53.       if (this.time > this.shrinkTime) {
  54.         this.rad -= this.shrinkSpeed
  55.         if (this.rad < 0) {
  56.           setTimeout(() => {
  57.             c.fillStyle = 'rgba(0, 0, 0, .02)'
  58.             c.fillRect(0, 0, w, h)
  59.             this.init()
  60.           }, 500)
  61.           return;
  62.         }
  63.       }
  64.       this.t = this.st
  65.       for (let i = 0; i < this.sides; i++) {
  66.         let p = {
  67.           x: this.rad * Math.cos(this.t),
  68.           y: this.rad * .6 * Math.sin(this.t)
  69.         }
  70.         this.t += this.step;
  71.         if (i === 0) {
  72.           c.moveTo(this.x + p.x, this.y + p.y)
  73.         } else {
  74.           c.lineTo(this.x + p.x, this.y + p.y)
  75.         }
  76.       }
  77.       c.closePath()
  78.       c.stroke() 
  79.       c.fill()
  80.  
  81.  
  82.       this.y -= 1
  83.     }
  84.   }
  85. }
  86.  
  87. let cryst = new Crystal()
  88.  
  89.  
  90. setInterval(() => {
  91.   for (let i = 0; i < 3; i++) cryst.draw()
  92. }, 16)

quick crystal for recent youtube short…

snippet.zone /// {s/z}