diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-06-01 09:58:22 +0200 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-06-01 09:58:22 +0200 |
commit | b27370f0cad27cb83d16ebacb42b4a472d1709b2 (patch) | |
tree | 5bb15367ec8994e49fc168b5a2a257f0e5937109 | |
parent | a9dc2dd77a68be9d94fe2679ea72fd23595d4538 (diff) | |
download | svg.js-b27370f0cad27cb83d16ebacb42b4a472d1709b2.tar.gz svg.js-b27370f0cad27cb83d16ebacb42b4a472d1709b2.zip |
renamed method names as suggested, added failing test
-rw-r--r-- | spec/spec/runner.js | 87 | ||||
-rw-r--r-- | src/runner.js | 16 |
2 files changed, 69 insertions, 34 deletions
diff --git a/spec/spec/runner.js b/spec/spec/runner.js index 2f889f3..50644e2 100644 --- a/spec/spec/runner.js +++ b/spec/spec/runner.js @@ -559,97 +559,134 @@ describe('SVG.Runner', function () { }) }) - describe('position()', function () { - it('get the position of a runner', function () { + describe('loops()', function () { + it('get the loops of a runner', function () { var spy = jasmine.createSpy('stepper') var runner = new SVG.Runner(1000).queue(null, spy) runner.step(300) expect(spy).toHaveBeenCalledWith(0.3) - expect(runner.position()).toBe(0.3) + expect(runner.loops()).toBe(0.3) }) - it('sets the position of the runner', function () { + it('sets the loops of the runner', function () { var spy = jasmine.createSpy('stepper') var runner = new SVG.Runner(1000).queue(null, spy) - expect(runner.position(0.5).position()).toBe(0.5) + expect(runner.loops(0.5).loops()).toBe(0.5) expect(spy).toHaveBeenCalledWith(0.5) - expect(runner.position(0.1).position()).toBe(0.1) + expect(runner.loops(0.1).loops()).toBe(0.1) expect(spy).toHaveBeenCalledWith(0.1) - expect(runner.position(1.5).position()).toBe(1) + expect(runner.loops(1.5).loops()).toBe(1) expect(spy).toHaveBeenCalledWith(1) }) - it('sets the position of the runner in a loop', function () { + it('sets the loops of the runner in a loop', function () { var spy = jasmine.createSpy('stepper') var runner = new SVG.Runner(1000).loop(5, true, 500).queue(null, spy) - expect(runner.position(1.3).position()).toBe(1.3) + expect(runner.loops(1.3).loops()).toBe(1.3) expect(spy).toHaveBeenCalledWith(0.7) - expect(runner.position(0.3).position()).toBe(0.3) + expect(runner.loops(0.3).loops()).toBe(0.3) }) }) - describe('absolute()', function () { - it('gets the absolute position of a runner', function () { + describe('progress()', function () { + it('gets the progress of a runner', function () { var spy = jasmine.createSpy('stepper') var runner = new SVG.Runner(1000).queue(null, spy) runner.step(300) expect(spy).toHaveBeenCalledWith(0.3) - expect(runner.absolute()).toBe(0.3) + expect(runner.progress()).toBe(0.3) }) - it('gets the absolute position of a runner when looping', function () { + it('gets the progress of a runner when looping', function () { var spy = jasmine.createSpy('stepper') var runner = new SVG.Runner(800).queue(null, spy).loop(10, false, 200) // duration should be 9800 // middle of animation, in the middle of wait time runner.step(4900) - expect(runner.absolute()).toBe(0.5) + expect(runner.progress()).toBe(0.5) expect(spy).toHaveBeenCalledWith(1) // start of next loop runner.step(100) expect(spy).toHaveBeenCalledWith(0) - // move 400 into current loop which is 0.5 in position - // the absolute value is 5400 / 9800 + // move 400 into current loop which is 0.5 progress + // the progress value is 5400 / 9800 runner.step(400) expect(spy).toHaveBeenCalledWith(0.5) - expect(runner.absolute()).toBe(5400 / 9800) + expect(runner.progress()).toBe(5400 / 9800) }) - it('sets the absolute position of a runner', function () { + it('sets the progress of a runner', function () { var spy = jasmine.createSpy('stepper') var runner = new SVG.Runner(1000).queue(null, spy) - expect(runner.absolute(0.5).absolute()).toBe(0.5) + expect(runner.progress(0.5).progress()).toBe(0.5) expect(spy).toHaveBeenCalledWith(0.5) }) - it('sets the absolute position of a runner when looping', function () { + it('sets the progress of a runner when looping', function () { var spy = jasmine.createSpy('stepper') var runner = new SVG.Runner(800).queue(null, spy).loop(10, false, 200) - // absolute 0.5 somewhere in the middle of wait time - expect(runner.absolute(0.5).absolute()).toBe(0.5) + // progress 0.5 somewhere in the middle of wait time + expect(runner.progress(0.5).progress()).toBe(0.5) expect(spy).toHaveBeenCalledWith(1) // start of next loop runner.step(100) expect(spy).toHaveBeenCalledWith(0) - // should move 0.4 into the next loop - expect(runner.absolute(5400 / 9800).absolute()).toBe(5400 / 9800) + // should move 0.5 into the next loop + expect(runner.progress(5400 / 9800).progress()).toBe(5400 / 9800) expect(spy.calls.mostRecent().args[0]).toBeCloseTo(0.5) }) }) + describe('position()', function () { + it('gets the position of a runner', function () { + var spy = jasmine.createSpy('stepper') + var runner = new SVG.Runner(1000).queue(null, spy) + + runner.step(300) + expect(spy).toHaveBeenCalledWith(0.3) + + expect(runner.position()).toBe(0.3) + }) + it('gets the position of a runner when looping', function () { + var spy = jasmine.createSpy('stepper') + var runner = new SVG.Runner(1000).loop(5, true, 100).queue(null, spy) + + runner.step(1200) + expect(spy).toHaveBeenCalledWith(0.9) + + expect(runner.position()).toBe(0.9) + }) + it('sets the position of a runner', function () { + var spy = jasmine.createSpy('stepper') + var runner = new SVG.Runner(1000).queue(null, spy) + + expect(runner.position(0.5).position()).toBe(0.5) + expect(spy).toHaveBeenCalledWith(0.5) + }) + it('sets the position of a runner', function () { + var spy = jasmine.createSpy('stepper') + var runner = new SVG.Runner(1000).loop(5, true, 100).queue(null, spy) + + runner.step(1200) + + expect(runner.position(0.4).position()).toBe(0.4) + expect(spy).toHaveBeenCalledWith(0.6) + }) + }) + describe('element()', function () { it('returns the element bound to this runner if any', function () { var runner1 = new SVG.Runner() diff --git a/src/runner.js b/src/runner.js index 8c58d2e..065967d 100644 --- a/src/runner.js +++ b/src/runner.js @@ -192,8 +192,7 @@ SVG.Runner = SVG.invent({ return this._times * (this._wait + this._duration) - this._wait }, - // FIXME: Proposal - this should be called loops - position: function (p) { + loops: function (p) { var loopDuration = this._duration + this._wait if (p == null) { var loopsDone = Math.floor(this._time / loopDuration) @@ -207,10 +206,9 @@ SVG.Runner = SVG.invent({ return this.time(time) }, - // FIXME: Proposal - this should be called position - local: function (p) { + position: function (p) { if (p == null) { - + /* This function converts a time to a position in the range [0, 1] The full explanation can be found in this desmos demonstration @@ -244,13 +242,13 @@ SVG.Runner = SVG.invent({ } // Work out the loops done and add the position to the loops done + var loopDuration = this._duration + this._wait var loopsDone = Math.floor(this._time / loopDuration) var position = loopsDone + p - return this.position(position) + return this.loops(position) }, - // FIXME - Proposal - this should be called progress - absolute: function (p) { + progress: function (p) { if (p == null) { return Math.min(1, this._time / this.duration()) } @@ -262,7 +260,7 @@ SVG.Runner = SVG.invent({ // Update the time and get the new position dt = dt == null ? 16 : dt this._time += dt - var position = this.local() + var position = this.position() // Figure out if we need to run the stepper in this frame var runNow = this._lastPosition !== position && this._time >= 0 |