diff --git a/src/components/Avatar/Avatar.component.tsx b/src/components/Avatar/Avatar.component.tsx index e1664ef8..bc81a013 100644 --- a/src/components/Avatar/Avatar.component.tsx +++ b/src/components/Avatar/Avatar.component.tsx @@ -97,7 +97,7 @@ export interface AvatarProps extends LightingProps { /** * Return base64 image after making screenshot of the canvas. */ - onCapture?: CaptureType; + capture?: CaptureType; } /** @@ -125,7 +125,7 @@ export const Avatar: FC = ({ style, emotion, idleRotation = false, - onCapture, + capture, background }) => { const AvatarModel = useMemo(() => { @@ -183,7 +183,7 @@ export const Avatar: FC = ({ )} {background?.src && } - {onCapture && } + {capture && } ); diff --git a/src/components/Capture/Capture.component.tsx b/src/components/Capture/Capture.component.tsx index 844698cf..aafae5ef 100644 --- a/src/components/Capture/Capture.component.tsx +++ b/src/components/Capture/Capture.component.tsx @@ -1,18 +1,32 @@ -import React, { FC } from 'react'; +import React, { FC, useEffect } from 'react'; import { useThree } from '@react-three/fiber'; -export type CaptureType = (image?: string) => void; +type CaptureSettingsType = { + quality?: number; + type?: string; +}; -type CaptureProps = { - onCapture?: CaptureType; +export type CaptureType = { + trigger: boolean; + callBack: (capture?: string) => void; + settings?: CaptureSettingsType; }; -const Capture: FC = ({ onCapture }) => { +type CaptureProps = CaptureType; + +const Capture: FC = ({ trigger, settings, callBack }) => { const gl = useThree((state) => state.gl); - if (onCapture) { - onCapture(gl.domElement.toDataURL('image/png', 0.1)); - } + const type = settings?.type || 'image/png'; + const quality = settings?.quality || 0.1; + + useEffect(() => { + if (trigger) { + const capture = gl.domElement.toDataURL(type, quality); + + callBack(capture); + } + }, [trigger]); // eslint-disable-next-line react/jsx-no-useless-fragment return <>;