Skip to content

Commit

Permalink
add new progress calculation algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
legomushroom committed Jan 28, 2015
1 parent a687b3e commit a275581
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 287 deletions.
41 changes: 25 additions & 16 deletions dist/tween.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Tween = (function() {
Tween.prototype.defaults = {
duration: 600,
delay: 0,
repeat: 0,
yoyo: false,
durationElapsed: 0,
delayElapsed: 0,
repeat: 0,
onStart: null,
onComplete: null
};
Expand All @@ -35,7 +35,8 @@ Tween = (function() {
Tween.prototype.start = function() {
var _ref;
this.props.startTime = Date.now() + this.o.delay;
this.props.endTime = this.props.startTime + this.o.duration;
this.props.totalDuration = (this.o.repeat + 1) * (this.o.duration + this.o.delay) - this.o.delay;
this.props.endTime = this.props.startTime + this.props.totalDuration;
this.isStarted = true;
if (!this.isOnStartFired) {
if ((_ref = this.o.onStart) != null) {
Expand All @@ -47,24 +48,32 @@ Tween = (function() {
};

Tween.prototype.update = function(time) {
var _ref;
if (time >= this.props.endTime) {
this.props.elapsed = this.o.duration;
if (!this.isCompleted) {
if ((_ref = this.o.onComplete) != null) {
_ref.apply(this);
var isFlip, start;
if ((time > this.props.startTime) && (time < this.props.endTime)) {
this.o.isIt && console.log('a');
this.props.elapsed = time - this.props.startTime;
if (this.props.elapsed < this.o.duration) {
return this.progress = this.props.elapsed / this.o.duration;
} else {
start = this.props.startTime;
isFlip = false;
while (start <= time) {
isFlip = !isFlip;
start += isFlip ? this.o.duration : this.o.delay;
}
if (isFlip) {
start = start - this.o.duration;
this.props.elapsed = time - start;
return this.progress = this.props.elapsed / this.o.duration;
} else {
return this.progress = 0;
}
this.isCompleted = true;
}
} else {
this.props.elapsed = this.props.endTime - this.props.startTime;
this.progress = 1;
return true;
}
this.props.elapsed = time - this.props.startTime;
return typeof this.onUpdate === "function" ? this.onUpdate(this.getProgress()) : void 0;
};

Tween.prototype.getProgress = function() {
var progress;
return progress = Math.min(this.props.elapsed / this.o.duration, 1);
};

return Tween;
Expand Down
49 changes: 40 additions & 9 deletions js/tween.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,63 @@ class Tween
defaults:
duration: 600
delay: 0
repeat: 0
yoyo: false
durationElapsed: 0
delayElapsed: 0
repeat: 0
onStart: null
onComplete: null
constructor:(@o={})-> @vars(); @extendDefaults(); @
vars:-> @h = h; @props = {}; @progress = 0
extendDefaults:-> @h.extend(@o, @defaults); @onUpdate = @o.onUpdate
start:->
@props.startTime = Date.now() + @o.delay
@props.endTime = @props.startTime + @o.duration
@props.totalDuration = (@o.repeat+1)*(@o.duration+@o.delay) - @o.delay
@props.endTime = @props.startTime + @props.totalDuration
@isStarted = true
if !@isOnStartFired then @o.onStart?.apply(@); @isOnStartFired = true
@

update:(time)->
if time >= @props.endTime
@props.elapsed = @o.duration
if !@isCompleted then @o.onComplete?.apply(@); @isCompleted = true
if (time > @props.startTime) and (time < @props.endTime)
@o.isIt and console.log 'a'
@props.elapsed = time - @props.startTime
# in the first repeat or without any repeats
if @props.elapsed < @o.duration
@progress = @props.elapsed/@o.duration
else # far in the repeats
start = @props.startTime
isFlip = false
while(start <= time)
isFlip = !isFlip
start += if isFlip then @o.duration else @o.delay
# is in start point + duration
if isFlip
start = start - @o.duration
@props.elapsed = time - start
@progress = @props.elapsed/@o.duration
# is in start point + delay
else @progress = 0
else
@props.elapsed = @props.endTime - @props.startTime
@progress = 1
return true
@props.elapsed = time - @props.startTime
@onUpdate? @getProgress()

getProgress:-> progress = Math.min (@props.elapsed/@o.duration), 1
# if @isReversed then 1-progress else progress
# if time >= @props.currEndTime and time < @props.endTime
# if @o.repeat
# @o.repeat--; @o.yoyo and (@isReversed = !@isReversed)

# @props.elapsed = time - @props.startTime
# @onUpdate? @getProgress()
# @o.isIt and
# if time >= @props.endTime
# @props.elapsed = @o.duration
# if !@isCompleted then @o.onComplete?.apply(@); @isCompleted = true
# return true

# getProgress:->
# progress = Math.min (@props.elapsed/@o.duration), 1
# if @isReversed and progress isnt 1 then 1-progress else progress

# tick:(step=1)->
# @props.totalElapsed += step
Expand Down
Binary file modified mockups/mojs.sketch
Binary file not shown.
Binary file added mockups/mojs.sketch.sb-b30c1a69-J0kmX3
Binary file not shown.

0 comments on commit a275581

Please sign in to comment.