Implement #_needsRedraw() mechanism in PaperScope and Project, to notify Views of redraws required by changes in appearance.

This commit is contained in:
Jürg Lehni 2011-06-19 23:03:18 +01:00
parent 25b31b6b7d
commit 596cc8f83b
5 changed files with 28 additions and 7 deletions

View file

@ -167,6 +167,15 @@ var PaperScope = this.PaperScope = Base.extend(/** @scope _global_ */{
delete PaperScope._scopes[this.id];
},
_needsRedraw: function() {
// Make sure we're not looping through the view list each time...
if (!this._redrawNotified) {
for (var i = this.views.length - 1; i >= 0; i--)
this.views[i]._redrawNeeded = true;
this._redrawNotified = true;
}
},
statics: {
_scopes: {},

View file

@ -42,7 +42,12 @@ var Item = this.Item = Base.extend({
* @param {ChangeFlag} flags describes what exactly has changed.
*/
_changed: function(flags) {
if (flags & ChangeFlag.APPEARANCE) {
if (this._project)
this._project._needsRedraw();
}
if (flags & ChangeFlag.GEOMETRY) {
// Clear cached bounds and position whenever geometry changes
delete this._bounds;
delete this._position;
}

View file

@ -58,7 +58,9 @@ var Layer = this.Layer = Group.extend({
if (deselect)
this.setSelected(false);
Base.splice(this._project.layers, null, this._index, 1);
this._project._changed(Change.HIERARCHY);
// Tell project we need a redraw. This is similar to _changed()
// mechanism.
this._project._needsRedraw();
return true;
}
return false;

View file

@ -57,10 +57,9 @@ var Project = this.Project = Base.extend({
this.activeLayer = new Layer();
},
_changed: function(flags) {
if (flags & ChangeFlag.GEOMETRY) {
// TODO: Mark as requireRedraw
}
_needsRedraw: function() {
if (this._scope)
this._scope._needsRedraw();
},
/**

View file

@ -204,13 +204,15 @@ var View = this.View = Base.extend({
this._inverse = null;
},
draw: function() {
draw: function(checkRedraw) {
if (checkRedraw && !this._redrawNeeded)
return false;
if (this._stats)
this._stats.update();
// Initial tests conclude that clearing the canvas using clearRect
// is always faster than setting canvas.width = canvas.width
// http://jsperf.com/clearrect-vs-setting-width/7
var ctx =this._context,
var ctx = this._context,
bounds = this._viewBounds;
ctx.clearRect(bounds._x, bounds._y,
// TODO: +1... what if we have multiple views in one canvas?
@ -221,6 +223,10 @@ var View = this.View = Base.extend({
// Just draw the active project for now
this._scope.project.draw(ctx);
ctx.restore();
// Update _redrawNotified in PaperScope as soon as one view was drawn
this._redrawNeeded = false;
this._scope._redrawNotified = false;
return true;
},
activate: function() {