From 2fcca38a9c4e49fe8ae0424d8baf95c6030e7228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Mon, 14 Mar 2011 22:34:09 +0100 Subject: [PATCH] Add support for onFrameLoop() callback to PaperScript. --- examples/Animated/animatedStar.html | 6 +----- src/util/PaperScript.js | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/examples/Animated/animatedStar.html b/examples/Animated/animatedStar.html index 8bf8aa4f..6c764be8 100644 --- a/examples/Animated/animatedStar.html +++ b/examples/Animated/animatedStar.html @@ -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); diff --git a/src/util/PaperScript.js b/src/util/PaperScript.js index 52e8d5a7..90bd964a 100644 --- a/src/util/PaperScript.js +++ b/src/util/PaperScript.js @@ -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; } }