Skip to content

Instantly share code, notes, and snippets.

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)));
}
set(velocity, add(velocity, multiply(dt, acc)));
const VELOCITY = 50;
function force(dt, position, velocity) {
return set(velocity, cond(lessThan(position, 0), VELOCITY, -VELOCITY));
}
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)]
);
set(position, add(position, multiply(velocity, dt)))
set(position, add(position, multiply(velocity, dt)))
const clock = new Clock();
const dt = divide(diff(clock), 1000);
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;
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)),
constructor(props) {
super(props);
const gestureX = new Value(0);
const state = new Value(-1);
this._onGestureEvent = event([
{
nativeEvent: {
translationX: gestureX,