Created
January 11, 2022 17:43
-
-
Save cawfree/5066df36e74624048c599b5cb8dde167 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import * as React from 'react'; | |
export type VectorizedBounds2D = readonly [width: number, height: number]; | |
export type ShaderToyDynamics = { | |
readonly iFrame: number; | |
readonly iTime: number; | |
}; | |
export type ShaderToyUniforms = ShaderToyDynamics & { | |
readonly iResolution: VectorizedBounds2D; | |
}; | |
type State = ShaderToyDynamics & { | |
readonly startTime: number; | |
}; | |
export default function useShaderToyUniforms({ | |
height, | |
width, | |
}: { | |
readonly height: number; | |
readonly width: number; | |
}): ShaderToyUniforms { | |
const [{iTime, iFrame}, setState] = React.useState<State>(() => ({ | |
iFrame: 0, | |
iTime: 0, | |
startTime: new Date().getTime(), | |
})); | |
const iResolution = React.useMemo<VectorizedBounds2D>(() => [width, height], [ | |
width, | |
height, | |
]); | |
React.useEffect(() => { | |
const i = setInterval(() => | |
setState(({iFrame, startTime}: State) => ({ | |
iFrame: iFrame + 1, | |
iTime: (new Date().getTime() - startTime) / 1000, | |
startTime, | |
})) | |
); | |
return () => { | |
clearInterval(i); | |
}; | |
}, []); | |
return { | |
iFrame, | |
iResolution, | |
iTime, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment