Pointer Events API 是Hmtl5的事件规范之一,它主要目的是用来将鼠标(Mouse)、触摸(touch)和触控笔(pen)三种事件整合为统一的API。

Pointer Event

Pointer指可以在屏幕上反馈一个指定坐标的输入设备。Pointer Event事件和Touch Event API对应的触摸事件类似,它继承扩展了Touch Event,因此拥有Touch Event的常用属性。Pointer属性如下图:

说明:

pointerId:代表每一个独立的Pointer。根据id,我们可以很轻松的实现多点触控应用。

width/height:Mouse Event在屏幕上只能覆盖一个点的位置,但是一个Pointer可能覆盖一个更大的区域。

isPrimary:当有多个Pointer被检测到的时候(比如多点触摸),对每一种类型的Pointer会存在一个Primary Poiter。只有Primary Poiter会产生与之对应的Mouse Event。

Pointer Event API核心事件:

Mouse events, pointer events和touch events 对照表

Pointer API 的好处

Poiter API 整合了鼠标、触摸和触控笔的输入,使得我们无需对各种类型的事件区分对待。

目前不论是web还是本地应用都被设计成跨终端(手机,平板,PC)应用,鼠标多数应用在桌面应用,触摸则出现在各种设备上。过去开发人员必须针对不同的输入设备写不同的代码,或者写一个polyfill 来封装不同的逻辑。Pointer Events 改变了这种状况。

Pointer API实例

使用canvas画布来展示追踪一个pointer移动轨迹:

<canvas id="mycanvas" width="400px" height="500px" style="border: 1px solid #000"></canvas>
<script type="text/javascript">
var DrawFigure = (function(){
function DrawFigure(canvas,options) {
var _this = this;
this.canvas = canvas;
this._ctx = this.canvas.getContext('2d');
this.lastPt = null;
if(options === void 0) {
options = {};
}
this.options = options;
this._handleMouseDown = function(event) {
_this._mouseButtonDown = true;
}
this._handleMouseMove = function(event) {
if(_this._mouseButtonDown) {
var event = window.event || event;
if(_this.lastPt !== null) {
_this._ctx.beginPath();
_this._ctx.moveTo(_this.lastPt.x,_this.lastPt.y);
_this._ctx.lineTo(event.pageX,event.pageY);
_this._ctx.strokeStyle = _this.options.strokeStyle || 'green';
_this._ctx.lineWidth = _this.options.lineWidth || 3;
_this._ctx.stroke();
}
_this.lastPt = {
x: event.pageX,
y: event.pageY
}
}
}
this._handleMouseUp = function(event) {
_this._mouseButtonDown = false;
_this.canvas.removeEventListener('pointermove',_this.__handleMouseMove,false);
_this.canvas.removeEventListener('mousemove',_this.__handleMouseMove,false);
_this.lastPt = null;
console.log('移除事件');
} DrawFigure.prototype.init = function() {
this._mouseButtonDown = false;
if(window.PointerEvent) {
this.canvas.addEventListener('pointerdown',this._handleMouseDown,false);
this.canvas.addEventListener('pointermove',this._handleMouseMove,false);
this.canvas.addEventListener('pointerup',this._handleMouseUp,false);
} else {
this.canvas.addEventListener('mousedownn',this._handleMouseDown,false);
this.canvas.addEventListener('mousemove',this._handleMouseMove,false);
this.canvas.addEventListener('mouseup',this._handleMouseUp,false);
}
} }
return DrawFigure;
}());
window.onload = function() {
var canvas = document.getElementById('mycanvas');
var drawFigure = new DrawFigure(canvas);
drawFigure.init();
}
</script>

结果如图所示:

多点触控实例

首先初始一个多个颜色的数组,用来追踪不同的pointer。

var colours = ['red', 'green', 'blue', 'yellow','black'];

画线的时候通过pointer的id来取色。

 multitouchctx.strokeStyle = colours[id%5];
multitouchctx.lineWidth = 3;

在这个demo中,我们要追踪每一个pointer,所以需要分别保存每一个pointer的相关坐标点。这里我们使用关联数组来存储数据,每个数据使用pointerId做key,我们使用一个Object对象作为关联数组,用如下方法添加数据:

// This will be our associative array
var multiLastPt=new Object();
...
// Get the id of the pointer associated with the event
var id = e.pointerId;
...
// Store coords
multiLastPt[id] = {x:e.pageX, y:e.pageY};

完整代码:

<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<style>
html,body{
margin:0;
padding:0;
width: 100%;
height: 100%;
overflow: hidden;
}
</style>
</head>
<body ontouchstart="return false;">
<canvas id="mycanvas"></canvas>
<script type="text/javascript">
var DrawFigure = (function(){
function DrawFigure(canvas,options) {
var _this = this;
this.canvas = canvas;
this.canvas.width = document.documentElement.clientWidth;
this.canvas.height = document.documentElement.clientHeight;
this._ctx = this.canvas.getContext('2d');
this.lastPt = {};
if(options === void 0) {
options = {};
}
this.options = options;
this.colours = ['red', 'green', 'blue', 'yellow', 'black']; // 初始一个多个颜色的数组,用来追踪不同的pointer
this._handleMouseDown = function(event) {
_this._mouseButtonDown = true;
}
this._handleMouseMove = function(event) {
var id = event.pointerId;
if(_this._mouseButtonDown) {
var event = window.event || event;
if(_this.lastPt[id]) {
_this._ctx.beginPath();
_this._ctx.moveTo(_this.lastPt[id].x,_this.lastPt[id].y);
_this._ctx.lineTo(event.pageX,event.pageY);
_this._ctx.strokeStyle = _this.colours[id%5]; // 画线的时候通过pointer的id来取色
_this._ctx.lineWidth = _this.options.lineWidth || 3;
_this._ctx.stroke();
}
_this.lastPt[id] = {
x: event.pageX,
y: event.pageY
}
}
}
this._handleMouseUp = function(event) {
var id = event.pointerId;
_this._mouseButtonDown = false;
_this.canvas.removeEventListener('pointermove',_this.__handleMouseMove,false);
_this.canvas.removeEventListener('mousemove',_this.__handleMouseMove,false);
delete _this.lastPt[id];
} DrawFigure.prototype.init = function() {
this._mouseButtonDown = false;
if(window.PointerEvent) {
this.canvas.addEventListener('pointerdown',this._handleMouseDown,false);
this.canvas.addEventListener('pointermove',this._handleMouseMove,false);
this.canvas.addEventListener('pointerup',this._handleMouseUp,false);
} else {
this.canvas.addEventListener('mousedownn',this._handleMouseDown,false);
this.canvas.addEventListener('mousemove',this._handleMouseMove,false);
this.canvas.addEventListener('mouseup',this._handleMouseUp,false);
}
} }
return DrawFigure;
}());
window.onload = function() {
var canvas = document.getElementById('mycanvas');
var drawFigure = new DrawFigure(canvas);
drawFigure.init();
}
</script>
</body>
</html>

手机效果如图所示:

参考地址:

Pointer Event Api-整合鼠标事件、触摸和触控笔事件的更多相关文章

  1. MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件

    原文  MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件 UI 前沿技术 WPF 中的多点触控操作事件 Charles Petzold 下载代码示例 就在过去几年,多点触控还只是科幻电 ...

  2. 浏览器的统一指针事件:Pointer Event

    在早期的浏览器,输入的事件其实相对单纯,只有考虑到鼠标和键盘两种:而当时的鼠标事件,其实就是 click.mousedown.mouseup 等等的事件.但是当手机.平板开始流行时候,再移动装置上的主 ...

  3. jquery mobile 对手势触控提供了如下几个事件监听:

    jquery mobile 对手势触控提供了如下几个事件监听: 复制代码代码如下: tap  当用户点屏幕时触发taphold 当用户点屏幕且保持触摸超过1秒时触发swipe 当页面被垂直或者水平拖动 ...

  4. windows phone 8.1开发:触控和指针事件1

    原文出自:http://www.bcmeng.com/windows-phone-touch/ UIElement类的触控事件: ManipulationStarting:当用户将手指放在 IsMan ...

  5. Pointer Lock API(2/3):属性、方法、事件

    Pointer Lock API 提供了三个属性.两个方法.两个事件 Tabel Of Content 属性 Document.pointerLockElement Document.onpointe ...

  6. iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

    -- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事 ...

  7. 转发:iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

    -- iOS事件全面解析 转载来自崔江涛(KenshinCui) 链接:http://www.cnblogs.com/kenshincui/p/3950646.html 概览 iPhone的成功很大一 ...

  8. Cocos Creator学习五:触摸和重力传感响应事件

    1.移动设备上主要涉及触摸响应事件以及重力传感响应事件的处理. 事件主要分两类: 针对节点事件处理的节点响应事件cc.Node.EventType(主要是触摸响应事件和鼠标响应事件): 针对全局系统事 ...

  9. cocos2d-x 源代码分析 : EventDispatcher、EventListener、Event 源代码分析 (新触摸机制,新的NotificationCenter机制)

    源代码版本号来自3.x,转载请注明 cocos2d-x 源代码分析总文件夹 http://blog.csdn.net/u011225840/article/details/31743129 1.继承结 ...

  10. Pointer Lock API(1/3):Pointer Lock 的总体认识

    前言 指针锁定(Pointer Lock),以前也叫鼠标锁定,提供了基于鼠标随时间的移动(如deltaΔ)的输入方法,不仅仅是视窗区域鼠标的绝对位置.指针锁定让你能够访问原始的鼠标移动,将鼠标事件的目 ...

随机推荐

  1. springboot实现异步调用demo

    springboot实现异步调用 异步调用特点 异步调用在开发程序中被广泛应用,在异步任务中,主线程不需要阻塞等待异步任务的完成,而是可以继续处理其他请求. 异步调用的特点如下: 非阻塞:主线程在调用 ...

  2. oeasy教您玩转vim - 58 - # 块可视化

    ​ 块可视化编辑 回忆上节课内容 上次我们了解到行可视模式 行可视模式 V 也可配合各种motion o切换首尾 选区的开头和结尾是mark标记 开头是 '< 结尾是 '> 可以在选区内进 ...

  3. Github关于PAT(Personal Access Token)

    Github关于PAT(Personal Access Token) 创建个人访问令牌 您应该通过命令行或 API 创建个人访问令牌来代替密码. 注意: 如果您在命令行上使用 GitHub CLI 向 ...

  4. Python 使用Python操作xmind文件

    使用Python操作xmind文件 by:授客 QQ:1033553122   测试环境 Win10 Python 3.5.4 XMind-1.2.0.tar.gz 下载地址: https://fil ...

  5. Python编写html文件

    背景:部门需要发送周报.月报,每次都需要去数据库导出数据整理统计发送给领导,人工操作显得繁琐且费时间. 1.可以定时用python将数据库查询数据结果写成html文件,达到浏览器访问的效果,定时发送给 ...

  6. 【WSDL】03 使用注解自定义服务信息

    对原来的自定义WebService设置注解: package cn.cloud9.jax_ws.server.intf; import javax.jws.WebMethod; import java ...

  7. 【Canal】01 入门 & Kafka模式

    什么是Canal (卡耐尔) ? Canal 是用 Java 开发的基于数据库增量日志解析,提供增量数据订阅&消费的中间件 原理基于MySQL的binlog从库监听       一.MySQL ...

  8. 【Oracle】Windiws-11G 安装

    教程参考: https://jingyan.baidu.com/article/363872eccfb9266e4aa16f5d.html 安装包文件目录: 注意,使用[管理员运行此文件] 然后稍等许 ...

  9. Git的分支管理和标签操作

    分支操作 分支是Git使用过程中非常重要的概念.使用分支意味着你可以把你的工作从开发主线上分离出来,以免影响开发主线. 同意一个仓库可以有多个分支,各个分支相互独立,互不干扰.通过git init 命 ...

  10. ( Ubuntu系统下 ) vim插件安装 supertab 实现tab键的补全功能 (vim+python环境下)

    本文前提已经安装了  Vundle   : 下载  supertab : git clone https://github.com/ervandew/supertab ~/.vim/bundle/ 编 ...