Files
@ e7e03c203c99
Branch filter:
Location: light9/web/Light9CursorCanvas.ts
e7e03c203c99
4.4 KiB
video/MP2T
resize cursor canvas for 400px tall spectros. fix canvas resolution code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | import debug from "debug";
import { css, html, LitElement, PropertyValues } from "lit";
import { customElement, property } from "lit/decorators.js";
import Sylvester from "sylvester";
import { line } from "./drawing";
import { Vector } from "./lib/sylvester";
const $V = Sylvester.Vector.create;
const log = debug("cursor");
export interface PlainViewState {
zoomSpec: { t1: () => number; t2: () => number };
fullZoomX: (t: number) => number;
zoomInX: (t: number) => number;
cursor: { t: () => number };
audioY: () => number;
audioH: () => number;
zoomedTimeY: () => number; // not what you think- it's the zone in between
zoomedTimeH: () => number;
mouse: { pos: () => Vector };
}
export interface PlainerViewState {
zoomSpec: { t1: number; t2: number };
fullZoomX: (t: number) => number;
zoomInX: (t: number) => number;
cursor: { t: number };
audioY: number;
audioH: number;
zoomedTimeY: number; // not what you think- it's the zone in between
zoomedTimeH: number;
mouse: { pos: Vector };
}
// For cases where you have a zoomed-out view on top of a zoomed-in view,
// overlay this element and it'll draw a time cursor on both views.
@customElement("light9-cursor-canvas")
export class Light9CursorCanvas extends LitElement {
cursorPath: null | {
top0: Vector;
top1: Vector;
mid0: Vector;
mid1: Vector;
mid2: Vector;
mid3: Vector;
bot0: Vector;
bot1: Vector;
} = null;
canvasEl!: HTMLCanvasElement;
ctx!: CanvasRenderingContext2D;
offsetWidth: any;
offsetHeight: any;
@property() viewState: PlainerViewState | null = null;
static styles = [
css`
:host {
display: inline-block;
}
canvas {
width: 100%;
height: 100%;
}
`,
];
resizeObserver!: ResizeObserver;
render() {
return html`<canvas></canvas>`;
}
updated(changedProperties: PropertyValues) {
if (changedProperties.has("viewState")) {
this.redrawCursor();
}
}
connectedCallback() {
super.connectedCallback();
this.resizeObserver = new ResizeObserver(this.onResize.bind(this));
this.resizeObserver.observe(this);
}
firstUpdated() {
this.canvasEl = this.shadowRoot!.firstElementChild as HTMLCanvasElement;
this.ctx = this.canvasEl.getContext("2d")!;
}
disconnectedCallback() {
this.resizeObserver.unobserve(this);
}
onResize() {
log('onResize', this.clientWidth, this.clientHeight);
if (!this.canvasEl) {
return;
}
this.canvasEl.width = this.clientWidth;
this.canvasEl.height = this.clientHeight;
this.redrawCursor();
}
redrawCursor() {
const vs = this.viewState;
if (!vs) {
return;
}
const xZoomedOut = vs.fullZoomX(vs.cursor.t);
const xZoomedIn = vs.zoomInX(vs.cursor.t);
this.cursorPath = {
top0: $V([xZoomedOut, vs.audioY]),
top1: $V([xZoomedOut, vs.audioY + vs.audioH]),
mid0: $V([xZoomedIn + 2, vs.zoomedTimeY + vs.zoomedTimeH]),
mid1: $V([xZoomedIn - 2, vs.zoomedTimeY + vs.zoomedTimeH]),
mid2: $V([xZoomedOut - 1, vs.audioY + vs.audioH]),
mid3: $V([xZoomedOut + 1, vs.audioY + vs.audioH]),
bot0: $V([xZoomedIn, vs.zoomedTimeY + vs.zoomedTimeH]),
bot1: $V([xZoomedIn, this.offsetHeight]),
};
this.redraw();
}
redraw() {
if (!this.ctx || !this.viewState) {
return;
}
this.ctx.clearRect(0, 0, this.canvasEl.width, this.canvasEl.height);
this.ctx.strokeStyle = "#fff";
this.ctx.lineWidth = 0.5;
this.ctx.beginPath();
const mouse = this.viewState.mouse.pos;
line(this.ctx, $V([0, mouse.e(2)]), $V([this.canvasEl.width, mouse.e(2)]));
line(this.ctx, $V([mouse.e(1), 0]), $V([mouse.e(1), this.canvasEl.height]));
this.ctx.stroke();
if (this.cursorPath) {
this.ctx.strokeStyle = "#ff0303";
this.ctx.lineWidth = 1.5;
this.ctx.beginPath();
line(this.ctx, this.cursorPath.top0, this.cursorPath.top1);
this.ctx.stroke();
this.ctx.fillStyle = "#9c0303";
this.ctx.beginPath();
this.ctx.moveTo(this.cursorPath.mid0.e(1), this.cursorPath.mid0.e(2));
for (let p of [this.cursorPath.mid1, this.cursorPath.mid2, this.cursorPath.mid3]) {
this.ctx.lineTo(p.e(1), p.e(2));
}
this.ctx.fill();
this.ctx.strokeStyle = "#ff0303";
this.ctx.lineWidth = 3;
this.ctx.beginPath();
line(this.ctx, this.cursorPath.bot0, this.cursorPath.bot1, "#ff0303", "3px");
this.ctx.stroke();
}
}
}
|