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

Flun Draw

  1. const canvas = document.createElement('canvas')
  2. const c = canvas.getContext('2d')
  3.  
  4. canvas.width = innerWidth * 2
  5. canvas.height = innerHeight * 2
  6. document.body.append(canvas)
  7. canvas.style.width = innerWidth + 'px'
  8. canvas.style.height = innerHeight + 'px'
  9. c.fillStyle = 'white'
  10. c.fillRect(0, 0, canvas.width, canvas.height)
  11.  
  12. const flun = (
  13.   ax = innerWidth,
  14.   ay = innerHeight,
  15.   r = 0,
  16.   x = 0, y = 0,
  17.   t = 0,
  18.   dt = 0,
  19.   spin = 0,
  20.   spinc = .1,
  21.   rinc = 0,
  22.   col = 'black',
  23.   hist = []
  24. ) => {
  25.  
  26.   const init = () => {
  27.     dt = Math.random() * Math.PI * 2
  28.     r = 0
  29.     rinc = Math.random() * 1
  30.     spinc = Math.random() * .01 - .005
  31.     col = ['black', 'white', 'rgba(0, 0, 0, 0.2)']
  32.       [~~(Math.random() * 3)]
  33.   }
  34.   init()
  35.  
  36.   return () => {
  37.  
  38.     spin += spinc
  39.     if (Math.random() < .005) {
  40.       init()
  41.     } else {
  42.       r += rinc
  43.     }
  44.  
  45.     if (Math.random() < .01) {
  46.       ax = x
  47.       ay = y
  48.       const inside = ax > 0 &&
  49.         ax < canvas.width &&
  50.         ay > 0 &&
  51.         ay < canvas.height
  52.       if (!inside) {
  53.         const p = hist[~~(Math.random() * hist.length)]
  54.         ax = p[0]
  55.         ay = p[1]
  56.       }
  57.       init()
  58.     } 
  59.  
  60.     t += (dt - t) / 22
  61.  
  62.     x = ax + Math.cos(t + spin) * r
  63.     y = ay + Math.sin(t + spin) * r
  64.  
  65.     if (x > 0 && x < canvas.width &&
  66.       y > 0 && y < canvas.height) {
  67.       hist.push([x, y])
  68.     }
  69.  
  70.     c.fillStyle = col
  71.     c.fillRect(x, y, 4, 4)
  72.   }
  73. }
  74.  
  75. const f = flun()
  76.  
  77. setInterval(() => {
  78.   for (let i = 0; i < 20; i++) f()
  79. }, 16)

Just randomly coded this one morning for a short – simple – but never did this exact motion before. It looks surprisingly organic and occasionally draws things that look like faces and other objects

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}