From 87bcd4e9b4034ddcc1f945dc161660f5ff255395 Mon Sep 17 00:00:00 2001 From: Stefan Mijucic Date: Mon, 1 Oct 2018 22:20:13 +0200 Subject: [PATCH] Add orbit touch controls --- main.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/main.js b/main.js index fbe5458e..10f280ad 100644 --- a/main.js +++ b/main.js @@ -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"); @@ -419,6 +423,7 @@ function resetCamera() { pitch = 0.0; translate = 4.0; mouseDown = false; + touchStart = false; } function handleMouseDown(ev) { @@ -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();