Comparing version 0.0.1 to 0.1.0
57
index.js
var Stream = require('stream'), | ||
supportsTouch = require('feature/touch'); | ||
function fakeTouch(evt) { | ||
return [{ | ||
identifier: evt.button, | ||
pageX: evt.pageX, | ||
pageY: evt.pageY | ||
}]; | ||
} | ||
var capture = exports.capture = function(node, options) { | ||
@@ -8,3 +16,4 @@ // create the new stream | ||
opts = options || {}, | ||
target = opts.target || node; | ||
target = opts.target || node, | ||
isDown = false; | ||
@@ -19,17 +28,41 @@ function data(type, touches, evt) { | ||
// add the appropriate listeners | ||
node.addEventListener('touchstart', function(evt) { | ||
data('start', evt.changedTouches, evt); | ||
}); | ||
// if this interface supports touch, use touch events | ||
if (supportsTouch) { | ||
// add the appropriate listeners | ||
node.addEventListener('touchstart', function(evt) { | ||
data('start', evt.changedTouches, evt); | ||
}); | ||
node.addEventListener('touchmove', function(evt) { | ||
data('move', evt.targetTouches, evt); | ||
}); | ||
node.addEventListener('touchmove', function(evt) { | ||
data('move', evt.targetTouches, evt); | ||
}); | ||
node.addEventListener('touchend', function(evt) { | ||
data('end', evt.changedTouches, evt); | ||
}); | ||
node.addEventListener('touchend', function(evt) { | ||
data('end', evt.changedTouches, evt); | ||
}); | ||
} | ||
// otherwise, capture mouse events | ||
else { | ||
node.addEventListener('mousedown', function(evt) { | ||
isDown = isDown || (evt.button === 0); | ||
data( | ||
'start', fakeTouch(evt), evt); | ||
}); | ||
// TODO: if the interface does not support touch, then capture mouse events and broadcast as touch | ||
node.addEventListener('mousemove', function(evt) { | ||
if (isDown) { | ||
data('move', fakeTouch(evt), evt); | ||
} | ||
}); | ||
node.addEventListener('mouseup', function(evt) { | ||
data('end', fakeTouch(evt), evt); | ||
}); | ||
// mouse up events are handled at the document level | ||
document.addEventListener('mouseup', function(evt) { | ||
isDown = isDown && (evt.button !== 0); | ||
}); | ||
} | ||
return stream; | ||
@@ -36,0 +69,0 @@ }; |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"dependencies": { | ||
@@ -14,0 +14,0 @@ "feature": "0.0.x" |
3409
76