Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add prop to configure canvasRenderingContext2DSettings #2104

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions docs/loaders/use-sprite-loader.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ Returns: `{spriteTexture:Texture, spriteData:{any[], object}, aspect:Vector3}`
- spriteData: A collection of the sprite frames, and some meta information (width, height)
- aspect: Information about the aspect ratio of the sprite sheet

```jsx
/** The texture url to load the sprite frames from */
input?: Url | null,
/** The JSON data describing the position of the frames within the texture (optional) */
json?: string | null,
/** The animation names into which the frames will be divided into (optional) */
animationNames?: string[] | null,
/** The number of frames on a standalone (no JSON data) spritesheet (optional)*/
numberOfFrames?: number | null,
/** The callback to call when all textures and data have been loaded and parsed */
onLoad?: (texture: Texture, textureData?: any) => void
```ts
type Props = {
/** The texture url to load the sprite frames from */
input?: Url | null
/** The JSON data describing the position of the frames within the texture (optional) */
json?: string | null
/** The animation names into which the frames will be divided into (optional) */
animationNames?: string[] | null
/** The number of frames on a standalone (no JSON data) spritesheet (optional)*/
numberOfFrames?: number | null
/** The callback to call when all textures and data have been loaded and parsed */
onLoad?: (texture: Texture, textureData?: any) => void
/** Allows the configuration of the canvas options */
canvasRenderingContext2DSettings?: CanvasRenderingContext2DSettings
}
```

```jsx
Expand Down
2 changes: 2 additions & 0 deletions docs/misc/sprite-animator.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type Props = {
maxItems?: number
/** External parsed sprite data, usually from useSpriteLoader ready for use */
spriteDataset?: any
/** Allows the configuration of the canvas options */
canvasRenderingContext2DSettings?: CanvasRenderingContext2DSettings
}
```

Expand Down
11 changes: 10 additions & 1 deletion src/core/SpriteAnimator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type SpriteAnimatorProps = {
maxItems?: number
instanceItems?: any[]
spriteDataset?: any
canvasRenderingContext2DSettings?: CanvasRenderingContext2DSettings
} & JSX.IntrinsicElements['group']

type SpriteAnimatorState = {
Expand Down Expand Up @@ -79,6 +80,7 @@ export const SpriteAnimator: React.FC<SpriteAnimatorProps> = /* @__PURE__ */ Rea
maxItems,
instanceItems,
spriteDataset,
canvasRenderingContext2DSettings,
...props
},
fref
Expand All @@ -100,7 +102,14 @@ export const SpriteAnimator: React.FC<SpriteAnimatorProps> = /* @__PURE__ */ Rea
const pos = React.useRef(offset)
const softEnd = React.useRef(false)
const frameBuffer = React.useRef<any[]>([])
const { spriteObj, loadJsonAndTexture } = useSpriteLoader(null, null, animationNames, numberOfFrames)
const { spriteObj, loadJsonAndTexture } = useSpriteLoader(
null,
null,
animationNames,
numberOfFrames,
undefined,
canvasRenderingContext2DSettings
)
//

function reset() {}
Expand Down
5 changes: 3 additions & 2 deletions src/core/useSpriteLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export function useSpriteLoader<Url extends string>(
json?: string | null,
animationNames?: string[] | null,
numberOfFrames?: number | null,
onLoad?: (texture: Texture, textureData?: any) => void
onLoad?: (texture: Texture, textureData?: any) => void,
canvasRenderingContext2DSettings?: CanvasRenderingContext2DSettings
): any {
const v = useThree((state) => state.viewport)
const spriteDataRef = React.useRef<any>(null)
Expand Down Expand Up @@ -210,7 +211,7 @@ export function useSpriteLoader<Url extends string>(
const getRowsAndColumns = (texture: THREE.Texture, totalFrames: number) => {
if (texture.image) {
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
const ctx = canvas.getContext('2d', canvasRenderingContext2DSettings)

if (!ctx) {
throw new Error('Failed to get 2d context')
Expand Down