Implement Item#_boundsType as a structure to control bounds handling and caching.

This commit is contained in:
Jürg Lehni 2011-11-24 16:03:05 +01:00
parent a6b90dea35
commit 92066a6ecb
4 changed files with 16 additions and 12 deletions

View file

@ -1250,23 +1250,28 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
return false;
}
}, Base.each(['bounds', 'strokeBounds', 'handleBounds', 'roughBounds'], function(name) {
// Produce getters for bounds properties. These handle caching, matrices and
// redirect the call to the private _getBounds, which can be overridden by
// subclasses, see below.
this['get' + Base.capitalize(name)] = function(/* matrix */) {
var matrix = arguments[0];
// If the matrix is an identity transformation, set it to null for
// faster processing
if (matrix && matrix.isIdentity())
matrix = null;
// Allow subclasses to override _boundsType if they use the same
// calculations for multiple types. The default is name:
var type = this._boundsType;
if (typeof type != 'string')
type = type && type[name] || name;
// See if we can cache these bounds. We only cache non-transformed
// bounds on items without children, as we do not receive hierarchy
// change notifiers from children, and walking up the parents and
// merging cache bounds is not expensive.
// Allow subclasses to define _simpleBounds if they want to share the
// cache across all different bound types.
var cache = !this._children && !matrix
&& (this._simpleBounds && 'bounds' || name);
var cache = !this._children && !matrix && type;
if (cache && this._bounds && this._bounds[cache])
return this._bounds[cache];
var bounds = this._getBounds(name, matrix);
var bounds = this._getBounds(type, matrix);
// If we're returning 'bounds', create a LinkedRectangle that uses
// the setBounds() setter to update the Item whenever the bounds are
// changed:

View file

@ -24,6 +24,8 @@
* @extends Item
*/
var PlacedItem = this.PlacedItem = Item.extend(/** @lends PlacedItem# */{
// PlacedItem uses strokeBounds for bounds
_boundsType: { bounds: 'strokeBounds' },
_transform: function(matrix, flags) {
// In order to set the right context transformation when drawing the
@ -49,9 +51,6 @@ var PlacedItem = this.PlacedItem = Item.extend(/** @lends PlacedItem# */{
},
_getBounds: function(type, matrix) {
// The bounds of PlacedItems are the same as the strokeBounds
if (type == 'bounds')
type = 'strokeBounds';
// Concatenate the passed matrix with the internal one
matrix = matrix ? matrix.clone().concatenate(this._matrix)
: this._matrix;

View file

@ -23,8 +23,8 @@
*/
var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
// Raster doesn't make the distinction between the different bounds,
// so use the same cache for all of them
_simpleBounds: true,
// so use the same name for all of them
_boundsType: 'bounds',
// TODO: Implement url / type, width, height.
// TODO: Have PlacedSymbol & Raster inherit from a shared class?

View file

@ -27,8 +27,8 @@
*/
var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{
// TextItem doesn't make the distinction between the different bounds,
// so use the same cache for all of them
_simpleBounds: true,
// so use the same name for all of them
_boundsType: 'bounds',
initialize: function() {
this.base();