game-village-politics-frontend/app/diorama/svgpath.ts

167 lines
5.2 KiB
TypeScript
Raw Normal View History

// SVG path data as plain points, for drawing the figures with WebGL and moving them along
// the same paths the scene hands out as strings. Covers what the dioramas write: M L H V Q
// A Z, absolute and relative; curves and arcs are cut into short straight pieces.
import type { V2 } from './iso'
const TOKEN = /[MmLlHhVvQqAaZz]|[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?/g
/** Each subpath of `d` as a polyline; a closed subpath does not repeat its first point. */
export function polylines(d: string, pieces = 6): V2[][] {
const tokens = d.match(TOKEN) ?? []
const out: V2[][] = []
let line: V2[] = []
let x = 0
let y = 0
let i = 0
let command = 'M'
const num = () => Number(tokens[i++])
const end = () => {
if (line.length > 1) out.push(line)
line = []
}
while (i < tokens.length) {
if (/[a-zA-Z]/.test(tokens[i]!)) command = tokens[i++]!
const rel = command === command.toLowerCase()
const ox = rel ? x : 0
const oy = rel ? y : 0
switch (command.toUpperCase()) {
case 'M':
end()
x = ox + num()
y = oy + num()
line.push([x, y])
// Further pairs after a moveto are linetos.
command = rel ? 'l' : 'L'
break
case 'L':
x = ox + num()
y = oy + num()
line.push([x, y])
break
case 'H':
x = ox + num()
line.push([x, y])
break
case 'V':
y = oy + num()
line.push([x, y])
break
case 'Q': {
const cx = ox + num()
const cy = oy + num()
const ex = ox + num()
const ey = oy + num()
for (let k = 1; k <= pieces; k++) {
const t = k / pieces
const u = 1 - t
line.push([u * u * x + 2 * u * t * cx + t * t * ex, u * u * y + 2 * u * t * cy + t * t * ey])
}
x = ex
y = ey
break
}
case 'A': {
const rx = num()
const ry = num()
const rotation = num()
const large = num()
const sweep = num()
const ex = ox + num()
const ey = oy + num()
arc(line, x, y, rx, ry, (rotation * Math.PI) / 180, large !== 0, sweep !== 0, ex, ey, pieces * 3)
x = ex
y = ey
break
}
case 'Z': {
const first = line[0]
end()
if (first) [x, y] = first
break
}
default:
i++
}
}
end()
return out
}
/** An elliptical arc from (x1, y1) to (x2, y2), endpoint form as in SVG, as points after the start. */
function arc(out: V2[], x1: number, y1: number, rx: number, ry: number, phi: number, large: boolean, sweep: boolean, x2: number, y2: number, pieces: number) {
rx = Math.abs(rx)
ry = Math.abs(ry)
if (!rx || !ry) {
out.push([x2, y2])
return
}
const cos = Math.cos(phi)
const sin = Math.sin(phi)
const dx = (x1 - x2) / 2
const dy = (y1 - y2) / 2
const px = cos * dx + sin * dy
const py = -sin * dx + cos * dy
const grow = (px * px) / (rx * rx) + (py * py) / (ry * ry)
if (grow > 1) {
rx *= Math.sqrt(grow)
ry *= Math.sqrt(grow)
}
const num = rx * rx * ry * ry - rx * rx * py * py - ry * ry * px * px
const den = rx * rx * py * py + ry * ry * px * px
const k = (large === sweep ? -1 : 1) * Math.sqrt(Math.max(0, num / den))
const cpx = (k * rx * py) / ry
const cpy = (-k * ry * px) / rx
const cx = cos * cpx - sin * cpy + (x1 + x2) / 2
const cy = sin * cpx + cos * cpy + (y1 + y2) / 2
const angle = (ux: number, uy: number, vx: number, vy: number) => Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy)
const t1 = angle(1, 0, (px - cpx) / rx, (py - cpy) / ry)
let dt = angle((px - cpx) / rx, (py - cpy) / ry, (-px - cpx) / rx, (-py - cpy) / ry)
if (!sweep && dt > 0) dt -= Math.PI * 2
if (sweep && dt < 0) dt += Math.PI * 2
for (let n = 1; n <= pieces; n++) {
const t = t1 + (dt * n) / pieces
out.push([cx + rx * Math.cos(t) * cos - ry * Math.sin(t) * sin, cy + rx * Math.cos(t) * sin + ry * Math.sin(t) * cos])
}
}
/** A path to move along: the point at a fraction of its length, like SVG's animateMotion. */
export class Track {
private readonly pts: V2[]
private readonly at: number[]
readonly length: number
constructor(d: string) {
this.pts = polylines(d, 8).flat()
if (!this.pts.length) this.pts = [[0, 0]]
this.at = [0]
for (let i = 1; i < this.pts.length; i++) {
const [ax, ay] = this.pts[i - 1]!
const [bx, by] = this.pts[i]!
this.at.push(this.at[i - 1]! + Math.hypot(bx - ax, by - ay))
}
this.length = this.at[this.at.length - 1]!
}
point(fraction: number): V2 {
const target = Math.min(1, Math.max(0, fraction)) * this.length
let lo = 0
let hi = this.at.length - 1
while (hi - lo > 1) {
const mid = (lo + hi) >> 1
if (this.at[mid]! <= target) lo = mid
else hi = mid
}
const span = this.at[hi]! - this.at[lo]!
const t = span > 0 ? (target - this.at[lo]!) / span : 0
const [ax, ay] = this.pts[lo]!
const [bx, by] = this.pts[hi]!
return [ax + (bx - ax) * t, ay + (by - ay) * t]
}
/** Where it starts and where it ends, for which way a walker faces. */
get ends(): [V2, V2] {
return [this.pts[0]!, this.pts[this.pts.length - 1]!]
}
}