2011-03-06 19:50:44 -05:00
|
|
|
/*
|
|
|
|
* Paper.js
|
|
|
|
*
|
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
2011-03-07 20:41:50 -05:00
|
|
|
* http://paperjs.org/
|
2011-03-06 19:50:44 -05:00
|
|
|
* http://scriptographer.org/
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-03-06 19:50:44 -05:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* All rights reserved.
|
2011-03-06 19:50:44 -05:00
|
|
|
*/
|
|
|
|
|
2011-05-07 15:50:32 -04:00
|
|
|
var PaperScript = this.PaperScript = new function() {
|
2011-03-03 20:25:28 -05:00
|
|
|
//TODO: Make sure there are all the correct copyrights for the inlined parse-js:
|
|
|
|
//#include "../../lib/parse-js-min.js"
|
2011-03-03 17:55:18 -05:00
|
|
|
|
2011-03-07 06:10:45 -05:00
|
|
|
// Math Operators
|
2011-03-03 17:19:12 -05:00
|
|
|
|
|
|
|
var operators = {
|
|
|
|
'+': 'add',
|
|
|
|
'-': 'subtract',
|
|
|
|
'*': 'multiply',
|
|
|
|
'/': 'divide',
|
|
|
|
'%': 'modulo',
|
|
|
|
'==': 'equals',
|
|
|
|
'!=': 'equals'
|
|
|
|
};
|
|
|
|
|
2011-03-04 06:15:04 -05:00
|
|
|
function $eval(left, operator, right) {
|
2011-03-03 17:19:12 -05:00
|
|
|
var handler = operators[operator];
|
|
|
|
if (left && left[handler]) {
|
|
|
|
var res = left[handler](right);
|
|
|
|
return operator == '!=' ? !res : res;
|
|
|
|
}
|
|
|
|
switch (operator) {
|
|
|
|
case '+': return left + right;
|
|
|
|
case '-': return left - right;
|
|
|
|
case '*': return left * right;
|
|
|
|
case '/': return left / right;
|
|
|
|
case '%': return left % right;
|
|
|
|
case '==': return left == right;
|
|
|
|
case '!=': return left != right;
|
|
|
|
default:
|
|
|
|
throw new Error('Implement Operator: ' + operator);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-03-07 06:10:45 -05:00
|
|
|
// Sign Operators
|
2011-03-03 17:19:12 -05:00
|
|
|
|
2011-03-04 06:15:04 -05:00
|
|
|
var signOperators = {
|
2011-03-03 17:19:12 -05:00
|
|
|
'-': 'negate'
|
|
|
|
};
|
|
|
|
|
2011-03-03 20:22:08 -05:00
|
|
|
function $sign(operator, value) {
|
2011-03-04 06:15:04 -05:00
|
|
|
var handler = signOperators[operator];
|
2011-03-03 20:22:08 -05:00
|
|
|
if (value && value[handler]) {
|
|
|
|
return value[handler]();
|
2011-03-03 17:19:12 -05:00
|
|
|
}
|
|
|
|
switch (operator) {
|
2011-03-03 20:22:08 -05:00
|
|
|
case '+': return +value;
|
|
|
|
case '-': return -value;
|
2011-03-03 17:19:12 -05:00
|
|
|
default:
|
|
|
|
throw new Error('Implement Sign Operator: ' + operator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AST Helpers
|
|
|
|
|
|
|
|
function isDynamic(exp) {
|
|
|
|
var type = exp[0];
|
|
|
|
return type != 'num' && type != 'string';
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleOperator(operator, left, right) {
|
2011-03-03 20:23:47 -05:00
|
|
|
// Only replace operators with calls to $operator if the left hand side
|
|
|
|
// is potentially an object.
|
2011-03-03 17:19:12 -05:00
|
|
|
if (operators[operator] && isDynamic(left)) {
|
2011-03-03 20:23:47 -05:00
|
|
|
// Replace with call to $operator(left, operator, right):
|
2011-03-04 06:15:04 -05:00
|
|
|
return ['call', ['name', '$eval'],
|
2011-03-03 20:16:09 -05:00
|
|
|
[left, ['string', operator], right]];
|
2011-03-03 17:19:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-03 13:31:56 -05:00
|
|
|
function compile(code) {
|
2011-03-03 17:19:12 -05:00
|
|
|
// Use parse-js to translate the code into a AST structure which is then
|
|
|
|
// walked and parsed for operators to overload. The resulting AST is
|
|
|
|
// translated back to code and evaluated.
|
|
|
|
var ast = parse_js.parse(code),
|
|
|
|
walker = parse_js.walker(),
|
|
|
|
walk = walker.walk;
|
|
|
|
|
|
|
|
ast = walker.with_walkers({
|
|
|
|
'binary': function(operator, left, right) {
|
|
|
|
// Handle simple mathematical operators here:
|
|
|
|
return handleOperator(operator, left = walk(left),
|
|
|
|
right = walk(right))
|
|
|
|
// Always return something since we're walking left and
|
|
|
|
// right for the handleOperator() call already.
|
|
|
|
|| [this[0], operator, left, right];
|
|
|
|
},
|
|
|
|
|
|
|
|
'assign': function(operator, left, right) {
|
|
|
|
// Handle assignments like +=, -=, etc:
|
|
|
|
// Check if the assignment operator needs to be handled by paper
|
|
|
|
// if so, convert the assignment to a simple = and use result of
|
|
|
|
// of handleOperator on the right hand side.
|
|
|
|
var res = handleOperator(operator, left = walk(left),
|
|
|
|
right = walk(right));
|
|
|
|
if (res)
|
|
|
|
return [this[0], true, left, res];
|
|
|
|
// Always return something for the same reason as in binary
|
|
|
|
return [this[0], operator, left, right];
|
|
|
|
},
|
|
|
|
|
|
|
|
'unary-prefix': function(operator, exp) {
|
2011-03-04 06:15:04 -05:00
|
|
|
if (signOperators[operator] && isDynamic(exp)) {
|
2011-03-03 20:20:29 -05:00
|
|
|
return ['call', ['name', '$sign'],
|
2011-03-03 17:19:12 -05:00
|
|
|
[['string', operator], walk(exp)]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, function() {
|
|
|
|
return walk(ast);
|
|
|
|
});
|
|
|
|
|
2011-03-03 20:09:22 -05:00
|
|
|
return parse_js.stringify(ast, true);
|
2011-03-03 13:31:56 -05:00
|
|
|
}
|
|
|
|
|
2011-05-14 07:15:31 -04:00
|
|
|
function run(code, scope) {
|
2011-05-14 15:08:14 -04:00
|
|
|
with (scope) { // Safe one indentation by grouping try and with
|
|
|
|
paper = scope;
|
2011-05-14 07:15:31 -04:00
|
|
|
var doc = scope.document;
|
|
|
|
// TODO: Add support for multiple tools
|
|
|
|
var tool = scope.tool =
|
|
|
|
/on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)
|
|
|
|
&& new Tool(null, doc);
|
|
|
|
// Define variables for potential handlers, so eval() calls below to
|
|
|
|
// fetch their values do not require try-catch around them.
|
|
|
|
var onEditOptions, onSelect, onDeselect, onReselect, onMouseDown,
|
|
|
|
onMouseUp, onMouseDrag, onMouseMove, onKeyDown, onKeyUp, onFrame;
|
|
|
|
var res = eval(compile(code));
|
2011-05-07 10:38:14 -04:00
|
|
|
if (tool) {
|
2011-05-08 09:09:55 -04:00
|
|
|
Base.each(['onEditOptions', 'onSelect', 'onDeselect',
|
|
|
|
'onReselect', 'onMouseDown', 'onMouseUp', 'onMouseDrag',
|
|
|
|
'onMouseMove', 'onKeyDown', 'onKeyUp'],
|
2011-05-07 10:38:14 -04:00
|
|
|
function(key) {
|
|
|
|
tool[key] = eval(key);
|
2011-03-03 20:13:21 -05:00
|
|
|
}
|
2011-05-07 10:38:14 -04:00
|
|
|
);
|
2011-03-03 20:13:21 -05:00
|
|
|
}
|
2011-05-14 07:15:31 -04:00
|
|
|
// TODO: Move onFrame support to DocumentView
|
|
|
|
var onFrame = eval('onFrame');
|
|
|
|
if (onFrame) {
|
2011-05-15 13:08:41 -04:00
|
|
|
var lastTime,
|
|
|
|
totalTime = 0,
|
|
|
|
count = 0;
|
2011-05-14 11:00:30 -04:00
|
|
|
function frame(dontSwitch) {
|
|
|
|
if (!dontSwitch)
|
2011-05-14 15:08:14 -04:00
|
|
|
paper = scope;
|
2011-05-14 07:15:31 -04:00
|
|
|
// Request next frame already
|
|
|
|
DomEvent.requestAnimationFrame(frame, doc && doc.canvas);
|
|
|
|
var time = Date.now() / 1000;
|
|
|
|
// Time elapsed since last redraw in seconds:
|
|
|
|
var delta = lastTime ? time - lastTime : 0;
|
|
|
|
// Time since first call of frame() in seconds:
|
|
|
|
totalTime += delta;
|
|
|
|
onFrame({
|
|
|
|
delta: delta,
|
2011-05-15 13:08:41 -04:00
|
|
|
time: totalTime,
|
|
|
|
count: count++
|
2011-05-14 07:15:31 -04:00
|
|
|
});
|
|
|
|
// Automatically redraw document each frame.
|
2011-05-08 13:44:33 -04:00
|
|
|
if (doc)
|
|
|
|
doc.redraw();
|
2011-05-14 07:15:31 -04:00
|
|
|
lastTime = time;
|
|
|
|
};
|
|
|
|
// Call the onFrame handler and redraw the document:
|
2011-05-14 11:00:30 -04:00
|
|
|
frame(true);
|
2011-05-14 07:15:31 -04:00
|
|
|
} else {
|
|
|
|
// Automatically redraw document at the end.
|
|
|
|
if (doc)
|
|
|
|
doc.redraw();
|
2011-03-14 17:34:09 -04:00
|
|
|
}
|
2011-03-03 20:05:23 -05:00
|
|
|
return res;
|
2011-03-03 13:31:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//#ifdef BROWSER
|
|
|
|
// Code borrowed from Coffee Script:
|
2011-05-14 07:15:31 -04:00
|
|
|
function request(url, scope) {
|
2011-04-26 10:16:05 -04:00
|
|
|
var xhr = new (window.ActiveXObject || XMLHttpRequest)(
|
|
|
|
'Microsoft.XMLHTTP');
|
2011-03-03 13:31:56 -05:00
|
|
|
xhr.open('GET', url, true);
|
2011-05-06 08:29:21 -04:00
|
|
|
if (xhr.overrideMimeType) {
|
2011-03-03 13:31:56 -05:00
|
|
|
xhr.overrideMimeType('text/plain');
|
|
|
|
}
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState === 4) {
|
2011-05-14 07:15:31 -04:00
|
|
|
return run(xhr.responseText, scope);
|
2011-03-03 13:31:56 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return xhr.send(null);
|
|
|
|
}
|
2011-03-07 19:17:19 -05:00
|
|
|
|
|
|
|
function load() {
|
2011-03-07 13:35:48 -05:00
|
|
|
var scripts = document.getElementsByTagName('script');
|
|
|
|
for (var i = 0, l = scripts.length; i < l; i++) {
|
|
|
|
var script = scripts[i];
|
2011-05-15 06:32:42 -04:00
|
|
|
// Only load this script if it not loaded already.
|
2011-05-07 15:50:12 -04:00
|
|
|
if (script.type === 'text/paperscript'
|
|
|
|
&& !script.getAttribute('loaded')) {
|
2011-05-14 07:15:31 -04:00
|
|
|
// Produce a new PaperScope for this script now. Scopes are
|
|
|
|
// cheap so let's not worry about the initial one that was
|
|
|
|
// already created.
|
2011-05-15 06:32:42 -04:00
|
|
|
var scope = new PaperScope(script.src || ('script-' + i));
|
2011-03-07 13:35:48 -05:00
|
|
|
// If a canvas id is provided, create a document for it now,
|
|
|
|
// so the active document is defined.
|
|
|
|
var canvas = script.getAttribute('canvas');
|
2011-05-07 15:50:12 -04:00
|
|
|
if (canvas = canvas && document.getElementById(canvas)) {
|
2011-05-14 07:15:31 -04:00
|
|
|
// Create a Document for this canvas, using the right scope
|
2011-05-14 15:08:14 -04:00
|
|
|
paper = scope;
|
2011-03-07 13:35:48 -05:00
|
|
|
new Document(canvas);
|
|
|
|
}
|
|
|
|
if (script.src) {
|
2011-05-14 07:15:31 -04:00
|
|
|
request(script.src, scope);
|
2011-03-07 13:35:48 -05:00
|
|
|
} else {
|
2011-05-14 07:15:31 -04:00
|
|
|
run(script.innerHTML, scope);
|
2011-03-03 13:31:56 -05:00
|
|
|
}
|
2011-05-07 15:50:12 -04:00
|
|
|
// Mark script as loaded now.
|
|
|
|
script.setAttribute('loaded', true);
|
2011-03-03 13:31:56 -05:00
|
|
|
}
|
|
|
|
}
|
2011-03-07 13:35:48 -05:00
|
|
|
}
|
2011-03-03 13:31:56 -05:00
|
|
|
|
2011-05-08 05:16:11 -04:00
|
|
|
DomEvent.add(window, { load: load });
|
2011-03-07 13:35:48 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
compile: compile,
|
|
|
|
run: run,
|
2011-03-07 19:17:19 -05:00
|
|
|
load: load
|
2011-03-07 13:35:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
//#else // !BROWSER
|
2011-03-03 13:31:56 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
compile: compile,
|
|
|
|
run: run
|
|
|
|
};
|
2011-03-07 13:35:48 -05:00
|
|
|
|
|
|
|
//#endif // !BROWSER
|
2011-03-03 13:31:56 -05:00
|
|
|
};
|
2011-05-07 15:50:32 -04:00
|
|
|
|
|
|
|
// Export load directly:
|
|
|
|
this.load = PaperScript.load;
|