Add support for onFrameLoop() callback to PaperScript.

This commit is contained in:
Jürg Lehni 2011-03-14 22:34:09 +01:00
parent 7e46eeabc4
commit 2fcca38a9c
2 changed files with 17 additions and 8 deletions

View file

@ -33,20 +33,16 @@
placedSymbol.position = center;
layer.appendBottom(placedSymbol);
}
// setInterval(draw, 30);
var count = 0;
function draw() {
function onFrameLoop() {
count++;
for (var i = 0, l = layer.children.length; i < l; i++) {
var item = layer.children[i];
var angle = (values.count - i) * Math.sin(count / 128) / 10;
item.rotate(angle);
}
document.redraw();
Event.requestAnimationFrame(draw, document.canvas);
}
Event.requestAnimationFrame(draw, document.canvas);
</script>
</head>
<body>

View file

@ -132,7 +132,8 @@ var PaperScript = new function() {
function run(code) {
with (paper) {
paper.tool = /onMouse(?:Up|Down|Move|Drag)/.test(code) && new Tool();
var res = eval(compile(code));
var res = eval(compile(code)),
doc = paper.document;
if (paper.tool) {
Base.each(['onEditOptions', 'onOptions', 'onSelect',
'onDeselect', 'onReselect', 'onMouseDown', 'onMouseUp',
@ -143,9 +144,21 @@ var PaperScript = new function() {
}
});
}
try {
var onFrameLoop = eval('onFrameLoop');
if (onFrameLoop) {
function loop() {
onFrameLoop();
// Automatically redraw document each frame.
doc && doc.redraw();
Event.requestAnimationFrame(loop, doc && doc.canvas);
}
Event.requestAnimationFrame(loop, doc && doc.canvas);
}
} catch (e) {
}
// Automatically redraw document at the end.
if (paper.document)
paper.document.redraw();
doc && doc.redraw();
return res;
}
}