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

问题描述 投票:0回答:1

[嗨,我是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);

为什么不使用回调函数时返回纸张Raphael对象?

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

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

它没有回调。

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

这里是关于这个问题的小提琴:jsfiddle.net/svb0y2un/

raphael
1个回答
0
投票

我不太确定您的意图是否正常,因为您在回调之外发出警报,因此可能无法定义文件(因为回调可能在此之后被调用。)>

我想,您需要了解的是,回调函数已通过paper元素作为函数的“上下文”传递。这意味着它将被传递到回调内部的“ 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

© www.soinside.com 2019 - 2024. All rights reserved.