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

Add orbit touch controls #88

Merged
merged 1 commit into from
Oct 9, 2018
Merged
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
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