svg缩放移动、移动轨迹追溯_svg-pan-zoom平移/缩放到任何对象

I'm using the svg-pan-zoom code https://github.com/ariutta/svg-pan-zoom to make an svg map of some kind, now it is time to add a feature to pan & zoom to an art component of the svg on a click event. However, I'm not sure how to use the panBy() function to get to a desired svg art item: I tried to use the getBBox() on the group I'm looking to pan to and use that with the panZoomInstance.getPan() and getSizes() information, but my experiments are not working out.

I'd like to accomplish the same king of animation as their example (http://ariutta.github.io/svg-pan-zoom/demo/simple-animation.html) but center the viewport to the item.

解决方案

Against all odds I was able to figure out this part of it!

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);

}

The key was in calculating the difference between the center of the viewport width & height from panZoomInstance.getSizes() and the center of the target element's client bounding rectangle.

Now the issue is trying to make an animated zoom. For now I've made it do a zoom to a specified location with a command at the end of the panning animation and set some css to make the zoom a smooth transition. The css gets removed after some time interval so normal zooming and panning isn't affected. In my attempts to make the zoom a step animation it always appeared to zoom past the intended max point.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值