Paper.js comes with its own flavor of JavaScript called PaperScript. PaperScript does two things: It executes your code in its own scope, where all Paper.js classes appear to be global (without polluting the actual global scope), but more importantly it adds operator overloading to the JavaScript language.

Operator overloading in PaperScript works by amending the code that you have written, and compiling it using new Function() . This approach is preferred over the somewhat evil eval() , since code dynamically compiled as its own function with no connection to the local scope can still be fully optimized by the JavaScript engine. Before looking at how this translation is accomplished, here an example of what happens behind the scenes:

Whenever you have a piece of code that contains a mathematical operator, this operation is replaced with a special function call. You can try it out on sketch.paperjs.org:

When you execute this PaperScript code with the developer console open, the debugger; statement should trigger a debugging session, where you can see how the executed code actually looks: