mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-07 13:22:07 -05:00
Implement Item#_boundsType as a structure to control bounds handling and caching.
This commit is contained in:
parent
a6b90dea35
commit
92066a6ecb
4 changed files with 16 additions and 12 deletions
|
@ -1250,23 +1250,28 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}, Base.each(['bounds', 'strokeBounds', 'handleBounds', 'roughBounds'], function(name) {
|
}, 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 */) {
|
this['get' + Base.capitalize(name)] = function(/* matrix */) {
|
||||||
var matrix = arguments[0];
|
var matrix = arguments[0];
|
||||||
// If the matrix is an identity transformation, set it to null for
|
// If the matrix is an identity transformation, set it to null for
|
||||||
// faster processing
|
// faster processing
|
||||||
if (matrix && matrix.isIdentity())
|
if (matrix && matrix.isIdentity())
|
||||||
matrix = null;
|
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
|
// See if we can cache these bounds. We only cache non-transformed
|
||||||
// bounds on items without children, as we do not receive hierarchy
|
// bounds on items without children, as we do not receive hierarchy
|
||||||
// change notifiers from children, and walking up the parents and
|
// change notifiers from children, and walking up the parents and
|
||||||
// merging cache bounds is not expensive.
|
// merging cache bounds is not expensive.
|
||||||
// Allow subclasses to define _simpleBounds if they want to share the
|
var cache = !this._children && !matrix && type;
|
||||||
// cache across all different bound types.
|
|
||||||
var cache = !this._children && !matrix
|
|
||||||
&& (this._simpleBounds && 'bounds' || name);
|
|
||||||
if (cache && this._bounds && this._bounds[cache])
|
if (cache && this._bounds && this._bounds[cache])
|
||||||
return 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
|
// If we're returning 'bounds', create a LinkedRectangle that uses
|
||||||
// the setBounds() setter to update the Item whenever the bounds are
|
// the setBounds() setter to update the Item whenever the bounds are
|
||||||
// changed:
|
// changed:
|
||||||
|
|
|
@ -24,6 +24,8 @@
|
||||||
* @extends Item
|
* @extends Item
|
||||||
*/
|
*/
|
||||||
var PlacedItem = this.PlacedItem = Item.extend(/** @lends PlacedItem# */{
|
var PlacedItem = this.PlacedItem = Item.extend(/** @lends PlacedItem# */{
|
||||||
|
// PlacedItem uses strokeBounds for bounds
|
||||||
|
_boundsType: { bounds: 'strokeBounds' },
|
||||||
|
|
||||||
_transform: function(matrix, flags) {
|
_transform: function(matrix, flags) {
|
||||||
// In order to set the right context transformation when drawing the
|
// 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) {
|
_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
|
// Concatenate the passed matrix with the internal one
|
||||||
matrix = matrix ? matrix.clone().concatenate(this._matrix)
|
matrix = matrix ? matrix.clone().concatenate(this._matrix)
|
||||||
: this._matrix;
|
: this._matrix;
|
||||||
|
|
|
@ -23,8 +23,8 @@
|
||||||
*/
|
*/
|
||||||
var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
|
var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
|
||||||
// Raster doesn't make the distinction between the different bounds,
|
// Raster doesn't make the distinction between the different bounds,
|
||||||
// so use the same cache for all of them
|
// so use the same name for all of them
|
||||||
_simpleBounds: true,
|
_boundsType: 'bounds',
|
||||||
|
|
||||||
// TODO: Implement url / type, width, height.
|
// TODO: Implement url / type, width, height.
|
||||||
// TODO: Have PlacedSymbol & Raster inherit from a shared class?
|
// TODO: Have PlacedSymbol & Raster inherit from a shared class?
|
||||||
|
|
|
@ -27,8 +27,8 @@
|
||||||
*/
|
*/
|
||||||
var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{
|
var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{
|
||||||
// TextItem doesn't make the distinction between the different bounds,
|
// TextItem doesn't make the distinction between the different bounds,
|
||||||
// so use the same cache for all of them
|
// so use the same name for all of them
|
||||||
_simpleBounds: true,
|
_boundsType: 'bounds',
|
||||||
|
|
||||||
initialize: function() {
|
initialize: function() {
|
||||||
this.base();
|
this.base();
|
||||||
|
|
Loading…
Reference in a new issue