2021-01-25 20:49:00 +03:00
|
|
|
import { h } from 'preact';
|
2021-01-30 19:52:37 +03:00
|
|
|
import ActivityIndicator from './ActivityIndicator';
|
2021-01-26 18:04:03 +03:00
|
|
|
import { useApiHost, useConfig } from '../api';
|
2021-02-09 22:35:33 +03:00
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'preact/hooks';
|
2021-02-12 19:23:58 +03:00
|
|
|
import { useResizeObserver } from '../hooks';
|
2021-01-25 20:49:00 +03:00
|
|
|
|
2021-02-13 18:24:53 +03:00
|
|
|
export default function CameraImage({ camera, onload, searchParams = '', stretch = false }) {
|
2021-01-26 18:04:03 +03:00
|
|
|
const { data: config } = useConfig();
|
|
|
|
|
const apiHost = useApiHost();
|
2021-02-01 03:43:12 +03:00
|
|
|
const [hasLoaded, setHasLoaded] = useState(false);
|
2021-01-27 03:18:45 +03:00
|
|
|
const containerRef = useRef(null);
|
2021-02-01 03:43:12 +03:00
|
|
|
const canvasRef = useRef(null);
|
2021-02-12 19:23:58 +03:00
|
|
|
const [{ width: availableWidth }] = useResizeObserver(containerRef);
|
2021-01-25 20:49:00 +03:00
|
|
|
|
2021-08-14 22:18:35 +03:00
|
|
|
const { name } = config.cameras[camera];
|
|
|
|
|
const { width, height } = config.cameras[camera].detect;
|
2021-01-25 20:49:00 +03:00
|
|
|
const aspectRatio = width / height;
|
2021-01-27 03:18:45 +03:00
|
|
|
|
2021-02-13 18:24:53 +03:00
|
|
|
const scaledHeight = useMemo(() => {
|
|
|
|
|
const scaledHeight = Math.floor(availableWidth / aspectRatio);
|
|
|
|
|
return stretch ? scaledHeight : Math.min(scaledHeight, height);
|
|
|
|
|
}, [availableWidth, aspectRatio, height, stretch]);
|
2021-02-01 03:43:12 +03:00
|
|
|
const scaledWidth = useMemo(() => Math.ceil(scaledHeight * aspectRatio), [scaledHeight, aspectRatio]);
|
2021-01-27 03:18:45 +03:00
|
|
|
|
2021-02-09 22:35:33 +03:00
|
|
|
const img = useMemo(() => new Image(), []);
|
2021-01-27 03:18:45 +03:00
|
|
|
img.onload = useCallback(
|
|
|
|
|
(event) => {
|
2021-02-01 03:43:12 +03:00
|
|
|
setHasLoaded(true);
|
2021-02-09 22:35:33 +03:00
|
|
|
if (canvasRef.current) {
|
|
|
|
|
const ctx = canvasRef.current.getContext('2d');
|
|
|
|
|
ctx.drawImage(img, 0, 0, scaledWidth, scaledHeight);
|
|
|
|
|
}
|
2021-01-27 03:18:45 +03:00
|
|
|
onload && onload(event);
|
2021-01-25 20:49:00 +03:00
|
|
|
},
|
2021-02-09 22:35:33 +03:00
|
|
|
[img, scaledHeight, scaledWidth, setHasLoaded, onload, canvasRef]
|
2021-01-25 20:49:00 +03:00
|
|
|
);
|
|
|
|
|
|
2021-01-27 03:18:45 +03:00
|
|
|
useEffect(() => {
|
2021-02-09 22:35:33 +03:00
|
|
|
if (scaledHeight === 0 || !canvasRef.current) {
|
2021-01-27 03:18:45 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
img.src = `${apiHost}/api/${name}/latest.jpg?h=${scaledHeight}${searchParams ? `&${searchParams}` : ''}`;
|
2021-02-09 22:35:33 +03:00
|
|
|
}, [apiHost, canvasRef, name, img, searchParams, scaledHeight]);
|
2021-01-27 03:18:45 +03:00
|
|
|
|
2021-01-25 20:49:00 +03:00
|
|
|
return (
|
2021-02-09 22:35:33 +03:00
|
|
|
<div className="relative w-full" ref={containerRef}>
|
2021-02-12 19:23:58 +03:00
|
|
|
<canvas data-testid="cameraimage-canvas" height={scaledHeight} ref={canvasRef} width={scaledWidth} />
|
2021-02-01 03:43:12 +03:00
|
|
|
{!hasLoaded ? (
|
|
|
|
|
<div className="absolute inset-0 flex justify-center" style={`height: ${scaledHeight}px`}>
|
|
|
|
|
<ActivityIndicator />
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
2021-01-27 03:18:45 +03:00
|
|
|
</div>
|
2021-01-25 20:49:00 +03:00
|
|
|
);
|
|
|
|
|
}
|