I have implemented pointer events for canvas object. I need to know how we can detect finger count for touch events. Here is a piece of my code:
canvasObj.addEventListener( 'pointerenter', mouseEnterCall, false );
canvasObj.addEventListener( 'pointerdown', mouseDownCall, false );
canvasObj.addEventListener( 'pointermove', mouseMoveCall, false );
canvasObj.addEventListener( 'pointerup', mouseUpCall, false );
canvasObj.addEventListener( 'pointerout', mouseOutCall, false );
Appreciate your help.
There's no built-in property that gives you the current number of fingers (active pointers) on the screen. But here's some simple code that would achieve this:
var pointerCount = 0; //Stores current number of "active pointers"
window.addEventListener("pointerdown", addPointer, true);
window.addEventListener("pointerup", removePointer, true);
window.addEventListener("pointercancel", removePointer, true);
function addPointer(e) { pointerCount++ }
function removePointer(e) { pointerCount-- }
You can modify addPointer to only count touch pointers, if that's what you want:
function addPointer(e) { if (e.pointerType === "touch") pointerCount++ }
Note that you'll need to modify this code somewhat if you want to also support IE10, which has an earlier (prefixed) version of the standard.