2016-10-22 185 views
2

我已經成功地實現縮放特徵基於this examplePaperJS縮放和平移

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視圖的位置。我的意思是我希望觀點能夠堅持畫布邊緣。

下面是截圖enter image description here

回答

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