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

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…

snippet.zone /// {s/z}