annotate web/timeline/viewstate.ts @ 2439:06da5db2fafe

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