Skip to content

Commit

Permalink
fix: clear extractImageTimeout timeout on onMount
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuripetusko committed Apr 22, 2024
1 parent da0202e commit 895565a
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/rmrk-2d-renderer/src/lib/rmrk-2d-renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import DOMPurify from 'isomorphic-dompurify';
import { Loader2 } from 'lucide-react';
import { Application, Resource, Texture } from 'pixi.js';
import type { ICanvas } from 'pixi.js';
import type { CSSProperties } from 'react';
import { type CSSProperties, useRef } from 'react';
import React, { useEffect, useMemo } from 'react';
import { useCallback, useState } from 'react';
import useImage from 'use-image';
Expand Down Expand Up @@ -73,18 +73,28 @@ const useBackdropImage = (

// Get image from canvas to apply as a backdrop background
const extractImage = async (pixiApp: Application<ICanvas>) => {
const blob = await pixiApp.renderer.extract.image(pixiApp.stage);
setBgImage(blob.src);
if (pixiApp.stage) {
const blob = await pixiApp.renderer.extract.image(pixiApp.stage);
setBgImage(blob.src);
}
};

const extractImageTimeout = useRef<NodeJS.Timeout>();

// Get image from canvas to apply as a backdrop background
useEffect(() => {
if (!bgImage && !!pixiApp && enabled) {
// Wait for the canvas to be rendered
setTimeout(() => {
extractImageTimeout.current = setTimeout(() => {
extractImage(pixiApp);
}, 600);
}

return () => {
if (extractImageTimeout.current) {
clearTimeout(extractImageTimeout.current);
}
};
}, [pixiApp, enabled, bgImage, extractImage]);

return bgImage;
Expand Down

0 comments on commit 895565a

Please sign in to comment.