Giter Site home page Giter Site logo

rebound-js's Introduction

Build Status

Rebound

Rebound is a simple library that models Spring dynamics for the purpose of driving physical animations.

Origin

Rebound was originally written in Java to provide a lightweight physics system for Home and Chat Heads on Android. It's now been adopted by several other Android applications. This JavaScript port was written to provide a quick way to demonstrate Rebound animations on the web for a conference talk. Since then the JavaScript version has been used to build some really nice interfaces. Check out brandonwalkin.com for an example.

Overview

The Library provides a SpringSystem for maintaining a set of Spring objects and iterating those Springs through a physics solver loop until equilibrium is achieved. The Spring class is the basic animation driver provided by Rebound. By attaching a listener to a Spring, you can observe its motion. The observer function is notified of position changes on the spring as it solves for equilibrium. These position updates can be mapped to an animation range to drive animated property updates on your user interface elements (translation, rotation, scale, etc).

Check out the tests, and examples for more details.

Example

Here's a simple example. Pressing and releasing on the logo below will cause it to scale up and down with a springy animation.

<div style="text-align:center; margin-bottom:50px; margin-top:50px">
  <img
    src="http://facebook.github.io/rebound/images/rebound.png"
    id="logo"
  />
</div>
<script src="../rebound.min.js"></script>
<script>
  function scale(el, val) {
    el.style.mozTransform =
    el.style.msTransform =
    el.style.webkitTransform =
    el.style.transform = 'scale3d(' + val + ', ' + val + ', 1)';
  }
  var el = document.getElementById('logo');

  var springSystem = new rebound.SpringSystem();
  var spring = springSystem.createSpring(50, 3);
  spring.addListener({
    onSpringUpdate: function(spring) {
      var val = spring.getCurrentValue();
      val = rebound.MathUtil.mapValueInRange(val, 0, 1, 1, 0.5);
      scale(el, val);
    }
  });

  el.addEventListener('mousedown', function() {
    spring.setEndValue(1);
  });

  el.addEventListener('mouseout', function() {
    spring.setEndValue(0);
  });

  el.addEventListener('mouseup', function() {
    spring.setEndValue(0);
  });
</script>

Here's how it works.

// Get a reference to the logo element.
var el = document.getElementById('logo');

// create a SpringSystem and a Spring with a bouncy config.
var springSystem = new rebound.SpringSystem();
var spring = springSystem.createSpring(50, 3);

// Add a listener to the spring. Every time the physics
// solver updates the Spring's value onSpringUpdate will
// be called.
spring.addListener({
  onSpringUpdate: function(spring) {
    var val = spring.getCurrentValue();
    val = rebound.MathUtil
                 .mapValueInRange(val, 0, 1, 1, 0.5);
    scale(el, val);
  }
});

// Listen for mouse down/up/out and toggle the
//springs endValue from 0 to 1.
el.addEventListener('mousedown', function() {
  spring.setEndValue(1);
});

el.addEventListener('mouseout', function() {
  spring.setEndValue(0);
});

el.addEventListener('mouseup', function() {
  spring.setEndValue(0);
});

// Helper for scaling an element with css transforms.
function scale(el, val) {
  el.style.mozTransform =
  el.style.msTransform =
  el.style.webkitTransform =
  el.style.transform = 'scale3d(' +
    val + ', ' + val + ', 1)';
}

Facebook has adopted a Code of Conduct that we expect project participants to adhere to. Please read the full text so that you can understand what actions will and will not be tolerated.

LICENSE

rebound-js is BSD-licensed. We also provide an additional patent grant.

rebound-js's People

Contributors

appsforartists avatar cpojer avatar flarnie avatar jamesgpearce avatar nucliweb avatar orta avatar philippamarkovics avatar porada avatar sophiebits avatar vjeux avatar wbinnssmith avatar willbailey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rebound-js's Issues

frames is unused

spring.destroy() sets spring.frames, which appears to be unused.

Should activateSpring be activateSpringById

Methods like registerSpring and deregisterSpring take a Spring instance; whereas, getSpringById takes a number. If activateSpring takes a number, perhaps it should be called activateSpringById.

Towards 1.0

I'd like to start tracking progress towards rebound.js 1.0. All it will be is the solidification of what has already been the accepted API for rebound for the past few years. At the same time, we can take the opportunity to clean up a few minor imperfections, make its core even smaller, and launch a shiny new website.

  • activateSpring -> activateSpringById (#24)
  • Split out utils as a separate entry point (can be import utils from 'rebound/utils') so that those who just need the core of rebound don't pull in extra bytes.
  • Establish browser support as the same as React's, meaning:
  • Docusaurus Website

Please let me know about any concerns, and if you feel something else should be a part of 1.0. We'll likely have a couple of prerelease versions before we publish.

In the meantime I'd like to launch rebound.js v0.1.0, which will simply be building and distributing rebound after its moderate refactoring, flowification, and new build infrastructure with rollup flat bundles. Keep an eye out for that soon.

cc @willbailey, @appsforartists, @JoelMarcey

Document units for velocity

What are the correct units for velocity? I'd expect pixels/millisecond, but I can't find it documented anywhere. I'd expect to see it in the docblock for setVelocity.

Spring overshoots even when clamping has been enabled

I'm not 100% sure if this is as expected but the impression I got was that by enabling overshoot clamping with Spring.prototype.setOvershootClampingEnabled the spring would never go beyond the end value. I however am experiencing the spring overshooting when setting the velocity of a spring which is already at rest, e.g.

var springSystem = new rebound.SpringSystem();
var spring = window.spring = springSystem.createSpring();

spring.setOvershootClampingEnabled(true);
spring.addListener({
    onSpringUpdate: function(spring) {
        console.log(spring.getCurrentValue());
    }
});

spring.setVelocity(-0.1);

// Console
// -0.00009908567306149528
// -0.0013381384439321623
// ... more negative numbers ...
// -0.000002966453601728263
// 0.00004496495274462767
// 0.00008388667615449482
// 0

Whereas I had expected it would remain at 0?

Great library btw!

Navigator.SceneConfigs.FloatFromBottom

I use Navigator to push to a Component with the Navigator.SceneConfigs.FloatFromBottom, and following is the resources:

FloatFromLeft: {
...BaseConfig,
animationInterpolators: {
into: buildStyleInterpolator(FromTheLeft),
out: buildStyleInterpolator(FadeToTheRight),
},
},

var ToTheBack = {
// Rotate requires you to break out each individual component of
// rotation (x, y, z, w)
transformTranslate: {
from: {x: 0, y: 0, z: 0},
to: {x: 0, y: 0, z: 0},
min: 0,
max: 1,
type: 'linear',
extrapolate: true,
round: PixelRatio.get(),
},
transformScale: {
from: {x: 1, y: 1, z: 1},
to: {x: 0.95, y: 0.95, z: 1},
min: 0,
max: 1,
type: 'linear',
extrapolate: true
},
opacity: {
from: 1,
to: 0.3,
min: 0,
max: 1,
type: 'linear',
extrapolate: false,
round: 100,
},
scaleX: {
from: 1,
to: 0.95,
min: 0,
max: 1,
type: 'linear',
extrapolate: true
},
scaleY: {
from: 1,
to: 0.95,
min: 0,
max: 1,
type: 'linear',
extrapolate: true
},
};

I really want to get the result like follow picture:
image

so i add some config into ToTheBack,
translateY: {
from: 0,
to: SCREEN_HEIGHT/3,
min: 0,
max: 1,
type: 'linear',
extrapolate: true,
round: PixelRatio.get(),
},

but it doesn't work, i still get the result like follow picture:
image

does the translateY not work?

Update NPM Version

I noticed that there has already been a commit which fixes the browserify compatibility.

No rush, but when you get a chance could you update the npm package?

I hope this is the right forum to bring this up, I just spent a few moments trying to figure out why my build wasn't working and I figured I should tell someone.

Cant read property 'systemShouldAdvance' of undefined

I have an instance where I want to animate multiple DOM elements at the same time. The function throwout is run multiple times to achieve this. And once the animation is done I destroy the spring in onSpringAtRest callback. But this throw error Cant read property 'systemShouldAdvance' of undefined if I run the function few times immediately after the other. If I remove the spring.destroy() the error doesnt occur. But I dont want any memory leaks as users might run this function several times.

  springSystem = new Rebound.SpringSystem();
  function Throwout(direction,target,fromX,fromY){
    let throwSpring = this.springSystem.createSpring(500,50);
    throwSpring.addListener({
      onSpringAtRest:  (spring) => {
        this.swipePan = false;
        spring.destroy();
      },
    throwSpring.setCurrentValue(0).setAtRest().setVelocity(throwVelocity).setEndValue(1);
}

Dropping Bower support

For the past year, Bower has been recommending migrating away and has pushed yarn as a replacement. They've even written a guide for maintainers for how to politely drop support.

I'd like to make our most recent release (from 2014), v0.0.10, the last to support bower. Moving forward we will continue to publish to npm.

New in the npm builds will be the minified version of rebound, so that it can be hosted for us on unpkg, exactly as React has done. Bower users can continue to use the latest version of rebound simply by using the https reference to unpkg.

This will have no effect on existing bower users, as we will not unpublish rebound from the bower registry, and instead will simply not provide a bower.json or build artifacts in the repo in upcoming tags.

Please let me know what you think, and if you believe there's a reason to continue to support bower moving forward.

Consider re-licensing to AL v2.0, as RocksDB has just done

Hi,

I assume you are aware of the recent discussions around the so-called 'Facebook BSD+Patents License'. The Apache Software Foundation Legal Affairs Committee has announced that this license is no longer allowed to be used as a direct dependency in Apache projects. Based on this decision, the RocksDB project re-licensed their code and people have reached out to other Facebook projects to consider doing the same.

I would kindly ask if you would consider changing the license of this project to something that’s easier to integrate for other companies.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.