Skip to content

Commit

Permalink
remove bad practices
Browse files Browse the repository at this point in the history
1.  never lookup location in the rendering loop.

    Looking up locations is slow.

2.  adjust canvas backstore to match size of canvas, not size of window.

    This means you can use the same code regardless of situation. Just
    setup your CSS how ever you want. If your canvas is inside some other
    container or it's set to 50% or whatever it will do the right
    thing.
  • Loading branch information
greggman committed Feb 23, 2014
1 parent 524425a commit 4f41f6b
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
<title>WebGL Boilerplate</title>

<style>
body {
html, body {
background-color: #000000;
margin: 0px;
overflow: hidden;
width: 100%;
height: 100%;
}
canvas {
width: 100%;
height: 100%;
}
</style>
</head>
Expand Down Expand Up @@ -71,7 +77,9 @@
buffer,
vertex_shader, fragment_shader,
currentProgram,
vertex_position,
vertex_position,
timeLocation,
resolutionLocation,
parameters = { start_time : new Date().getTime(),
time : 0,
screenWidth : 0,
Expand Down Expand Up @@ -113,9 +121,9 @@
// Create Program

currentProgram = createProgram( vertex_shader, fragment_shader );
onWindowResize();
window.addEventListener( 'resize', onWindowResize, false );

timeLocation = gl.getUniformLocation( currentProgram, 'time' );
resolutionLocation = gl.getUniformLocation( currentProgram, 'resolution' );

}

Expand Down Expand Up @@ -170,22 +178,28 @@

}

function onWindowResize( event ) {
function resizeCanvas( event ) {

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

parameters.screenWidth = canvas.width;
parameters.screenHeight = canvas.height;

gl.viewport( 0, 0, canvas.width, canvas.height );
if ( canvas.width != canvas.clientWidth ||
canvas.height != canvas.clientHeight ) {

canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;

parameters.screenWidth = canvas.width;
parameters.screenHeight = canvas.height;

gl.viewport( 0, 0, canvas.width, canvas.height );

}

}

function animate() {

requestAnimationFrame( animate );
resizeCanvas();
render();
requestAnimationFrame( animate );

}

Expand All @@ -203,8 +217,8 @@

// Set values to program variables

gl.uniform1f( gl.getUniformLocation( currentProgram, 'time' ), parameters.time / 1000 );
gl.uniform2f( gl.getUniformLocation( currentProgram, 'resolution' ), parameters.screenWidth, parameters.screenHeight );
gl.uniform1f( timeLocation, parameters.time / 1000 );
gl.uniform2f( resolutionLocation, parameters.screenWidth, parameters.screenHeight );

// Render geometry

Expand Down

0 comments on commit 4f41f6b

Please sign in to comment.