Skip to content

Commit

Permalink
Updated hook useTexture
Browse files Browse the repository at this point in the history
  • Loading branch information
neki-dev committed Oct 23, 2023
1 parent 5e94176 commit 3f88144
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ useInteraction(

#### Render texture image
```ts
<Texture name={string} clone={boolean?} />
<Texture name={string} />
```

.
Expand Down
1 change: 1 addition & 0 deletions dist/adapter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 0 additions & 1 deletion dist/components/texture.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
type Props = {
name: string;
clone?: boolean;
};
export declare const Texture: React.FC<Props>;
export {};
2 changes: 1 addition & 1 deletion dist/hooks/use-texture.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
*
* @param key - Texture key
*/
export declare function useTexture(key: string): HTMLImageElement;
export declare function useTexture(key: string): string;
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions dist/types/texture.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type HTMLTextureElement = HTMLImageElement & {
originBlob: Blob;
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "phaser-react-ui",
"description": "React interface render for Phaser engine",
"version": "1.13.2",
"version": "1.14.0",
"keywords": [
"phaser",
"interface",
Expand Down
14 changes: 14 additions & 0 deletions src/adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Phaser from 'phaser';
import { HTMLTextureElement } from './types/texture';

const defaultFunction = Phaser.Loader.File.createObjectURL;

Phaser.Loader.File.createObjectURL = function createObjectURL(
image: HTMLTextureElement,
blob: Blob,
defaultType: string,
) {
// eslint-disable-next-line no-param-reassign
image.originBlob = blob;
defaultFunction.call(this, image, blob, defaultType);
};
18 changes: 5 additions & 13 deletions src/components/texture.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import React, { useRef, useLayoutEffect } from 'react';
/* eslint-disable jsx-a11y/aria-role */
import React from 'react';
import { useTexture } from '../hooks';

type Props = {
name: string
clone?: boolean
};

export const Texture: React.FC<Props> = ({ name, clone }) => {
const image = useTexture(name);
export const Texture: React.FC<Props> = ({ name }) => {
const imageSrc = useTexture(name);

const ref = useRef<HTMLDivElement>(null);

useLayoutEffect(() => {
const element = clone ? image.cloneNode() : image;

ref.current.appendChild(element);
}, []);

return <div ref={ref} data-texture-container />;
return <img src={imageSrc} role="texture" alt={name} />;
};
4 changes: 3 additions & 1 deletion src/hooks/use-texture.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useMemo } from 'react';
import { useGame } from './use-game';
import { HTMLTextureElement } from '../types/texture';

/**
* Get texture source image.
Expand All @@ -11,7 +12,8 @@ export function useTexture(key: string) {

return useMemo(() => {
const texture = game.textures.get(key);
const image = texture.getSourceImage() as HTMLTextureElement;

return texture.getSourceImage() as HTMLImageElement;
return URL.createObjectURL(image.originBlob);
}, [key]);
}
3 changes: 3 additions & 0 deletions src/types/texture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type HTMLTextureElement = HTMLImageElement & {
originBlob: Blob
};

0 comments on commit 3f88144

Please sign in to comment.