annotate web/timeline/viewstate.ts @ 2419:e3af0ac507c8

new exposure-finder algorithm
author drewp@bigasterisk.com
date Tue, 21 May 2024 14:08:17 -0700
parents 4556eebe5d73
children 06da5db2fafe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
1 import * as ko from "knockout";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
2 import * as d3 from "d3";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
3 import debug from "debug";
1733
82c6e4f75221 extract viewstate
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
4
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
5 const log = debug("viewstate");
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
6 export class ViewState {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
7 zoomSpec: {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
8 duration: ko.Observable<number>; // current song duration
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
9 t1: ko.Observable<number>;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
10 t2: ko.Observable<number>;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
11 };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
12 cursor: { t: ko.Observable<number> };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
13 mouse: { pos: ko.Observable<Vector> };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
14 width: ko.Observable<number>;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
15 coveredByDiagramTop: ko.Observable<number>;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
16 audioY: ko.Observable<number>;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
17 audioH: ko.Observable<number>;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
18 zoomedTimeY: ko.Observable<number>;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
19 zoomedTimeH: ko.Observable<number>;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
20 rowsY: ko.Observable<number>;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
21 fullZoomX: d3.ScaleLinear<number, number>;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
22 zoomInX: d3.ScaleLinear<number, number>;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
23 zoomAnimSec: number;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
24 constructor() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
25 // caller updates all these observables
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
26 this.zoomSpec = {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
27 duration: ko.observable(100), // current song duration
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
28 t1: ko.observable(0),
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
29 t2: ko.observable(100),
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
30 };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
31 this.cursor = { t: ko.observable(20) }; // songTime
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
32 this.mouse = { pos: ko.observable($V([0, 0])) };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
33 this.width = ko.observable(500);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
34 this.coveredByDiagramTop = ko.observable(0); // page coords
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
35 // all these are relative to #coveredByDiagram:
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
36 this.audioY = ko.observable(0);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
37 this.audioH = ko.observable(0);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
38 this.zoomedTimeY = ko.observable(0);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
39 this.zoomedTimeH = ko.observable(0);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
40 this.rowsY = ko.observable(0);
1733
82c6e4f75221 extract viewstate
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
41
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
42 this.fullZoomX = d3.scaleLinear();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
43 this.zoomInX = d3.scaleLinear();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
44
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
45 this.zoomAnimSec = 0.1;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
46
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
47 ko.computed(this.maintainZoomLimitsAndScales.bind(this));
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
48 }
1733
82c6e4f75221 extract viewstate
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
49
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
50 setWidth(w: any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
51 this.width(w);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
52 this.maintainZoomLimitsAndScales(); // before other handlers run
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
53 }
1733
82c6e4f75221 extract viewstate
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
54
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
55 maintainZoomLimitsAndScales() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
56 // not for cursor updates
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
57
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
58 if (this.zoomSpec.t1() < 0) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
59 this.zoomSpec.t1(0);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
60 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
61 if (this.zoomSpec.duration() && this.zoomSpec.t2() > this.zoomSpec.duration()) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
62 this.zoomSpec.t2(this.zoomSpec.duration());
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
63 }
1733
82c6e4f75221 extract viewstate
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
64
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
65 const rightPad = 5; // don't let time adjuster fall off right edge
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
66 this.fullZoomX.domain([0, this.zoomSpec.duration()]);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
67 this.fullZoomX.range([0, this.width() - rightPad]);
1733
82c6e4f75221 extract viewstate
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
68
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
69 this.zoomInX.domain([this.zoomSpec.t1(), this.zoomSpec.t2()]);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
70 this.zoomInX.range([0, this.width() - rightPad]);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
71 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
72
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
73 latestMouseTime(): number {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
74 return this.zoomInX.invert(this.mouse.pos().e(1));
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
75 }
1733
82c6e4f75221 extract viewstate
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
76
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
77 onMouseWheel(deltaY: any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
78 const zs = this.zoomSpec;
1733
82c6e4f75221 extract viewstate
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
79
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
80 const center = this.latestMouseTime();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
81 const left = center - zs.t1();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
82 const right = zs.t2() - center;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
83 const scale = Math.pow(1.005, deltaY);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
84
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
85 zs.t1(center - left * scale);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
86 zs.t2(center + right * scale);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
87 log("view to", ko.toJSON(this));
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
88 }
1733
82c6e4f75221 extract viewstate
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
89
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
90 frameCursor() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
91 const zs = this.zoomSpec;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
92 const visSeconds = zs.t2() - zs.t1();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
93 const margin = visSeconds * 0.4;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
94 // buggy: really needs t1/t2 to limit their ranges
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
95 if (this.cursor.t() < zs.t1() || this.cursor.t() > zs.t2() - visSeconds * 0.6) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
96 const newCenter = this.cursor.t() + margin;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
97 this.animatedZoom(newCenter - visSeconds / 2, newCenter + visSeconds / 2, this.zoomAnimSec);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
98 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
99 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
100 frameToEnd() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
101 this.animatedZoom(this.cursor.t() - 2, this.zoomSpec.duration(), this.zoomAnimSec);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
102 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
103 frameAll() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
104 this.animatedZoom(0, this.zoomSpec.duration(), this.zoomAnimSec);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
105 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
106 animatedZoom(newT1: number, newT2: number, secs: number) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
107 const fps = 30;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
108 const oldT1 = this.zoomSpec.t1();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
109 const oldT2 = this.zoomSpec.t2();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
110 let lastTime = 0;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
111 for (let step = 0; step < secs * fps; step++) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
112 const frac = step / (secs * fps);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
113 ((frac) => {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
114 const gotoStep = () => {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
115 this.zoomSpec.t1((1 - frac) * oldT1 + frac * newT1);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
116 return this.zoomSpec.t2((1 - frac) * oldT2 + frac * newT2);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
117 };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
118 const delay = frac * secs * 1000;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
119 setTimeout(gotoStep, delay);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
120 lastTime = delay;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
121 })(frac);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
122 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
123 setTimeout(() => {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
124 this.zoomSpec.t1(newT1);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
125 return this.zoomSpec.t2(newT2);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
126 }, lastTime + 10);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
127 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1771
diff changeset
128 }