Fixes #580 where resizable views were only taking into account their offset from the top left of the viewport.

This commit is contained in:
George Reith 2014-12-04 13:01:02 +00:00
parent f193acf31b
commit 6f1c3145bf
6 changed files with 136 additions and 135 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
.DS_Store .DS_Store
build/zip

74
dist/paper-core.js vendored
View file

@ -9,7 +9,7 @@
* *
* All rights reserved. * All rights reserved.
* *
* Date: Sat Nov 22 09:01:01 2014 -0800 * Date: Tue Dec 2 22:31:20 2014 -0800
* *
*** ***
* *
@ -219,10 +219,8 @@ var Base = new function() {
|| ctor.name === 'Object'); || ctor.name === 'Object');
}, },
pick: function() { pick: function(a, b) {
for (var i = 0, l = arguments.length; i < l; i++) return a !== undefined ? a : b;
if (arguments[i] !== undefined)
return arguments[i];
} }
} }
}); });
@ -257,6 +255,10 @@ Base.inject({
}, []).join(', ') + ' }'; }, []).join(', ') + ' }';
}, },
getClassName: function() {
return this._class || '';
},
exportJSON: function(options) { exportJSON: function(options) {
return Base.exportJSON(this, options); return Base.exportJSON(this, options);
}, },
@ -2833,10 +2835,6 @@ var Item = Base.extend(Emitter, {
return this._id; return this._id;
}, },
getClassName: function() {
return this._class;
},
getName: function() { getName: function() {
return this._name; return this._name;
}, },
@ -5361,6 +5359,7 @@ var SegmentPoint = Point.extend({
var Curve = Base.extend({ var Curve = Base.extend({
_class: 'Curve', _class: 'Curve',
initialize: function Curve(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) { initialize: function Curve(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) {
var count = arguments.length; var count = arguments.length;
if (count === 3) { if (count === 3) {
@ -10321,31 +10320,30 @@ var View = Base.extend(Emitter, {
userDrag: none, userDrag: none,
tapHighlightColor: 'rgba(0,0,0,0)' tapHighlightColor: 'rgba(0,0,0,0)'
}); });
if (PaperScope.hasAttribute(element, 'resize')) {
var offset = DomElement.getOffset(element, true), var getCanvasSize = function() {
that = this; size = DomElement.getSize(element);
size = DomElement.getViewportBounds(element) if (size.isNaN() || size.isZero()) {
.getSize().subtract(offset); var getSize = function(name) {
this._windowEvents = { return element[name]
resize: function() { || parseInt(element.getAttribute(name), 10);
if (!DomElement.isInvisible(element)) };
offset = DomElement.getOffset(element, true); size = new Size(getSize('width'), getSize('height'));
that.setViewSize(DomElement.getViewportBounds(element)
.getSize().subtract(offset));
} }
return size;
}; };
DomEvent.add(window, this._windowEvents);
} else { this._resizable = PaperScope.hasAttribute(element, 'resize');
size = DomElement.getSize(element); if (this._resizable) {
if (size.isNaN() || size.isZero()) { var that = this;
var getSize = function(name) { this._windowEvents = {
return element[name] resize: function() {
|| parseInt(element.getAttribute(name), 10); that.setViewSize(getCanvasSize());
}
}; };
size = new Size(getSize('width'), getSize('height')); DomEvent.add(window, this._windowEvents);
} }
} this._setViewSize(getCanvasSize());
this._setViewSize(size);
if (PaperScope.hasAttribute(element, 'stats') if (PaperScope.hasAttribute(element, 'stats')
&& typeof Stats !== 'undefined') { && typeof Stats !== 'undefined') {
this._stats = new Stats(); this._stats = new Stats();
@ -10759,11 +10757,13 @@ var CanvasView = View.extend({
style = element.style; style = element.style;
element.width = width * pixelRatio; element.width = width * pixelRatio;
element.height = height * pixelRatio; element.height = height * pixelRatio;
if (pixelRatio !== 1) { if (pixelRatio !== 1) {
style.width = width + 'px'; if (this._resizable === false) {
style.height = height + 'px'; style.width = width + 'px';
this._context.scale(pixelRatio, pixelRatio); style.height = height + 'px';
} }
this._context.scale(pixelRatio, pixelRatio);
}
}, },
getPixelSize: function(size) { getPixelSize: function(size) {
@ -11735,11 +11735,11 @@ new function() {
scale = decomposed.scaling; scale = decomposed.scaling;
if (trans && !trans.isZero()) if (trans && !trans.isZero())
parts.push('translate(' + formatter.point(trans) + ')'); parts.push('translate(' + formatter.point(trans) + ')');
if (angle)
parts.push('rotate(' + formatter.number(angle) + ')');
if (!Numerical.isZero(scale.x - 1) if (!Numerical.isZero(scale.x - 1)
|| !Numerical.isZero(scale.y - 1)) || !Numerical.isZero(scale.y - 1))
parts.push('scale(' + formatter.point(scale) +')'); parts.push('scale(' + formatter.point(scale) +')');
if (angle)
parts.push('rotate(' + formatter.number(angle) + ')');
attrs.transform = parts.join(' '); attrs.transform = parts.join(' ');
} else { } else {
attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')'; attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')';

74
dist/paper-full.js vendored
View file

@ -9,7 +9,7 @@
* *
* All rights reserved. * All rights reserved.
* *
* Date: Sat Nov 22 09:01:01 2014 -0800 * Date: Tue Dec 2 22:31:20 2014 -0800
* *
*** ***
* *
@ -219,10 +219,8 @@ var Base = new function() {
|| ctor.name === 'Object'); || ctor.name === 'Object');
}, },
pick: function() { pick: function(a, b) {
for (var i = 0, l = arguments.length; i < l; i++) return a !== undefined ? a : b;
if (arguments[i] !== undefined)
return arguments[i];
} }
} }
}); });
@ -257,6 +255,10 @@ Base.inject({
}, []).join(', ') + ' }'; }, []).join(', ') + ' }';
}, },
getClassName: function() {
return this._class || '';
},
exportJSON: function(options) { exportJSON: function(options) {
return Base.exportJSON(this, options); return Base.exportJSON(this, options);
}, },
@ -2833,10 +2835,6 @@ var Item = Base.extend(Emitter, {
return this._id; return this._id;
}, },
getClassName: function() {
return this._class;
},
getName: function() { getName: function() {
return this._name; return this._name;
}, },
@ -5361,6 +5359,7 @@ var SegmentPoint = Point.extend({
var Curve = Base.extend({ var Curve = Base.extend({
_class: 'Curve', _class: 'Curve',
initialize: function Curve(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) { initialize: function Curve(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) {
var count = arguments.length; var count = arguments.length;
if (count === 3) { if (count === 3) {
@ -10321,31 +10320,30 @@ var View = Base.extend(Emitter, {
userDrag: none, userDrag: none,
tapHighlightColor: 'rgba(0,0,0,0)' tapHighlightColor: 'rgba(0,0,0,0)'
}); });
if (PaperScope.hasAttribute(element, 'resize')) {
var offset = DomElement.getOffset(element, true), var getCanvasSize = function() {
that = this; size = DomElement.getSize(element);
size = DomElement.getViewportBounds(element) if (size.isNaN() || size.isZero()) {
.getSize().subtract(offset); var getSize = function(name) {
this._windowEvents = { return element[name]
resize: function() { || parseInt(element.getAttribute(name), 10);
if (!DomElement.isInvisible(element)) };
offset = DomElement.getOffset(element, true); size = new Size(getSize('width'), getSize('height'));
that.setViewSize(DomElement.getViewportBounds(element)
.getSize().subtract(offset));
} }
return size;
}; };
DomEvent.add(window, this._windowEvents);
} else { this._resizable = PaperScope.hasAttribute(element, 'resize');
size = DomElement.getSize(element); if (this._resizable) {
if (size.isNaN() || size.isZero()) { var that = this;
var getSize = function(name) { this._windowEvents = {
return element[name] resize: function() {
|| parseInt(element.getAttribute(name), 10); that.setViewSize(getCanvasSize());
}
}; };
size = new Size(getSize('width'), getSize('height')); DomEvent.add(window, this._windowEvents);
} }
} this._setViewSize(getCanvasSize());
this._setViewSize(size);
if (PaperScope.hasAttribute(element, 'stats') if (PaperScope.hasAttribute(element, 'stats')
&& typeof Stats !== 'undefined') { && typeof Stats !== 'undefined') {
this._stats = new Stats(); this._stats = new Stats();
@ -10759,11 +10757,13 @@ var CanvasView = View.extend({
style = element.style; style = element.style;
element.width = width * pixelRatio; element.width = width * pixelRatio;
element.height = height * pixelRatio; element.height = height * pixelRatio;
if (pixelRatio !== 1) { if (pixelRatio !== 1) {
style.width = width + 'px'; if (this._resizable === false) {
style.height = height + 'px'; style.width = width + 'px';
this._context.scale(pixelRatio, pixelRatio); style.height = height + 'px';
} }
this._context.scale(pixelRatio, pixelRatio);
}
}, },
getPixelSize: function(size) { getPixelSize: function(size) {
@ -11735,11 +11735,11 @@ new function() {
scale = decomposed.scaling; scale = decomposed.scaling;
if (trans && !trans.isZero()) if (trans && !trans.isZero())
parts.push('translate(' + formatter.point(trans) + ')'); parts.push('translate(' + formatter.point(trans) + ')');
if (angle)
parts.push('rotate(' + formatter.number(angle) + ')');
if (!Numerical.isZero(scale.x - 1) if (!Numerical.isZero(scale.x - 1)
|| !Numerical.isZero(scale.y - 1)) || !Numerical.isZero(scale.y - 1))
parts.push('scale(' + formatter.point(scale) +')'); parts.push('scale(' + formatter.point(scale) +')');
if (angle)
parts.push('rotate(' + formatter.number(angle) + ')');
attrs.transform = parts.join(' '); attrs.transform = parts.join(' ');
} else { } else {
attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')'; attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')';

33
dist/paper-node.js vendored
View file

@ -9,7 +9,7 @@
* *
* All rights reserved. * All rights reserved.
* *
* Date: Sat Nov 22 09:01:01 2014 -0800 * Date: Tue Dec 2 22:31:20 2014 -0800
* *
*** ***
* *
@ -219,10 +219,8 @@ var Base = new function() {
|| ctor.name === 'Object'); || ctor.name === 'Object');
}, },
pick: function() { pick: function(a, b) {
for (var i = 0, l = arguments.length; i < l; i++) return a !== undefined ? a : b;
if (arguments[i] !== undefined)
return arguments[i];
} }
} }
}); });
@ -247,6 +245,10 @@ Base.inject({
}, []).join(', ') + ' }'; }, []).join(', ') + ' }';
}, },
getClassName: function() {
return this._class || '';
},
exportJSON: function(options) { exportJSON: function(options) {
return Base.exportJSON(this, options); return Base.exportJSON(this, options);
}, },
@ -2802,10 +2804,6 @@ var Item = Base.extend(Emitter, {
return this._id; return this._id;
}, },
getClassName: function() {
return this._class;
},
getName: function() { getName: function() {
return this._name; return this._name;
}, },
@ -5345,6 +5343,7 @@ var SegmentPoint = Point.extend({
var Curve = Base.extend({ var Curve = Base.extend({
_class: 'Curve', _class: 'Curve',
initialize: function Curve(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) { initialize: function Curve(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) {
var count = arguments.length; var count = arguments.length;
if (count === 3) { if (count === 3) {
@ -10480,11 +10479,13 @@ var CanvasView = View.extend({
style = element.style; style = element.style;
element.width = width * pixelRatio; element.width = width * pixelRatio;
element.height = height * pixelRatio; element.height = height * pixelRatio;
if (pixelRatio !== 1) { if (pixelRatio !== 1) {
style.width = width + 'px'; if (this._resizable === false) {
style.height = height + 'px'; style.width = width + 'px';
this._context.scale(pixelRatio, pixelRatio); style.height = height + 'px';
} }
this._context.scale(pixelRatio, pixelRatio);
}
}, },
getPixelSize: function(size) { getPixelSize: function(size) {
@ -11075,11 +11076,11 @@ new function() {
scale = decomposed.scaling; scale = decomposed.scaling;
if (trans && !trans.isZero()) if (trans && !trans.isZero())
parts.push('translate(' + formatter.point(trans) + ')'); parts.push('translate(' + formatter.point(trans) + ')');
if (angle)
parts.push('rotate(' + formatter.number(angle) + ')');
if (!Numerical.isZero(scale.x - 1) if (!Numerical.isZero(scale.x - 1)
|| !Numerical.isZero(scale.y - 1)) || !Numerical.isZero(scale.y - 1))
parts.push('scale(' + formatter.point(scale) +')'); parts.push('scale(' + formatter.point(scale) +')');
if (angle)
parts.push('rotate(' + formatter.number(angle) + ')');
attrs.transform = parts.join(' '); attrs.transform = parts.join(' ');
} else { } else {
attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')'; attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')';

View file

@ -68,13 +68,17 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
// Upscale the canvas if the two ratios don't match. // Upscale the canvas if the two ratios don't match.
element.width = width * pixelRatio; element.width = width * pixelRatio;
element.height = height * pixelRatio; element.height = height * pixelRatio;
if (pixelRatio !== 1) { if (pixelRatio !== 1) {
style.width = width + 'px'; // If the canvas is resizable then don't override it otherwise
style.height = height + 'px'; // give it fixed dimensions so it doesn't get resized.
// Now scale the context to counter the fact that we've manually if (this._resizable === false) {
// scaled our canvas element. style.width = width + 'px';
this._context.scale(pixelRatio, pixelRatio); style.height = height + 'px';
} }
// Now scale the context to counter the fact that we've manually
// scaled our canvas element.
this._context.scale(pixelRatio, pixelRatio);
}
}, },
/** /**

View file

@ -52,48 +52,43 @@ var View = Base.extend(Emitter, /** @lends View# */{
userDrag: none, userDrag: none,
tapHighlightColor: 'rgba(0,0,0,0)' tapHighlightColor: 'rgba(0,0,0,0)'
}); });
// If the element has the resize attribute, resize the it to fill the
// window and resize it again whenever the user resizes the window. var getCanvasSize = function() {
if (PaperScope.hasAttribute(element, 'resize')) { // Try visible size first, since that will help handling previously
// Subtract element' viewport offset from the total size, to // scaled canvases (e.g. when dealing with pixel-ratio)
// stretch it in size = DomElement.getSize(element);
var offset = DomElement.getOffset(element, true), if (size.isNaN() || size.isZero()) {
that = this; // If the element is invisible, we cannot directly access
size = DomElement.getViewportBounds(element) // element.width / height, because they would appear 0.
.getSize().subtract(offset); // Reading the attributes should still work.
this._windowEvents = { var getSize = function(name) {
resize: function() { return element[name]
// Only update element offset if it's not invisible, as || parseInt(element.getAttribute(name), 10);
// otherwise the offset would be wrong. };
if (!DomElement.isInvisible(element)) size = new Size(getSize('width'), getSize('height'));
offset = DomElement.getOffset(element, true); }
// Set the size now, which internally calls onResize return size;
// and redraws the view };
that.setViewSize(DomElement.getViewportBounds(element)
.getSize().subtract(offset)); // If the element has the resize attribute, listen to resize events and
} // update its coordinate space accordingly
}; this._resizable = PaperScope.hasAttribute(element, 'resize');
DomEvent.add(window, this._windowEvents); if (this._resizable) {
} else { var that = this;
// Try visible size first, since that will help handling previously this._windowEvents = {
// scaled canvases (e.g. when dealing with pixel-ratio) resize: function() {
size = DomElement.getSize(element); // setViewSize is aware of a resizable canvas and only updates the
if (size.isNaN() || size.isZero()) { // coordinate space based on the physical dimensions and pixel ratio
// If the element is invisible, we cannot directly access that.setViewSize(getCanvasSize());
// element.width / height, because they would appear 0. }
// Reading the attributes should still work. };
var getSize = function(name) { DomEvent.add(window, this._windowEvents);
return element[name] }
|| parseInt(element.getAttribute(name), 10);
};
size = new Size(getSize('width'), getSize('height'));
}
}
// Set canvas size even if we just deterined the size from it, since // Set canvas size even if we just deterined the size from it, since
// it might have been set to a % size, in which case it would use some // it might have been set to a % size, in which case it would use some
// default internal size (300x150 on WebKit) and scale up the pixels. // default internal size (300x150 on WebKit) and scale up the pixels.
// We also need this call here for HiDPI support. // We also need this call here for HiDPI support.
this._setViewSize(size); this._setViewSize(getCanvasSize());
// TODO: Test this on IE: // TODO: Test this on IE:
if (PaperScope.hasAttribute(element, 'stats') if (PaperScope.hasAttribute(element, 'stats')
&& typeof Stats !== 'undefined') { && typeof Stats !== 'undefined') {