Last active
June 24, 2018 05:46
-
-
Save gabrielflorit/5e1defd20f4ff91c270b72ca43db0559 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
// title: sine easing | |
const reproject = p => [p[0], 127 - p[1]] | |
const minX = 32 | |
const maxX = 96 | |
const minY = 13 | |
const maxY = 110 | |
initialState = { | |
x: minX, | |
dx: 1 | |
} | |
update = state => { | |
state.x += state.dx | |
if (state.x > maxX || state.x < minX) { | |
state.dx *= -1 | |
} | |
} | |
const ease = x => { | |
const spanX = maxX - minX | |
const spanY = maxY - minY | |
return Math.cos((spanX-minX)*Math.PI/spanX + x * Math.PI/spanX) * spanY/2 + spanY/2 + minY | |
} | |
draw = state => { | |
clear() | |
// draw axes | |
line(...reproject([0, minY]), ...reproject([0, maxY]), 6) | |
line(...reproject([minX, 0]), ...reproject([maxX, 0]), 6) | |
range(minX, maxX, 1).forEach(i => { | |
circFill(...reproject([i, ease(i)]), 1, 4) | |
}) | |
// draw balls | |
circFill(...reproject([state.x, 0]), 2, 0) | |
circFill(...reproject([0, ease(state.x)]), 2, 0) | |
circFill(...reproject([state.x, ease(state.x)]), 2, 4) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment