-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
63 lines (50 loc) · 1.45 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Canvas to draw on
var canvas
// WebGL context
var gl
// Engine
var engine = new Engine()
// When window loads, perform all steps to initialize & draw the WebGL scene
window.onload = async function() {
initializeWebGL()
engine.initializeScene()
updateCanvasSize()
engine.run()
}
// Initializes canvas & webGl context
function initializeWebGL() {
canvas = document.getElementById("canvas")
// Disables right click context menu
canvas.oncontextmenu = function() {
return false
}
gl = canvas.getContext("webgl", { antialias: false, depth: true })
if (!gl) {
alert("Failed to initialize WebGL. Check your browser & settings compatibility.")
return
}
gl.clearColor(0.0, 0.0, 0.0, 0.0)
gl.enable(gl.DEPTH_TEST)
gl.enable(gl.CULL_FACE)
gl.cullFace(gl.BACK)
}
// Updates canvas size & redraws scene
function windowResize() {
updateCanvasSize()
}
function updateCanvasSize() {
// Calculate viewport size
canvas.style.width = "100%"
canvas.style.height = "100%"
const pixelRatio = window.devicePixelRatio || 1
canvas.width = pixelRatio * canvas.clientWidth
canvas.height = pixelRatio * canvas.clientHeight
const width = (canvas.width / pixelRatio)
const height = (canvas.height / pixelRatio)
canvas.style.width = width + 'px'
canvas.style.height = height + 'px'
// Set WebGL to match viewport size
gl.viewport(0, 0, canvas.width, canvas.height)
// Upon resizing canvas, notify scene objects
engine.notifyViewportUpdated()
}