mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-19 06:00:56 -05:00
Improve versioning of _globalMatrix
This commit is contained in:
parent
663836ae41
commit
2cd3de8188
3 changed files with 23 additions and 18 deletions
|
@ -207,13 +207,12 @@ var Item = Base.extend(Callback, /** @lends Item# */{
|
|||
var parent = this._parent,
|
||||
project = this._project,
|
||||
symbol = this._parentSymbol;
|
||||
// Reset _updateCount on each change.
|
||||
this._updateCount = null;
|
||||
if (flags & /*#=*/ ChangeFlag.GEOMETRY) {
|
||||
// Clear cached bounds and position whenever geometry changes
|
||||
delete this._bounds;
|
||||
delete this._position;
|
||||
delete this._decomposed;
|
||||
delete this._globalMatrix;
|
||||
}
|
||||
if (parent && (flags
|
||||
& (/*#=*/ ChangeFlag.GEOMETRY | /*#=*/ ChangeFlag.STROKE))) {
|
||||
|
@ -1071,14 +1070,16 @@ var Item = Base.extend(Callback, /** @lends Item# */{
|
|||
* @bean
|
||||
*/
|
||||
getGlobalMatrix: function() {
|
||||
var matrix = this._updateCount === this._project._updateCount
|
||||
&& this._globalMatrix || null;
|
||||
// If _updateCount is out of sync or no _globalMatrix was calculated
|
||||
// when rendering, iteratively calculate it now.
|
||||
var matrix = this._globalMatrix,
|
||||
updateVersion = this._project._updateVersion;
|
||||
// If _globalMatrix is out of sync, recalculate it now
|
||||
if (matrix && matrix._updateVersion !== updateVersion)
|
||||
matrix = null;
|
||||
if (!matrix) {
|
||||
matrix = this._globalMatrix = item._matrix.clone();
|
||||
if (this._parent)
|
||||
matrix.concatenate(this._parent.getGlobalMatrix());
|
||||
matrix._updateVersion = updateVersion;
|
||||
}
|
||||
return matrix;
|
||||
},
|
||||
|
@ -3343,10 +3344,10 @@ var Item = Base.extend(Callback, /** @lends Item# */{
|
|||
draw: function(ctx, param) {
|
||||
if (!this._visible || this._opacity === 0)
|
||||
return;
|
||||
// Each time the project gets drawn, it's _updateCount is increased.
|
||||
// Keep the _updateCount of drawn items in sync, so we have an easy
|
||||
// Each time the project gets drawn, it's _updateVersion is increased.
|
||||
// Keep the _updateVersion of drawn items in sync, so we have an easy
|
||||
// way to know for which selected items we need to draw selection info.
|
||||
this._updateCount = this._project._updateCount;
|
||||
var updateVersion = this._updateVersion = this._project._updateVersion;
|
||||
// Keep calculating the current global matrix, by keeping a history
|
||||
// and pushing / popping as we go along.
|
||||
var trackTransforms = param.trackTransforms,
|
||||
|
@ -3360,8 +3361,12 @@ var Item = Base.extend(Callback, /** @lends Item# */{
|
|||
if (!globalMatrix.isInvertible())
|
||||
return;
|
||||
// Only keep track of transformation if told so. See Project#draw()
|
||||
if (trackTransforms)
|
||||
if (trackTransforms) {
|
||||
transforms.push(this._globalMatrix = globalMatrix);
|
||||
// We also keep the cached _globalMatrix versioned.
|
||||
globalMatrix._updateVersion = updateVersion;
|
||||
}
|
||||
|
||||
// If the item has a blendMode or is defining an opacity, draw it on
|
||||
// a temporary canvas first and composite the canvas afterwards.
|
||||
// Paths with an opacity < 1 that both define a fillColor
|
||||
|
|
|
@ -59,8 +59,8 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
|||
this.view = view instanceof View ? view : View.create(view);
|
||||
this._selectedItems = {};
|
||||
this._selectedItemCount = 0;
|
||||
// See Item#draw() for an explanation of _updateCount
|
||||
this._updateCount = 0;
|
||||
// See Item#draw() for an explanation of _updateVersion
|
||||
this._updateVersion = 0;
|
||||
// Change tracking, not in use for now. Activate once required:
|
||||
// this._changes = [];
|
||||
// this._changesById = {};
|
||||
|
@ -421,9 +421,9 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
|||
*/
|
||||
|
||||
draw: function(ctx, matrix, ratio) {
|
||||
// Increase the _updateCount before the draw-loop. After that, items
|
||||
// that are visible will have their _updateCount set to the new value.
|
||||
this._updateCount++;
|
||||
// Increase the _updateVersion before the draw-loop. After that, items
|
||||
// that are visible will have their _updateVersion set to the new value.
|
||||
this._updateVersion++;
|
||||
ctx.save();
|
||||
matrix.applyToContext(ctx);
|
||||
// Use new Base() so we can use param.extend() to easily override
|
||||
|
@ -450,7 +450,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
|||
ctx.strokeWidth = 1;
|
||||
for (var id in this._selectedItems) {
|
||||
var item = this._selectedItems[id];
|
||||
if (item._updateCount === this._updateCount
|
||||
if (item._updateVersion === this._updateVersion
|
||||
&& (item._drawSelected || item._boundsSelected)) {
|
||||
// Allow definition of selected color on a per item and per
|
||||
// layer level, with a fallback to #009dec
|
||||
|
@ -464,7 +464,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
|||
if (item._boundsSelected) {
|
||||
// We need to call the internal _getBounds, to get non-
|
||||
// transformed bounds.
|
||||
// TODO: Implement caching for these too!
|
||||
// TODO: Implement caching for these too?
|
||||
var coords = mx._transformCorners(
|
||||
item._getBounds('getBounds'));
|
||||
// Now draw a rectangle that connects the transformed
|
||||
|
|
|
@ -421,7 +421,7 @@ var View = Base.extend(Callback, /** @lends View# */{
|
|||
* @name View#update
|
||||
* @function
|
||||
*/
|
||||
// update: function(checkUpdate) {
|
||||
// update: function() {
|
||||
// },
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue