为什么这个javascript变量未定义?

时间:2015-09-05 13:33:17

标签: javascript

无法在jointjs中缩放或缩放纸张。我在函数paper is not defined内的第var svgPoint = paper.svg.createSVGPoint();行发出错误:offsetToLocalPoint

论文是在第一点定义的,我不确定出了什么问题?

代码:

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

1 个答案:

答案 0 :(得分:1)

当您致电offsetToLocalPoint时,您需要传递paper,因为您已将其声明为offsetToLocalPoint的参数

var p = offsetToLocalPoint(offsetX, offsetY);

应该是

var p = offsetToLocalPoint(offsetX, offset, paper);