Pointer Event Api-整合滑鼠事件、觸摸和觸控筆事件

来源:https://www.cnblogs.com/moqiutao/archive/2020/07/05/13237093.html
-Advertisement-
Play Games

Pointer Events API 是Hmtl5的事件規範之一,它主要目的是用來將滑鼠(Mouse)、觸摸(touch)和觸控筆(pen)三種事件整合為統一的API。 Pointer Event Pointer指可以在屏幕上反饋一個指定坐標的輸入設備。Pointer Event事件和Touch E ...


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>

 手機效果如圖所示:

參考地址:


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一.Shell介紹:shell是一個命令行解釋器,腳本語言,活動在應用程式與內核之間,用於接收應用程式(用戶)命令,調用操作系統內核。 (1)編譯型語言: 程式在執行之前需要個專門的編譯過程,把程式編譯成為機器語言文件,運行時不需要重新翻譯,直接使用編譯的結果就行了。程式執行效率高,依養編譯器,跨平 ...
  • mysql分組計數,然後進行範圍彙總. 兩種方式 第一種:常規操作 SELECT SUM(ddd) AS count_days, CASE WHEN aa.days >= 1 AND aa.days < 3 THEN '1-3' WHEN aa.days >= 3 AND aa.days < 5 T ...
  • SQL語言大致分為`DCL`、`DDL`、`DML`三種,本文主要介紹`MySQL 5.7`版本的`DCL`語句。 ...
  • #創建資料庫 CREATE DATABASE database_name; create database mysql_test; #刪除資料庫 DROP DATABASE database_name; > drop database mysql_test; #數據類型 完整數據類型請參考MySQL ...
  • MySQL 索引結構 hash 有序數組 除了最常見的樹形索引結構,Hash索引也有它的獨到之處。 Hash演算法 Hash本身是一種函數,又被稱為散列函數。 它的思路很簡單:將key放在數組裡,用一個hash演算法把不同的key轉換成一個確定的value,然後放在這個數組的指定位置 相同的輸入永遠可以 ...
  • 半同步指的是在主節點發生寫操作事件後,它會把該操作的事件發送給從節點,當從節點接收到主節點發送過來的事件後,就立刻告訴主節點,從節點已經接收到主節點發送過來的事件,此時主機點並不會等到從節點重放完成,而是接收到從節點接收到主節點發送過去的的事件確認消息後,就返回給客戶端;而在mariadb/mys... ...
  • MySQL 樹形索引結構 B樹 B+樹 如何評估適合索引的數據結構 索引的本質是一種數據結構 記憶體只是臨時存儲,容量有限且容易丟失數據。因此我們需要將數據放在硬碟上。 在硬碟上進行查詢時也就產生了硬碟的I/O操作,而硬碟的I/O存取消耗的時間要比讀取記憶體大很多。因此數據查詢的時間主要決定於I/O操作 ...
  • 一、數組中其餘的常用方法 包括map,filter,every,some方法,我們分別進行舉例 //map定義一個函數用來遍歷原來老的數組 var arr = [10,20,5,1000,50]; var newArr = arr.map(function(value, index, array){ ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...