Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added user interaction #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions 02 - Rotating Cube/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,53 @@ var fragmentShaderText =
'}'
].join('\n');

var angle = 0;
var manualPerformance = 0.01;
var flagAnimation = true;

var stopAnimation = function (){
flagAnimation = false;
manualPerformance = performance.now() / 1000;
}

var playAnimation = function (){
flagAnimation = true;
}

var rotateRight = function () {
if (!flagAnimation){
manualPerformance += 0.04;
angle = manualPerformance / 6 * 2 * Math.PI;
} else {
flagAnimation = false;
manualPerformance = performance.now() / 1000;
}
}

var rotateLeft = function () {
if(!flagAnimation){
manualPerformance -= 0.04;
angle = manualPerformance / 6 * 2 * Math.PI;
} else {
flagAnimation = false;
manualPerformance = performance.now() / 1000;
}
}

var pressKey = function (key){

var tecla = key.which;

if(tecla == 97){
rotateLeft();
}

if(tecla == 100){
rotateRight();
}

}

var InitDemo = function () {
console.log('This is working');

Expand Down Expand Up @@ -209,9 +256,10 @@ var InitDemo = function () {
//
var identityMatrix = new Float32Array(16);
mat4.identity(identityMatrix);
var angle = 0;
angle = 0;
var loop = function () {
angle = performance.now() / 1000 / 6 * 2 * Math.PI;
if (flagAnimation)
angle = performance.now() / 1000 / 6 * 2 * Math.PI;
mat4.rotate(yRotationMatrix, identityMatrix, angle, [0, 1, 0]);
mat4.rotate(xRotationMatrix, identityMatrix, angle / 4, [1, 0, 0]);
mat4.mul(worldMatrix, yRotationMatrix, xRotationMatrix);
Expand Down
6 changes: 5 additions & 1 deletion 02 - Rotating Cube/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
<head>
<title>WebGL - Rotating Cube</title>
</head>
<body onload="InitDemo();">
<body onload="InitDemo();" onkeypress="pressKey(event);">
<canvas id="game-surface" width="800" height="600">
Your browser does not support HTML5
</canvas>
<br />
<i>Demo is above this text</i>
<script src="gl-matrix.js"></script>
<script src="app.js"></script>
<button onclick="playAnimation();">Play Animation</button>
<button onclick="stopAnimation();">Stop Animation</button>
<button onclick="rotateLeft();">Rotate Left (A)</button>
<button onclick="rotateRight();">Rotate Right (D)</button>
</body>
</html>