Last active
March 29, 2020 17:18
-
-
Save adekau/59d2935d3b52bffa61be37539fa2202f to your computer and use it in GitHub Desktop.
Llama using Javascript (Typescript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as M from 'mathjs'; | |
let points = []; | |
let pts = []; | |
let complexPts = []; | |
let coeffs = []; | |
let time = 0; | |
export class Llama { | |
public load() { | |
pts = await this.sampleSvgPoints(text, this.samples); | |
complexPts = pts.map(pt => ptToComplex(pt)); | |
coeffs = dft(complexPts); | |
} | |
private parameterize(cn: M.Complex[]) { | |
return async (t: number): Promise<M.Complex> => await this.sigma(k => | |
M.multiply(M.complex(cn[k - 1].re, cn[k - 1].im), M.exp(M.multiply(M.i, (k - 1) * t))), [1, this.samples]) as M.Complex; | |
} | |
private async sigma(exp: (k: number) => M.MathType, interval: [number, number]): Promise<M.MathType> { | |
let sum: M.MathType = 0; | |
for (let i = interval[0]; i <= interval[1]; i++) | |
sum = M.add(sum, exp(i)); | |
return sum; | |
} | |
public setup() { | |
createCanvas(800,600); | |
} | |
public draw(p) { | |
const pFn = this.parameterize(coeffs); | |
p.background(25); | |
p.translate(300, 200); | |
const pt = await pFn(time); | |
points.unshift(p.createVector(pt.re, pt.im)); | |
p.stroke(230); | |
p.noFill(); | |
p.beginShape(); | |
for (let i =0 ; i < points.length; i++) | |
p.vertex(points[i].x, points[i].y); | |
p.endShape(); | |
if (time > M.pi * 2) | |
points.pop(); | |
time += ((2 * M.pi) / pts.length); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full code for this is available as https://github.com/adekau/fourier