Skip to content

Commit

Permalink
Merge pull request #88 from fanna/orbit-touch-controls
Browse files Browse the repository at this point in the history
Add orbit touch controls
  • Loading branch information
emackey authored Oct 9, 2018
2 parents 88eda8c + 87bcd4e commit c69349a
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ function init(vertSource, fragSource) {
document.onmousemove = function(ev) { handleMouseMove(ev, redraw); };
document.onwheel = function(ev) { handleWheel(ev, redraw); };

canvas2d.ontouchstart = function(ev) { handleToucheStart(ev); };
document.ontouchend = function(ev) { handleTouchEnd(ev); };
document.ontouchmove = function(ev) { handleTouchMove(ev, redraw); };

// Initialize GUI
var gui = new dat.GUI();
var folder = gui.addFolder("Metallic-Roughness Material");
Expand Down Expand Up @@ -419,6 +423,7 @@ function resetCamera() {
pitch = 0.0;
translate = 4.0;
mouseDown = false;
touchStart = false;
}

function handleMouseDown(ev) {
Expand Down Expand Up @@ -463,6 +468,41 @@ function handleWheel(ev, redraw) {
redraw();
}

// ***** Touch Controls ***** //
var touchStart;
var lastTouchX = null;
var lastTouchY = null;

function handleToucheStart(ev) {
touchStart = true;
lastTouchX = ev.touches[0].clientX;
lastTouchY = ev.touches[0].clientY;
}

function handleTouchEnd(ev) {
touchStart = false;
}

function handleTouchMove(ev, redraw) {
if (!touchStart) {
return;
}

var newX = ev.touches[0].clientX;
var newY = ev.touches[0].clientY;

var deltaX = newX - lastTouchX;
roll += (deltaX / 100.0);

var deltaY = newY - lastTouchY;
pitch += (deltaY / 100.0);

lastTouchX = newX;
lastTouchY = newY;

redraw();
}

var prev = Date.now();
function animate(angle) {
var curr = Date.now();
Expand Down

0 comments on commit c69349a

Please sign in to comment.