[seed] ClutterPad: Spring example, similar to Processing.



commit d9ccd897c4e225385b1d410be7883430b5ffbf5e
Author: Tim Horton <hortont svn gnome org>
Date:   Thu May 14 04:10:08 2009 -0400

    ClutterPad: Spring example, similar to Processing.
---
 examples/clutter-pad/examples/Makefile.am |    6 +-
 examples/clutter-pad/examples/spring.js   |   92 +++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+), 2 deletions(-)

diff --git a/examples/clutter-pad/examples/Makefile.am b/examples/clutter-pad/examples/Makefile.am
index ef83f44..a109197 100644
--- a/examples/clutter-pad/examples/Makefile.am
+++ b/examples/clutter-pad/examples/Makefile.am
@@ -2,10 +2,12 @@ if BUILD_SEED_EXAMPLES
 clutterpadexamplesdir=$(datadir)/doc/seed/examples/clutter-pad/examples
 clutterpadexamples_DATA = \
 	animated-rectangle.js \
-	pink-stage.js
+	pink-stage.js \
+	spring.js
 endif
 
 EXTRA_DIST = \
 	animated-rectangle.js \
-	pink-stage.js
+	pink-stage.js \
+	spring.js
 
diff --git a/examples/clutter-pad/examples/spring.js b/examples/clutter-pad/examples/spring.js
new file mode 100644
index 0000000..ab7285a
--- /dev/null
+++ b/examples/clutter-pad/examples/spring.js
@@ -0,0 +1,92 @@
+GLib = imports.gi.GLib;
+GObject = imports.gi.GObject;
+Clutter = imports.gi.Clutter;
+stage = Clutter.Stage.get_default();
+
+Clutter.set_motion_events_frequency(30);
+
+var vs = 0, as = 0, f = 0, dragging = false;
+
+var spring = new Clutter.Rectangle();
+spring.height = 200;
+spring.width = spring.height / 2;
+spring.anchor_x = spring.width / 2;
+spring.x = stage.width / 2;
+spring.color = {red: 100, green: 100, blue: 100, alpha: 255};
+
+var handle = new Clutter.Rectangle();
+handle.width = stage.width / 2;
+handle.height = 50;
+handle.anchor_x = handle.width / 2;
+handle.x = stage.width / 2;
+handle.y = spring.height;
+
+function click()
+{
+	dragging = true;
+	return false;
+}
+
+function release()
+{
+	dragging = false;
+	return false;
+}
+
+function drag(actor, event)
+{
+	if(!dragging)
+		return false;
+
+	spring.height = event.motion.y;
+
+	if(spring.height > 300)
+		spring.height = 300;
+	else if(spring.height < 100)
+		spring.height = 100;
+
+	update_relative_sizes();
+	return false;
+}
+
+function update_relative_sizes()
+{
+	handle.y = spring.height;
+
+	spring.width = spring.height / 2;
+	spring.anchor_x = spring.width / 2;
+
+	var red = spring.height > 200 ? 100 + (spring.height - 200) : 100;
+	var green = spring.height < 200 ? 100 + (200 - spring.height) : 100;
+
+	spring.color = {red: red, green: green, blue: 100, alpha: 255};
+}
+
+function update_spring()
+{
+	if(dragging)
+		return false;
+
+	f = -0.1 * (spring.height - 200);
+	as = f / 0.8;
+	vs = 0.92 * (vs + as);
+	spring.height += vs;
+
+	if(Math.abs(vs) < 0.1)
+		vs = 0.0;
+
+	update_relative_sizes();
+
+	return false;
+}
+
+stage.add_actor(handle);
+stage.add_actor(spring);
+
+stage.signal.button_press_event.connect(click);
+stage.signal.button_release_event.connect(release);
+stage.signal.motion_event.connect(drag);
+
+var update = new Clutter.Timeline({fps:60, num_frames:10000, loop:true});
+update.signal.new_frame.connect(update_spring);
+update.start();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]