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

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…

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…

WebGL Tangled Supershape Points

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

An interesting accident while playing around with Supershapes and WebgGL points…

WebGL Particles in Sphere

  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 = Math.max(Math.min(30, ~~(innerWidth / 70)), 3)
  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., .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.   with (gl) {
  42.     const NUM = 4000
  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 = Math.random() * 2 - 1
  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.     }
  57.     const overts = verts.concat()
  58.     const leng = verts.length / 3
  59.  
  60.     bindBuffer(ARRAY_BUFFER, createBuffer())
  61.     bufferData(ARRAY_BUFFER, new Float32Array(verts), STATIC_DRAW)
  62.  
  63.     const vs = createShader(VERTEX_SHADER)
  64.     shaderSource(vs, vert)
  65.     compileShader(vs)
  66.  
  67.     const fs = createShader(FRAGMENT_SHADER)
  68.     const sp = createProgram()
  69.  
  70.     shaderSource(fs, frag)
  71.     compileShader(fs)
  72.     attachShader(sp, vs)
  73.     attachShader(sp, fs)
  74.     linkProgram(sp)
  75.     useProgram(sp)
  76.  
  77.     const vec = getAttribLocation(sp, 'vec')
  78.     vertexAttribPointer(vec, 3, FLOAT, false, 0, 0)
  79.     enableVertexAttribArray(vec)
  80.  
  81.     const matLoc = getUniformLocation(sp, 'mat')
  82.  
  83.     function rot(x, y, z) {
  84.       // https://wikimedia.org/api/rest_v1/media/math/render/svg/a8e16f4967571b7a572d1a19f3f6468512f9843e
  85.  
  86.       const sinA = Math.sin(x)
  87.       const cosA = Math.cos(x)
  88.       const sinB = Math.sin(y)
  89.       const cosB = Math.cos(y)
  90.       const sinY = Math.sin(z)
  91.       const cosY = Math.cos(z)
  92.  
  93.       m[0] = cosA * cosB
  94.       m[1] = cosA * sinB * sinY - sinA * cosY
  95.       m[2] = cosA * sinB * cosY + sinA * sinY
  96.       m[3] = 0
  97.  
  98.       m[4] = sinA * cosB
  99.       m[5] = sinA * sinB * sinY + cosA * cosY
  100.       m[6] = sinA * sinB * cosY - cosA * sinY
  101.       m[7] = 0
  102.  
  103.       m[8] = -sinB
  104.       m[9] = cosB * sinY
  105.       m[10] = cosB * cosY
  106.       m[11] = m[12] = m[13] = 0
  107.       m[15] = 1
  108.  
  109.       uniformMatrix4fv(matLoc, false, m)
  110.     }
  111.  
  112.     onresize = () => {
  113.       const { canvas } = gl
  114.       const size = Math.min(innerWidth, innerHeight) - 20
  115.       canvas.width = canvas.height = size
  116.       viewport(0, 0, size, size)
  117.     }
  118.  
  119.     onresize()
  120.  
  121.     let rx = 0,
  122.       ry = 0,
  123.       rz = 0
  124.     let t = 0
  125.  
  126.     function loop() {
  127.       rx += 0.01
  128.       ry += 0.01
  129.       rot(rx, ry, rz)
  130.  
  131.       disable(DEPTH_TEST)
  132.       enable(BLEND)
  133.       blendFunc(SRC_ALPHA, ONE_MINUS_SRC_ALPHA)
  134.  
  135.       clearColor(0, 0, 0, 1)
  136.  
  137.       clear(COLOR_BUFFER_BIT)
  138.  
  139.       for (let i = 0; i < verts.length; i += 3) {
  140.         let tof = i * 0.001 + t;
  141.         verts[i] = Math.sin(tof) * overts[i]
  142.         verts[i + 1] = Math.sin(tof) * overts[i + 1]
  143.         verts[i + 2] = Math.sin(tof) * overts[i + 2]
  144.       }
  145.  
  146.       t += 0.01
  147.  
  148.       bufferSubData(ARRAY_BUFFER, 0, new Float32Array(verts))
  149.  
  150.       drawArrays(POINTS, 0, leng)
  151.       window.requestAnimationFrame(loop)
  152.     }
  153.     loop()
  154.   }
  155. })()

Playing around with animating points…

Responsive Radial Gradient Animation

  1. function rect(x1, y1, x2, y2, col, blur=.1) {
  2.   const dx = x1 - x2;
  3.   const dy = y1 - y2;
  4.   const dist = Math.sqrt(dx * dx + dy * dy);
  5.   return `radial-gradient(circle at ${x1}% ${y1}%, ${col} 0, ${col} ${dist}%, transparent ${dist +
  6.     blur}%)`;
  7. }
  8.  
  9. const NUM = 8; 
  10.  
  11. let rects = [];
  12. const colors = ['rgba(0, 0, 0, 0.2)', 'white', 'black'];
  13.  
  14. let x = 0;
  15. let y = 0;
  16. let t = 8;
  17. function loop() { 
  18.   rects = [];
  19.  
  20.   for (let i = 0; i < NUM; i++) { 
  21.     x = 50 + 30 * Math.sin(t + i / 2);
  22.     y = 50 + 30 * Math.cos(t * 1.5 * i / 10);
  23.     rects.push(rect(x, y, x + 5, y + 5, 'rgba(255, 255, 255, 1)', 1));
  24.     rects.push(rect(x, y, x + 5, y + 5, 'rgba(0, 0, 0, 0.4)', 
  25.       8 + 6 * Math.cos(y / 10)));
  26.   }
  27.   t += .04;
  28.   document.body.style.backgroundImage = rects.join(', ');
  29.   window.requestAnimationFrame(loop);
  30. }
  31. loop()
  32.  
  33. document.body.innerHTML += `
  34.   <style>
  35.     body, html {
  36.       height: 100%;
  37.       background: #ccc;
  38.       margin: 0;
  39.       background-repeat: no-repeat;
  40.       width: 100%;
  41.     }
  42.   </style>
  43. `;

Animated variation on yesterdays post – many animating circles with no divs or canvas, just radial-gradients…

// animation // css // dom // graphics // hacks // javascript // math
snippet.zone ~ 2021-24 /// {s/z}