Skip to content

Commit 82f19ea

Browse files
committed
...
1 parent c89cea5 commit 82f19ea

File tree

1 file changed

+68
-30
lines changed

1 file changed

+68
-30
lines changed

webgl/resources/same-code-02.js

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* Licensed under a BSD license. See license.html for license */
2+
/* eslint strict: off */
23
"use strict";
34

45
var vertexShaderSource = `
@@ -100,11 +101,9 @@ function main() {
100101
return d * Math.PI / 180;
101102
}
102103

103-
var cameraAngleRadians = degToRad(0);
104-
var fieldOfViewRadians = degToRad(40);
105-
var cameraHeight = 40;
106-
var zNear = 1;
107-
var zFar = 150;
104+
const fieldOfViewRadians = degToRad(40);
105+
const zNear = 1;
106+
const zFar = 150;
108107

109108
var uniformsThatAreTheSameForAllObjects = {
110109
u_lightWorldPos: [-50, 30, 100],
@@ -129,13 +128,51 @@ function main() {
129128
u_specularFactor: 1,
130129
};
131130

132-
requestAnimationFrame(drawScene);
131+
let requestId;
132+
let running;
133+
let then = 0;
134+
let time = 0;
135+
function startAnimation() {
136+
running = true;
137+
requestAnimation();
138+
}
139+
140+
function stopAnimation() {
141+
running = false;
142+
}
143+
144+
function requestAnimation() {
145+
if (!requestId) {
146+
requestId = requestAnimationFrame(drawScene);
147+
}
148+
}
149+
150+
const motionQuery = matchMedia('(prefers-reduced-motion)');
151+
function handleReduceMotionChanged() {
152+
if (motionQuery.matches) {
153+
stopAnimation();
154+
} else {
155+
startAnimation();
156+
}
157+
}
158+
motionQuery.addEventListener('change', handleReduceMotionChanged);
159+
handleReduceMotionChanged();
160+
requestAnimation();
161+
162+
const observer = new ResizeObserver(requestAnimation);
163+
observer.observe(gl.canvas);
133164

134165
// Draw the scene.
135-
function drawScene(time) {
136-
webglUtils.resizeCanvasToDisplaySize(canvas);
166+
function drawScene(now) {
167+
requestId = undefined;
168+
169+
const elapsed = Math.min(now - then, 1000 / 10);
170+
then = now;
171+
if (running) {
172+
time += elapsed * 0.001;
173+
}
137174

138-
time *= 0.001; // convert to seconds
175+
webglUtils.resizeCanvasToDisplaySize(canvas);
139176

140177
// Set the viewport to match the canvas
141178
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
@@ -145,21 +182,21 @@ function main() {
145182
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
146183

147184
// Compute the projection matrix
148-
var aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
149-
var projectionMatrix = m4.perspective(fieldOfViewRadians, aspect, 1, 150);
185+
const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
186+
const projectionMatrix = m4.perspective(fieldOfViewRadians, aspect, 1, 150);
150187

151188
// Compute the camera's matrix using look at.
152-
var orbitRadius = 100;
153-
var orbitTime = 1 + time * 0.05;
154-
var cameraPosition = [Math.cos(orbitTime) * orbitRadius, Math.sin(orbitTime * 1.123) * orbitRadius, Math.sin(orbitTime) * orbitRadius];
155-
var target = [0, 0, 0];
156-
var up = [0, 1, 0];
157-
var cameraMatrix = m4.lookAt(cameraPosition, target, up, uniformsThatAreTheSameForAllObjects.u_viewInverse);
189+
const orbitRadius = 100;
190+
const orbitTime = 1 + time * 0.05;
191+
const cameraPosition = [Math.cos(orbitTime) * orbitRadius, Math.sin(orbitTime * 1.123) * orbitRadius, Math.sin(orbitTime) * orbitRadius];
192+
const target = [0, 0, 0];
193+
const up = [0, 1, 0];
194+
const cameraMatrix = m4.lookAt(cameraPosition, target, up, uniformsThatAreTheSameForAllObjects.u_viewInverse);
158195

159196
// Make a view matrix from the camera matrix.
160-
var viewMatrix = m4.inverse(cameraMatrix);
197+
const viewMatrix = m4.inverse(cameraMatrix);
161198

162-
var viewProjectionMatrix = m4.multiply(projectionMatrix, viewMatrix);
199+
const viewProjectionMatrix = m4.multiply(projectionMatrix, viewMatrix);
163200

164201
gl.useProgram(programInfo.program);
165202

@@ -170,27 +207,27 @@ function main() {
170207
webglUtils.setUniforms(programInfo, uniformsThatAreTheSameForAllObjects);
171208

172209
// Draw objects
173-
var num = 4;
174-
var spread = 20;
175-
for (var zz = -num; zz <= num; ++zz) {
176-
for (var yy = -num; yy <= num; ++yy) {
177-
for (var xx = -num; xx <= num; ++xx) {
210+
const num = 4;
211+
const spread = 20;
212+
for (let zz = -num; zz <= num; ++zz) {
213+
for (let yy = -num; yy <= num; ++yy) {
214+
for (let xx = -num; xx <= num; ++xx) {
178215
var worldMatrix = m4.translation(xx * spread, yy * spread, zz * spread);
179216

180217
// Multiply the matrices.
181218
m4.multiply(
182219
viewProjectionMatrix, worldMatrix,
183220
uniformsThatAreComputedForEachObject.u_worldViewProjection);
184-
var matrix = m4.multiply(viewMatrix, worldMatrix);
221+
// var matrix = m4.multiply(viewMatrix, worldMatrix);
185222
m4.transpose(m4.inverse(worldMatrix), uniformsThatAreComputedForEachObject.u_worldInverseTranspose);
186223

187224
// Set the uniforms we just computed
188225
webglUtils.setUniforms(programInfo, uniformsThatAreComputedForEachObject);
189226

190227
// Set a color for this object.
191-
materialUniforms.u_diffuse[0] = 1; xx / num * 0.5 + 0.5;
192-
materialUniforms.u_diffuse[1] = 1; yy / num * 0.5 + 0.5;
193-
materialUniforms.u_diffuse[2] = 1; zz / num * 0.5 + 0.5;
228+
materialUniforms.u_diffuse[0] = 1; // xx / num * 0.5 + 0.5;
229+
materialUniforms.u_diffuse[1] = 1; // yy / num * 0.5 + 0.5;
230+
materialUniforms.u_diffuse[2] = 1; // zz / num * 0.5 + 0.5;
194231

195232
// Set the uniforms that are specific to the this object.
196233
webglUtils.setUniforms(programInfo, materialUniforms);
@@ -200,8 +237,9 @@ function main() {
200237
}
201238
}
202239
}
203-
204-
requestAnimationFrame(drawScene);
240+
if (running) {
241+
requestAnimation(drawScene);
242+
}
205243
}
206244
}
207245

0 commit comments

Comments
 (0)