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

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

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…

squigs

  1. const canvas = document.body.appendChild(document.createElement("canvas"));
  2. const c = canvas.getContext("2d");
  3. document.body.style.margin = 0;
  4.  
  5. function resize() {
  6.   canvas.width = innerWidth * 2;
  7.   canvas.height = innerHeight * 2;
  8.   canvas.style.width = innerWidth + "px";
  9.   canvas.style.height = innerHeight + "px";
  10. }
  11. addEventListener("resize", resize);
  12. resize();
  13.  
  14. const PAD = 50;
  15. const RAD = 2;
  16. const SPEED = 40;
  17. const TWO_PI = Math.PI * 2;
  18.  
  19. let mode = "draw";
  20.  
  21. let t = Math.random() * TWO_PI,
  22.   x = innerWidth,
  23.   y = innerHeight,
  24.   vx = 0,
  25.   vy = 0,
  26.   ta = 0,
  27.   w = 10,
  28.   h = 1,
  29.   hue = 20;
  30.  
  31. function loop() {
  32.   c.globalAlpha = 1;
  33.   if (Math.random() < 0.12) {
  34.     let alpha = 0.02;
  35.     if (Math.random() < 0.1) alpha = 0.1;
  36.     c.fillStyle = `rgba(255, 255, 255, ${alpha})`;
  37.     c.fillRect(0, 0, canvas.width, canvas.height);
  38.   }
  39.   for (var i = 0; i < SPEED; i++) {
  40.     t = Math.sin(ta) * TWO_PI;
  41.     vx = RAD * Math.cos(t);
  42.     vy = RAD * Math.sin(t);
  43.     ta += Math.random() * 0.1 - 0.05;
  44.     x += vx;
  45.     y += vy;
  46.  
  47.     if (Math.random() < 0.005) {
  48.       mode = "no draw";
  49.       hue = [200, 20, 10, 40][~~(Math.random() * 4)];
  50.       if (Math.random() < 0.5) {
  51.         w = Math.random() * 200;
  52.         h = Math.random() * Math.random() * 10;
  53.       } else {
  54.         w = Math.random() * 20;
  55.         h = Math.random() * Math.random() * 200;
  56.       }
  57.     } else if (Math.random() < 0.005) {
  58.       mode = "draw";
  59.     }
  60.  
  61.     if (mode === "draw") {
  62.       c.shadowColor = "rgba(0, 205, 205, 0.2)";
  63.       c.shadowBlur = 5;
  64.       c.fillStyle = `hsl(${hue}, 100%, 50%)`;
  65.       c.fillRect(x, y, w, h);
  66.  
  67.       c.shadowColor = null;
  68.       c.shadowBlur = 0;
  69.     }
  70.  
  71.     if (x < -PAD) {
  72.       x = innerWidth + PAD;
  73.     } else if (x > canvas.width + PAD) {
  74.       x = -PAD;
  75.     }
  76.     if (y < -PAD) {
  77.       y = innerHeight + PAD;
  78.     } else if (y > canvas.height + PAD) {
  79.       y = -PAD;
  80.     }
  81.   }
  82.   c.globalAlpha = 0.9;
  83.   c.drawImage(canvas, -2, -1, canvas.width + 3, canvas.height + 4);
  84.  
  85.   requestAnimationFrame(loop);
  86. }
  87. loop();

More fun speed coding for some recent youtube shorts…

Swrls

  1. const canvas = document.createElement("canvas");
  2. const c = canvas.getContext("2d");
  3.  
  4. canvas.width = innerWidth;
  5. canvas.height = innerHeight;
  6. document.body.append(canvas); 
  7.  
  8. const TWO_PI = 2 * Math.PI;
  9.  
  10. class Dot {
  11.   constructor(id, x, y, theta, parent) {
  12.     this.x = x;
  13.     this.y = y;
  14.     this.t = theta;
  15.     this.vx = Math.cos(this.t) * Math.random() * 2;
  16.     this.vy = Math.sin(this.t) * Math.random() * 2;
  17.     this.life = 30;
  18.     this.col = parent.col;
  19.  
  20.     if (Math.random() < 0.005) this.other = true;
  21.     if (this.other) {
  22.       this.life = 1000;
  23.       this.t = Math.random() * 7;
  24.       this.vx = Math.cos(this.t) * Math.random() * 2;
  25.       this.vy = Math.sin(this.t) * Math.random() * 2;
  26.       this.col = "black";
  27.     }
  28.     this.iter = 0;
  29.     this.parent = parent;
  30.     this.id = id;
  31.     this.alpha = 0.25;
  32.  
  33.     if (Math.random() < 0.3) this.alpha = 0.25;
  34.   }
  35.  
  36.   draw() {
  37.     this.x += this.vx;
  38.     this.y += this.vy;
  39.     this.iter++;
  40.     if (this.iter > this.life) {
  41.       delete this.parent.dots[this.id];
  42.     }
  43.     c.fillStyle = this.col;
  44.     c.globalAlpha = this.alpha;
  45.     if (!this.no) this.alpha -= 0.00125;
  46.     c.fillRect(this.x, this.y, this.parent.size, this.parent.size);
  47.   }
  48. }
  49.  
  50. class Stain {
  51.   constructor(x = innerWidth, y = innerHeight) {
  52.     this.x = x / 2;
  53.     this.y = y / 2;
  54.     this.num = 1;
  55.     this.step = TWO_PI / this.num;
  56.  
  57.     this.size = 1 + Math.random() * Math.random() * 2;
  58.     this.rad = Math.random() * 300;
  59.     this.dots = {};
  60.     this.dotId = 0;
  61.     this.col = Math.random() < 0.8 ? "white" : "black";
  62.     this.to = Math.random() * 7;
  63.     this.S = 20 + Math.random() * 50;
  64.     this.R = 100 + Math.random() * 300;
  65.     this.rr = 50 + Math.random() * 300;
  66.     this.dir = Math.random() < 0.6 ? 1 : Math.random() * 10 - 5;
  67.     this.v = 1;
  68.     this.tt = 0;
  69.     if (Math.random() * 0.25) this.v = Math.random() * 5;
  70.   }
  71.  
  72.   draw() { 
  73.     const circ = this.rad * TWO_PI;
  74.     this.rad -= this.v;
  75.  
  76.     this.step = TWO_PI / circ;
  77.  
  78.     if (this.rad > 0) {
  79.       for (let i = 1; i <= this.R; i += this.S) {
  80.         let t = (i * this.step + this.to) * this.dir;
  81.         let x = this.x + this.rad * Math.cos(t);
  82.         let y = this.y + this.rad * Math.sin(t);
  83.         this.dots[this.dotId] = new Dot(this.dotId, x, y, t, this);
  84.         this.dotId++;
  85.       }
  86.     }
  87.     for (let i in this.dots) {
  88.       if (this.dots[i]) this.dots[i].draw();
  89.     }
  90.   }
  91. }
  92.  
  93. onresize = e => {
  94.   canvas.width = innerWidth;
  95.   canvas.height = innerHeight;
  96.   c.fillStyle = "black";
  97.   c.fillRect(0, 0, innerWidth, innerHeight)
  98. }
  99.  
  100. onresize()
  101.  
  102. let ss = [new Stain()];
  103.  
  104. function loop() {
  105.   if (Math.random() < 0.05) {
  106.     ss.push(
  107.       new Stain(Math.random() * innerWidth * 2, Math.random() * innerHeight * 2)
  108.     );
  109.   }
  110.   ss.forEach((s) => s.draw());
  111.   c.globalAlpha = 0.05;
  112.   c.drawImage(canvas, 0, 3, canvas.width + 0.2, canvas.height - 6);
  113.   c.globalAlpha = 1;
  114.   requestAnimationFrame(loop);
  115. }
  116. loop();

Speed coding for some recent youtube shorts…

Polar Forking Tweak

  1. const FOUR_PI = 6 * Math.PI;
  2. const { cos, sin } = Math;
  3.  
  4. const canvas = document.body.appendChild(
  5.   document.createElement('canvas')
  6. );
  7. const c = canvas.getContext('2d');
  8.  
  9. function resize() {
  10.   canvas.width = window.innerWidth;
  11.   canvas.height = window.innerHeight;
  12. }
  13.  
  14. let inc = 0;
  15. function draw() { 
  16.   c.fillStyle = 'rgba(0, 0, 0, .3)'
  17.   c.fillRect(0, 0, canvas.width, canvas.height)
  18.   c.fillStyle = 'white';
  19.  
  20.   const halfWidth = window.innerWidth / 2;
  21.   const halfHeight = window.innerHeight / 2;
  22.   let theta = 0,
  23.     a = 20 * Math.min(window.innerWidth, window.innerHeight) * 0.005,
  24.     x,
  25.     y;
  26.  
  27.   c.save();
  28.   c.translate(halfWidth, halfHeight)
  29.  
  30.   let b = 5 * cos(inc);
  31.   inc += .02;
  32.  
  33.   for (let i = 0; theta < FOUR_PI; i++) {
  34.     let rad = a * (b + 10 * sin(theta / 3));
  35.     // randomly speed-coded and tweaked... leaving as is :D
  36.     x = rad * cos(theta + b / 10) * cos(b / 10 +theta * 2) * cos(theta * 2);
  37.     y = rad * sin(theta * 2) * cos(theta + b / 3) * cos(theta * 2);
  38.     c.fillRect(x,y, 2, 2);
  39.     theta += 0.04;
  40.   }
  41.   c.restore();
  42.  
  43.   requestAnimationFrame(draw)
  44. }
  45.  
  46. resize();
  47. addEventListener('resize', resize);
  48.  
  49. draw();

Just randomly futzing with sin/cos…

snippet.zone /// {s/z}