2011-03-14 17:14:20 -04:00
|
|
|
/*
|
|
|
|
* Paper.js
|
2011-06-22 18:56:05 -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-22 18:56:05 -04:00
|
|
|
*
|
2011-03-14 17:14:20 -04:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
2011-06-22 18:56:05 -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-22 18:56:05 -04:00
|
|
|
*
|
2011-03-14 17:14:20 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2011-05-08 05:16:11 -04:00
|
|
|
var DomElement = new function() {
|
2011-06-02 15:43:54 -04:00
|
|
|
function cumulate(el, name, parent, positioned) {
|
2011-05-07 19:54:07 -04:00
|
|
|
var left = name + 'Left',
|
|
|
|
top = name + 'Top',
|
|
|
|
x = 0,
|
|
|
|
y = 0;
|
2011-06-02 15:43:54 -04:00
|
|
|
// If we're asked to calculate positioned offset, stop at any
|
|
|
|
// parent element that has relative or absolute position.
|
|
|
|
while (el && (!positioned
|
|
|
|
|| !/^(relative|absolute)$/.test(el.style.position))) {
|
2011-05-07 19:54:07 -04:00
|
|
|
x += el[left] || 0;
|
|
|
|
y += el[top] || 0;
|
|
|
|
el = el[parent];
|
2011-03-14 17:14:20 -04:00
|
|
|
}
|
|
|
|
return Point.create(x, y);
|
2011-05-07 19:54:07 -04:00
|
|
|
}
|
2011-03-14 17:14:20 -04:00
|
|
|
|
2011-05-07 19:54:07 -04:00
|
|
|
return {
|
2011-06-02 15:43:54 -04:00
|
|
|
getOffset: function(el, positioned, scroll) {
|
|
|
|
var point = cumulate(el, 'offset', 'offsetParent', positioned);
|
2011-05-07 19:54:07 -04:00
|
|
|
return scroll
|
|
|
|
? point.subtract(cumulate(el, 'scroll', 'parentNode'))
|
|
|
|
: point;
|
|
|
|
},
|
2011-03-14 17:14:20 -04:00
|
|
|
|
2011-05-07 19:54:07 -04:00
|
|
|
getSize: function(el) {
|
|
|
|
return Size.create(el.offsetWidth, el.offsetHeight);
|
|
|
|
},
|
2011-03-14 17:14:20 -04:00
|
|
|
|
2011-06-02 15:43:54 -04:00
|
|
|
getBounds: function(el, positioned, scroll) {
|
|
|
|
return new Rectangle(DomElement.getOffset(el, positioned, scroll),
|
2011-05-08 05:16:11 -04:00
|
|
|
DomElement.getSize(el));
|
2011-05-07 19:54:07 -04:00
|
|
|
},
|
2011-03-14 17:37:31 -04:00
|
|
|
|
2011-05-07 19:54:07 -04:00
|
|
|
getWindowSize: function() {
|
|
|
|
var doc = document.getElementsByTagName(
|
|
|
|
document.compatMode === 'CSS1Compat' ? 'html' : 'body')[0];
|
|
|
|
return Size.create(
|
|
|
|
window.innerWidth || doc.clientWidth,
|
|
|
|
window.innerHeight || doc.clientHeight
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2011-06-13 19:20:27 -04:00
|
|
|
/**
|
|
|
|
* Checks if element is invisibile (display: none, ...)
|
|
|
|
*/
|
|
|
|
isInvisible: function(el) {
|
|
|
|
return DomElement.getSize(el).equals([0, 0]);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if element is visibile in current viewport
|
|
|
|
*/
|
2011-05-07 19:54:07 -04:00
|
|
|
isVisible: function(el) {
|
|
|
|
// See if the scrolled bounds intersect with the windows rectangle
|
|
|
|
// which always starts at 0, 0
|
2011-06-20 17:51:05 -04:00
|
|
|
return !DomElement.isInvisible(el)
|
|
|
|
&& new Rectangle([0, 0], DomElement.getWindowSize())
|
|
|
|
.intersects(DomElement.getBounds(el, false, true));
|
2011-05-07 19:54:07 -04:00
|
|
|
}
|
|
|
|
};
|
2011-03-14 17:14:20 -04:00
|
|
|
};
|