OpenLayers3-2-View Animation

原文地址:http://openlayers.org/en/v3.12.1/examples/animation.html
This example shows how to use the beforeRender function on the Map to run one or more animations.

Related API documentation:
ol.Map,ol.View,ol.animation,ol.control,ol.layer.Tile,ol.proj,ol.source.OSM

map.beforeRender(var_args)
Add functions to be called before rendering. This can be used for attaching animations before updating the map’s view. The ol.animation namespace provides several static methods for creating prerender functions.

翻译:
这个例子主要演示了如何在map上使用beforeRender函数来展示动画效果

相关的api文档包括:
ol.Map,ol.View,ol.animation,ol.control,ol.layer.Tile,ol.proj,ol.source.OSM

map.beforeRender(var_args)
在渲染前调用这个函数。这个函数可以在更新map view之前制作动画效果。ol.animation命名空间提供了几个静态方法来创造渲染前的函数。

<!DOCTYPE html>
<html>
  <head>
    <title>View Animation</title>
    <link rel="stylesheet" href="http://openlayers.org/en/v3.12.1/css/ol.css" type="text/css">
    <script src="http://openlayers.org/en/v3.12.1/build/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <button id="rotate-left" title="Rotate clockwise"></button>
    <button id="rotate-right" title="Rotate counterclockwise"></button>
    <button id="rotate-around-rome">Rotate around Rome</button>
    <button id="pan-to-london">Pan to London</button>
    <button id="elastic-to-moscow">Elastic to Moscow</button>
    <button id="bounce-to-istanbul">Bounce to Istanbul</button>
    <button id="spin-to-rome">Spin to Rome</button>
    <button id="fly-to-bern">Fly to Bern</button>
    <button id="spiral-to-madrid">Spiral to Madrid</button>
    <script>
      // from https://github.com/DmitryBaranovskiy/raphael
      function bounce(t) {
        var s = 7.5625, p = 2.75, l;
        if (t < (1 / p)) {
          l = s * t * t;
        } else {
          if (t < (2 / p)) {
            t -= (1.5 / p);
            l = s * t * t + 0.75;
          } else {
            if (t < (2.5 / p)) {
              t -= (2.25 / p);
              l = s * t * t + 0.9375;
            } else {
              t -= (2.625 / p);
              l = s * t * t + 0.984375;
            }
          }
        }
        return l;
      }

      // from https://github.com/DmitryBaranovskiy/raphael
      function elastic(t) {
        return Math.pow(2, -10 * t) * Math.sin((t - 0.075) * (2 * Math.PI) / 0.3) + 1;
      }
      //定义各个地点坐标
      var london = ol.proj.fromLonLat([-0.12755, 51.507222]);
      var moscow = ol.proj.fromLonLat([37.6178, 55.7517]);
      var istanbul = ol.proj.fromLonLat([28.9744, 41.0128]);
      var rome = ol.proj.fromLonLat([12.5, 41.9]);
      var bern = ol.proj.fromLonLat([7.4458, 46.95]);
      var madrid = ol.proj.fromLonLat([-3.683333, 40.4]);

      var view = new ol.View({
        // the view's initial state
        center: istanbul,
        zoom: 6
      });

      var map = new ol.Map({
        layers: [
          new ol.layer.Tile({
            //提前加载的比例级别
            preload: 4,
            source: new ol.source.OSM()
          })
        ],
        // Improve user experience by loading tiles while animating. Will make
        // animations stutter on mobile or slow devices.
        loadTilesWhileAnimating: true,
        target: 'map',
        controls: ol.control.defaults({
          attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
            collapsible: false
          })
        }),
        view: view
      });

      var rotateLeft = document.getElementById('rotate-left');
      rotateLeft.addEventListener('click', function() {
        var rotateLeft = ol.animation.rotate({
          //动画持续时间
          duration: 2000,
          //顺时针2圈
          rotation: -4 * Math.PI
        });
        map.beforeRender(rotateLeft);
      }, false);
      var rotateRight = document.getElementById('rotate-right');
      rotateRight.addEventListener('click', function() {
        var rotateRight = ol.animation.rotate({
          duration: 2000,
          rotation: 4 * Math.PI
        });
        map.beforeRender(rotateRight);
      }, false);

      var rotateAroundRome = document.getElementById('rotate-around-rome');
      rotateAroundRome.addEventListener('click', function() {
        //获取当前旋转度数
        var currentRotation = view.getRotation();
        var rotateAroundRome = ol.animation.rotate({
          //锚点 rome
          anchor: rome,
          duration: 1000,
          rotation: currentRotation
        });
        map.beforeRender(rotateAroundRome);
        //使用view rotate方法来进行旋转 不是rotateAroundRome进行旋转了
        view.rotate(currentRotation + (Math.PI / 2), rome);
      }, false);

      //2秒平移到伦敦
      var panToLondon = document.getElementById('pan-to-london');
      panToLondon.addEventListener('click', function() {
        var pan = ol.animation.pan({
          duration: 2000,
          source: /** @type {ol.Coordinate} */ (view.getCenter())
        });
        map.beforeRender(pan);
        //平移
        view.setCenter(london);
      }, false);

      //动画效果为擦除
      var elasticToMoscow = document.getElementById('elastic-to-moscow');
      elasticToMoscow.addEventListener('click', function() {
        var pan = ol.animation.pan({
          duration: 2000,
          easing: elastic,
          source: /** @type {ol.Coordinate} */ (view.getCenter())
        });
        map.beforeRender(pan);
        view.setCenter(moscow);
      }, false);

      //动画效果为弹跳
      var bounceToIstanbul = document.getElementById('bounce-to-istanbul');
      bounceToIstanbul.addEventListener('click', function() {
        var pan = ol.animation.pan({
          duration: 2000,
          easing: bounce,
          source: /** @type {ol.Coordinate} */ (view.getCenter())
        });
        map.beforeRender(pan);
        view.setCenter(istanbul);
      }, false);

      //
      var spinToRome = document.getElementById('spin-to-rome');
      spinToRome.addEventListener('click', function() {
        var duration = 2000;
        var start = +new Date();
        console.log(start);
        var pan = ol.animation.pan({
          duration: duration,
          source: /** @type {ol.Coordinate} */ (view.getCenter()),
          //开始时间
          start: start
        });
        var rotate = ol.animation.rotate({
          duration: duration,
          rotation: 2 * Math.PI,
          start: start
        });
        map.beforeRender(pan, rotate);
        view.setCenter(rome);
      }, false);

      var flyToBern = document.getElementById('fly-to-bern');
      flyToBern.addEventListener('click', function() {
        var duration = 2000;
        var start = +new Date();
        var pan = ol.animation.pan({
          duration: duration,
          source: /** @type {ol.Coordinate} */ (view.getCenter()),
          start: start
        });
        //缩放效果
        var bounce = ol.animation.bounce({
          duration: duration,
          resolution: 4 * view.getResolution(),
          start: start
        });
        map.beforeRender(pan, bounce);
        view.setCenter(bern);
      }, false);

      var spiralToMadrid = document.getElementById('spiral-to-madrid');
      spiralToMadrid.addEventListener('click', function() {
        var duration = 2000;
        var start = +new Date();
        var pan = ol.animation.pan({
          duration: duration,
          source: /** @type {ol.Coordinate} */ (view.getCenter()),
          start: start
        });
        var bounce = ol.animation.bounce({
          duration: duration,
          resolution: 2 * view.getResolution(),
          start: start
        });
        var rotate = ol.animation.rotate({
          duration: duration,
          rotation: -4 * Math.PI,
          start: start
        });
        map.beforeRender(pan, bounce, rotate);
        view.setCenter(madrid);
      }, false);
    </script>
  </body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值