Revision: 1025
Author: allain.lalonde
Date: Tue Jun  8 07:48:28 2010
Log: Making use of conditional painting with the introduction of invalidatePaint(). Meaning it won't pin the CPU when there's nothing to repaint.


http://code.google.com/p/piccolo2d/source/detail?r=1025

Modified:
 /piccolo2d.js/trunk/piccolo2d.js

=======================================
--- /piccolo2d.js/trunk/piccolo2d.js    Mon Jan  4 10:15:57 2010
+++ /piccolo2d.js/trunk/piccolo2d.js    Tue Jun  8 07:48:28 2010
@@ -287,7 +287,6 @@

             this.transform = new PTransform();
             this.visible = true;
-            this.invalidPaint = true;

             if (params) {
                 this.fillStyle = params.fillStyle || null;
@@ -298,6 +297,13 @@
             }
         },

+        invalidatePaint: function() {
+            var root = this.getRoot();
+            if (root) {
+                root.invalidPaint = true;
+            }
+        },
+
         paint: function (ctx) {
             if (this.fillStyle && !this.bounds.isEmpty()) {
                 ctx.fillStyle = this.fillStyle;
@@ -331,19 +337,22 @@
         scale: function (ratio) {
             this.transform.scale(ratio);
             this.fullBounds = null;
-
+            this.invalidatePaint();
+
             return this;
         },

         translate: function (dx, dy) {
             this.transform.translate(dx, dy);
             this.fullBounds = null;
+            this.invalidatePaint();

             return this;
         },

         rotate: function (theta) {
             this.transform.rotate(theta);
+            this.invalidatePaint();

             return this;
         },
@@ -352,6 +361,7 @@
             this.children.push(child);
             this.fullBounds = null;
             child.parent = this;
+            this.invalidatePaint();

             return this;
         },
@@ -360,6 +370,7 @@
             child.parent = null;
             this.fullBounds = null;
             this.children = this.children.remove(child);
+            this.invalidatePaint();

             return this;
         },
@@ -367,6 +378,7 @@
         setTransform: function (transform) {
             this.transform = transform;
             this.fullBounds = null;
+            this.invalidatePaint();

             return this;
         },
@@ -452,6 +464,8 @@
         init: function (args) {
             this._super(args);

+            this.invalidPaint = true;
+
             this.scheduler = new PActivityScheduler(50);
         },

@@ -559,10 +573,14 @@
                 this.layers.push(layer);
                 layer.addCamera(this);
             }
+
+            this.invalidatePaint();
         },

         removeLayer: function (layer) {
             this.layers = this.layers.remove(layer);
+
+            this.invalidatePaint();
         },

         getPickedNodes: function (x, y) {
@@ -602,6 +620,8 @@

         setViewTransform: function (transform) {
             this.viewTransform = transform;
+
+            this.invalidatePaint();
         }

     });
@@ -618,6 +638,7 @@

             var _pCanvas = this;
             this.canvas = canvas;
+
             this.root =  root || new PRoot();
             this.camera = new PCamera();
this.camera.bounds = new PBounds(0, 0, canvas.width, canvas.height);
@@ -703,13 +724,18 @@
         },

         paint: function () {
-            var ctx = this.canvas.getContext('2d');
-            ctx.font = "16pt Helvetica";
-            ctx.fillStyle = this.fillStyle || "rgb(255,255,255)";
-
-            ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
-
-            this.camera.fullPaint(ctx);
+            var root = this.camera.getRoot();
+            if (root.invalidPaint) {
+                var ctx = this.canvas.getContext('2d');
+                ctx.font = "16pt Helvetica";
+                ctx.fillStyle = this.fillStyle || "rgb(255,255,255)";
+
+                ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
+
+                this.camera.fullPaint(ctx);
+
+                root.invalidPaint = false;
+            }
         },

         getPickedNodes: function (x, y) {

--
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en

Reply via email to