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. #9

Closed
wants to merge 1 commit into from
Closed
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
47 changes: 44 additions & 3 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 @@ -210,14 +212,27 @@
var moonRotationMatrix = mat4.create();
mat4.identity(moonRotationMatrix);



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

function handleTouchStart(evt) {
evt.preventDefault();
var touches = evt.changedTouches;

if (touches.length > 0) {
lastMouseX = touches[0].pageX,
lastMouseY = touches[0].pageY;
}
}



function handleMouseUp(event) {
function handleMouseUpOrTouchEnd(event) {
mouseDown = false;
}

Expand All @@ -229,6 +244,11 @@
var newX = event.clientX;
var newY = event.clientY;

processDrag(newX, newY);

}

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

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

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

}

var moonVertexPositionBuffer;
var moonVertexNormalBuffer;
Expand Down Expand Up @@ -390,6 +420,7 @@
drawScene();
}

var mydiv;

function webGLStart() {
var canvas = document.getElementById("lesson11-canvas");
Expand All @@ -402,12 +433,20 @@
gl.enable(gl.DEPTH_TEST);

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

el = document.getElementById('mydiv');
el.addEventListener("touchstart", handleTouchStart, false);
el.addEventListener("touchend", handleMouseUpOrTouchEnd, false);
el.addEventListener("touchcancel", handleMouseUpOrTouchEnd, false);
el.addEventListener("touchleave", handleMouseUpOrTouchEnd, false);
el.addEventListener("touchmove", handleTouchMove, false);

tick();
}


</script>


Expand All @@ -417,7 +456,9 @@
<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>
<div id="mydiv">
<canvas id="lesson11-canvas" style="border: none;" width="500" height="500"></canvas>
</div>
<br/>

<input type="checkbox" id="lighting" checked /> Use lighting<br/>
Expand Down