Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #52 from AdactiveSAS/feature-request-tick
Browse files Browse the repository at this point in the history
[Feature] onRequestTick
  • Loading branch information
dalisoft authored Jun 28, 2018
2 parents 362e98b + a1ea7cd commit 6cec0a6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
20 changes: 19 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const {add, remove, isRunning, autoPlay} = TWEEN
* [.onTick(fn)](#TWEEN.onTick)
* [.FrameThrottle([frameCount])](#TWEEN.FrameThrottle)
* [.autoPlay(state)](#TWEEN.autoPlay)
* [.onRequestTick(fn)](#TWEEN.onRequestTick)
* [.removeAll()](#TWEEN.removeAll)
* [.get(tween)](#TWEEN.get) ⇒ <code>Tween</code>
* [.has(tween)](#TWEEN.has) ⇒ <code>Boolean</code>
Expand Down Expand Up @@ -543,7 +544,7 @@ TWEEN.FrameThrottle(60)
<a name="TWEEN.autoPlay"></a>

### TWEEN.autoPlay(state)
Runs update loop automaticlly
Runs update loop automatically

**Kind**: static method of [<code>TWEEN</code>](#TWEEN)

Expand All @@ -555,6 +556,23 @@ Runs update loop automaticlly
```js
TWEEN.autoPlay(true)
```
<a name="TWEEN.onRequestTick"></a>

### TWEEN.onRequestTick(fn)
Add a function called when another animation frame is requested. This is only called when autoPlay is false.

**Kind**: static method of [<code>TWEEN</code>](#TWEEN)

| Param | Type | Description |
| --- | --- | --- |
| fn | <code>function</code> | Function called when another animation frame is requested |

**Example**
```js
TWEEN.onRequestTick(() => {
requestAnimationFrame(TWEEN.update);
})
```
<a name="TWEEN.removeAll"></a>

### TWEEN.removeAll()
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
},
"dependencies": {},
"standard": {
"ignore": ["node_modules", "bundled", "examples", "performance"]
"ignore": [
"node_modules",
"bundled",
"examples",
"performance"
]
}
}
37 changes: 25 additions & 12 deletions src/core.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global process */
import { cancelAnimationFrame, requestAnimationFrame, root } from './shim'
import { requestAnimationFrame, root } from './shim'

/**
* Get browser/Node.js current time-stamp
Expand Down Expand Up @@ -50,12 +50,21 @@ const now = (function () {
const _tweens = []
let isStarted = false
let _autoPlay = false
let _tick
let _onRequestTick = []
const _ticker = requestAnimationFrame
const _stopTicker = cancelAnimationFrame
let emptyFrame = 0
let powerModeThrottle = 120

const onRequestTick = (fn) => {
_onRequestTick.push(fn)
}

const _requestTick = () => {
for (let i = 0; i < _onRequestTick.length; i++) {
_onRequestTick[i]()
}
}

/**
* Adds tween to list
* @param {Tween} tween Tween instance
Expand All @@ -77,8 +86,10 @@ const add = (tween) => {
emptyFrame = 0

if (_autoPlay && !isStarted) {
_tick = _ticker(update)
_ticker(update)
isStarted = true
} else {
_requestTick()
}
}

Expand Down Expand Up @@ -179,21 +190,22 @@ const remove = (tween) => {
*/

const update = (time = now(), preserve) => {
if (emptyFrame >= powerModeThrottle) {
isStarted = false
emptyFrame = 0
return false
}

if (_autoPlay && isStarted) {
_tick = _ticker(update)
_ticker(update)
} else {
_requestTick()
}

if (!_tweens.length) {
emptyFrame++
}

if (emptyFrame > powerModeThrottle) {
_stopTicker(_tick)
isStarted = false
emptyFrame = 0
return false
}

let i = 0
let length = _tweens.length
while (i < length) {
Expand Down Expand Up @@ -243,6 +255,7 @@ export {
update,
autoPlay,
onTick,
onRequestTick,
isRunning,
FrameThrottle
}
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
has,
isRunning,
now,
onRequestTick,
FrameThrottle,
Plugins,
remove,
removeAll,
Expand All @@ -33,6 +35,8 @@ export {
update,
autoPlay,
isRunning,
onRequestTick,
FrameThrottle,
Tween,
Easing,
Interpolation
Expand Down

0 comments on commit 6cec0a6

Please sign in to comment.