Skip to content

Instantly share code, notes, and snippets.

@tompng
Created August 18, 2024 06:32
Show Gist options
  • Save tompng/d72141fecda9c9ce7c163d251e424c0c to your computer and use it in GitHub Desktop.
Save tompng/d72141fecda9c9ce7c163d251e424c0c to your computer and use it in GitHub Desktop.
<body>
<script>
const width = 1600
const height = 900
function f(x) {
return 0.48+(-Math.cos(12*x+3*Math.sin(6*x-4)/4-1)*(1.1-x)+(3*x-(2*x-1)**2)*0.5)/20
}
function g(x, a) {
return (
Math.sin(12.4*x-a)+Math.sin(14.3*x+a)+
Math.sin(11.3*x-1.5*a)+Math.sin(9.1*x+1.5*a)+
Math.sin(10.2*x-2*a)+Math.sin(13.1*x+2*a)+
Math.sin(16.7*x-2.5*a)+Math.sin(15.7*x+2.5*a)
)/80
}
function fillWave(ctx, f) {
ctx.beginPath()
ctx.moveTo(0, 1)
ctx.lineTo(0, f(0))
for (let i = 1; i <= width; i ++) {
const x = i / width
ctx.lineTo(x, f(x))
}
ctx.lineTo(1, 1)
ctx.fill()
}
function colorMix(a, b, t) {
return a.map((x, i) => x * (1 - t) + b[i] * t)
}
function colorToString(color) {
return `rgb(${color.map(x => Math.round(x)).join(',')})`
}
white = [248,248,248]
blue = [65,157,191]
tmax = 1
function lineSegment(ctx, x, y, w) {
const cw = 0.025
const offset = 0.01
ctx.save()
ctx.rotate(0.18)
for (let i = 0; i < w; i++) {
ctx.fillStyle = colorToString(colorMix(white, blue, tmax * Math.random()))
ctx.fillRect(cw * (x + i), (2 * cw + offset) * y, cw + (i == w - 1 ? 0 : 1/width), 2 * cw)
}
ctx.restore()
}
canvas = document.createElement('canvas')
document.body.appendChild(canvas)
canvas.width = width
canvas.height = height
ctx = canvas.getContext('2d')
ctx.scale(width, width)
ctx.fillStyle = colorToString(white)
ctx.fillRect(0, 0, 1, 1)
tmax = 0.4
lineSegment(ctx, 8, 0, 8)
lineSegment(ctx, 20, 0, 8)
ctx.fillStyle = colorToString(colorMix(white, blue, 0.1))
fillWave(ctx, x => 0.16 + g(x, 1))
tmax = 0.5
lineSegment(ctx, 30, 0, 4)
lineSegment(ctx, 8, 1, 3)
lineSegment(ctx, 17, 1, 10)
lineSegment(ctx, 8, 2, 12)
lineSegment(ctx, 22, 2, 4)
ctx.fillStyle = colorToString(colorMix(white, blue, 0.3))
fillWave(ctx, x => 0.24 + g(x, 10))
tmax = 0.8
lineSegment(ctx, 28, 1, 5)
lineSegment(ctx, 27, 2, 8)
lineSegment(ctx, 38, 2, 8)
lineSegment(ctx, 8, 3, 8)
ctx.fillStyle = colorToString(colorMix(white, blue, 0.5))
fillWave(ctx, x => 0.32 + g(x, 100))
tmax = 1
lineSegment(ctx, 19, 3, 5)
lineSegment(ctx, 26, 3, 8)
lineSegment(ctx, 8, 4, 5)
lineSegment(ctx, 20, 4, 10)
lineSegment(ctx, 8, 5, 8)
lineSegment(ctx, 13, 6, 9)
ctx.fillStyle = colorToString(colorMix(white, blue, 0.7))
fillWave(ctx, x => 0.4 + g(x, 1000))
tmax = 1
lineSegment(ctx, 33, 4, 5)
lineSegment(ctx, 21, 5, 9)
lineSegment(ctx, 34, 5, 5)
lineSegment(ctx, 8, 6, 4)
lineSegment(ctx, 16, 6, 3)
lineSegment(ctx, 26, 6, 8)
lineSegment(ctx, 8, 7, 6)
lineSegment(ctx, 17, 7, 7)
lineSegment(ctx, 30, 7, 8)
lineSegment(ctx, 14, 8, 5)
lineSegment(ctx, 25, 8, 5)
ctx.fillStyle = colorToString(blue)
fillWave(ctx, f)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment