Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean code challenge: Flipper animation #4

Open
dtinth opened this issue Sep 22, 2017 · 0 comments
Open

Clean code challenge: Flipper animation #4

dtinth opened this issue Sep 22, 2017 · 0 comments

Comments

@dtinth
Copy link
Owner

dtinth commented Sep 22, 2017

This Flipper component is responsible for the QR Code’s spinning gesture (see video demo).

Some physics and maths tricks are employed to deliver a fluid user experience. When spinning the QR code, it calculates the gesture’s intensity and adjust the rotation speed to match. Some damping is added to give a bouncy feeling. And we have to handle a handful of edge cases as well, e.g. what if the user changes the ID while this thing is spinning.

Here’s the code:

// This model object takes care of the state and animation and handles input.
function createFlipperModel (setRotationDegrees, onFlip) {
// XXX: A lot of mutable variables here!!!
// If you are up for a challenge, please help clean up this code!
// Current rotation to be rendered (degrees)
let current = 0
// Target rotation (degrees)
let target = 0
// Current rotation speed (degrees/frame)
let currentSpeed = 0
// Spring strength
const springK = 1 / 15
// Rotation acceleration (degrees/frame^2)
const acceleration = 0.7
// Animation parameters...
let animationActive = false
let animationStartTime = Date.now()
let lastFrameNumber = 0
// State of dragging
let pointerIsDown = false
let history = [ ]

As you can see there are about 8 mutable variables. It’s about 100 lines of impure, imperative code. I would appreciate it if someone could make this code more functional and clean. So, hence this clean code challenge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant