Skip to content

Commit 7dac06c

Browse files
Fixed memory leaks on Request Animation Frame
1 parent 4bfffab commit 7dac06c

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

page/css/style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ html, body {
44
width: 100%;
55
margin: 0;
66
padding: 0;
7+
min-width: 930px;
78
}
89

910
body {

page/js/effects.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,28 +123,28 @@ var switchy = false;
123123

124124
function animate() {
125125

126-
count += 0.1;
126+
if (window.HTMLGL.scrollY > 1000) {
127+
count += 0.1;
127128

128-
var blurAmount = Math.cos(count) ;
129-
var blurAmount2 = Math.sin(count * 0.8) ;
129+
var blurAmount = Math.cos(count) ;
130+
var blurAmount2 = Math.sin(count * 0.8) ;
130131

131-
var filtersToApply = [];
132+
var filtersToApply = [];
132133

133-
filtersSwitchs[0] ? overlay.visible = true : overlay.visible = false;
134+
filtersSwitchs[0] ? overlay.visible = true : overlay.visible = false;
134135

135-
for (var i = 0; i < filterCollection.length; i++) {
136-
if(filtersSwitchs[i])filtersToApply.push(filterCollection[i]);
137-
};
136+
for (var i = 0; i < filterCollection.length; i++) {
137+
if(filtersSwitchs[i])filtersToApply.push(filterCollection[i]);
138+
};
138139

139-
pondContainer.filters = filtersToApply.length > 0 ? filtersToApply : null;
140+
pondContainer.filters = filtersToApply.length > 0 ? filtersToApply : null;
140141

141-
displacementFilter.offset.x = count * 10//blurAmount * 40;
142-
displacementFilter.offset.y = count * 10
142+
displacementFilter.offset.x = count * 10//blurAmount * 40;
143+
displacementFilter.offset.y = count * 10
143144

144-
overlay.tilePosition.x = count * -10//blurAmount * 40;
145-
overlay.tilePosition.y = count * -10
145+
overlay.tilePosition.x = count * -10//blurAmount * 40;
146+
overlay.tilePosition.y = count * -10
146147

147-
if (window.HTMLGL.scrollY > 1000) {
148148
renderer.render(stage);
149149
}
150150
requestAnimFrame( animate );

src/gl-context.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
w.HTMLGL.context = this;
2525

2626
this.createStage(); //Creating stage before showing it
27-
console.log('context updated position');
2827
this.updateScrollPosition(); //Initialize scroll position for first time
2928
this.initListeners();
3029
this.elementResolver = new w.HTMLGL.GLElementResolver(this);
@@ -82,7 +81,7 @@
8281
w.HTMLGL.scrollX = scrollOffset.left;
8382
w.HTMLGL.scrollY = scrollOffset.top;
8483

85-
this.stage.changed = true;
84+
this.markStageAsChanged();
8685
}
8786

8887
p.createViewer = function () {
@@ -105,7 +104,8 @@
105104

106105
this.renderer.resize(width, height);
107106
this.updateTextures();
108-
this.stage.changed = true;
107+
108+
this.markStageAsChanged();
109109
}
110110

111111
p.createStage = function () {
@@ -114,11 +114,10 @@
114114
this.stage.addChild(w.HTMLGL.document);
115115
}
116116

117+
//Avoiding function.bind() for performance and memory consuming reasons
117118
p.redrawStage = function () {
118-
requestAnimFrame(this.redrawStage.bind(this));
119-
120-
if (this.stage.changed) {
121-
this.renderer.render(this.stage);
119+
if (w.HTMLGL.stage.changed) {
120+
w.HTMLGL.renderer.render(w.HTMLGL.stage);
122121
w.HTMLGL.stage.changed = false;
123122
}
124123
}
@@ -142,6 +141,7 @@
142141
//We would like to rerender if something changed, otherwise stand by
143142
p.markStageAsChanged = function () {
144143
if (w.HTMLGL.stage && !w.HTMLGL.stage.changed) {
144+
requestAnimFrame(this.redrawStage);
145145
w.HTMLGL.stage.changed = true;
146146
}
147147
}

0 commit comments

Comments
 (0)