how to access PaperScript scope from JavaScript

614 views
Skip to first unread message

Irakli

unread,
Jul 15, 2012, 12:50:01 PM7/15/12
to pap...@googlegroups.com
Hey guys

Here is the problem

I have one PaperScript file and one JavaScript file, they are independent but sometimes I want to call functions defined in paperscript file from javascript

Now I am doing this:

view.pjs (PaperScript)

window.view = { 
  paint: function(){
  }

main.js (Javascript)

view.paint();

so.. what is the best way to get a PaperScript scope to call some functions?

Thanks

Ryan Beasley

unread,
Jul 20, 2012, 3:08:20 PM7/20/12
to pap...@googlegroups.com
I'm not sure I understand exactly what you want.  In the first case, your javascript file can access window.paper to call functions on the project, e.g., window.paper.project.activeLayer.remove();

Second, if you are trying to access functions that you defined then your above example works...as long as the paperscript file has been loaded and parsed before your javascript runs.
Your view.pjs may not be loaded until after both $(document).ready and $(window).load, where I normally stick my javascript. (See http://ablogaboutcode.com/2011/06/14/how-javascript-loading-works-domcontentloaded-and-onload/ for a similar experience with AJAX.)
To get around that, I just followed this answer http://stackoverflow.com/a/7247912/783037 :  At the end of your paperscript file, paper.pjs, call a function attached to the global object, e.g., window.paperscriptLoaded();
Then in your javascript file, main.js, define that function so that it calls the code you need to run, e.g., window.paperscriptLoaded = function() {window.view.paint();}

~Ryan
Reply all
Reply to author
Forward
0 new messages