BHoft
BHoft

Reputation: 1663

getting paper object when raphael is created with callback function

Hi I am a beginner with raphael and have question. Whenever I create a raphael object with a callback function (like below), the paper object isn't returned by the call.

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

Why isn't paper the raphael object returned when a callback function is used?

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

Whenever i use a callback function paper.setViewBox fails because paper is some kind of On() event function of eve.

It works without the callback.

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

here is the fiddle about this problem: jsfiddle.net/svb0y2un/

Upvotes: 0

Views: 67

Answers (1)

Ian
Ian

Reputation: 13852

I'm not quite sure about your none working code of your intent, as you are alerting outside of the callback, so paper may not be defined (as the callback may be called after that).

I 'think' what you need is to understand that the callback function is passed the paper element as the 'context' for the function. This means it will be passed into the 'this' variable inside the callback. So for example this should work....

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

Upvotes: 1

Related Questions