// The moving parts of the Eyes pattern. Each eye lives a cycle: it
// picks a free spot and a size, appears there as a line (its closed
// lids' seam), opens, stays open 10 to 60 s blinking and looking about,
// slowly closes back to a line, and the line fades. Then it starts over
// somewhere else. While open it sometimes looks at the pointer, in
// spells, rather than always.
//
// Times are in the field's clock (seconds times the speed setting),
// positions in units of the canvas's shorter side, y down, as the
// shader has them. They are handed over as separate vec4 uniforms (Qt's
// ShaderEffect can't pass arrays), eye0 to eye11 and look0 to look11:
// eyeN: x, y, radius (half the eye's width), openness 0-1 (blinks included)
// lookN: gaze x, gaze y (-1 to 1, the iris's full travel), shade 0-1, fade 0-1
// Unused ones are zero.
export const MAX_EYES = 24
const rand = (lo, hi) => lo + Math.random() * (hi - lo)
const TAU = 2 * Math.PI
const smooth = t => t * t * (3 - 2 * t)
// The life cycle, in order, with how long each part takes (seconds).
export const TIMES = {
wait: [0.5, 3], // unseen, before taking a spot
appear: 0.8, // the line fading in
opening: 1.6,
open: [10, 60],
closing: 3.2, // slowly
vanish: 1.5, // the line fading out
blink: 0.15,
blinkEvery: [2, 9],
glance: [0.6, 3.5], // between saccades
attention: [1.5, 6], // a spell of looking at the pointer
}
// The chance, at each saccade, of a spell of looking at the pointer.
export const ATTENTION = 0.3
// The radius range for n eyes: the largest `ratio` times the smallest,
// both smaller the more eyes there are, so they have room.
export function sizes(n, ratio) {
const big = Math.min(0.3, 0.55 / Math.sqrt(Math.max(1, n)))
return [big / Math.max(1, ratio), big]
}
// The extent an eye takes up around its centre, to keep eyes apart.
const reach = e => e.r * 1.15
function lookAround() {
if (Math.random() < 0.25) return [0, 0]
const a = Math.random() * TAU
const m = 0.35 + 0.6 * Math.sqrt(Math.random())
return [m * Math.cos(a), m * Math.sin(a)]
}
export class Eyes {
constructor() {
this.list = []
this.settled = null
}
// A fresh eye, unseen, waiting `delay` before it takes a spot.
make(delay) {
return {
state: "wait",
timer: delay ?? rand(...TIMES.wait),
x: 0,
y: 0,
r: 0,
open: 0,
fade: 0,
gaze: [0, 0],
target: [0, 0],
shade: Math.random(),
blink: -1,
nextBlink: rand(...TIMES.blinkEvery),
nextGlance: rand(...TIMES.glance),
attending: 0,
}
}
// Somewhere free for an eye of a random size, or false if none was
// found (it waits a moment and tries again).
place(e, extent, n, ratio) {
const [lo, hi] = sizes(n, ratio)
for (let tries = 0; tries < 40; tries++) {
// Log-uniform, so small and large are equally common by ratio.
const r = lo * Math.pow(hi / lo, Math.random())
const mx = r * 1.15
const my = r * 0.75
const x = rand(mx, Math.max(mx, extent[0] - mx))
const y = rand(my, Math.max(my, extent[1] - my))
const clear = this.list.every(
o => o === e || o.state === "wait" || Math.hypot(o.x - x, o.y - y) > (reach(o) + r * 1.15) * 1.1,
)
if (clear) {
Object.assign(e, { x, y, r })
return true
}
}
return false
}
// `pointer`: { x, y, active } in shader units, or null.
step(dt, extent, n, ratio, pointer = null) {
n = Math.min(MAX_EYES, Math.max(1, Math.round(n)))
this.settled = null
// The first ones staggered, so they don't all open together.
while (this.list.length < n) this.list.push(this.make(this.list.length ? rand(0, 4) : 0.3))
// Too many: the extra ones close now, and are dropped once gone.
for (let i = n; i < this.list.length; i++) {
const e = this.list[i]
if (e.state === "open") (e.state = "closing"), (e.timer = TIMES.closing)
else if (e.state === "wait") e.drop = true
}
const now = pointer && pointer.active ? pointer : null
const fresh = now && !this.pointerWas
this.pointerWas = !!now
for (const e of this.list) this.live(e, dt, extent, n, ratio, now, fresh)
this.list = this.list.filter(e => !e.drop)
}
live(e, dt, extent, n, ratio, pointer, fresh) {
e.timer -= dt
switch (e.state) {
case "wait":
if (e.timer > 0) break
if (this.list.indexOf(e) >= n) {
e.drop = true
break
}
if (!this.place(e, extent, n, ratio)) {
e.timer = 0.5
break
}
Object.assign(e, { state: "appear", timer: TIMES.appear, open: 0, fade: 0, shade: Math.random() })
e.gaze = [0, 0]
e.target = [0, 0]
break
case "appear":
e.fade = smooth(Math.min(1, 1 - e.timer / TIMES.appear))
if (e.timer <= 0) (e.state = "opening"), (e.timer = TIMES.opening), (e.fade = 1)
break
case "opening":
e.open = smooth(Math.min(1, 1 - e.timer / TIMES.opening))
if (e.timer <= 0) (e.state = "open"), (e.timer = rand(...TIMES.open)), (e.open = 1)
break
case "open":
if (e.timer <= 0) (e.state = "closing"), (e.timer = TIMES.closing)
break
case "closing":
e.open = smooth(Math.max(0, e.timer / TIMES.closing))
if (e.timer <= 0) (e.state = "vanish"), (e.timer = TIMES.vanish), (e.open = 0)
break
case "vanish":
e.fade = smooth(Math.max(0, e.timer / TIMES.vanish))
if (e.timer <= 0) {
if (this.list.indexOf(e) >= n) e.drop = true
else Object.assign(e, this.make(), { shade: e.shade })
}
break
}
if (e.state === "open" || e.state === "opening" || e.state === "closing") this.look(e, dt, pointer, fresh)
// Blinks while open, each its own time: shut and open in 150 ms,
// now and then twice.
if (e.state === "open") {
if (e.blink >= 0) {
e.blink += dt
if (e.blink >= TIMES.blink) {
e.blink = -1
e.nextBlink = Math.random() < 0.12 ? 0.12 : rand(...TIMES.blinkEvery)
}
} else if ((e.nextBlink -= dt) <= 0) e.blink = 0
} else e.blink = -1
}
look(e, dt, pointer, fresh) {
const toPointer = () => {
const dx = pointer.x - e.x
const dy = pointer.y - e.y
const k = 1 / Math.sqrt(dx * dx + dy * dy + 2.25 * e.r * e.r)
return [dx * k, dy * k]
}
// A pointer that has just turned up catches some eyes' attention.
if (fresh && e.state === "open" && Math.random() < 0.5) e.nextGlance = Math.min(e.nextGlance, rand(0.1, 0.6))
if (e.attending > 0) {
e.attending -= dt
if (!pointer) e.attending = 0
}
if ((e.nextGlance -= dt) <= 0) {
e.nextGlance = rand(...TIMES.glance)
if (pointer && e.attending <= 0 && Math.random() < (fresh ? 0.6 : ATTENTION)) e.attending = rand(...TIMES.attention)
if (e.attending <= 0 || !pointer) e.target = lookAround()
}
// Following the pointer smoothly while attending; otherwise quick
// saccades to each new target.
let tau = 0.035
if (e.attending > 0 && pointer) {
e.target = toPointer()
tau = 0.08
}
const k = 1 - Math.exp(-dt / tau)
e.gaze = [e.gaze[0] + (e.target[0] - e.gaze[0]) * k, e.gaze[1] + (e.target[1] - e.gaze[1]) * k]
}
// Openness with any blink taken out.
openness(e) {
if (e.blink < 0) return e.open
const t = e.blink / TIMES.blink
return e.open * (1 - Math.min(1, 2 * Math.min(t, 1 - t) * 1.25))
}
// For reduced motion: n eyes open and looking ahead, placed once and
// kept while the count, ratio and canvas shape stay the same.
settle(extent, n, ratio) {
n = Math.min(MAX_EYES, Math.max(1, Math.round(n)))
const key = `${n} ${ratio} ${extent[0].toFixed(3)} ${extent[1].toFixed(3)}`
if (this.settled === key) return
this.settled = key
this.list = []
for (let i = 0; i < n; i++) {
const e = this.make(0)
if (!this.place(e, extent, n, ratio)) continue
Object.assign(e, { state: "open", timer: rand(...TIMES.open), open: 1, fade: 1 })
this.list.push(e)
}
}
pack() {
const out = {}
for (let i = 0; i < MAX_EYES; i++) {
const e = this.list[i]
const shown = e && e.state !== "wait"
out[`eye${i}`] = shown ? [e.x, e.y, e.r, this.openness(e)] : [0, 0, 0, 0]
out[`look${i}`] = shown ? [e.gaze[0], e.gaze[1], e.shade, e.fade] : [0, 0, 0, 0]
}
return { ...out, count: Math.min(MAX_EYES, this.list.length) }
}
}
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
⭐
Log in to mark this repository.
8.4 KiB · text
History
11accb3