2015-09-05 68 views
-1

無法縮放或放大jointjs中的紙張。我不斷收到一個錯誤:paper is not defined在這一行var svgPoint = paper.svg.createSVGPoint();裏面的函數offsetToLocalPoint爲什麼這個javascript變量未定義?

論文是在第一點定義的,我不確定哪裏出了問題?

代碼:

var clientX, clientY, offsetX, offsetY; 
var x, y; 
var shapeId = ""; 
var paper = ""; 
var graph = new joint.dia.Graph; 
paper = new joint.dia.Paper({ 
    el: $('#canvas'), 
    id: 'myPaper', 
    model: graph, 
    gridSize: 1, 
    defaultLink: new joint.shapes.devs.Link({ 
     router: {name: 'orthogonal', args: {maximumLoops: 1000, excludeTypes: ['container']}}, 
     connector: {name: 'rounded'}, 
     attrs: {'.marker-target': {d: 'M 10 0 L 0 5 L 10 10 z'}}, 
     labels: [{position: .5, attrs: {text: {text: 'label', 'font-size': '13', 'stroke': 'black', 'stroke-width': '0.1'}}}] 
    }), 
    validateConnection: function (cellViewS, magnetS, cellViewT, magnetT, end, linkView) { 
     if (magnetS && magnetS.getAttribute('type') === 'input') 
      return true; 
     if (cellViewS === cellViewT) 
      return false; 
     return (magnetS !== magnetT); 
     return magnetT && magnetT.getAttribute('type') === 'input'; 
    }, 
    markAvailable: true 
}); 

//paper zooming/scaling 
paper.$el.on('mousewheel DOMMouseScroll', onMouseWheel); 

function onMouseWheel(e) { 
    console.log("hi"); 

    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); // offsetX is not defined in FF 
    var offsetY = (e.offsetY || e.clientY - $(this).offset().top); // offsetY is not defined in FF 
    var p = offsetToLocalPoint(offsetX, offsetY); 
    var newScale = V(paper.viewport).scale().sx + delta; // the current paper scale changed by delta 

    if (newScale > 0.4 && newScale < 2) { 
     paper.setOrigin(0, 0); // reset the previous viewport translation 
     paper.scale(newScale, newScale, p.x, p.y); 
    } 
} 
function offsetToLocalPoint(offsetX, offsetY, paper) { 
    var svgPoint = paper.svg.createSVGPoint(); 
    svgPoint.x = offsetX; 
    svgPoint.y = offsetY; 
    var offsetTransformed = svgPoint.matrixTransform(paper.viewport.getCTM().inverse()); 
    return offsetTransformed; 
} 
+0

爲什麼downvote?評論會有幫助 – Satyadev

回答

1

當你調用offsetToLocalPoint你需要paper傳遞,因爲你宣佈它作爲offsetToLocalPoint

var p = offsetToLocalPoint(offsetX, offsetY); 

參數應該是

var p = offsetToLocalPoint(offsetX, offset, paper); 
相關問題