2011-03-14 17:14:20 -04:00
|
|
|
/*
|
|
|
|
* Paper.js
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-03-14 17:14:20 -04:00
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
|
|
|
* http://paperjs.org/
|
|
|
|
* http://scriptographer.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-03-14 17:14:20 -04:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-01 06:17:45 -04:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-03-14 17:14:20 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2011-07-31 09:13:29 -04:00
|
|
|
var DomElement = {
|
|
|
|
getBounds: function(el, viewport) {
|
|
|
|
var rect = el.getBoundingClientRect(),
|
|
|
|
doc = el.ownerDocument,
|
|
|
|
body = doc.body,
|
|
|
|
docEl = doc.documentElement,
|
|
|
|
x = rect.left - (docEl.clientLeft || body.clientLeft || 0),
|
|
|
|
y = rect.top - (docEl.clientTop || body.clientTop || 0);
|
|
|
|
if (!viewport) {
|
|
|
|
var win = DomElement.getViewport(doc);
|
|
|
|
x += win.pageXOffset || docEl.scrollLeft || body.scrollLeft;
|
|
|
|
y += win.pageYOffset || docEl.scrollTop || body.scrollTop;
|
2011-03-14 17:14:20 -04:00
|
|
|
}
|
2011-07-31 09:13:29 -04:00
|
|
|
return new Rectangle(x, y, rect.width, rect.height);
|
|
|
|
},
|
2011-03-14 17:14:20 -04:00
|
|
|
|
2011-07-31 09:13:29 -04:00
|
|
|
getOffset: function(el, viewport) {
|
|
|
|
return this.getBounds(el, viewport).getPoint();
|
|
|
|
},
|
2011-03-14 17:14:20 -04:00
|
|
|
|
2011-07-31 09:13:29 -04:00
|
|
|
getSize: function(el) {
|
|
|
|
return this.getBounds(el, true).getSize();
|
|
|
|
},
|
2011-03-14 17:14:20 -04:00
|
|
|
|
2011-07-31 09:13:29 -04:00
|
|
|
/**
|
|
|
|
* Checks if element is invisibile (display: none, ...)
|
|
|
|
*/
|
|
|
|
isInvisible: function(el) {
|
|
|
|
return this.getSize(el).equals([0, 0]);
|
|
|
|
},
|
2011-03-14 17:37:31 -04:00
|
|
|
|
2011-07-31 09:13:29 -04:00
|
|
|
/**
|
|
|
|
* Checks if element is visibile in current viewport
|
|
|
|
*/
|
|
|
|
isVisible: function(el) {
|
|
|
|
// See if the viewport bounds intersect with the windows rectangle
|
|
|
|
// which always starts at 0, 0
|
|
|
|
return !this.isInvisible(el) && this.getViewportBounds(el).intersects(
|
2011-07-31 09:25:23 -04:00
|
|
|
this.getBounds(el, true));
|
2011-07-31 09:13:29 -04:00
|
|
|
},
|
2011-06-13 19:20:27 -04:00
|
|
|
|
2011-07-31 09:13:29 -04:00
|
|
|
getViewport: function(doc) {
|
|
|
|
return doc.defaultView || doc.parentWindow;
|
|
|
|
},
|
2011-06-26 04:16:28 -04:00
|
|
|
|
2011-07-31 09:13:29 -04:00
|
|
|
getViewportBounds: function(el) {
|
|
|
|
var doc = el.ownerDocument,
|
|
|
|
view = this.getViewport(doc),
|
|
|
|
body = doc.getElementsByTagName(
|
|
|
|
doc.compatMode === 'CSS1Compat' ? 'html' : 'body')[0];
|
|
|
|
return Rectangle.create(0, 0,
|
|
|
|
view.innerWidth || body.clientWidth,
|
|
|
|
view.innerHeight || body.clientHeight
|
|
|
|
);
|
|
|
|
},
|
2011-06-26 04:16:28 -04:00
|
|
|
|
2011-07-31 09:13:29 -04:00
|
|
|
getComputedStyle: function(el, name) {
|
|
|
|
if (el.currentStyle)
|
|
|
|
return el.currentStyle[Base.camelize(name)];
|
|
|
|
var style = this.getViewport(el.ownerDocument)
|
|
|
|
.getComputedStyle(el, null);
|
|
|
|
return style ? style.getPropertyValue(Base.hyphenate(name)) : null;
|
|
|
|
}
|
2011-03-14 17:14:20 -04:00
|
|
|
};
|