-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhooks.js
38 lines (29 loc) · 988 Bytes
/
hooks.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
import {createContext, useContext, useEffect, useRef} from 'react'
import * as PIXI from 'pixi.js'
const Context = createContext({}) // { project, scale }
export const PixiOverlayProvider = Context.Provider
export const PixiOverlayConsumer = Context.Consumer
export function useProject(latlng, parentPosition = [0, 0]) {
const { project } = useContext(Context)
const myPosition = project(latlng)
// 返回值就是应该被填入 PixiJS 元素的 xy 值
return [myPosition.x - parentPosition[0], myPosition.y - parentPosition[1]]
}
export function useScale() {
const { scale } = useContext(Context)
return scale
}
export function useTick(callback) {
const savedRef = useRef(null)
useEffect(() => {
savedRef.current = callback
}, [callback])
useEffect(() => {
const ticker = PIXI.Ticker.shared
const tick = delta => savedRef.current?.apply(ticker, [delta, ticker])
ticker.add(tick)
return () => {
ticker.remove(tick)
}
}, [])
}