Pointer Event Api-整合鼠标事件、触摸和触控笔事件

前端之家收集整理的这篇文章主要介绍了Pointer Event Api-整合鼠标事件、触摸和触控笔事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

Pointer Event 

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

@H_404_7@

说明:

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

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

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

Pointer Event API核心事件:

@H_404_7@

Mouse events,pointer events和touch events 对照表

@H_404_7@

Pointer API 的好处

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


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

Pointer API实例

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

  1. <canvas id="mycanvas" width="400px" height="500px" style="border: 1px solid #000"></canvas>
  2. script type="text/javascript">
  3. var DrawFigure = (function(){
  4. DrawFigure(canvas,options) {
  5. _this = this;
  6. .canvas canvas;
  7. ._ctx .canvas.getContext('2d);
  8. .lastPt nullif(options === void 0) {
  9. options {};
  10. }
  11. .options options;
  12. ._handleMouseDown (event) {
  13. _this._mouseButtonDown true;
  14. }
  15. ._handleMouseMove (event) {
  16. (_this._mouseButtonDown) {
  17. event window.event || event;
  18. (_this.lastPt !== ) {
  19. _this._ctx.beginPath();
  20. _this._ctx.moveTo(_this.lastPt.x,_this.lastPt.y);
  21. _this._ctx.lineTo(event.pageX,event.pageY);
  22. _this._ctx.strokeStyle _this.options.strokeStyle || green;
  23. _this._ctx.lineWidth _this.options.lineWidth 3;
  24. _this._ctx.stroke();
  25. }
  26. _this.lastPt {
  27. x: event.pageX,y: event.pageY
  28. }
  29. }
  30. }
  31. ._handleMouseUp false;
  32. _this.canvas.removeEventListener(pointermove,_this.__handleMouseMove,);
  33. _this.canvas.removeEventListener(mousemove);
  34. _this.lastPt ;
  35. console.log(移除事件);
  36. }
  37. DrawFigure.prototype.init () {
  38. ._mouseButtonDown ;
  39. (window.PointerEvent) {
  40. .canvas.addEventListener(pointerdown._handleMouseDown,1)">);
  41. ._handleMouseMove,1)">pointerup._handleMouseUp,1)">);
  42. } else {
  43. mousedownnmouseup);
  44. }
  45. }
  46. }
  47. return DrawFigure;
  48. }());
  49. window.onload () {
  50. canvas document.getElementById(mycanvas);
  51. drawFigure new DrawFigure(canvas);
  52. drawFigure.init();
  53. }
  54. </script>

 结果如图所示:

@H_404_7@

 多点触控实例

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

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

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

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

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

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

 完整代码

  1. <!DOCTYPE htmlhtmlheadtitle>testMeta charset="utf-8"name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
  2. style>
  3. html,body{
  4. margin:;
  5. padding
  6. width 100%
  7. height
  8. overflow hidden;
  9. }
  10. body ontouchstart="return false;"="mycanvas".canvas.width document.documentElement.clientWidth;
  11. .canvas.height document.documentElement.clientHeight;
  12. {};
  13. .colours [redblueyellowblack]; // 初始一个多个颜色的数组,用来追踪不同的pointer
  14. id event.pointerId;
  15. (_this.lastPt[id]) {
  16. _this._ctx.beginPath();
  17. _this._ctx.moveTo(_this.lastPt[id].x,_this.lastPt[id].y);
  18. _this._ctx.lineTo(event.pageX,1)"> _this.colours[id%5 画线的时候通过pointer的id来取色
  19. _this._ctx.lineWidth ;
  20. _this._ctx.stroke();
  21. }
  22. _this.lastPt[id] event.pointerId;
  23. _this._mouseButtonDown );
  24. delete _this.lastPt[id];
  25. }
  26. DrawFigure.prototype.init body>

 手机效果如图所示:

@H_404_7@

参考地址:

猜你在找的HTML5相关文章