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
function spring(dt, position, velocity, anchor, mass = 1, tension = 300) { | |
const dist = sub(position, anchor); | |
const acc = divide(multiply(-1, tension, dist), mass); | |
return set(velocity, add(velocity, multiply(dt, acc))); | |
} |
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
set(velocity, add(velocity, multiply(dt, acc))); | |
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
const VELOCITY = 50; | |
function force(dt, position, velocity) { | |
return set(velocity, cond(lessThan(position, 0), VELOCITY, -VELOCITY)); | |
} |
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
const POSITION_THRESHOLD = 1; | |
function stopWhenNeeded(dt, position, velocity, clock) { | |
return cond( | |
and( | |
lessThan(position, POSITION_THRESHOLD), | |
lessThan(-POSITION_THRESHOLD, position) | |
), | |
[stopClock(clock), set(velocity, 0), set(position, 0)] | |
); |
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
set(position, add(position, multiply(velocity, dt))) | |
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
set(position, add(position, multiply(velocity, dt))) |
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
const clock = new Clock(); | |
const dt = divide(diff(clock), 1000); |
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
function interaction() { | |
let dragging = 0; | |
let start = 0; | |
let position = 0; | |
return (gestureTranslation, gestureState) => { | |
if (gestureState === State.ACTIVE) { | |
if (dragging === 0) { | |
dragging = 1; | |
start = position; |
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
function interaction(gestureTranslation, gestureState) { | |
const start = new Value(0); | |
const dragging = new Value(0); | |
const position = new Value(0); | |
return cond( | |
eq(gestureState, State.ACTIVE), | |
[ | |
cond(eq(dragging, 0), [set(dragging, 1), set(start, position)]), | |
set(position, add(start, gestureTranslation)), |
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
constructor(props) { | |
super(props); | |
const gestureX = new Value(0); | |
const state = new Value(-1); | |
this._onGestureEvent = event([ | |
{ | |
nativeEvent: { | |
translationX: gestureX, |