mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-08-13 22:48:54 -04:00
Add beginning of support for <script type="text/paperscript">, inspired by Coffee Script.
This commit is contained in:
parent
08970abd6d
commit
2c78a0a08b
4 changed files with 88 additions and 32 deletions
src/util
54
src/util/PaperScript.js
Normal file
54
src/util/PaperScript.js
Normal file
|
@ -0,0 +1,54 @@
|
|||
var PaperScript = new function() {
|
||||
function compile(code) {
|
||||
return code;
|
||||
}
|
||||
|
||||
function run(code) {
|
||||
with (paper) {
|
||||
return eval(compile(code));
|
||||
}
|
||||
}
|
||||
|
||||
//#ifdef BROWSER
|
||||
// Code borrowed from Coffee Script:
|
||||
function load(url) {
|
||||
var xhr = new (window.ActiveXObject || XMLHttpRequest)('Microsoft.XMLHTTP');
|
||||
xhr.open('GET', url, true);
|
||||
if ('overrideMimeType' in xhr) {
|
||||
xhr.overrideMimeType('text/plain');
|
||||
}
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
return run(xhr.responseText);
|
||||
}
|
||||
};
|
||||
return xhr.send(null);
|
||||
}
|
||||
|
||||
function runScripts() {
|
||||
var scripts = document.getElementsByTagName('script');
|
||||
for (var i = 0, l = scripts.length; i < l; i++) {
|
||||
var script = scripts[i];
|
||||
if (script.type === 'text/paperscript') {
|
||||
if (script.src) {
|
||||
load(script.src);
|
||||
} else {
|
||||
run(script.innerHTML);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (window.addEventListener) {
|
||||
addEventListener('DOMContentLoaded', runScripts, false);
|
||||
} else {
|
||||
attachEvent('onload', runScripts);
|
||||
}
|
||||
//#endif // BROWSER
|
||||
|
||||
return {
|
||||
compile: compile,
|
||||
run: run
|
||||
};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue