Mapbox相机动画整理(3)动作播放整理

播放整理

简单播放

随着时间的递增,不断刷新数据

function animateMarker(timestamp) {
  map.getSource('point').setData(pointOnCircle(timestamp / 1000));

  // Request the next frame of the animation.
  requestAnimationFrame(animateMarker);
}

// Start the animation.
animateMarker(0);

循环播放

在一定时间内,循环播放

var animationDuration = 80000; 
var start;
 
function frame(time) {
	// time 该时间戳是一个十进制数,单位毫秒,最小精度为1ms
	if (!start) start = time;
	// phase determines how far through the animation we are
	var phase = (time - start) / animationDuration;
	 
	// phase is normalized between 0 and 1
	// when the animation is finished, reset start to loop the animation
	if (phase > 1) {
		// wait 1.5 seconds before looping
		setTimeout(function () {
			start = 0.0;
		}, 1500);
	}
 
	// 地图动画 。。。
 	// 根据phase,变化对应的地理要素
 	// 可参考 https://docs.mapbox.com/mapbox-gl-js/example/free-camera-path/
 	
 	window.requestAnimationFrame(frame);
}
window.requestAnimationFrame(frame);

多个动作连续播放

  1. 多个动作连续播放
  2. 单个动作播放时,平滑过渡
// 可参考 https://docs.mapbox.com/mapbox-gl-js/example/free-camera-point/
var animations = [
  {
    duration: 4000.0,
    animate: function (phase) {
      var start = [138.73375, 35.41914];
      var end = [138.72649, 35.33974];
      var alt = [7000.0, 6000.0];

      // interpolate camera position while keeping focus on a target lat/lng
      var position = lerp(start, end, phase);
      var altitude = lerp(alt[0], alt[1], phase);
      var target = [138.73036, 35.36197];

      updateCameraPosition(position, altitude, target);
    }
  }
]
var lastTime = 0.0;
function frame(time) {
	animationIndex %= animations.length;
	var current = animations[animationIndex];
	 
	if (animationTime < current.duration) {
		current.animate(animationTime / current.duration);
	}
	 
	// allow requestAnimationFrame to control the speed of the animation
	animationTime += 1 / (time - lastTime);
	lastTime = time;
	 
	if (animationTime > current.duration) {
		animationIndex++;
		animationTime = 0.0;
	}
	 
	window.requestAnimationFrame(frame);
}
 
window.requestAnimationFrame(frame);

重播

在这里插入图片描述

// Number of steps to use in the arc and animation, more steps means
// a smoother arc and animation, but too many steps will result in a
// low frame rate
var steps = 500;
// Used to increment the value of the point measurement against the route.
var counter = 0;

function animate() {
  // Update the source with this new data
  map.getSource('point').setData(point);

  // Request the next frame of animation as long as the end has not been reached
  if (counter < steps) {
    requestAnimationFrame(animate);
  }

  counter = counter + 1;
}

document
  .getElementById('replay')
  .addEventListener('click', function () {
    // Update the source layer
    map.getSource('point').setData(point);

    // Reset the counter
    counter = 0;

    // Restart the animation
    animate(counter);
  });

// Start the animation
animate(counter);

播放与暂停控制

在这里插入图片描述

var speedFactor = 30; // number of frames per longitude degree
var animation; // to store and cancel the animation
var startTime = 0;
var progress = 0; // progress = timestamp - startTime
var resetTime = false; // indicator of whether time reset is needed for the animation
var pauseButton = document.getElementById('pause');

startTime = performance.now();

animateLine();

// click the button to pause or play
pauseButton.addEventListener('click', function () {
  pauseButton.classList.toggle('pause');
  if (pauseButton.classList.contains('pause')) {
  	// 停止动画
    cancelAnimationFrame(animation);
  } else {
    resetTime = true;
    animateLine();
  }
});

// reset startTime and progress once the tab loses or gains focus
// requestAnimationFrame also pauses on hidden tabs by default
document.addEventListener('visibilitychange', function () {
  resetTime = true;
});

// animated in a circle as a sine wave along the map.
function animateLine(timestamp) {
  if (resetTime) {
    // resume previous progress
    startTime = performance.now() - progress;
    resetTime = false;
  } else {
    progress = timestamp - startTime;
  }

  // restart if it finishes a loop
  if (progress > speedFactor * 360) {
    startTime = timestamp;
    // 将线数据置空
    geojson.features[0].geometry.coordinates = [];
  } else {
    var x = progress / speedFactor;
    // draw a sine wave with some math.
    var y = Math.sin((x * Math.PI) / 90) * 40;
    // append new coordinates to the lineString
    geojson.features[0].geometry.coordinates.push([x, y]);
    // then update the map
    map.getSource('line').setData(geojson);
  }
  // Request the next frame of the animation.
  animation = requestAnimationFrame(animateLine);
}

动作播放完后进入下一个动作

function playback(index) {
	// Animate the map position based on camera properties
	map.flyTo(locations[index].camera);
	 
	map.once('moveend', function () {
		// Duration the slide is on screen after interaction
		window.setTimeout(function () {
			// Increment index
			index = index + 1 === locations.length ? 0 : index + 1;
			playback(index);
		}, 3000); // After callback, show the location for 3 seconds.
	});
}
// Start the playback animation for each borough
playback(0);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值