Skip to content

Commit

Permalink
Merge pull request #12 from readyplayerme/feature/capture-extension
Browse files Browse the repository at this point in the history
feat(avatar): capture props updated & callback screenshot added
  • Loading branch information
BDenysovets authored Aug 4, 2022
2 parents 6d529f7 + 5e788e5 commit 858286c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/components/Avatar/Avatar.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export interface AvatarProps extends LightingProps {
/**
* Return base64 image after making screenshot of the canvas.
*/
onCapture?: CaptureType;
capture?: CaptureType;
}

/**
Expand Down Expand Up @@ -125,7 +125,7 @@ export const Avatar: FC<AvatarProps> = ({
style,
emotion,
idleRotation = false,
onCapture,
capture,
background
}) => {
const AvatarModel = useMemo(() => {
Expand Down Expand Up @@ -183,7 +183,7 @@ export const Avatar: FC<AvatarProps> = ({
</group>
)}
{background?.src && <Box {...background} />}
{onCapture && <Capture onCapture={onCapture} />}
{capture && <Capture {...capture} />}
</Suspense>
</BaseCanvas>
);
Expand Down
30 changes: 22 additions & 8 deletions src/components/Capture/Capture.component.tsx
Original file line number Diff line number Diff line change
@@ -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<CaptureProps> = ({ onCapture }) => {
type CaptureProps = CaptureType;

const Capture: FC<CaptureProps> = ({ 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 <></>;
Expand Down

0 comments on commit 858286c

Please sign in to comment.