Skip to content

Instantly share code, notes, and snippets.

@gabrielflorit
Last active January 19, 2019 05:14
Show Gist options
  • Save gabrielflorit/189ddaa733925c86f45560c5493da813 to your computer and use it in GitHub Desktop.
Save gabrielflorit/189ddaa733925c86f45560c5493da813 to your computer and use it in GitHub Desktop.
SCRIPT-8
// title: sine spinner
// number of rectangles
const count = 14
initialState = {
totalElapsed: 0, // total time passed since we started
rotations: [] // each rectangle's individual rotation
}
// given a time and rectangle index,
// returns the rectangle's rotation in degrees
const rotation = (time, i) => {
return Math.cos(time / 400) * 16 * (i + 1)
}
// each frame, increase totalElapsed,
// and set each rectangle's rotation
update = (state, input, elapsed) => {
state.totalElapsed += elapsed
state.rotations = range(count).map(i => rotation(state.totalElapsed, i))
}
// each frame, draw each rectangle
// using polyStroke, which allows rotating a polygon
draw = (state) => {
const { rotations, elapsed } = state
clear()
range(count).forEach(i => {
const side = 8 + 6 * i
const xy = 64 - side/2
polyStroke([
[xy, xy], [xy + side, xy], [xy + side, xy + side], [xy, xy + side]
], rotations[i], i / 2 % 7)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment