【问题标题】:svg-pan-zoom pan/zoom to any objectsvg-pan-zoom 平移/缩放到任何对象
【发布时间】:2018-08-25 23:04:38
【问题描述】:

我正在使用 svg-pan-zoom 代码 https://github.com/ariutta/svg-pan-zoom 制作某种 svg 地图,现在是时候在点击事件中添加平移和缩放到 svg 的艺术组件的功能了。但是,我不确定如何使用 panBy() 函数来获取所需的 svg 艺术项目:我尝试在要平移的组上使用 getBBox() 并将其与 panZoomInstance.getPan 一起使用() 和 getSizes() 信息,但我的实验没有成功。 我想完成与他们的示例相同的动画之王 (http://ariutta.github.io/svg-pan-zoom/demo/simple-animation.html),但将视口居中放置在项目上。

【问题讨论】:

    标签: javascript svg


    【解决方案1】:

    我不顾一切地想出了这部分!

    function customPanByZoomAtEnd(amount, endZoomLevel, animationTime){ // {x: 1, y: 2}
      if(typeof animationTime == "undefined"){
          animationTime =   300; // ms
      }
      var animationStepTime = 15 // one frame per 30 ms
        , animationSteps = animationTime / animationStepTime
        , animationStep = 0
        , intervalID = null
        , stepX = amount.x / animationSteps
        , stepY = amount.y / animationSteps;
    
      intervalID = setInterval(function(){
        if (animationStep++ < animationSteps) {
          panZoomInstance.panBy({x: stepX, y: stepY})
        } else {
          // Cancel interval
          if(typeof endZoomLevel != "undefined"){
            var viewPort = $(".svg-pan-zoom_viewport")[0];
            viewPort.style.transition = "all " + animationTime / 1000 + "s ease";
            panZoomInstance.zoom(endZoomLevel);
            setTimeout(function(){
                viewPort.style.transition = "none";
                $("svg")[0].style.pointerEvents = "all"; // re-enable the pointer events after auto-panning/zooming.
                        panZoomInstance.enablePan();
                        panZoomInstance.enableZoom();
                        panZoomInstance.enableControlIcons();
                        panZoomInstance.enableDblClickZoom();
                        panZoomInstance.enableMouseWheelZoom();
            }, animationTime + 50);
          }
          clearInterval(intervalID)
        }
      }, animationStepTime)
    }
    
    function panToElem(targetElem) {
    
        var initialSizes = panZoomInstance.getSizes();
        var initialLoc = panZoomInstance.getPan();
        var initialBounds = targetElem.getBoundingClientRect();
        var initialZoom = panZoomInstance.getZoom();
        var initialCX = initialBounds.x + (initialBounds.width / 2);
        var initialCY = initialBounds.y + (initialBounds.height / 2);
    
        var dX = (initialSizes.width / 2) - initialCX;
        var dY = (initialSizes.height / 2) - initialCY;
    
      customPanByZoomAtEnd({x: dX, y: dY}, 2, 700);
    }
    

    关键在于计算panZoomInstance.getSizes() 的视口宽度和高度的中心与目标元素的客户端边界矩形的中心之间的差异。

    现在的问题是尝试进行动画缩放。现在,我在平移动画结束时使用命令使其缩放到指定位置,并设置一些 css 以使缩放平滑过渡。 css 会在一段时间后被删除,因此正常的缩放和平移不会受到影响。在我尝试使缩放成为步进动画时,它似乎总是缩放超过预期的最大点。

    【讨论】:

      猜你喜欢
      • 2019-04-26
      • 1970-01-01
      • 2019-10-21
      • 2016-02-10
      • 1970-01-01
      • 2020-10-21
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多