-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
69 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './relative-scale'; | ||
export * from './relative-position'; | ||
export * from './texture'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react'; | ||
import { TextureProps } from '../types/texture'; | ||
type Props = TextureProps; | ||
export declare const Texture: React.FC<Props>; | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export declare function useTexture(key: string): HTMLImageElement; | ||
import { TextureProps } from '../types/texture'; | ||
export declare function useTexture({ key, frame }: TextureProps): HTMLImageElement; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type TextureProps = { | ||
key: string; | ||
frame?: string | number; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './relative-scale'; | ||
export * from './relative-position'; | ||
export * from './texture'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,17 @@ | ||
import React, { useEffect } from 'react'; | ||
import React, { useLayoutEffect } from 'react'; | ||
import { useRelativePosition } from '../hooks'; | ||
import { RelativePositionProps } from '../types/relative-position'; | ||
|
||
type Props = RelativePositionProps & { | ||
children: React.ReactNode | ||
}; | ||
|
||
export const RelativePosition: React.FC<Props> = ({ | ||
x, y, children, | ||
}) => { | ||
const ref = useRelativePosition<HTMLDivElement>({ x, y }); | ||
export const RelativePosition: React.FC<Props> = ({ children, ...props }) => { | ||
const ref = useRelativePosition<HTMLDivElement>(props); | ||
|
||
useEffect(() => { | ||
useLayoutEffect(() => { | ||
ref.current.style.position = 'absolute'; | ||
}, []); | ||
|
||
return ( | ||
<div ref={ref}> | ||
{children} | ||
</div> | ||
); | ||
return <div ref={ref}>{children}</div>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,17 @@ | ||
import React, { useEffect } from 'react'; | ||
import React, { useLayoutEffect } from 'react'; | ||
import { useRelativeScale } from '../hooks'; | ||
import { RelativeScaleProps } from '../types/relative-scale'; | ||
|
||
type Props = RelativeScaleProps & { | ||
children: React.ReactNode | ||
}; | ||
|
||
export const RelativeScale: React.FC<Props> = ({ | ||
target, min, max, round, children, | ||
}) => { | ||
const ref = useRelativeScale<HTMLDivElement>({ | ||
target, min, max, round, | ||
}); | ||
export const RelativeScale: React.FC<Props> = ({ children, ...props }) => { | ||
const ref = useRelativeScale<HTMLDivElement>(props); | ||
|
||
useEffect(() => { | ||
useLayoutEffect(() => { | ||
ref.current.style.position = 'absolute'; | ||
}, []); | ||
|
||
return ( | ||
<div ref={ref}> | ||
{children} | ||
</div> | ||
); | ||
return <div ref={ref}>{children}</div>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React, { useRef, useLayoutEffect } from 'react'; | ||
import { useTexture } from '../hooks'; | ||
import { TextureProps } from '../types/texture'; | ||
|
||
type Props = TextureProps; | ||
|
||
export const Texture: React.FC<Props> = (props) => { | ||
const image = useTexture(props); | ||
|
||
const ref = useRef<HTMLDivElement>(null); | ||
|
||
useLayoutEffect(() => { | ||
ref.current.appendChild(image); | ||
}, []); | ||
|
||
return <div ref={ref} data-texture-container />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
import { useMemo } from 'react'; | ||
import { useGame } from './use-game'; | ||
import { TextureProps } from '../types/texture'; | ||
|
||
export function useTexture(key: string) { | ||
export function useTexture({ key, frame }: TextureProps) { | ||
const game = useGame(); | ||
|
||
return useMemo( | ||
() => game.textures.get(key).getSourceImage() as HTMLImageElement, | ||
[key], | ||
); | ||
return useMemo(() => { | ||
const texture = game.textures.get(key); | ||
|
||
if (frame === undefined) { | ||
return texture.getSourceImage() as HTMLImageElement; | ||
} | ||
|
||
// @ts-ignore | ||
return texture.frames[frame].source.image as HTMLImageElement; | ||
}, [key, frame]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type TextureProps = { | ||
key: string | ||
frame?: string | number | ||
}; |