我想我找到了解决方案:我将小提琴从@freejosh 扩展到也可以使用回调(例如调整大小):诀窍是在回调函数中重新获取正确的范围:
http://jsfiddle.net/rassoh/mx9n3vsf/7/
var mypapers = [];
initPaper(0, $("#canvas1")[0]);
initPaper(1, $("#canvas2")[0]);
function initPaper(id, canvasElement) {
var mousePosition = new paper.Point(0,0);
mypapers[id] = new paper.PaperScope();
paper = mypapers[id];
paper.setup(canvasElement);
var myCircle;
createCircle = function() {
if( "undefined" !== typeof myCircle) {
myCircle.remove();
}
// this function is called on resize, so we have to re-fetch the scope!
paper = mypapers[id];
myCircle = new paper.Path.Circle(30, 30, 20);
var lightRed = new paper.Color(1, 0.5, 0.5);
var lightBlue = new paper.Color(0.5, 0.5, 1);
myCircle.style = {
fillColor: id === 0 ? lightRed : lightBlue,
strokeColor: "black"
};
}
createCircle();
var tool = new paper.Tool();
tool.onMouseMove = function(event) {
mousePosition = event.lastPoint;
};
paper.view.onFrame = function() {
if( "undefined" === typeof myCircle) {
return;
}
var dist = mousePosition.subtract( myCircle.position );
dist = dist.divide( 3 );
myCircle.position = myCircle.position.add( dist );
};
paper.view.onResize = function() {
createCircle();
};
}
$(window).resize(function() {
var width = $(".container").width() / 2;
var height = $(".container").height();
// this automatically triggeres paper's onResize event registered above
mypapers[0].view.viewSize = new paper.Size( width, height );
mypapers[1].view.viewSize = new paper.Size( width, height );
});
请注意,我还包括了与圈子的简单交互,以测试那里的正确行为。