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

Make example work on touch screen as well. #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
51 changes: 42 additions & 9 deletions lesson11/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@

<script type="text/javascript">



var gl;

function initGL(canvas) {
Expand Down Expand Up @@ -203,32 +205,57 @@
}


var mouseDown = false;
var mouseDownOrTouchActive = false;
var lastMouseX = null;
var lastMouseY = null;
var touchId = null;

var moonRotationMatrix = mat4.create();
mat4.identity(moonRotationMatrix);

function handleMouseDown(event) {
mouseDown = true;
mouseDownOrTouchActive = true;
lastMouseX = event.clientX;
lastMouseY = event.clientY;
}


function handleMouseUp(event) {
mouseDown = false;
function handleTouchStart(evt) {
evt.preventDefault();
var touches = evt.targetTouches;

if (touches.length == 1 && !mouseDownOrTouchActive ) {
touchId = touches[0].identifier;
lastMouseX = touches[0].pageX,
lastMouseY = touches[0].pageY;
}
}

function handleMouseUpOrTouchEnd(event) {
mouseDownOrTouchActive = false;
}

function handleMouseMove(event) {
if (!mouseDown) {
if (!mouseDownOrTouchActive) {
return;
}
var newX = event.clientX;
var newY = event.clientY;

processDrag(newX, newY);
}

function handleTouchMove(evt) {
evt.preventDefault();
var touches = evt.targetTouches;

if (touches.length == 1 && touchId == touches[0].identifier) {
var newX = touches[0].pageX;
var newY = touches[0].pageY;
processDrag(newX, newY);
}
}

function processDrag(newX, newY) {
var deltaX = newX - lastMouseX
var newRotationMatrix = mat4.create();
mat4.identity(newRotationMatrix);
Expand All @@ -243,8 +270,6 @@
lastMouseY = newY;
}



var moonVertexPositionBuffer;
var moonVertexNormalBuffer;
var moonVertexTextureCoordBuffer;
Expand Down Expand Up @@ -402,12 +427,19 @@
gl.enable(gl.DEPTH_TEST);

canvas.onmousedown = handleMouseDown;
document.onmouseup = handleMouseUp;
document.onmouseup = handleMouseUpOrTouchEnd;
document.onmousemove = handleMouseMove;

canvas.addEventListener("touchstart", handleTouchStart, false);
canvas.addEventListener("touchend", handleMouseUpOrTouchEnd, false);
canvas.addEventListener("touchcancel", handleMouseUpOrTouchEnd, false);
canvas.addEventListener("touchleave", handleMouseUpOrTouchEnd, false);
canvas.addEventListener("touchmove", handleTouchMove, false);

tick();
}


</script>


Expand All @@ -417,6 +449,7 @@
<body onload="webGLStart();">
<a href="http://learningwebgl.com/blog/?p=1253">&lt;&lt; Back to Lesson 11</a><br />


<canvas id="lesson11-canvas" style="border: none;" width="500" height="500"></canvas>
<br/>

Expand Down