Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ When using a module bundler you can import the transition function and optionall
```javascript
import { transition, easeInOut } from 'plain-transition';

transition({
const myTransition = transition({
easing: easeInOut,
// ...options
});
Expand All @@ -39,7 +39,7 @@ transition({
When using the `plain-transition.min.js` bundle, all functions get bundled in the global `plainTransition` object:

```javascript
plainTransition.transition({
var myTransition = plainTransition.transition({
easing: plainTransition.easeInOut,
// ...options
});
Expand All @@ -53,6 +53,7 @@ transition({
to: 100,
duration: 1000,
easing: easeIn,
autostart: true,
onChange: value => {

},
Expand Down Expand Up @@ -80,12 +81,23 @@ Built-in functions:
* easeOut
* easeInOut

**`autostart`** boolean *(default: true)*
Wheather this transition should start immediately or when transition.start() gets called.

**`onChange`** function
This function gets called everytime the value updates. The first argument will be the current value.

**`onDone`** function
This function gets called once, when the transition is finished and in its final value.

## API

**`myTransition.start()`**
This starts the transition. This is only relevant when `autostart: false` is set in the options.

**`myTransition.cancel()`**
This stops the transition immediately. once it's canceled, it can't be started again.

## Browser support

| Chrome | Safari | IE / Edge | Firefox | Opera |
Expand Down
44 changes: 39 additions & 5 deletions dist/transition.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,30 @@ function easeInOut(t) {
return -Math.cos(t * Math.PI) * 0.5 + 0.5;
}

var transitions = [];

function animate(time) {
transitions.forEach(function (t, i) {
t._frame(time);
});

requestAnimationFrame(animate);
}

requestAnimationFrame(animate);

function transition(_options) {
var _this = this;

if (!(this instanceof transition)) {
return new transition(_options);
}

var options = {
from: 0,
to: 100,
duration: 1000,
autostart: true,
easing: function easing(v) {
return v;
},
Expand All @@ -27,8 +46,23 @@ function transition(_options) {
}

var start = 0;
var started = options.autostart;

var frame = function frame(time) {
// Gets called at the end of the transition or manually by the user
this.cancel = function () {
// remove this transition from queue
transitions.splice(transitions.indexOf(this), 1);
};

this.start = function () {
started = true;
};

this._frame = function (time) {
// Don't do anything until this transition has started
if (started !== true) {
return;
}

// Set the inital timestamp
if (start <= 0) {
Expand All @@ -42,19 +76,19 @@ function transition(_options) {
if (progress < options.duration) {
// Call the onChange event
options.onChange(options.easing(Math.min(progress / options.duration, 1)) * (options.to - options.from) + options.from);

requestAnimationFrame(frame);
} else {
// Call the onChange the last time and ensure the last value emitted is the final value
options.onChange(options.to);

// Call the onDone event to finish this off
options.onDone();

// Ensure nothing happens anymore
_this.cancel();
}
};

// Start the transition
requestAnimationFrame(frame);
transitions.push(this);
}

export { transition, easeIn, easeOut, easeInOut };
2 changes: 1 addition & 1 deletion dist/transition.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading