2014-03-30 78 views

回答

6

聯合文件有一個方法叫做規模()

我有縮放(變焦)我的圖表此實現:

var graphScale = 1; 

var paperScale = function(sx, sy) { 
    paper.scale(sx, sy); 
}; 

var zoomOut = function() { 
    graphScale -= 0.1; 
    paperScale(graphScale, graphScale); 
}; 

var zoomIn = function() { 
    graphScale += 0.1; 
    paperScale(graphScale, graphScale); 
}; 

var resetZoom = function() { 
    graphScale = 1; 
    paperScale(graphScale, graphScale); 
}; 

也許這就是你需要什麼?

4

paper.$el.on('mousewheel DOMMouseScroll', function onMouseWheel(e) { 
 
    //function onMouseWheel(e){ 
 
    e.preventDefault(); 
 
    e = e.originalEvent; 
 

 
    var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)))/50; 
 
    var offsetX = (e.offsetX || e.clientX - $(this).offset().left); 
 

 
    var offsetY = (e.offsetY || e.clientY - $(this).offset().top); 
 
    var p = offsetToLocalPoint(offsetX, offsetY); 
 
    var newScale = V(paper.viewport).scale().sx + delta; 
 
    console.log(' delta' + delta + ' ' + 'offsetX' + offsetX + 'offsety--' + offsetY + 'p' + p.x + 'newScale' + newScale) 
 
    if (newScale > 0.4 && newScale < 2) { 
 
    paper.setOrigin(0, 0); 
 
    paper.scale(newScale, newScale, p.x, p.y); 
 
    } 
 
}); 
 

 
function offsetToLocalPoint(x, y) { 
 
    var svgPoint = paper.svg.createSVGPoint(); 
 
    svgPoint.x = x; 
 
    svgPoint.y = y; 
 

 
    var pointTransformed = svgPoint.matrixTransform(paper.viewport.getCTM().inverse()); 
 
    return pointTransformed; 
 
}

+0

哦,我的上帝,我一直在掙扎了一會兒現在(不算長,但足夠長的時間讓惱人的),那一條線,原點設置爲( 0,0)在縮放之前,就是我所需要的。謝謝。它的工作方式還有一些缺陷,但是你爲我節省了一些痛苦。 –