Skip to content

Commit

Permalink
refactor:
Browse files Browse the repository at this point in the history
  • Loading branch information
activeguild committed Dec 18, 2023
1 parent 1031d8d commit 044d373
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
10 changes: 5 additions & 5 deletions example/vite/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function App() {
<>
<Suspense fallback={null}>
<Canvas
style={{ position: "absolute", top: 100 }}
style={{ position: "absolute", top: 30 }}
camera={{ position: [-5, 2, 10], fov: 60 }}
flat={false}
>
Expand All @@ -44,20 +44,20 @@ export default function App() {
<Suspense fallback={null}>
<AnimationTexture
url={framePngUrl}
position={new THREE.Vector3(0, 0, 0)}
position={new THREE.Vector3(-1, 1, 0)}
/>
<AnimationTexture
url={framesPngUrl}
position={new THREE.Vector3(1, 0, 0)}
position={new THREE.Vector3(0, 1, 0)}
isPlaying={isPlaying}
/>
<AnimationTexture
url={frameGifUrl}
position={new THREE.Vector3(0, 1, 0)}
position={new THREE.Vector3(-1, 0, 0)}
/>
<AnimationTexture
url={framesGifUrl}
position={new THREE.Vector3(1, 1, 0)}
position={new THREE.Vector3(0, 0, 0)}
/>
</Suspense>
<gridHelper
Expand Down
69 changes: 38 additions & 31 deletions src/useAnimationTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import { AnimationTexture } from "./AnimationTexture";

interface UseAnimationTextureArgs {
url: string;
// enabledInterval?: boolean;
interval?: number;
loop?: boolean;
autoplay?: boolean;
}

const DEFAULT_INTERVAL = 100;
const DEFAULT_ENABLED_LOOP = true;
const DEFAULT_LOOP = true;
const DEFAULT_AUTOPLAY = true;

const framesMap = new Map<
Expand Down Expand Up @@ -44,34 +43,6 @@ export const preLoad = (url: UseAnimationTextureArgs["url"]) => {
load(url);
};

const drawPatch = (
ctx: CanvasRenderingContext2D,
image: ImageData,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
frame: any
) => {
const dims = frame.dims;
const tempCanvas = document.createElement("canvas");
tempCanvas.width = dims.width;
tempCanvas.height = dims.height;
const tempCtx = tempCanvas.getContext("2d");
if (!tempCtx) {
return null;
}
tempCtx.putImageData(image, 0, 0);
ctx.drawImage(
tempCanvas,
0,
0,
dims.width,
dims.height,
dims.left,
dims.top,
dims.width,
dims.height
);
};

const initializeWorker = () => {
if (!worker) {
worker = new framesWorker();
Expand Down Expand Up @@ -108,7 +79,7 @@ const initializeWorker = () => {
export const useAnimationTexture = ({
url,
interval = DEFAULT_INTERVAL,
loop = DEFAULT_ENABLED_LOOP,
loop = DEFAULT_LOOP,
autoplay = DEFAULT_AUTOPLAY,
}: UseAnimationTextureArgs) => {
const [animationTexture, setAnimationTexture] =
Expand All @@ -117,6 +88,42 @@ export const useAnimationTexture = ({
const isGif = url.endsWith(".gif");
const needsDisposal = useRef(false);
const [playing, setPlaying] = useState(autoplay);
const tempCanvas = useRef<HTMLCanvasElement | null>(null);
const tempCtx = useRef<CanvasRenderingContext2D | null>(null);

const drawPatch = (
ctx: CanvasRenderingContext2D,
image: ImageData,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
frame: any
) => {
const dims = frame.dims;
if (!tempCanvas.current) {
tempCanvas.current = document.createElement("canvas");
}
tempCanvas.current.width = dims.width;
tempCanvas.current.height = dims.height;
if (tempCtx.current) {
tempCtx.current = tempCanvas.current.getContext("2d");
}

if (!tempCtx.current) {
return null;
}
tempCtx.current.putImageData(image, 0, 0);
ctx.drawImage(
tempCanvas.current,
0,
0,
dims.width,
dims.height,
dims.left,
dims.top,
dims.width,
dims.height
);
};

const frameUpdate = useCallback(() => {
const currentFrames = getFrameses(url);
if (
Expand Down

0 comments on commit 044d373

Please sign in to comment.