• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    JavaScript
  • Created over 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Custom timelines for manipulate sprite animation.

Sprite Timeline

npm status build status dependency status Maintainability Test Coverage

Custom timelines for manipulate sprite animation.

Installation

npm install sprite-timeline

Usage

in browser

<script src="https://s3.ssl.qhres.com/!670d1b37/sprite-timeline.min.js"></script>

Demos

DEMO 1

const T = 2000
let timeline

requestAnimationFrame(function update() {
  if(!timeline) timeline = new Timeline()
  const rotation = 360 * timeline.currentTime / T
  ball.style.transform = `rotate(${rotation}deg)`
  requestAnimationFrame(update)
})

speedUp.onclick = function(){
  if(timeline) timeline.playbackRate += 0.2
  rate.innerHTML = timeline.playbackRate.toFixed(1)
}

speedDown.onclick = function(){
  if(timeline) timeline.playbackRate -= 0.2
  rate.innerHTML = timeline.playbackRate.toFixed(1)
}

reverse.onclick = function(){
  if(timeline) timeline.playbackRate = -timeline.playbackRate
  rate.innerHTML = timeline.playbackRate.toFixed(1)
}

pause.onclick = function(){
  if(timeline) timeline.playbackRate = 0
  rate.innerHTML = timeline.playbackRate.toFixed(1)
}

DEMO 2

const T = 2000
let timeline = new Timeline()

timeline.setInterval(function update() {
  ball.innerHTML = Math.round(timeline.currentTime / 100)
  if(timeline.playbackRate < 0){
    ball.style.backgroundColor = 'green'
  } else {
    ball.style.backgroundColor = 'red'
  }
}, {entropy: 100})

speedUp.onclick = function(){
  if(timeline) timeline.playbackRate += 0.2
  rate.innerHTML = timeline.playbackRate.toFixed(1)
}

speedDown.onclick = function(){
  if(timeline) timeline.playbackRate -= 0.2
  rate.innerHTML = timeline.playbackRate.toFixed(1)
}

reverse.onclick = function(){
  if(timeline) timeline.playbackRate = -timeline.playbackRate
  rate.innerHTML = timeline.playbackRate.toFixed(1)
}

pause.onclick = function(){
  if(timeline) timeline.playbackRate = 0
  rate.innerHTML = timeline.playbackRate.toFixed(1)
}

API

Properties
Methods

constructor

new Timeline({originTime, playbackRate})

Create a new timeline. If the originTime is set, currentTime is -originTime (see. currentTime) and entropy is -originTime (see. entropy).

If the playbackRate is set to 1.0(by default), the time-lapse rate is normal. And if the playbackRate is set to 2.0, the time-laspe rate should be double. And if the playbackRate is set to -1.0, the time-laspe go backwards.

properties

currentTime

Get or set the currentTime of the timeline according to the lastest playbackRate(see. playbackRate).

const timeline = new Timeline({originTime: 500})

let i = 0
const timerID = setInterval(() => {
  console.log(Math.round(timeline.currentTime / 100))
  if(++i >= 10){
    clearInterval(timerID)
  }
}, 100)

//output: -4,-3,-2,-1,0,1,2,3,4,5

entropy

Both currentTime and entropy should be influenced by playbackRate. If current playbackRate is negative, the currentTime should go backwards while the entropy remain to go forwards. Both currentTime and entropy's initial values should be -originTime.

const timeline = new Timeline({originTime: 500})

let i = 0
const timerID = setInterval(() => {
  console.log([Math.round(timeline.currentTime / 100), Math.round(timeline.entropy / 100)])
  if(++i >= 10){
    clearInterval(timerID)
  }
}, 100)

setTimeout(() => {
  timeline.playbackRate = -timeline.playbackRate
}, 500)

//output: -4,-4,-3,-3,-2,-2,-1,-1,0,0,-1,1,-2,2,-3,3,-4,4,-5,5

playbackRate

Speed up or slow down the time-lapse. If playbackRate set to negative the time go backwards.

const timeline = new Timeline({playbackRate: -2})

const startTime = timeline.globalTime

timeline.setTimeout(() => {
  console.log(timeline.currentTime, timeline.globalTime - startTime)
}, -2000)

//output: -2000, 1000

globalTime

readonly

Return performance.now() or fallback to Date.now() if no performance API.

methods

setTimeout

Create a timer according to timeline.playbackRate.

const timeline = new Timeline({playbackRate: 2})

const startTime = timeline.globalTime

timeline.setTimeout(() => {
  console.log(timeline.currentTime, timeline.globalTime - startTime)
}, 1000)

//output: 1000, 500

Note: If you change the playbackRate before timeout, the timer will be adjust to the new playbackRate.

const timeline = new Timeline({playbackRate: 1})

const startTime = timeline.globalTime

timeline.setTimeout(() => {
  console.log(timeline.currentTime, timeline.globalTime - startTime)
}, 1000)

setTimeout(() => {
  timeline.playbackRate = 2
}, 200)

//output: 1000, 600

set entropy timer

You can set timers by entropy.

const timeline = new Timeline({playbackRate: 1})

const startTime = timeline.globalTime

timeline.setTimeout(() => {
  console.log(timeline.currentTime, timeline.globalTime - startTime)
}, 1000)

timeline.setTimeout(() => {
  console.log(timeline.currentTime, timeline.globalTime - startTime)
}, {entropy: 1000})

setTimeout(() => {
  timeline.playbackRate = -2
}, 200)

//output: 200, 200
//output: -600, 600

setInterval

Similar with setTimeout.

clearTimeout

Clear the timer according to the corresponding timerID.

const timeline = new Timeline({playbackRate: 1})

let i = 0
const timerID = timeline.setInterval(function(){
  console.log(++i)
  if(!(i % 10)){
    timeline.playbackRate ++
  }
  if(!(i % 100)){
    timeline.clearTimeout(timerID)
  }
}, 1000)

clearInterval

The same as clearTimeout

fork

Fork a new timeline based on current timeline.

const baseTimeline = new Timeline()

const timeline1 = baseTimeline.fork(),
      timeline2 = baseTimeline.fork({playbackRate: 2})

...

baseTimeline.playbackRate = 2 // Should speed up all forked timelines.

seekLocalTime

Seek localTime by entropy.

seekGlobalTime

Seek globalTime by entropy.

License

MIT

More Repositories

1

spritejs

A cross platform high-performance graphics system.
JavaScript
5,321
star
2

sprite-wxapp

spritejs 小程序版
JavaScript
159
star
3

sprite-vue

SpriteJS for Vue.js.
JavaScript
60
star
4

sprite-core

Platform-independent canvas render object model api.
JavaScript
49
star
5

sprite-flex-layout

grid-layout is a layout engine which implements flex, can use in canvas/node-canvas
JavaScript
45
star
6

sprite-extend-dagre

Draws simple flow chart diagrams with spritejs. Powered by dagre.js.
JavaScript
26
star
7

sprite-react

Rendering spritejs elements with React.
JavaScript
25
star
8

q-charts

A visulization library based on spritejs
JavaScript
24
star
9

sprite-extend-3d

JavaScript
17
star
10

sprite-workflow

Draws workflow chart diagrams with spritejs
JavaScript
16
star
11

cat-charts-vue

A visulization library based on spritejs , qcharts and Vue
JavaScript
15
star
12

sprite-extend-shapes

JavaScript
14
star
13

sprite-draggable

let sprite can draggable,让sprite对象拥有draggable的能力
JavaScript
13
star
14

sprite-devtools

JavaScript
12
star
15

sprite-animator

An implementation of the abstract Animation interface.
JavaScript
9
star
16

qcharts-wxapp-demo

qcharts微信小程序DEMO
JavaScript
5
star
17

typescript-examples

SpriteJS typescript examples.
TypeScript
4
star
18

next-draggable

let @spritejs/next spritejs v3 can draggable,让 spritejs v3 对象拥有 draggable 的能力
JavaScript
4
star
19

spritly

JavaScript
4
star
20

cat-charts-react

A visulization library based on spritejs , qcharts and React
Vue
4
star
21

sprite-vue-demo

SpriteJS 的 Vue Runtime 的 DEMO
JavaScript
3
star
22

sprite-math

Matrix and Vector model for spritejs.
JavaScript
3
star
23

sprite-extend-echarts

Rendering echarts with spritejs.
JavaScript
3
star
24

sprite-extend-matter

matter.js renderer for spritejs
JavaScript
2
star
25

sprite-extend-proton

particle renderer for proton-js
JavaScript
2
star
26

sprite-extend-bmfont

JavaScript
2
star
27

sprite-template

The standard project template.
JavaScript
2
star
28

extrude-contours

extrude polyline contours
JavaScript
1
star
29

sprite-extend-rough

SpriteJS extension for Rough.js.
JavaScript
1
star
30

sprite-extend-d3axis

JavaScript
1
star
31

sprite-extend-curvejs

this is a sprite extend for curvejs
JavaScript
1
star