Last active
January 19, 2019 05:14
-
-
Save gabrielflorit/189ddaa733925c86f45560c5493da813 to your computer and use it in GitHub Desktop.
SCRIPT-8
This file contains 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
{} |
This file contains 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
// 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) | |
}) | |
} |
This file contains 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
[] |
This file contains 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
{} |
This file contains 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
{} |
This file contains 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
{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment