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

Implement touch event rotate + "pinch to zoom" #18

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions docs/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var bunny = require('bunny')
var normals = require('angle-normals')

window.addEventListener('resize', function () {
camera.dirty = true;
});
camera.dirty = true
})

var drawBunny = regl({
frag: `
Expand Down
13 changes: 6 additions & 7 deletions docs/index.html

Large diffs are not rendered by default.

86 changes: 76 additions & 10 deletions regl-camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function createCamera (regl, props_) {

// Preserve backward-compatibilty while renaming preventDefault -> noScroll
if (typeof props.noScroll === 'undefined') {
props.noScroll = props.preventDefault;
props.noScroll = props.preventDefault
}

var cameraState = {
Expand Down Expand Up @@ -50,6 +50,7 @@ function createCamera (regl, props_) {

var prevX = 0
var prevY = 0
var prevDistance = null

if (isBrowser && props.mouse !== false) {
var source = element || regl._gl.canvas
Expand All @@ -69,15 +70,80 @@ function createCamera (regl, props_) {

cameraState.dtheta += cameraState.rotationSpeed * 4.0 * dx
cameraState.dphi += cameraState.rotationSpeed * 4.0 * dy
cameraState.dirty = true;
cameraState.dirty = true
}
prevX = x
prevY = y
})

source.addEventListener('touchstart', function (ev) {
if (ev.cancelable) {
ev.preventDefault()
}
var touch = ev.touches[0]
var bounds = source.getBoundingClientRect()
var x = touch.clientX - bounds.left
var y = touch.clientY - bounds.top
prevX = x
prevY = y
})

source.addEventListener('touchend', function (ev) {
prevDistance = null

// reset prevX and prevY to prevent jumping
if (ev.touches.length === 1) {
var touch = ev.touches[0]
var bounds = source.getBoundingClientRect()
var x = touch.clientX - bounds.left
var y = touch.clientY - bounds.top
prevX = x
prevY = y
}
})

source.addEventListener('touchmove', function (ev) {
if (ev.cancelable) {
ev.preventDefault()
}
// One touch, treat as a rotate
if (ev.touches.length === 1) {
var touch = ev.touches[0]
var bounds = source.getBoundingClientRect()
var x = touch.clientX - bounds.left
var y = touch.clientY - bounds.top

var dx = (x - prevX) / getWidth()
var dy = (y - prevY) / getHeight()

cameraState.dtheta += cameraState.rotationSpeed * 4.0 * dx
cameraState.dphi += cameraState.rotationSpeed * 4.0 * dy
cameraState.dirty = true

prevX = x
prevY = y
// Two fingers, treat as a pinch zoom
} else if (ev.touches.length === 2) {
var touch1 = ev.touches[0]
var touch2 = ev.touches[1]

var pinchX = touch1.clientX - touch2.clientX
var pinchY = touch1.clientY - touch2.clientY
var distance = Math.sqrt(pinchX * pinchX + pinchY * pinchY)
if (prevDistance != null) {
var distanceDelta = prevDistance - distance
ddistance +=
(distanceDelta / getHeight()) * cameraState.zoomSpeed * 2.0
cameraState.dirty = true
}

prevDistance = distance
}
})

mouseWheel(source, function (dx, dy) {
ddistance += dy / getHeight() * cameraState.zoomSpeed
cameraState.dirty = true;
cameraState.dirty = true
}, props.noScroll)
}

Expand All @@ -86,7 +152,7 @@ function createCamera (regl, props_) {
if (Math.abs(xd) < 0.1) {
return 0
}
cameraState.dirty = true;
cameraState.dirty = true
return xd
}

Expand Down Expand Up @@ -134,12 +200,12 @@ function createCamera (regl, props_) {
lookAt(cameraState.view, eye, center, up)
}

cameraState.dirty = true;
cameraState.dirty = true

var injectContext = regl({
context: Object.assign({}, cameraState, {
dirty: function () {
return cameraState.dirty;
return cameraState.dirty
},
projection: function (context) {
perspective(cameraState.projection,
Expand All @@ -160,14 +226,14 @@ function createCamera (regl, props_) {
function setupCamera (props, block) {
if (typeof setupCamera.dirty !== 'undefined') {
cameraState.dirty = setupCamera.dirty || cameraState.dirty
setupCamera.dirty = undefined;
setupCamera.dirty = undefined
}

if (props && block) {
cameraState.dirty = true;
cameraState.dirty = true
}

if (cameraState.renderOnDirty && !cameraState.dirty) return;
if (cameraState.renderOnDirty && !cameraState.dirty) return

if (!block) {
block = props
Expand All @@ -176,7 +242,7 @@ function createCamera (regl, props_) {

updateCamera(props)
injectContext(block)
cameraState.dirty = false;
cameraState.dirty = false
}

Object.keys(cameraState).forEach(function (name) {
Expand Down