gpt4 book ai didi

javascript - PaperJS 缩放和平移

转载 作者:行者123 更新时间:2023-11-29 10:07:41 25 4
gpt4 key购买 nike

我已经成功实现了基于 this example 的缩放功能

 zoom: function(delta, point){
if (!delta) return;

var
oldZoom = paper.view.zoom,
oldCenter = paper.view.center,
viewPos = paper.view.viewToProject(point);
newZoom = delta > 0 ? oldZoom * 1.05 : oldZoom / 1.05;

if (!this.allowedZoom(newZoom)) return;

var
zoomScale = oldZoom / newZoom,
centerAdjust = viewPos.subtract(oldCenter),
offset = viewPos.subtract(centerAdjust.multiply(zoomScale)).subtract(oldCenter);

paper.view.center = view.center.add(offset);
},

allowedZoom: function(zoom){
zoom = Math.max(zoom, this.settings.minZoom);
zoom = Math.min(zoom, this.settings.maxZoom);

if (zoom !== paper.view.zoom)
{
paper.view.zoom = zoom;
return zoom;
}
return null;
}

然而,当您缩小回到原始 scale = 1 时,这并没有考虑 View 的位置。我的意思是我希望 View 坚持 Canvas 边缘。

这是一个截图 enter image description here

最佳答案

如果其他人偶然发现了这个问题,这里就是答案。关键是简单地检查 View 的边界然后相应地调整:

    bounds = paper.view.bounds;

if (bounds.x < 0) paper.view.center = paper.view.center.subtract(new Point(bounds.x, 0));
if (bounds.y < 0) paper.view.center = paper.view.center.subtract(new Point(0, bounds.y));

bounds = paper.view.bounds;
var
w = bounds.x + bounds.width,
h = bounds.y + bounds.height;

if (w > paper.view.viewSize.width) paper.view.center = paper.view.center.subtract(new Point(w - paper.view.viewSize.width, 0));
if (h > paper.view.viewSize.height) paper.view.center = paper.view.center.subtract(new Point(0, h - paper.view.viewSize.height));

关于javascript - PaperJS 缩放和平移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40189777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com