forked from blakeblackshear/frigate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(web): responsive images on content size, throttle AutoUpdati…
…ngCameraImage
- Loading branch information
1 parent
75a01f6
commit 2ec9215
Showing
6 changed files
with
129 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
import { h } from 'preact'; | ||
import CameraImage from './CameraImage'; | ||
import { ApiHost, Config } from '../context'; | ||
import { useCallback, useEffect, useContext, useState } from 'preact/hooks'; | ||
import { useCallback, useState } from 'preact/hooks'; | ||
|
||
export default function AutoUpdatingCameraImage({ camera, searchParams }) { | ||
const apiHost = useContext(ApiHost); | ||
const MIN_LOAD_TIMEOUT_MS = 200; | ||
|
||
export default function AutoUpdatingCameraImage({ camera, searchParams, showFps = true }) { | ||
const [key, setKey] = useState(Date.now()); | ||
useEffect(() => { | ||
const timeoutId = setTimeout(() => { | ||
setKey(Date.now()); | ||
}, 500); | ||
return () => { | ||
clearTimeout(timeoutId); | ||
}; | ||
}, [key, searchParams]); | ||
const [fps, setFps] = useState(0); | ||
|
||
return <CameraImage camera={camera} searchParams={`cache=${key}&${searchParams}`} />; | ||
const handleLoad = useCallback(() => { | ||
const loadTime = Date.now() - key; | ||
setFps((1000 / Math.max(loadTime, MIN_LOAD_TIMEOUT_MS)).toFixed(1)); | ||
setTimeout( | ||
() => { | ||
setKey(Date.now()); | ||
}, | ||
loadTime > MIN_LOAD_TIMEOUT_MS ? 1 : MIN_LOAD_TIMEOUT_MS | ||
); | ||
}, [key, searchParams, setFps]); | ||
|
||
return ( | ||
<div> | ||
<CameraImage camera={camera} onload={handleLoad} searchParams={`cache=${key}&${searchParams}`} /> | ||
{showFps ? <span className="text-xs">Displaying at {fps}fps</span> : null} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,60 @@ | ||
import { h } from 'preact'; | ||
import { ApiHost, Config } from '../context'; | ||
import { useCallback, useEffect, useContext, useState } from 'preact/hooks'; | ||
import { useCallback, useEffect, useContext, useMemo, useRef, useState } from 'preact/hooks'; | ||
|
||
export default function CameraImage({ camera, searchParams = '', imageRef }) { | ||
export default function CameraImage({ camera, onload, searchParams = '' }) { | ||
const config = useContext(Config); | ||
const apiHost = useContext(ApiHost); | ||
const { name, width, height } = config.cameras[camera]; | ||
const [availableWidth, setAvailableWidth] = useState(0); | ||
const [loadedSrc, setLoadedSrc] = useState(null); | ||
const containerRef = useRef(null); | ||
|
||
const { name, width, height } = config.cameras[camera]; | ||
const aspectRatio = width / height; | ||
const innerWidth = parseInt(window.innerWidth, 10); | ||
|
||
const responsiveWidths = [640, 768, 1024, 1280]; | ||
if (innerWidth > responsiveWidths[responsiveWidths.length - 1]) { | ||
responsiveWidths.push(innerWidth); | ||
} | ||
|
||
const src = `${apiHost}/api/${camera}/latest.jpg`; | ||
const { srcset, sizes } = responsiveWidths.reduce( | ||
(memo, w, i) => { | ||
memo.srcset.push(`${src}?h=${Math.ceil(w / aspectRatio)}&${searchParams} ${w}w`); | ||
memo.sizes.push(`(max-width: ${w}) ${Math.ceil((w / innerWidth) * 100)}vw`); | ||
return memo; | ||
|
||
const resizeObserver = useMemo(() => { | ||
return new ResizeObserver((entries) => { | ||
window.requestAnimationFrame(() => { | ||
if (Array.isArray(entries) && entries.length) { | ||
setAvailableWidth(entries[0].contentRect.width); | ||
} | ||
}); | ||
}); | ||
}, [setAvailableWidth, width]); | ||
|
||
useEffect(() => { | ||
if (!containerRef.current) { | ||
return; | ||
} | ||
resizeObserver.observe(containerRef.current); | ||
}, [resizeObserver, containerRef.current]); | ||
|
||
const scaledHeight = useMemo(() => Math.min(Math.ceil(availableWidth / aspectRatio), height), [ | ||
availableWidth, | ||
aspectRatio, | ||
height, | ||
]); | ||
|
||
const img = useMemo(() => new Image(), [camera]); | ||
img.onload = useCallback( | ||
(event) => { | ||
const src = event.path[0].currentSrc; | ||
setLoadedSrc(src); | ||
onload && onload(event); | ||
}, | ||
{ srcset: [], sizes: [] } | ||
[searchParams, onload] | ||
); | ||
|
||
useEffect(() => { | ||
if (!scaledHeight) { | ||
return; | ||
} | ||
img.src = `${apiHost}/api/${name}/latest.jpg?h=${scaledHeight}${searchParams ? `&${searchParams}` : ''}`; | ||
}, [apiHost, name, img, searchParams, scaledHeight]); | ||
|
||
return ( | ||
<img | ||
className="w-full" | ||
srcset={srcset.join(', ')} | ||
sizes={sizes.join(', ')} | ||
src={`${srcset[srcset.length - 1]}`} | ||
alt={name} | ||
ref={imageRef} | ||
/> | ||
<div ref={containerRef}> | ||
{loadedSrc ? <img width={scaledHeight * aspectRatio} height={scaledHeight} src={loadedSrc} alt={name} /> : null} | ||
</div> | ||
); | ||
} |