From 7ccc8b65f0182397aa9f506ddf36a280e3300052 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Thu, 9 Sep 2021 22:01:52 +0200 Subject: [PATCH] Update FORMATS.mdx --- docs/docs/guides/FORMATS.mdx | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/docs/docs/guides/FORMATS.mdx b/docs/docs/guides/FORMATS.mdx index c233280..5a2b936 100644 --- a/docs/docs/guides/FORMATS.mdx +++ b/docs/docs/guides/FORMATS.mdx @@ -69,9 +69,9 @@ This example shows how you would pick the format with the _highest frame rate_: ```tsx function getMaxFps(format: CameraDeviceFormat): number { return format.frameRateRanges.reduce((prev, curr) => { - if (curr.maxFrameRate > prev) return curr.maxFrameRate; - else return prev; - }, 0); + if (curr.maxFrameRate > prev) return curr.maxFrameRate + else return prev + }, 0) } function App() { @@ -80,11 +80,11 @@ function App() { const format = useMemo(() => { return device?.formats.reduce((prev, curr) => { - if (prev == null) return curr; - if (getMaxFps(curr) > getMaxFps(prev)) return curr; - else return prev; - }, undefined); - }, [device?.formats]); + if (prev == null) return curr + if (getMaxFps(curr) > getMaxFps(prev)) return curr + else return prev + }, undefined) + }, [device?.formats]) if (device == null) return return ( @@ -108,17 +108,17 @@ Implement this however you want, I personally use a "point-based system": ```ts export const sortFormatsByResolution = (left: CameraDeviceFormat, right: CameraDeviceFormat): number => { // in this case, points aren't "normalized" (e.g. higher resolution = 1 point, lower resolution = -1 points) - let leftPoints = left.photoHeight * left.photoWidth; - let rightPoints = right.photoHeight * right.photoWidth; + let leftPoints = left.photoHeight * left.photoWidth + let rightPoints = right.photoHeight * right.photoWidth // we also care about video dimensions, not only photo. - leftPoints += left.videoWidth * left.videoHeight; - rightPoints += right.videoWidth * right.videoHeight; + leftPoints += left.videoWidth * left.videoHeight + rightPoints += right.videoWidth * right.videoHeight // you can also add points for FPS, etc - return rightPoints - leftPoints; -}; + return rightPoints - leftPoints +} // and then call it: const formats = useMemo(() => device?.formats.sort(sortFormatsByResolution), [device?.formats])