Skip to content

Commit 9651f25

Browse files
committed
Fix multiple-views-items examples
I have no idea when this broke or why it broke. If you run the examples outside the editor they work. If you export them to jsfiddle, codepen, jsgist, stackoverflow, they all work. But for some reason they don't work in these iframes. To see the issue, reset to the previous version of this repo, build, view the webgl/lessons/webgl-multiple-views.html lesson. Scroll down to the last two examples. In the example itself, scroll down. Eventually the 3D stuff will disappear. The issue is the browser stops updating window.scrollY at some point. I don't know why. As it says above, outside an iframe and even inside an iframe on all those sites it works fine. Here it's an iframe in an iframe but I'm not convinced that's it as I think stackoverflow's snippet editor is an iframe in an iframe. I was able to repo at least one issue in an iframe but it didn't lead to what the problem is. Using an outer div and scrolling inside that seems to fix the issue.
1 parent 0d6c975 commit 9651f25

File tree

4 files changed

+58
-21
lines changed

4 files changed

+58
-21
lines changed

webgl/lessons/ko/webgl-multiple-views.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,16 +358,24 @@ function render() {
358358
359359
```html
360360
<body>
361-
<canvas id="canvas"></canvas>
362-
<div id="content"></div>
361+
<div id="outer">
362+
<canvas id="canvas"></canvas>
363+
<div id="content"></div>
364+
</div>
363365
</body>
364366
```
365367
366368
다음은 CSS입니다.
367369
368370
```css
369-
body {
371+
html, body {
370372
margin: 0;
373+
height: 100%;
374+
}
375+
#outer {
376+
position: absolute;
377+
height: 100%;
378+
overflow: auto;
371379
}
372380
#content {
373381
margin: 10px;
@@ -478,6 +486,8 @@ for (let i = 0; i < numItems; ++i) {
478486
그렇다면 뷰포트와 시저가 일치하도록 설정한 다음 해당 객체를 그립니다.
479487
480488
```js
489+
const outerElem = document.querySelector('#outer');
490+
481491
function render(time) {
482492
time *= 0.001; // 초 단위로 변환
483493

@@ -488,7 +498,7 @@ function render(time) {
488498
gl.enable(gl.SCISSOR_TEST);
489499

490500
// 캔버스를 현재 스크롤 위치의 상단으로 이동
491-
gl.canvas.style.transform = `translateY(${window.scrollY}px)`;
501+
gl.canvas.style.transform = `translateY(${outerElem.scrollTop}px)`;
492502

493503
for (const {bufferInfo, element, color} of items) {
494504
const rect = element.getBoundingClientRect();
@@ -544,7 +554,7 @@ requestAnimationFrame(render);
544554
코드에서 한 가지 더 주목할 만한 점은 이 라인으로 캔버스를 움직이고 있다는 겁니다.
545555
546556
```js
547-
gl.canvas.style.transform = `translateY(${window.scrollY}px)`;
557+
gl.canvas.style.transform = `translateY(${outerElem.scrollTop}px)`;
548558
```
549559
550560
대신 캔버스를 `position: fixed;`로 설정할 수 있는데, 이 경우에는 페이지에 따라 스크롤되지 않습니다.
@@ -570,13 +580,13 @@ gl.canvas.style.transform = `translateY(${window.scrollY}px)`;
570580
가로 스크롤을 처리하고 싶다면 이 라인을 바꾸면 되는데요.
571581
572582
```js
573-
gl.canvas.style.transform = `translateY(${window.scrollY}px)`;
583+
gl.canvas.style.transform = `translateY(${outerElem.scrollTop}px)`;
574584
```
575585
576586
이렇게 변경합니다.
577587
578588
```js
579-
gl.canvas.style.transform = `translateX(${window.scrollX}px) translateY(${window.scrollY}px)`;
589+
gl.canvas.style.transform = `translateX(${outerElem.scrollLeft}px) translateY(${outerElem.scrollTop}px)`;
580590
```
581591
582592
{{{example url="../webgl-multiple-views-items-horizontal-scrolling.html"}}}

webgl/lessons/webgl-multiple-views.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,16 +398,24 @@ that goes in front. First the HTML
398398
399399
```html
400400
<body>
401-
<canvas id="canvas"></canvas>
402-
<div id="content"></div>
401+
<div id="outer">
402+
<canvas id="canvas"></canvas>
403+
<div id="content"></div>
404+
</div>
403405
</body>
404406
```
405407
406408
Then the CSS
407409
408410
```css
409-
body {
411+
html, body {
410412
margin: 0;
413+
height: 100%;
414+
}
415+
#outer {
416+
position: absolute;
417+
height: 100%;
418+
overflow: auto;
411419
}
412420
#content {
413421
margin: 10px;
@@ -521,6 +529,8 @@ with the canvas. If it does we set the viewport and scissor to
521529
match and then draw that object.
522530
523531
```js
532+
const outerElem = document.querySelector('#outer');
533+
524534
function render(time) {
525535
time *= 0.001; // convert to seconds
526536

@@ -531,7 +541,7 @@ function render(time) {
531541
gl.enable(gl.SCISSOR_TEST);
532542

533543
// move the canvas to top of the current scroll position
534-
gl.canvas.style.transform = `translateY(${window.scrollY}px)`;
544+
gl.canvas.style.transform = `translateY(${outerElem.scrollTop}px)`;
535545

536546
for (const {bufferInfo, element, color} of items) {
537547
const rect = element.getBoundingClientRect();
@@ -590,7 +600,7 @@ One other notable thing about the code is we're moving the canvas
590600
with this line
591601
592602
```js
593-
gl.canvas.style.transform = `translateY(${window.scrollY}px)`;
603+
gl.canvas.style.transform = `translateY(${outerElem.scrollTop}px)`;
594604
```
595605
596606
Why? We could instead set the canvas to `position: fixed;` in which case
@@ -618,13 +628,13 @@ faster than we can draw our objects. Because of this we have 2 options.
618628
If you want to handle horizontal scrolling just change this line
619629
620630
```js
621-
gl.canvas.style.transform = `translateY(${window.scrollY}px)`;
631+
gl.canvas.style.transform = `translateY(${outerElem.scrollTop}px)`;
622632
```
623633
624634
to this
625635
626636
```js
627-
gl.canvas.style.transform = `translateX(${window.scrollX}px) translateY(${window.scrollY}px)`;
637+
gl.canvas.style.transform = `translateX(${outerElem.scrollLeft}px) translateY(${outerElem.scrollTop}px)`;
628638
```
629639
630640
{{{example url="../webgl-multiple-views-items-horizontal-scrolling.html"}}}

webgl/webgl-multiple-views-items-horizontal-scrolling.html

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
<style>
99
html, body {
1010
margin: 0;
11-
width: 200%;
1211
height: 100%;
1312
}
13+
#outer {
14+
position: absolute;
15+
width: 100%;
16+
height: 100%;
17+
overflow: auto;
18+
}
1419
#content {
20+
width: 200%;
1521
margin: 10px;
1622
}
1723
#canvas {
@@ -38,8 +44,10 @@
3844
</style>
3945
</head>
4046
<body>
41-
<canvas id="canvas"></canvas>
42-
<div id="content"></div>
47+
<div id="outer">
48+
<canvas id="canvas"></canvas>
49+
<div id="content"></div>
50+
</div>
4351
</body>
4452
<!--
4553
for most samples webgl-utils only provides shader compiling/linking and
@@ -162,6 +170,7 @@
162170
}
163171

164172
const fieldOfViewRadians = degToRad(60);
173+
const outerElem = document.querySelector('#outer');
165174

166175
function drawScene(projectionMatrix, cameraMatrix, worldMatrix, bufferInfo) {
167176
// Clear the canvas AND the depth buffer.
@@ -198,7 +207,7 @@
198207
gl.enable(gl.SCISSOR_TEST);
199208

200209
// move the canvas to top of the current scroll position
201-
gl.canvas.style.transform = `translateX(${window.scrollX}px) translateY(${window.scrollY}px)`;
210+
gl.canvas.style.transform = `translateX(${outerElem.scrollLeft}px) translateY(${outerElem.scrollTop}px)`;
202211

203212
for (const {bufferInfo, element, color} of items) {
204213
const rect = element.getBoundingClientRect();

webgl/webgl-multiple-views-items.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
margin: 0;
1111
height: 100%;
1212
}
13+
#outer {
14+
position: absolute;
15+
height: 100%;
16+
overflow: auto;
17+
}
1318
#content {
1419
margin: 10px;
1520
}
@@ -37,8 +42,10 @@
3742
</style>
3843
</head>
3944
<body>
40-
<canvas id="canvas"></canvas>
41-
<div id="content"></div>
45+
<div id="outer">
46+
<canvas id="canvas"></canvas>
47+
<div id="content"></div>
48+
</div>
4249
</body>
4350
<!--
4451
for most samples webgl-utils only provides shader compiling/linking and
@@ -161,6 +168,7 @@
161168
}
162169

163170
const fieldOfViewRadians = degToRad(60);
171+
const outerElem = document.querySelector('#outer');
164172

165173
function drawScene(projectionMatrix, cameraMatrix, worldMatrix, bufferInfo) {
166174
// Clear the canvas AND the depth buffer.
@@ -197,7 +205,7 @@
197205
gl.enable(gl.SCISSOR_TEST);
198206

199207
// move the canvas to top of the current scroll position
200-
gl.canvas.style.transform = `translateY(${window.scrollY}px)`;
208+
gl.canvas.style.transform = `translateY(${outerElem.scrollTop}px)`;
201209

202210
for (const {bufferInfo, element, color} of items) {
203211
const rect = element.getBoundingClientRect();

0 commit comments

Comments
 (0)