From 52ca40b96baa6bd0f4bce48a69b9bb34582386f3 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 27 Aug 2023 15:14:52 +0300 Subject: [WGL/P] Added movement pausing --- .../Project/Project.html | 4 ++-- Year 2/Computer Graphics with WebGL/Project/level.js | 20 ++++++++++++++------ Year 2/Computer Graphics with WebGL/Project/menu.js | 7 +++++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/Year 2/Computer Graphics with WebGL/Project/Project.html b/Year 2/Computer Graphics with WebGL/Project/Project.html index 1dc3ffe..202b01c 100644 --- a/Year 2/Computer Graphics with WebGL/Project/Project.html +++ b/Year 2/Computer Graphics with WebGL/Project/Project.html @@ -123,6 +123,7 @@ h2 { } window.onresize = resizeglcanvas; + var pauseAnimations = false; function drawFrame() { gl.clear(gl.COLOR_BUFFER_BIT+gl.DEPTH_BUFFER_BIT); @@ -130,9 +131,8 @@ h2 { updateCamera(); gl.uniform3fv(uEye, pos); - // stars[0].moveTo(vecSum(stars[0].sphere.center, [-0.5,0,0])); - skybox.draw(); + if (!pauseAnimations) animations.forEach((a) => a()); stars.forEach((s) => s.draw()); requestAnimationFrame(drawFrame); diff --git a/Year 2/Computer Graphics with WebGL/Project/level.js b/Year 2/Computer Graphics with WebGL/Project/level.js index 65c3cc6..68eed7a 100644 --- a/Year 2/Computer Graphics with WebGL/Project/level.js +++ b/Year 2/Computer Graphics with WebGL/Project/level.js @@ -1,5 +1,7 @@ stars = []; blackHoles = []; +animations = []; + levels = []; Level = function(name, description, cameraCoords) { @@ -8,17 +10,23 @@ Level = function(name, description, cameraCoords) { this.cameraCoords = cameraCoords; this.stars = []; this.blackHoles = []; + this.animations = []; levels.push(this); } -Level.prototype.addStar = function(center, size) { - this.stars.push(new Star(center, size)); +Level.prototype.addStar = function(center, size, animation) { + var star = new Star(center, size); + this.stars.push(star); + if (animation) this.animations.push(() => star.moveTo(animation(star))); } -Level.prototype.addBlackHole = function(center, size) { - this.blackHoles.push(new BlackHole(center, size)); +Level.prototype.addBlackHole = function(center, size, animation) { + var bh = new BlackHole(center, size); + this.blackHoles.push(bh); + if (animation) this.animations.push(() => bh.moveTo(animation(bh))); } Level.prototype.load = function() { stars = this.stars; blackHoles = this.blackHoles; + animations = this.animations; pos = this.cameraCoords; } @@ -27,6 +35,6 @@ function createLevels() { "Star passing behind black hole", "You can see how the black hole (correctly) warps the light of the star behind it when it passes", [1000,0,650]); - l1.addStar([1100,0,1000], 10); - l1.addBlackHole([1000,0,900], 30); + l1.addStar([1100,0,1000], 10, (star) => (vecSum(star.sphere.center, [-0.5,0,0]))); + l1.addBlackHole([1000,0,900], 30, undefined); } diff --git a/Year 2/Computer Graphics with WebGL/Project/menu.js b/Year 2/Computer Graphics with WebGL/Project/menu.js index 83a8e3c..1a9555f 100644 --- a/Year 2/Computer Graphics with WebGL/Project/menu.js +++ b/Year 2/Computer Graphics with WebGL/Project/menu.js @@ -50,3 +50,10 @@ function levelChange() { levels[this.value].load(); } levelSelect.addEventListener('change', levelChange); + +const animPause = document.getElementById('pause-movement'); +function pauseMovement() { + pauseAnimations = this.checked; +} +pauseMovement.call(animPause); +animPause.addEventListener('change', pauseMovement); -- cgit v1.3.1-23-gd82d