我已经为canvas对象实现了指针事件。我需要知道如何检测触摸事件的手指数。这是我的一段代码:
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 );
感谢您的帮助。
答案 0 :(得分:2)
没有内置属性可以为您提供屏幕上当前手指数(活动指针)。但是这里有一些简单的代码可以达到这个目的:
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-- }
您可以修改addPointer以仅计算触控指针,如果这是您想要的:
function addPointer(e) { if (e.pointerType === "touch") pointerCount++ }
请注意,如果您还想支持IE10,您需要稍微修改此代码,IE10具有该标准的早期(带前缀)版本。