Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust animation speed #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 30 additions & 2 deletions MovingMarker.js
Expand Up @@ -18,16 +18,27 @@ L.Marker.MovingMarker = L.Marker.extend({
options: {
autostart: false,
loop: false,
speed: 1
},

initialize: function (latlngs, durations, options) {
options = options || {};
L.Marker.prototype.initialize.call(this, latlngs[0], options);

this._latlngs = latlngs.map(function(e, index) {
return L.latLng(e);
});

this._durations = durations;
this._speed = options.speed || 1;
Object.defineProperty(this, '_currentDuration', {
set: function(duration) {
this.__currentDuration = duration;
},
get: function() {
return this.__currentDuration / this._speed;
}
});
this._currentDuration = 0;
this._currentIndex = 0;

Expand Down Expand Up @@ -75,11 +86,25 @@ L.Marker.MovingMarker = L.Marker.extend({
return;
}
// update the current line
this._currentLine[0] = this.getLatLng();
this._currentDuration -= (this._pauseStartTime - this._startTime);
this._startAnimation();
},

setSpeed: function(speed) {
if (! this.isPaused()) {
this._stopAnimation();
var speedChangeTime = Date.now();

this._currentLine[0] = this.getLatLng();
this._currentDuration = (this._currentDuration - (speedChangeTime - this._startTime))*this._speed;

this._speed = speed;

this._startAnimation();
} else {
this._speed = speed;
}
},

addLatLng: function(latlng, duration) {
this._latlngs.push(L.latLng(latlng));
this._durations.push(duration);
Expand Down Expand Up @@ -235,6 +260,9 @@ L.Marker.MovingMarker = L.Marker.extend({
//force animation to place the marker at the right place
this._animate(this._startTimeStamp
+ (this._pauseStartTime - this._startTime), true);

this._currentLine[0] = this.getLatLng();
this._currentDuration = (this._currentDuration - (this._pauseStartTime - this._startTime))*this._speed;
},

stop: function(elapsedTime) {
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -82,6 +82,7 @@ All the marker's options are available.
- ```start()```: the marker begins its path or resumes if it is paused.
- ``` stop()```: manually stops the marker, if you call ```start`` after, the marker starts again the polyline at the beginning.
- ```pause()```: just pauses the marker
- ```setSpeed(number)```: the marker will move the multiple fast, i.e. `setSpeed(2)` is the double speed
- ``` resume()```: the marker resumes its animation
- ```addLatLng(latlng, duration)```: adds a point to the polyline. Useful, if we have to set the path one by one.
- ``` moveTo(latlng, duration)```: stops current animation and make the marker move to ```latlng``` in ```duration``` ms.
Expand Down
26 changes: 25 additions & 1 deletion examples/script.js
Expand Up @@ -48,8 +48,11 @@ marker1.openPopup();
//========================================================================

var marker2 = L.Marker.movingMarker(londonParisRomeBerlinBucarest,
[3000, 9000, 9000, 4000], {autostart: true}).addTo(map);
[3000, 9000, 9000, 8000], {autostart: true}).addTo(map);
L.polyline(londonParisRomeBerlinBucarest, {color: 'red'}).addTo(map);
setTimeout(function() {
marker2.setSpeed(2);
},23000)


marker2.on('end', function() {
Expand Down Expand Up @@ -95,4 +98,25 @@ map.on("click", function(e) {
marker4.moveTo(e.latlng, 2000);
});


var marker5 = L.Marker.movingMarker([[50.85, 4.35], [50.45, 30.523333], [50.85, 4.35]], [12000, 12000]).addTo(map);

var start = Date.now();
marker5.start();
setTimeout(function() {
marker5.pause();
}, 6000)
setTimeout(function() {
marker5.setSpeed(2);
}, 9000)
setTimeout(function() {
marker5.resume();
}, 12000)
setTimeout(function() {
marker5.setSpeed(1);
}, 18000)
marker5.on('end', function() {
console.log('Approx 24s: ', Date.now() - start); // approx 24000
})

//=========================================================================