-
Notifications
You must be signed in to change notification settings - Fork 22
/
useSprite.js
161 lines (146 loc) · 3.98 KB
/
useSprite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { useState, useEffect, useCallback, useRef } from 'react'
const noop = () => {}
export const loadImage = (url, callback = noop) => {
const img = new Image()
img.onload = () => {
callback(null, img)
}
img.onerror = err => {
callback(err)
}
img.src = url
}
export const useSprite = ({
startFrame = 0,
sprite,
width,
height,
direction = 'horizontal',
onError = noop,
onLoad = noop,
onEnd = noop,
frameCount,
fps = 60,
shouldAnimate = true,
stopLastFrame,
reset,
scale = 1,
wrapAfter,
frame
}) => {
const prevTime = useRef()
const [currentFrame, setCurrentFrame] = useState(startFrame)
const [spriteWidth, setSpriteWidth] = useState(0)
const [spriteHeight, setSpriteHeight] = useState(0)
const [isLoaded, setIsLoaded] = useState(false)
const [isLoading, setIsLoading] = useState(false)
const [hasErrored, setHasErrored] = useState(false)
const [maxFrames, setMaxFrames] = useState(0)
const interval = 1000 / fps
const loadSprite = useCallback(
url => {
let unmounted = false
if (!isLoading && (!isLoaded || !hasErrored)) {
setIsLoading(true)
loadImage(url, (err, image) => {
if (unmounted) {
return
}
if (err) {
onError(err)
setHasErrored(true)
return
}
onLoad()
setIsLoaded(true)
setIsLoading(false)
setMaxFrames(
frameCount ||
Math.floor(
direction === 'horizontal'
? image.width / width
: image.height / height
)
)
setSpriteWidth(image.width)
setSpriteHeight(image.height)
})
}
return () => (unmounted = true)
},
[sprite, isLoaded, hasErrored]
)
const animate = useCallback(
(nextFrame, time) => {
if (!prevTime.current) {
prevTime.current = time
}
if (shouldAnimate) {
const delta = time - prevTime.current
if (delta < interval) {
return requestAnimationFrame(time => animate(nextFrame, time))
}
prevTime.current = time - (delta % interval)
setCurrentFrame(nextFrame)
} else {
prevTime.current = 0
}
},
[shouldAnimate]
)
const getSpritePosition = useCallback(
(frame = 0) => {
const isHorizontal = direction === 'horizontal'
let row, col
if (typeof wrapAfter === 'undefined') {
row = isHorizontal ? 0 : frame
col = isHorizontal ? frame : 0
} else {
row = isHorizontal ? Math.floor(frame / wrapAfter) : frame % wrapAfter
col = isHorizontal ? frame % wrapAfter : Math.floor(frame / wrapAfter)
}
const _width = (-width * col) / scale
const _height = (-height * row) / scale
return `${_width}px ${_height}px`
},
[direction, width, height, wrapAfter, scale]
)
useEffect(() => {
setIsLoaded(false)
setHasErrored(false)
return loadSprite(sprite)
}, [sprite])
useEffect(() => {
if (shouldAnimate) {
const nextFrame =
currentFrame + 1 >= maxFrames ? startFrame : currentFrame + 1
if (!shouldAnimate) {
return
}
if (currentFrame === maxFrames - 1 && stopLastFrame) {
return onEnd()
}
let id = requestAnimationFrame(time => {
id = animate(nextFrame, time)
})
return () => {
cancelAnimationFrame(id)
}
}
}, [shouldAnimate, maxFrames, currentFrame, startFrame])
useEffect(() => {
setCurrentFrame(startFrame)
}, [reset])
useEffect(() => {
if (typeof frame === 'number' && frame !== currentFrame) {
setCurrentFrame(frame)
}
}, [frame])
return {
backgroundImage: isLoaded ? `url(${sprite})` : null,
backgroundPosition: isLoaded ? getSpritePosition(currentFrame) : null,
backgroundSize: `${spriteWidth / scale}px ${spriteHeight / scale}px`,
width: `${width / scale}px`,
height: `${height / scale}px`,
}
}