Animated sprite hook for react-three-fiber

Overview

use-animated-sprite

Animated sprite hook for react-three-fiber

Dependencies

npm install @react-three/drei @react-three/fiber react three

Installation

npm install use-animated-sprite

Component Usage

See Sprite and SpriteSheetConfig

import { AnimatedSprite, SpriteSheetConfig } from 'use-animated-sprite';
import { SpriteProps } from '@react-three/fiber';

// The config interface

interface SpriteSheetConfig {
		spriteSheetUrl; string; // Required - The path or full URL to the sprite sheet
		xCount: number, // Required - the number of sprites along the X axis the spritesheet is divided into
		yCount: number; // Required - the number of sprites along the Y axis the spritesheet is divided into
		spriteFrames: number; // Required - the number of frames for this sprite
		spriteX: number; // Required - the start x position of this sprite (not pixels, but number of sprites from "left")
		spriteY: number; // Required - the start y position of this sprite (not pixels, but the number of sprites from "bottom")

		// One of interval or intervalFunc are required
		interval?: number; // Optional - the number of seconds between sprite frames

		// Optional - a function returning a number to use for the next interval between sprite frames
		intervalFunc?: () => number;
}

function MySprite () {
	const config = {
		spriteSheetUrl: `/path/to/spritesheet.png`,
		xCount: 40,
		yCount: 32,
		spriteFrames: 4
		spriteX: 20
		spriteY: 10
		interval: 0.5
		// - or -
		intervalFunc: () => {
			return (300 + Math.random() * 500) / 1000;
		}
	}
	return <AnimatedSprite {...config}>
}

Hook Usage

import { Sprite } from 'three';
import { useRef } from 'react';
import { useAnimatedSprite } from 'use-animated-sprite';

function MySprite () {

	const spriteRef = useRef<Sprite>();
	const texture = useAnimatedSprite(spriteRef, {
		spriteSheetUrl: `/path/to/spritesheet.png`,
		xCount: 40,
		yCount: 32,
		spriteFrames: 4
		spriteX: 20
		spriteY: 10
		interval: 0.5
		// - or -
		intervalFunc: () => {
			return (300 + Math.random() * 500) / 1000;
		}
	})

	return (
		<sprite ref={spriteRef}>
    	<spriteMaterial map={texture} />
  	</sprite>
	)
}
You might also like...

A React hook to fetch ENS records from a domain.

use-ens A React hook to fetch ENS records from a domain. Install pnpm i use-ens Example import React, { useEffect } from 'react' import { useWeb3React

Jul 31, 2022

Animated Select Component (React)

Spring Chain We built a custom select component with a menu with animations and beautiful gradients and a glassy style, is called "Spring Chain" becau

Feb 6, 2022

Directus Hook Extension: Version Control Changelog

Directus Hook Extension: Version Control Changelog

A Directus hook extension to push user written change summaries (from a singleton collection text field) to a changelog in a VCS server

Nov 27, 2022

An enchanced useState hook which keeps track of the states history, allowing you to undo and redo states.

useTimeline An enchanced useState hook which keeps track of the states history, allowing you to undo and redo states. useTimeline is a simple hook bas

Apr 22, 2022

A trpc-ified useReducer hook. ⚡

trpc-reducer A trpc-ified react useReducer hook that lets you perform state logic in reducers just like React's useReducer hook When you dispatch an a

Aug 27, 2022

A tiny, SSR-friendly hook for listening to gamepad events.

useGamepadEvents useGamepadEvents is a tiny, SSR-friendly hook for listening to gamepad events. It's a wrapper around the Gamepad API designed for fir

Oct 2, 2022

Planets fact site with animated solar system built with ReactJS.

Planets fact site with animated solar system built with ReactJS.

Frontend Mentor - Planets fact site Table of contents Overview Intro The challenge Links My process Built with Features Setup Useful resources Overvie

Jan 1, 2023

A javascript library to generate animated wavy text!

A javascript library to generate animated wavy text!

Wavy Text Animation Library A JavaScript Library which allows you to animate infinite words in an infinite loop in a modern wavy way! Options Vanilla

Oct 9, 2022

Replaces native cursor with custom animated cursor.

Animated Cursor A pure JS library to replace native cursor with a custom animated cursor. Check out the Demo Contents 📌 Features 🎯 Quickstart 🧬 Opt

Jul 18, 2022
Owner
Brit Gardner
Engineering @coherence
Brit Gardner
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
A Go Fiber based Featureful Chatroom Application.

Go-Chat Fast Fiber-ous Chat Application Deployed At heroku Currently Supports: Creation of chatrooms Adding users to chatroom Messaging to chatroom us

Imon Roy 11 Dec 16, 2022
three.js examples. if you are first in learning three.js , this will give you much help.

three-projected-material Three.js Material which lets you do Texture Projection on a 3d Model. Installation After having installed three.js, install i

null 22 Nov 2, 2022
Text Engraving & Extrusion demo based on Three.js is implemented with Typescript and webpack5. Used THREE-CSGMesh as the core tech to achieve engraving and extrusion results

Text Engraving & Extrusion Text Engraving & Extrusion demo is implemented using Three.js, with Typescript and webpack5. Used THREE-CSGMesh as the core

Jiahong Li 3 Oct 12, 2022
A generative engine that takes various png layers on a sprite sheet format, combines them and then converts them into a .gif file

Welcome to the Generative GIF Engine v2.0.4 ?? [8 minute read] This python and node app generates layered-based gifs to create NFT gif art! It is fast

Jalagar 112 Jan 2, 2023
A generative engine that takes various png layers on a sprite sheet format, combines them and then converts them into a .gif file

Welcome to the Generative Animated Engine v3.0.1 ?? [8 minute read] This repo used to be called jalagar/Generative_Gif_Engine but because it now suppo

Jalagar 47 May 24, 2022
CSS sprite to represent iOS shortcut icons

shortcut-icons A CSS sprite to represent iOS shortcut icons. Grab the stylesheet and the default (iOS 15) glyph set, and add a shortcut icon to your H

Antonio Bueno 18 Nov 14, 2022
Small example showing how you can make game sprite animations using CSS with Javascript movement.

Hi there, I'm Björn Hjorth ?? I like combining the web and game development, if you like what you see please do not be a stranger and say "Hi" on Twit

Björn Hjorth 29 Nov 9, 2022
🪝 React hook for subscribing to user's color scheme preference.

use-prefers-color-scheme React hook for subscribing to user's color scheme preference. ?? Getting Started ?? Jump Start npm install @anatoliygatt/use-

Anatoliy Gatt 30 Dec 19, 2022