使用回调函数创建拉斐尔时获取纸张对象

getting paper object when raphael is created with callback function

您好,我是 raphael 的初学者,有疑问。 每当我创建带有回调函数的 raphael 对象时(如下所示),调用都不会返回纸对象。

var w = 1000;
var h = 400;
var paper = Raphael('svgContainer', w, h, function(){
    console.log('callback');
});
paper.setViewBox(0,0,w,h,true);

为什么使用回调函数时paper没有返回raphael对象?

https://dmitrybaranovskiy.github.io/raphael/reference.html#Raphael

每当我使用回调函数时 paper.setViewBox 都会失败,因为纸张是 eve 的某种 On() 事件函数。

没有回调也能正常工作。

var paper = Raphael('svgContainer', w, h);

这里是关于这个问题的fiddle: jsfiddle.net/svb0y2un/

我不太确定你的 none 你的意图的工作代码,因为你在回调之外发出警报,所以可能没有定义纸张(因为回调可能会在之后调用)。

我 'think' 您需要了解回调函数作为函数的 'context' 传递给纸元素。这意味着它将被传递到回调中的 'this' 变量中。因此,例如这应该有效....

var paper = Raphael('svgContainer', w, h, function(){
    console.log('callback');
    alert('working typeof of paper: '+typeof(this));
    this.setViewBox(0,0,w,h,true);
});

jsfiddle