Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | import type { ICamera, Point2, Point3 } from '../../../types';
import ResolvedViewportView from '../ResolvedViewportView';
import {
getAnchorWorldForPan,
getPanForVideoCanvasMapping,
resolveVideoCanvasMapping,
type VideoCanvasMapping,
} from './videoViewportCamera';
import type {
VideoViewState,
VideoProperties,
VideoStreamPayload,
} from './VideoViewportTypes';
type VideoResolvedViewState = {
viewState: VideoViewState;
containerHeight: number;
containerWidth: number;
frameOfReferenceUID?: string;
intrinsicHeight: number;
intrinsicWidth: number;
objectFit?: VideoProperties['objectFit'];
payload?: VideoStreamPayload;
};
class VideoResolvedView extends ResolvedViewportView<VideoResolvedViewState> {
private cachedCanvasMapping?: VideoCanvasMapping;
get zoom(): number {
return Math.max(this.state.viewState.scale ?? 1, 0.001);
}
get pan(): Point2 {
return this.getCanvasMapping()
? getPanForVideoCanvasMapping(this.getCanvasMapping())
: [0, 0];
}
get rotation(): number {
return this.state.viewState.rotation ?? 0;
}
canvasToWorld(canvasPos: Point2): Point3 {
const mapping = this.getCanvasMapping();
return [
(canvasPos[0] - mapping.left) / mapping.worldToCanvasRatio,
(canvasPos[1] - mapping.top) / mapping.worldToCanvasRatio,
0,
];
}
worldToCanvas(worldPos: Point3): Point2 {
const mapping = this.getCanvasMapping();
return [
mapping.left + worldPos[0] * mapping.worldToCanvasRatio,
mapping.top + worldPos[1] * mapping.worldToCanvasRatio,
];
}
getFrameOfReferenceUID(): string | undefined {
return this.state.frameOfReferenceUID;
}
withZoom(zoom: number, canvasPoint?: Point2): VideoResolvedView {
const nextZoom = Math.max(zoom, 0.001);
if (!canvasPoint) {
return this.cloneWithViewState({
...this.state.viewState,
scale: nextZoom,
scaleMode: 'fit',
});
}
const worldPoint = this.canvasToWorld(canvasPoint);
return this.cloneWithViewState({
...this.state.viewState,
anchorCanvas: [
canvasPoint[0] / Math.max(this.state.containerWidth, 1),
canvasPoint[1] / Math.max(this.state.containerHeight, 1),
],
anchorWorld: [worldPoint[0], worldPoint[1]],
scale: nextZoom,
scaleMode: 'fit',
});
}
withPan(pan: Point2): VideoResolvedView {
return this.cloneWithViewState({
...this.state.viewState,
anchorWorld: getAnchorWorldForPan(
[pan[0], pan[1]],
this.getCanvasMapping()
),
});
}
protected buildICamera(): ICamera {
const mapping = this.getCanvasMapping();
const focalPoint = this.canvasToWorld([
this.state.containerWidth / 2,
this.state.containerHeight / 2,
]);
return {
parallelProjection: true,
focalPoint,
position: [0, 0, 0],
viewUp: [0, -1, 0],
parallelScale:
this.state.containerHeight /
2 /
Math.max(mapping.worldToCanvasRatio, 0.001),
viewPlaneNormal: [0, 0, 1],
rotation: this.rotation,
};
}
private getCanvasMapping(): VideoCanvasMapping {
this.cachedCanvasMapping ||= resolveVideoCanvasMapping({
camera: this.state.viewState,
containerHeight: this.state.containerHeight,
containerWidth: this.state.containerWidth,
intrinsicHeight: this.state.intrinsicHeight,
intrinsicWidth: this.state.intrinsicWidth,
objectFit: this.state.objectFit,
}) || {
left: 0,
top: 0,
width: Math.max(this.state.intrinsicWidth, 1),
height: Math.max(this.state.intrinsicHeight, 1),
containerWidth: Math.max(this.state.containerWidth, 1),
containerHeight: Math.max(this.state.containerHeight, 1),
intrinsicWidth: Math.max(this.state.intrinsicWidth, 1),
intrinsicHeight: Math.max(this.state.intrinsicHeight, 1),
anchorWorld: [0, 0],
anchorCanvas: [0.5, 0.5],
worldToCanvasRatio: 1,
};
return this.cachedCanvasMapping;
}
private cloneWithViewState(viewState: VideoViewState): VideoResolvedView {
return new VideoResolvedView({
...this.state,
viewState,
});
}
}
export type { VideoResolvedViewState };
export default VideoResolvedView;
|