Skip to content

Instantly share code, notes, and snippets.

@twelch
Last active December 9, 2015 09:18
Show Gist options
  • Save twelch/bb0acef6f711741abf6b to your computer and use it in GitHub Desktop.
Save twelch/bb0acef6f711741abf6b to your computer and use it in GitHub Desktop.
Animated path with two videos
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.12.0/mapbox-gl.js'></script>
<script src='http://api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.2/turf.min.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.12.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
var Tour = function() {
mapboxgl.accessToken = 'pk.eyJ1IjoidHdlbGNoIiwiYSI6Il9pX3dtb3cifQ.YcYnsO0X2p3x0HpHPFfleg';
};
Tour.prototype.start = function() {
this.map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/twelch/cihxvsxy000oeaikoz39jjthw',
center: [-122.580067, 45.548602],
zoom: 14,
minzoom: 18
});
this.map.on('style.load', function () {
this.startTour();
}.bind(this));
};
Tour.prototype.startTour = function() {
var time = 0;
var timePerStep = 100;
var interval = setInterval(function() {
time += timePerStep;
if (time === 2000) {this.easeIn();}
if (time === 4500) {this.startPerson();}
else if (time === 4100) {this.startVideo('front');}
else if (time === 4200) {this.startVideo('back');}
else if (time === 14000) {this.stopPerson();}
else if (time === 14100) {this.stopVideo('front');}
else if (time === 14200) {this.stopVideo('back');}
else if (time === 14300) {this.easeOut();}
}.bind(this), timePerStep);
};
Tour.prototype.easeIn = function(text) {
this.map.easeTo({
zoom: 20,
duration: 3000
});
}
Tour.prototype.easeOut = function(text) {
this.map.easeTo({
zoom: 17,
bearing: 45,
pitch: 50,
duration: 5000
});
}
Tour.prototype.startPerson = function() {
var iPath = turf.linestring([
[ -122.579915799999981, 45.5485545, 0.0 ], [ -122.57994530000002, 45.5485536, 0.0 ], [ -122.579944, 45.5485996, 0.0 ], [ -122.580011, 45.54860810000001, 0.0 ], [ -122.5799507, 45.5486043, 0.0 ], [ -122.579954700000016, 45.5485489, 0.0 ], [ -122.579903799999983, 45.5485489, 0.0 ]
]);
var iPathLength = turf.lineDistance(iPath, 'miles');
var iPoint = turf.along(iPath, 0, 'miles');
var iPathSoFar = turf.linestring([]);
this.map.addSource("pDot", {
"type": "geojson",
"data": iPoint,
"maxzoom": 20
});
this.map.addLayer({
"id": "pDot",
"type": "symbol",
"source": "pDot",
"layout": {
"icon-image": "person-gray",
"icon-size": {
"base": 0.5,
"stops": [
[0,0.01],
[15,0.01],
[20,0.5]]
},
},
"paint": {},
"interactive": true
});
var step = 0;
var numSteps = 400; //Change this to set animation resolution
var timePerStep = 25; //Change this to alter animation speed
var pDotSource = this.map.getSource('pDot');
this.personinterval = setInterval(function() {
step += 1;
if (step > numSteps) {
clearInterval(this.personInterval);
} else {
var curDistance = step / numSteps * iPathLength;
var iPoint = turf.along(iPath, curDistance, 'miles');
pDotSource.setData(iPoint);
iPathSoFar.geometry.coordinates.push(iPoint.geometry.coordinates);
if (iPathSoFar.geometry.coordinates.length === 2) {
this.pLineSource = new mapboxgl.GeoJSONSource({
"type": "geojson",
"data": iPathSoFar,
"maxzoom": 20
});
this.map.addSource("pLine", this.pLineSource);
this.map.addLayer({
"id": "pLine",
"type": "line",
"source": "pLine",
"layout": {
"line-join": "round",
"line-cap": "round"
},
"paint": {
"line-color": "#4999D2",
"line-opacity": "0.6",
"line-width": 6,
"line-dasharray": [0.25,2],
"line-blur": 1
}
}, "pDot");
}
if (iPathSoFar.geometry.coordinates.length >= 2) {
this.pLineSource.setData(iPathSoFar);
}
}
}.bind(this), timePerStep);
};
Tour.prototype.stopPerson = function() {
this.map.removeLayer('pLine');
this.map.removeLayer('pDot');
this.map.removeSource('pLine');
this.map.removeSource('pDot');
clearInterval(this.personInterval);
};
Tour.prototype.startVideo = function(name) {
var videos = {
'front': {
"urls": ["https://cldup.com/KpVLU35SZv.webm", "https://cldup.com/B-IRzEIuWZ.mp4"],
"coordinates": [
[-122.580085, 45.548739],
[-122.579842, 45.548739],
[-122.579842, 45.548654],
[-122.580085, 45.548654]
]
},
'back': {
"urls": ["https://cldup.com/3JLxT-6DlL.webm"],
"coordinates": [
[-122.580393, 45.548566],
[-122.580200, 45.548566],
[-122.580200, 45.548498],
[-122.580393, 45.548498]
]
}
};
var videoParams = videos[name];
var videoSrc = new mapboxgl.VideoSource(videoParams);
videoSrc.on('change', function (e) {
this.video = videoSrc.getVideo();
}.bind(this));
this.map.addSource(name, videoSrc);
this.map.addLayer({
"id": name,
"type": "raster",
"source": name
});
};
Tour.prototype.stopVideo = function(name) {
this.video.pause();
this.map.removeLayer(name);
this.map.removeSource(name);
};
var tour = new Tour();
tour.start();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment