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

Speed Coded Space

  1. (() => {
  2.   const TWO_PI = Math.PI * 2;
  3.   const m = new Float32Array([
  4.     0, 0, 0, 0,
  5.     0, 0, 0, 0,
  6.     0, 0, 0, 0,
  7.     0, 0, 0, 0
  8.   ])
  9.  
  10.   const vert = `
  11.     attribute vec3 vec;
  12.     uniform mat4 mat;
  13.     void main(void) {
  14.       gl_Position = mat * vec4(vec, 1.0);
  15.       gl_PointSize = 2.0;
  16.     }
  17.   `
  18.  
  19.   const frag = `
  20.     void main(void) {
  21.       gl_FragColor = vec4(1., 1., 1., .25);
  22.     }
  23.   `
  24.  
  25.   document.body.style.background = '#232323'
  26.   const gl = document.body
  27.     .appendChild(document.createElement('canvas'))
  28.     .getContext('webgl', {
  29.       preserveDrawingBuffer: true,
  30.       powerPreference: 'high-performance'
  31.     })
  32.  
  33.   Object.assign(gl.canvas.style, {
  34.     position: 'absolute',
  35.     left: '50%',
  36.     top: '50%',
  37.     transform: 'translate(-50%, -50%)',
  38.     outline: '1px solid gray'
  39.   })
  40.  
  41.  
  42.   with(gl) {
  43.     const NUM = 50
  44.     const radius = 0.7
  45.     let verts = []
  46.  
  47.     // Superformula (equations from):
  48.     // https://bsapubs.onlinelibrary.wiley.com/doi/10.3732/ajb.90.3.333
  49.     // http://en.wikipedia.org/wiki/Superformula
  50.     function superShape(a, b, m, n1, n2, n3, scale, x = 0, y = 0, z = 0, rx=0, ry=0, rz=0) {
  51.       const { random, pow, abs, cos, sin } = Math
  52.       // with(Math) { // destrucuring vs the dreaded `with`
  53.         let r = 0
  54.         let p = 0
  55.         let xp = 0
  56.         let yp = 0
  57.         let zp = 0
  58.  
  59.         let rotX = rx
  60.         let rotY = ry  
  61.         let rotZ = rz
  62.  
  63.         let cosX = cos(rotX)
  64.         let cosY = cos(rotY)
  65.         let sinX = sin(rotX)
  66.         let sinY = sin(rotY)
  67.  
  68.         while (p <= TWO_PI) {
  69.           let ang = (m * p) / 4
  70.  
  71.           r = pow(pow(abs(cos(ang) / a), n2) + pow(abs(sin(ang) / b), n3), -1 / n1)
  72.           xp = r * cos(p)
  73.           yp = r * sin(p)
  74.  
  75.           p += 0.05
  76.  
  77.           zp = zp * cosX - xp * sinX
  78.           xp = zp * sinX + xp * cosX
  79.           yp = yp * cosY - zp * sinY
  80.           zp = yp * sinY + zp * cosY
  81.  
  82.           verts[inc] = xp * scale + x
  83.           verts[inc + 1] = yp * scale + y
  84.           verts[inc + 2] = zp * scale + z
  85.           inc += 3;
  86.         }
  87.       // }
  88.     }
  89.  
  90.     let inc = 0; 
  91.     for (let i = 0; i < NUM; i++) {
  92.       let m =  Math.random() * 20
  93.       let n1 = Math.random() * 30
  94.       let n2 = Math.random() * 30
  95.       let n3 = Math.random() * 30
  96.       let scl = Math.random() * .2
  97.       let x = Math.random() * Math.cos(i)
  98.       let y = Math.random() * Math.sin(i);
  99.       let z = Math.random() - .5
  100.       let rx = Math.random() * TWO_PI,
  101.           ry = Math.random() * TWO_PI,
  102.           rz = Math.random() * TWO_PI;
  103.  
  104.       for (let j = 0; j < 10; j++) {
  105.         superShape(1, 1, 1 + m,
  106.           n1 / j,
  107.           n2,
  108.           n3, 
  109.           scl- j / 100,
  110.           x,
  111.           y,
  112.           z, rx, ry, rz)
  113.         }
  114.     }
  115.  
  116.     const overts = verts.concat()
  117.     const leng = verts.length / 3
  118.  
  119.     bindBuffer(ARRAY_BUFFER, createBuffer())
  120.     bufferData(ARRAY_BUFFER, new Float32Array(verts), STATIC_DRAW)
  121.  
  122.     const vs = createShader(VERTEX_SHADER)
  123.     shaderSource(vs, vert)
  124.     compileShader(vs)
  125.  
  126.     const fs = createShader(FRAGMENT_SHADER)
  127.     const sp = createProgram()
  128.  
  129.     shaderSource(fs, frag)
  130.     compileShader(fs)
  131.     attachShader(sp, vs)
  132.     attachShader(sp, fs)
  133.     linkProgram(sp)
  134.     useProgram(sp)
  135.  
  136.     const vec = getAttribLocation(sp, 'vec')
  137.     vertexAttribPointer(vec, 3, FLOAT, false, 0, 0)
  138.     enableVertexAttribArray(vec)
  139.  
  140.     const matLoc = getUniformLocation(sp, 'mat')
  141.  
  142.     function rot(x, y, z) {
  143.       // https://wikimedia.org/api/rest_v1/media/math/render/svg/a8e16f4967571b7a572d1a19f3f6468512f9843e
  144.  
  145.       const sinA = Math.sin(x)
  146.       const cosA = Math.cos(x)
  147.       const sinB = Math.sin(y)
  148.       const cosB = Math.cos(y)
  149.       const sinY = Math.sin(z)
  150.       const cosY = Math.cos(z)
  151.  
  152.       m[0] = cosA * cosB
  153.       m[1] = cosA * sinB * sinY - sinA * cosY
  154.       m[2] = cosA * sinB * cosY + sinA * sinY
  155.       m[3] = 0
  156.  
  157.       m[4] = sinA * cosB
  158.       m[5] = sinA * sinB * sinY + cosA * cosY
  159.       m[6] = sinA * sinB * cosY - cosA * sinY
  160.       m[7] = 0
  161.  
  162.       m[8] = -sinB
  163.       m[9] = cosB * sinY
  164.       m[10] = cosB * cosY
  165.       m[11] = m[12] = m[13] = 0
  166.       m[15] = 1
  167.  
  168.       uniformMatrix4fv(matLoc, false, m)
  169.     }
  170.  
  171.     onresize = () => {
  172.       const {
  173.         canvas
  174.       } = gl
  175.       const size = Math.min(innerWidth, innerHeight) - 20
  176.       canvas.width = canvas.height = size
  177.       viewport(0, 0, size, size)
  178.     }
  179.  
  180.     onresize()
  181.  
  182.     let rx = 0, ry = 0, rz = 0
  183.     let t = 0;
  184.     function loop() {
  185.       // rx += 0.01
  186.       // ry += 0.01
  187.       rz += 0.01
  188.       rot(rx, ry, rz)
  189.  
  190.       disable(DEPTH_TEST)
  191.       enable(BLEND)
  192.       blendFunc(SRC_ALPHA, ONE_MINUS_SRC_ALPHA)
  193.       clearColor(0, 0, 0, 1)
  194.       clear(COLOR_BUFFER_BIT)
  195.  
  196.       for (let i = 0; i < verts.length; i += 3) {
  197.         let tof = t + i / 50000;  
  198.         verts[i] = Math.sin(tof) * overts[i]
  199.         verts[i + 1] = Math.sin(tof) * overts[i + 1]
  200.         verts[i + 2] = Math.sin(tof) * overts[i + 2]
  201.       }
  202.  
  203.       t += 0.01
  204.  
  205.       bufferSubData(ARRAY_BUFFER, 0, new Float32Array(verts))
  206.       drawArrays(POINTS, 0, leng)
  207.       window.requestAnimationFrame(loop)
  208.     }
  209.     loop()
  210.   }
  211. })()

Forked a recent snippet using supershapes for #genuary2022 – prompt was space…

Speed Coded Cosine Dither

  1. d = document
  2. b = d.body
  3. wh = 300
  4. hs = wh / 2
  5. S = 1.5
  6. with(
  7.   b.appendChild(Object.assign(
  8.     d.createElement`canvas`, {
  9.       width: wh, height: wh
  10.     })).getContext`2d`) {
  11.  
  12.   canvas.style.transformOrigin = '0 0'
  13.   canvas.style.transform = `scale(${S})`
  14.   canvas.style.imageRendering = 'pixelated'
  15.  
  16.   fillStyle = 'gray'
  17.  
  18.   fillRect(0, 0, wh, wh)
  19.   shadowBlur = 80
  20.   shadowColor = 'black';
  21.   shadowOffsetY = 20
  22.   for (let i = 0; i < 70; i++) {
  23.     save()
  24.     translate(hs, hs)
  25.     rotate(i / 33)
  26.     scale(1 - i / 100, 1)
  27.     translate(-hs, -hs)
  28.     fillStyle = `hsl(${i << 2}, 50%, 50%)`
  29.     beginPath()
  30.     arc(hs, hs, hs * .8, 0, 7)
  31.     fill()
  32.     restore()
  33.     shadowColor = 'transparent'
  34.     shadowBlur = 0
  35.   }
  36.  
  37.   C = Object.assign(d.createElement`canvas`, {
  38.     width: wh, height: wh
  39.   }).getContext('2d')
  40.   C.drawImage(canvas, 0, 0);
  41.  
  42.   im = getImageData(0, 0, wh, wh);
  43.  
  44.   p = im.data
  45.   size = wh * wh * 4
  46.  
  47.   modd = Math.random() * 5550
  48.   for (let i = 0; i < size; i += 4) {
  49.     if (Math.random() < 0.0001) modd = Math.random() * 5550
  50.     M = Math.cos(i % modd) * 255
  51.     p[i] = M < p[i] ? 255 : 0;
  52.     p[i + 1] = M < p[i + 1] ? 255 : 0;
  53.     p[i + 2] = M < p[i + 2] ? 255 : 0;
  54.   }
  55.  
  56.   putImageData(im, 0, 0);
  57.   globalCompositeOperation = 'hard-light'
  58.   drawImage(C.canvas, 0, 0);
  59. }

Some speed-coded canvas stuff with a dither inspired by #genuary2022

WebGL Hemisphere Lines

  1. (() => {
  2.   const m = new Float32Array([
  3.     0, 0, 0, 0, 
  4.     0, 0, 0, 0, 
  5.     0, 0, 0, 0, 
  6.     0, 0, 0, 0])
  7.  
  8.   const pointSize = 4; 
  9.  
  10.   const vert = `
  11.     attribute vec3 vec;
  12.     uniform mat4 mat;
  13.     void main(void) {
  14.       gl_Position = mat * vec4(vec, 1.0);
  15.       gl_PointSize = ${pointSize}.0;
  16.     }
  17.   `
  18.  
  19.   const frag = `
  20.     void main(void) {
  21.       gl_FragColor = vec4(1., 1., 1., .5);
  22.     }
  23.   `
  24.  
  25.   document.body.style.background = '#232323'
  26.   const gl = document.body
  27.     .appendChild(document.createElement('canvas'))
  28.     .getContext('webgl', {
  29.       preserveDrawingBuffer: true,
  30.       powerPreference: 'high-performance'
  31.     })
  32.  
  33.   Object.assign(gl.canvas.style, {
  34.     position: 'absolute',
  35.     left: '50%',
  36.     top: '50%',
  37.     transform: 'translate(-50%, -50%)',
  38.     outline: '1px solid gray'
  39.   })
  40.  
  41.   with (gl) {
  42.     const NUM = 2000
  43.     const radius = 0.7
  44.     const verts = []
  45.  
  46.     for (let i = 0; i < NUM; i += 3) {
  47.       let xp = Math.random() * 2 - 1
  48.       let yp = Math.random() * 2 - 1
  49.       let zp = i / NUM; 
  50.       let dist = Math.sqrt(xp * xp + yp * yp + zp * zp)
  51.       // normalize and scale x,y,z
  52.       verts[i] = (xp / dist) * radius
  53.       verts[i + 1] = (yp / dist) * radius
  54.       verts[i + 2] = (zp / dist) * radius
  55.     }
  56.     const overts = verts.concat()
  57.     const leng = verts.length / 3
  58.  
  59.     bindBuffer(ARRAY_BUFFER, createBuffer())
  60.     bufferData(ARRAY_BUFFER, new Float32Array(verts), STATIC_DRAW)
  61.  
  62.     const vs = createShader(VERTEX_SHADER)
  63.     shaderSource(vs, vert)
  64.     compileShader(vs)
  65.  
  66.     const fs = createShader(FRAGMENT_SHADER)
  67.     const sp = createProgram()
  68.  
  69.     shaderSource(fs, frag)
  70.     compileShader(fs)
  71.     attachShader(sp, vs)
  72.     attachShader(sp, fs)
  73.     linkProgram(sp)
  74.     useProgram(sp)
  75.  
  76.     const vec = getAttribLocation(sp, 'vec')
  77.     vertexAttribPointer(vec, 3, FLOAT, false, 0, 0)
  78.     enableVertexAttribArray(vec)
  79.  
  80.     const matLoc = getUniformLocation(sp, 'mat')
  81.  
  82.     function rot(x, y, z) {
  83.       // https://wikimedia.org/api/rest_v1/media/math/render/svg/a8e16f4967571b7a572d1a19f3f6468512f9843e
  84.  
  85.       const sinA = Math.sin(x)
  86.       const cosA = Math.cos(x)
  87.       const sinB = Math.sin(y)
  88.       const cosB = Math.cos(y)
  89.       const sinY = Math.sin(z)
  90.       const cosY = Math.cos(z)
  91.  
  92.       m[0] = cosA * cosB
  93.       m[1] = cosA * sinB * sinY - sinA * cosY
  94.       m[2] = cosA * sinB * cosY + sinA * sinY
  95.       m[3] = 0
  96.  
  97.       m[4] = sinA * cosB
  98.       m[5] = sinA * sinB * sinY + cosA * cosY
  99.       m[6] = sinA * sinB * cosY - cosA * sinY
  100.       m[7] = 0
  101.  
  102.       m[8] = -sinB
  103.       m[9] = cosB * sinY
  104.       m[10] = cosB * cosY
  105.       m[11] = m[12] = m[13] = 0
  106.       m[15] = 1
  107.  
  108.       uniformMatrix4fv(matLoc, false, m)
  109.     }
  110.  
  111.     onresize = () => {
  112.       const { canvas } = gl
  113.       const size = Math.min(innerWidth, innerHeight) - 20
  114.       canvas.width = canvas.height = size
  115.       viewport(0, 0, size, size)
  116.     }
  117.  
  118.     onresize()
  119.  
  120.     let rx = 0
  121.     let ry = 0
  122.     let rz = 0
  123.     let t = 0
  124.  
  125.     function loop() {
  126.       rx += 0.005
  127.       ry += 0.005
  128.       rot(rx, ry, rz)
  129.  
  130.       disable(DEPTH_TEST)
  131.       enable(BLEND)
  132.       blendFunc(SRC_ALPHA, ONE_MINUS_SRC_ALPHA)
  133.  
  134.       clearColor(0, 0, 0, 1)
  135.  
  136.       clear(COLOR_BUFFER_BIT)
  137.  
  138.       for (let i = 0; i < verts.length; i += 3) {
  139.         let tof = i * 0.001 + t;
  140.         verts[i] = Math.sin(tof) * overts[i]
  141.         verts[i + 1] = Math.sin(tof) * overts[i + 1]
  142.         verts[i + 2] = Math.sin(tof) * overts[i + 2]
  143.       }
  144.  
  145.       t += 0.005
  146.  
  147.       bufferSubData(ARRAY_BUFFER, 0, new Float32Array(verts))
  148.  
  149.       drawArrays(LINES, 0, leng)
  150.       drawArrays(POINTS, 0, leng)
  151.       window.requestAnimationFrame(loop)
  152.     }
  153.     loop()
  154.   }
  155. })()

WebGL lines and points…

Canvas Blur Repeatedly

  1. document.body.style.margin = 0
  2. const canvas = document.body.appendChild(
  3.   document.createElement('canvas')
  4. )
  5. const c = canvas.getContext('2d')
  6.  
  7. function clear() {
  8.   c.fillStyle = 'rgb(95 182 209)'
  9.   c.fillRect(0, 0, innerWidth, innerHeight)
  10. }
  11.  
  12. function resize() {
  13.   canvas.width = innerWidth
  14.   canvas.height = innerHeight
  15.   clear()
  16. }
  17. resize()
  18.  
  19. addEventListener('resize', resize)
  20.  
  21. function loop() {
  22.   const size = Math.min(innerWidth / 2, 
  23.   innerHeight / 2)
  24.   const halfSize = size / 2
  25.  
  26.   c.filter = 'blur(50px)'
  27.   c.fillStyle = 'rgba(0, 0, 0, 0.02)'
  28.   c.fillRect(
  29.     innerWidth / 2 - halfSize, 
  30.     innerHeight / 2 - halfSize,
  31.     size, size)
  32.  
  33.   requestAnimationFrame(loop)
  34. }
  35. loop()

Apply a blur filter to a rectangle over and over again…

Browser support at the time of the post is still lacking – and there are some interesting rendering differences between Chrome and Firefox:

Firefox on left and Chrome on right…

As nice as it would be to be able to use these I don’t recommend it yet. Using a GLSL shader is the way to go for now…

Speed Coded Mouse Toy

  1. d = document
  2. b = d.body
  3. b.style.margin = 0
  4. with (b.appendChild(
  5.   Object.assign(d.createElement`canvas`, {
  6.     width: innerWidth,
  7.     height: innerHeight,
  8.   })
  9. ).getContext`2d`) {
  10.   mx = 0
  11.   my = 0
  12.  
  13.   onresize = _ => {
  14.     canvas.width = innerWidth
  15.     canvas.height = innerHeight
  16.   }
  17.  
  18.   onpointermove = e => {
  19.     mx = e.clientX, 
  20.     my = e.clientY
  21.   }
  22.  
  23.   r = 0
  24.   loop = () => {
  25.     save()
  26.     globalCompositeOperation = 'hard-light'
  27.     translate(innerWidth / 2, innerHeight / 2)
  28.  
  29.     rotate(r += .02)
  30.     translate(-innerWidth / 2, -innerHeight / 2)
  31.  
  32.     fillStyle = 'rgba(55, 55, 55, .01)'
  33.     fillRect(0, 0, innerWidth, innerHeight)
  34.  
  35.     fillStyle = 'rgba(116, 196, 221, .02)'
  36.     fillRect(0, my, innerWidth, 20);
  37.  
  38.     fillStyle = 'rgba(255, 255, 255, .02)'
  39.     fillRect(mx, 0, 20, innerHeight);
  40.  
  41.     fillStyle = 'rgba(0, 0, 0, .03)'
  42.     fillRect(0, innerHeight - my, innerWidth, 20);
  43.  
  44.     fillStyle = 'rgba(116, 196, 221, .02)'
  45.     fillRect(innerWidth - mx, 0, 20, innerHeight);
  46.     restore()
  47.     requestAnimationFrame(loop)
  48.   }
  49.   loop()
  50. }

Speed coded mouse toy…

snippet.zone ~ 2021-24 /// {s/z}