Skip to content

Commit 350df16

Browse files
Improved decomposition
1 parent 32307f9 commit 350df16

32 files changed

+6384
-164
lines changed

demo/advanced-content-webgl.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ <h1>SLIDER BELOW IS RENDERED IN WEBGL, DRAG IT</h1>
178178
<script src="./js/vendor/velocity.js"></script>
179179
<script src="./js/vendor/html2canvas.js"></script>
180180
<script src="../bower_components/pixi/bin/pixi.dev.js"></script>
181+
<script src="../src/util.js"></script>
182+
<script src="../src/gl-element-resolver.js"></script>
181183
<script src="../src/gl-context.js"></script>
182184
<script src="../src/images-loaded.js"></script>
183185
<script src="../src/gl-element.js"></script>

demo/basic-webgl.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<script src="./js/vendor/velocity.js"></script>
99
<script src="./js/vendor/html2canvas.js"></script>
1010
<script src="../bower_components/pixi/bin/pixi.dev.js"></script>
11+
<script src="../src/util.js"></script>
12+
<script src="../src/gl-element-resolver.js"></script>
1113
<script src="../src/gl-context.js"></script>
1214
<script src="../src/images-loaded.js"></script>
1315
<script src="../src/gl-element.js"></script>

demo/filters-with-fish.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ <h1>SLIDER BELOW IS RENDERED IN WEBGL, DRAG IT</h1>
178178
<script src="./js/vendor/velocity.js"></script>
179179
<script src="./js/vendor/html2canvas.js"></script>
180180
<script src="../bower_components/pixi/bin/pixi.dev.js"></script>
181+
<script src="../src/util.js"></script>
182+
<script src="../src/gl-element-resolver.js"></script>
181183
<script src="../src/gl-context.js"></script>
182184
<script src="../src/images-loaded.js"></script>
183185
<script src="../src/gl-element.js"></script>

demo/filters.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ <h1>SLIDER BELOW IS RENDERED IN WEBGL, DRAG IT</h1>
178178
<script src="./js/vendor/velocity.js"></script>
179179
<script src="./js/vendor/html2canvas.js"></script>
180180
<script src="../bower_components/pixi/bin/pixi.dev.js"></script>
181+
<script src="../src/util.js"></script>
182+
<script src="../src/gl-element-resolver.js"></script>
181183
<script src="../src/gl-context.js"></script>
182184
<script src="../src/images-loaded.js"></script>
183185
<script src="../src/gl-element.js"></script>

dist/htmlgl.js

Lines changed: 135 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -8433,42 +8433,130 @@ return function (global, window, document, undefined) {
84338433
/* The CSS spec mandates that the translateX/Y/Z transforms are %-relative to the element itself -- not its parent.
84348434
Velocity, however, doesn't make this distinction. Thus, converting to or from the % unit with these subproperties
84358435
will produce an inaccurate conversion value. The same issue exists with the cx/cy attributes of SVG circles and ellipses. */
8436+
/*
8437+
* Util is a part of HTML GL library
8438+
* Copyright (c) 2015 pixelscommander.com
8439+
* Distributed under MIT license
8440+
* http://htmlgl.com
8441+
* */
8442+
8443+
(function(w){
8444+
w.HTMLGL = w.HTMLGL || {};
8445+
w.HTMLGL.util = {
8446+
getterSetter: function (variableParent, variableName, getterFunction, setterFunction) {
8447+
if (Object.defineProperty) {
8448+
Object.defineProperty(variableParent, variableName, {
8449+
get: getterFunction,
8450+
set: setterFunction
8451+
});
8452+
}
8453+
else if (document.__defineGetter__) {
8454+
variableParent.__defineGetter__(variableName, getterFunction);
8455+
variableParent.__defineSetter__(variableName, setterFunction);
8456+
}
8457+
8458+
variableParent["get" + variableName] = getterFunction;
8459+
variableParent["set" + variableName] = setterFunction;
8460+
},
8461+
emitEvent: function (element, event) {
8462+
var newEvent = new MouseEvent(event.type, event);
8463+
newEvent.dispatcher = 'html-gl';
8464+
event.stopPropagation();
8465+
element.dispatchEvent(newEvent);
8466+
}
8467+
}
8468+
})(window);
8469+
/*
8470+
* GLElementResolver is a part of HTML GL library for resolving elements by coordinates given
8471+
* Copyright (c) 2015 pixelscommander.com
8472+
* Distributed under MIT license
8473+
* http://htmlgl.com
8474+
* */
8475+
84368476
(function (w) {
8477+
var GLElementResolver = function (context) {
8478+
}
84378479

8438-
w.HTMLGL = {
8439-
context: undefined,
8440-
stage: undefined,
8441-
renderer: undefined,
8442-
elements: [],
8443-
scrollX: 0,
8444-
scrollY: 0,
8445-
};
8480+
var p = GLElementResolver.prototype;
8481+
8482+
p.getElementByCoordinates = function (x, y) {
8483+
var element,
8484+
self = this,
8485+
result;
8486+
8487+
w.HTMLGL.elements.forEach(function (glelement) {
8488+
element = document.elementFromPoint(x - parseInt(glelement.transformObject.translateX || 0), y - parseInt(glelement.transformObject.translateY || 0))
8489+
if (self.isChildOf(element, glelement)) {
8490+
result = element;
8491+
}
8492+
});
8493+
8494+
return result;
8495+
}
8496+
8497+
p.isChildOf = function (child, parent) {
8498+
var current = child;
8499+
while (current) {
8500+
if (current === parent) return true;
8501+
current = current.parentNode;
8502+
}
8503+
return false;
8504+
}
8505+
8506+
w.HTMLGL.GLElementResolver = GLElementResolver;
8507+
})(window);
8508+
/*
8509+
* GLContext is a part of HTML GL library describing rendering context
8510+
* Copyright (c) 2015 pixelscommander.com
8511+
* Distributed under MIT license
8512+
* http://htmlgl.com
8513+
* */
84468514

8447-
var HTMLGL = function () {
8515+
(function (w) {
8516+
8517+
//Defining global namespace with respect if exists
8518+
HTMLGL = w.HTMLGL = w.HTMLGL || {};
8519+
8520+
//Defining it`s properties
8521+
HTMLGL.context = undefined;
8522+
HTMLGL.stage = undefined;
8523+
HTMLGL.renderer = undefined;
8524+
HTMLGL.elements = [];
8525+
8526+
//Cache for window`s scroll position, filled in by updateScrollPosition
8527+
HTMLGL.scrollX = 0;
8528+
HTMLGL.scrollY = 0;
8529+
8530+
var GLContext = function () {
84488531
w.HTMLGL.context = this;
84498532

8450-
this.createStage();
8451-
this.onScroll();
8452-
this.addListenerers();
8533+
this.createStage(); //Creating stage before showing it
8534+
this.updateScrollPosition(); //Initialize scroll position for first time
8535+
this.initListeners();
8536+
this.elementResolver = new w.HTMLGL.GLElementResolver(this);
84538537

8538+
//Wait for DOMContentLoaded and initialize viewer then
84548539
if (!document.body) {
8455-
document.addEventListener("DOMContentLoaded", this.init.bind(this));
8540+
document.addEventListener("DOMContentLoaded", this.initViewer.bind(this));
84568541
} else {
8457-
this.init();
8542+
this.initViewer();
84588543
}
84598544
}
84608545

8461-
var p = HTMLGL.prototype;
8546+
var p = GLContext.prototype;
84628547

8463-
p.init = function () {
8548+
p.initViewer = function () {
84648549
this.createViewer();
84658550
this.resizeViewer();
84668551
this.appendViewer();
84678552
}
84688553

8469-
p.addListenerers = function () {
8470-
w.addEventListener('scroll', this.onScroll.bind(this));
8554+
p.initListeners = function () {
8555+
//window listeners
8556+
w.addEventListener('scroll', this.updateScrollPosition.bind(this));
84718557
w.addEventListener('resize', this.resizeViewer.bind(this));
8558+
8559+
//document listeners - mouse and touch events
84728560
document.addEventListener('click', this.onMouseEvent.bind(this), true);
84738561
document.addEventListener('mousemove', this.onMouseEvent.bind(this), true);
84748562
document.addEventListener('mouseup', this.onMouseEvent.bind(this), true);
@@ -8477,7 +8565,7 @@ will produce an inaccurate conversion value. The same issue exists with the cx/c
84778565
document.addEventListener('touchend', this.onMouseEvent.bind(this));
84788566
}
84798567

8480-
p.onScroll = function () {
8568+
p.updateScrollPosition = function () {
84818569
var scrollOffset = {};
84828570

84838571
if (window.pageYOffset != undefined) {
@@ -8494,6 +8582,7 @@ will produce an inaccurate conversion value. The same issue exists with the cx/c
84948582
top: sy
84958583
};
84968584
}
8585+
84978586
this.document.x = -scrollOffset.left;
84988587
this.document.y = -scrollOffset.top;
84998588
w.HTMLGL.scrollX = scrollOffset.left;
@@ -8513,7 +8602,7 @@ will produce an inaccurate conversion value. The same issue exists with the cx/c
85138602

85148603
p.appendViewer = function () {
85158604
document.body.appendChild(this.renderer.view);
8516-
requestAnimFrame(this.redraw.bind(this));
8605+
requestAnimFrame(this.redrawStage.bind(this));
85178606
}
85188607

85198608
p.resizeViewer = function () {
@@ -8530,8 +8619,8 @@ will produce an inaccurate conversion value. The same issue exists with the cx/c
85308619
this.stage.addChild(w.HTMLGL.document);
85318620
}
85328621

8533-
p.redraw = function () {
8534-
requestAnimFrame(this.redraw.bind(this));
8622+
p.redrawStage = function () {
8623+
requestAnimFrame(this.redrawStage.bind(this));
85358624

85368625
if (this.stage.changed) {
85378626
this.renderer.render(this.stage);
@@ -8542,59 +8631,23 @@ will produce an inaccurate conversion value. The same issue exists with the cx/c
85428631
p.onMouseEvent = function (event) {
85438632
var x = event.x || event.pageX,
85448633
y = event.y || event.pageY,
8545-
element = event.dispatcher !== 'html-gl' ? this.getGLElementByCoordinates(x, y) : null;
8546-
8547-
element ? this.emitEvent(element, event) : null;
8548-
}
8549-
8550-
p.getGLElementByCoordinates = function (x, y) {
8551-
var element,
8552-
self = this,
8553-
result;
8554-
8555-
function isContained(child, parent) {
8556-
var current = child;
8557-
while (current) {
8558-
if (current === parent) return true;
8559-
current = current.parentNode;
8560-
}
8561-
return false;
8562-
}
8563-
8564-
w.HTMLGL.elements.forEach(function (glelement) {
8565-
element = document.elementFromPoint(x - parseInt(glelement.transformObject.translateX || 0), y - parseInt(glelement.transformObject.translateY || 0))
8566-
if (isContained(element, glelement)) {
8567-
result = element;
8568-
}
8569-
});
8570-
return result;
8571-
}
8634+
//Finding element under mouse position
8635+
element = event.dispatcher !== 'html-gl' ? this.elementResolver.getElementByCoordinates(x, y) : null;
85728636

8573-
p.emitEvent = function (element, event) {
8574-
var newEvent = new MouseEvent(event.type, event);
8575-
newEvent.dispatcher = 'html-gl';
8576-
event.stopPropagation();
8577-
element.dispatchEvent(newEvent);
8637+
//Emit event if there is an element under mouse position
8638+
element ? w.HTMLGL.util.emitEvent(element, event) : null;
85788639
}
85798640

8580-
new HTMLGL();
8641+
w.HTMLGL.GLContext = GLContext;
8642+
new GLContext();
85818643
})(window);
8644+
/*
8645+
* ImagesLoaded is a part of HTML GL library which is a robust solution for determining "are images loaded or not?"
8646+
* Copyright (c) 2015 pixelscommander.com
8647+
* Distributed under MIT license
8648+
* http://htmlgl.com
8649+
* */
85828650

8583-
function getterSetter(variableParent, variableName, getterFunction, setterFunction) {
8584-
if (Object.defineProperty) {
8585-
Object.defineProperty(variableParent, variableName, {
8586-
get: getterFunction,
8587-
set: setterFunction
8588-
});
8589-
}
8590-
else if (document.__defineGetter__) {
8591-
variableParent.__defineGetter__(variableName, getterFunction);
8592-
variableParent.__defineSetter__(variableName, setterFunction);
8593-
}
8594-
8595-
variableParent["get" + variableName] = getterFunction;
8596-
variableParent["set" + variableName] = setterFunction;
8597-
}
85988651
(function (w) {
85998652
var ImagesLoaded = function (element, callback) {
86008653
this.element = element;
@@ -8642,10 +8695,15 @@ function getterSetter(variableParent, variableName, getterFunction, setterFuncti
86428695
w.HTMLGL.ImagesLoaded = ImagesLoaded;
86438696
})(window);
86448697
/*
8645-
Take into account
8646-
- updateTexture is expensive
8647-
- updateSpriteTransform is cheap
8648-
*/
8698+
* GLElement is a part of HTML GL library describing single HTML-GL element
8699+
* Copyright (c) 2015 pixelscommander.com
8700+
* Distributed under MIT license
8701+
* http://htmlgl.com
8702+
*
8703+
* Please, take into account:
8704+
* - updateTexture is expensive
8705+
* - updateSpriteTransform is cheap
8706+
* */
86498707

86508708
(function (w) {
86518709
var style = document.createElement('style');
@@ -8656,6 +8714,7 @@ function getterSetter(variableParent, variableName, getterFunction, setterFuncti
86568714
p = Object.create(HTMLElement.prototype);
86578715

86588716
p.createdCallback = function () {
8717+
//Check needed
86598718
if (this.baseURI.length > 0) {
86608719
w.HTMLGL.elements.push(this);
86618720
this.renderer = 'webgl';
@@ -8784,7 +8843,7 @@ function getterSetter(variableParent, variableName, getterFunction, setterFuncti
87848843
var self = this;
87858844
self.styleGL = {};
87868845

8787-
getterSetter(this.styleGL, this.transformProperty, function () {
8846+
w.HTMLGL.util.getterSetter(this.styleGL, this.transformProperty, function () {
87888847
var result = '';
87898848

87908849
for (var transformPropertyName in self.transformObject) {
@@ -8829,7 +8888,7 @@ function getterSetter(variableParent, variableName, getterFunction, setterFuncti
88298888
}
88308889
}
88318890

8832-
w.GLElement = document.registerElement(CUSTOM_ELEMENT_TAG_NAME, {
8891+
w.HTMLGL.GLElement = document.registerElement(CUSTOM_ELEMENT_TAG_NAME, {
88338892
prototype: p
88348893
});
88358894
})(window);

dist/htmlgl.min.js

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var BUNDLE_NAME = 'htmlgl',
44
uglify = require('gulp-uglifyjs');
55

66
gulp.task('default', function() {
7-
gulp.src(['bower_components/promise-polyfill/Promise.min.js', 'bower_components/webcomponents.js/CustomElements.min.js', 'bower_components/pixi/bin/pixi.js', 'demo/js/vendor/html2canvas.js', 'demo/js/vendor/velocity.js', 'src/gl-context.js','src/images-loaded.js' , 'src/gl-element.js'])
7+
gulp.src(['bower_components/promise-polyfill/Promise.min.js', 'bower_components/webcomponents.js/CustomElements.min.js', 'bower_components/pixi/bin/pixi.js', 'demo/js/vendor/html2canvas.js', 'demo/js/vendor/velocity.js', 'src/util.js', 'src/gl-element-resolver.js', 'src/gl-context.js','src/images-loaded.js' , 'src/gl-element.js'])
88
.pipe(concat(BUNDLE_NAME + '.js'))
99
.pipe(gulp.dest('dist'))
1010
.pipe(uglify(BUNDLE_NAME + '.min.js'))

0 commit comments

Comments
 (0)