Javascript 纸张缩放和平移

Javascript 纸张缩放和平移,javascript,canvas,zooming,paperjs,pan,Javascript,Canvas,Zooming,Paperjs,Pan,我已经成功实现了基于的缩放功能 但是,当您缩小到原始的scale=1时,这并不考虑视图的位置。我的意思是,我想把视图粘贴到画布边缘 这是一个屏幕截图,如果有人偶然发现这个问题,这里有答案。关键是检查视图的边界,然后进行相应调整: bounds = paper.view.bounds; if (bounds.x < 0) paper.view.center = paper.view.center.subtract(new Point(bounds.x, 0)); i

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

但是,当您缩小到原始的
scale=1
时,这并不考虑视图的位置。我的意思是,我想把视图粘贴到画布边缘


这是一个屏幕截图,如果有人偶然发现这个问题,这里有答案。关键是检查视图的边界,然后进行相应调整:

    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));
bounds=paper.view.bounds;
如果(bounds.x<0)paper.view.center=paper.view.center.subtract(新点(bounds.x,0));
如果(bounds.y<0)paper.view.center=paper.view.center.subtract(新点(0,bounds.y));
边界=paper.view.bounds;
变量
w=bounds.x+bounds.width,
h=边界.y+边界.height;
如果(w>paper.view.viewSize.width)paper.view.center=paper.view.center.subtract(新点(w-paper.view.viewSize.width,0));
如果(h>paper.view.viewSize.height)paper.view.center=paper.view.center.subtract(新点(0,h-paper.view.viewSize.height));

为什么两次给边界赋值?
    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));