search
HomeWeb Front-endJS TutorialHow to use Point event

This time I will show you how to use Point events, and what are the precautions for using Point events. The following is a practical case, let's take a look.

Preface

This article has been lying in the draft box for a long time. Because I encountered related problems recently, I sorted it out again. Please note that this is not about css pointer-events. I won’t say everything below, let’s take a look at the detailed introduction.

Cause

Starting from a dark and stormy night, someone discovered that our web-app began to appear in the Chrome simulator. When an error is reported, the error message is probably as follows.

VM1023:1 Uncaught TypeError: Cannot read property '0' of undefined

But only his browser has a problem, and it has no impact on the function. In the spirit of The spirit did not reappear on my machine (well, I was quite busy at the time). The

priority of this problem was not high, but after a while, someone gradually had the same problem, so I I started to pay attention to this issue.

Positioning problem

The code was quickly located based on the call stack, and the source code was located to the component code written by a previous colleague, which is probably like this of:

dom.on('touchstart pointerdown', function (event) {
 /*部分业务代码*/
 var touch = event.touches[0]; //报错的地方
 /*部分业务代码*/
})
debug found that the pointdown event was triggered because the event did not touch the field, causing

to throw an exception. But it worked fine before. Could it be that the browser API has changed? And I haven’t learned about the pointerdown event. What is this event used to handle? So I started my search journey with two questions:

  • What is the pointerdown event

  • Why did errors suddenly start to occur

Let’s talk about pointer events

To check the problem, the simplest question is to read through the W3C official documents. Here is a brief explanation of my understanding.

Diversification of device input forms

In the PC era, we interact with the screen through the mouse. At this time, we only need to consider mouse events when designing the system. . But nowadays, there are many new devices, such as smartphones and tablets, which include other input methods, such as touch and stylus. Officials also provide new events for these input forms.

But for developers, this is a very troublesome thing, because it means that you need to adapt various events to your web page. For example, if you want to draw pictures based on the user's movement, you need to be compatible For PC and mobile phone, your code may be as follows

dom.addEventListener('mousemove',
 draw);
dom.addEventListener('touchmove',
 draw);
What if you need to be compatible with more input devices? For example, a stylus, in which case the code will be very complicated. Moreover, in order to be compatible with existing mouse event-based code, many browsers will trigger mouse events for all input types (for example, mousemove is triggered when touchmove is triggered. I tested it in Chrome and it did not trigger, but because there is no device, the stylus The situation has not been tested), which will also lead to the inability to confirm whether the event is actually triggered by the mouse.

How to be compatible with multiple input forms

In order to solve this series of problems, W3C defines a new input form, namely pointer. Any contact on the screen triggered by a mouse, touch, stylus, or other input device counts as a pointer event.

Its API is very similar to mouse events and is very easy to migrate. In addition to providing commonly used properties for mouse events, such as clientX, target, etc., it also provides some properties for other input devices, such as pressure, contact surface, tilt angle, etc., so that developers can use pointer events to provide all input The device develops its own functions!

Provided properties

The pointer event provides some unique event properties

  • pointerId: the unique identifier of the current pointer event , mainly to identify the only input source during multi-touch

  • width: the width of the contact surface

  • height: the contact surface high

  • pressure:接触的压力值,范围是0-1,对于不支持压力的硬件,比如鼠标,按压时该值必须为 0.5,否则为 0

  • tiltX,titltY:手写笔的角度

  • pointerType:事件类型,目前有 mouse,pen,touch,如果是无法探测的指针类型,则该值为空字符串

  • isPrimary:用于标识是否是主指针,主要是在多点触控中生效,开发者也可以通过忽略非主指针的指针事件来实现单点触控。

如何确定主指针:

鼠标输入:一定是主指针

触摸输入:如果 pointerdown 触发时没有其他激活的触摸事件,isPrimary 为 true

手写笔输入:与触摸事件类似,pointerdown 触发时没有其他激活的 pointer 事件

相关事件

事件名称 作用
pointerover 与 mouseover 行为一致
pointerenter 与 mouseenter 行为一致
pointerdown 指针进入活动状态,比如触摸了屏幕,类似于 touchstart
pointermove 指针进行了移动
pointerup 指针取消活动状态,比如手指离开了屏幕,类似于 touchend
pointercancel 类似于 touchcancel
pointerout 指针离开元素边缘或者离开屏幕,类似于 mouseout
pointerleave 类似于 mouseleave
gotpointercapture 元素捕获到指针事件时触发
lostpointercapture 指针被释放时触发

可以看到,pointer 事件与已知的事件类型基本一致,但是有一点区别:在触摸屏上,我们可能会滑动屏幕来触发页面滚动,缩放或者刷新,对于 touch 事件,这时会触发 touchmove,但是对于 pointer 事件,当触发这些浏览器行为时,你却会接收到 pointercancel 事件以便于通知你浏览器已经接管了你的指针事件。

如何检测

首先,pointer 事件的支持程度已经很不错了,你可以使用 Pointer Events polyfill (本地下载)来进行兼容,也可以自行检测

if (window.PointerEvent) {
 // 支持
} else {
 // 不支持
}

导致问题的原因

这时候,对于本文一开始提到的问题就显而易见了,因为 point events 是没有 touches 这个属性的。那么我们还有两个问题。

为什么之前会用到 point events?

后来我看了下 zepto 的源码,在事件处理时是考虑到了 point event 的,同事之前写的代码大概是参考了 zepto 的事件系统。

为什么会突然爆发这个问题?

很简答,Chrome 55 开始支持这个 API,Chrome 具体的支持信息可以参考官方日志,至于怎么检测浏览器支持,可以参考上面的内容

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

怎样使用Vue中字符串模板

如何处理Mac安装thrift因bison报错

The above is the detailed content of How to use Point event. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
事件 ID 4660:已删除对象 [修复]事件 ID 4660:已删除对象 [修复]Jul 03, 2023 am 08:13 AM

我们的一些读者遇到了事件ID4660。他们通常不确定该怎么做,所以我们在本指南中解释。删除对象时通常会记录事件ID4660,因此我们还将探索一些实用的方法在您的计算机上修复它。什么是事件ID4660?事件ID4660与活动目录中的对象相关,将由以下任一因素触发:对象删除–每当从ActiveDirectory中删除对象时,都会记录事件ID为4660的安全事件。手动更改–当用户或管理员手动更改对象的权限时,可能会生成事件ID4660。更改权限设置、修改访问级别或添加或删除人员或组时,可能会发生这种情

在iPhone锁屏上获取即将到来的日历事件在iPhone锁屏上获取即将到来的日历事件Dec 01, 2023 pm 02:21 PM

在运行iOS16或更高版本的iPhone上,您可以直接在锁定屏幕上显示即将到来的日历事件。继续阅读以了解它是如何完成的。由于表盘复杂功能,许多AppleWatch用户习惯于能够看一眼手腕来查看下一个即将到来的日历事件。随着iOS16和锁定屏幕小部件的出现,您可以直接在iPhone上查看相同的日历事件信息,甚至无需解锁设备。日历锁定屏幕小组件有两种风格,允许您跟踪下一个即将发生的事件的时间,或使用更大的小组件来显示事件名称及其时间。若要开始添加小组件,请使用面容ID或触控ID解锁iPhone,长按

在JavaScript中,"oninput"事件的用途是什么?在JavaScript中,"oninput"事件的用途是什么?Aug 26, 2023 pm 03:17 PM

当在输入框中添加值时,就会发生oninput事件。您可以尝试运行以下代码来了解如何在JavaScript中实现oninput事件-示例<!DOCTYPEhtml><html>&nbsp;&nbsp;<body>&nbsp;&nbsp;&nbsp;<p>Writebelow:</p>&nbsp;&nbsp;&nbsp;<inputtype="text"

如何在PHP项目中实现日历功能和事件提醒?如何在PHP项目中实现日历功能和事件提醒?Nov 02, 2023 pm 12:48 PM

如何在PHP项目中实现日历功能和事件提醒?在开发Web应用程序时,日历功能和事件提醒是常见的需求之一。无论是个人日程管理、团队协作,还是在线活动安排,日历功能都可以提供便捷的时间管理和事务安排。在PHP项目中实现日历功能和事件提醒可以通过以下步骤来完成。数据库设计首先,需要设计数据库表来存储日历事件的相关信息。一个简单的设计可以包含以下字段:id:事件的唯一

jQuery中如何实现select元素的改变事件绑定jQuery中如何实现select元素的改变事件绑定Feb 23, 2024 pm 01:12 PM

jQuery是一个流行的JavaScript库,可以用来简化DOM操作、事件处理、动画效果等。在web开发中,经常会遇到需要对select元素进行改变事件绑定的情况。本文将介绍如何使用jQuery实现对select元素改变事件的绑定,并提供具体的代码示例。首先,我们需要使用标签来创建一个包含选项的下拉菜单:

jquery中常用的事件有哪些jquery中常用的事件有哪些Jan 03, 2023 pm 06:13 PM

jquery中常用的事件有:1、window事件;2、鼠标事件,是当用户在文档上面移动或单击鼠标时而产生的事件,包括鼠标单击、移入事件、移出事件等;3、键盘事件,是用户每次按下或者释放键盘上的按键时都会产生事件,包括按下按键事件、释放按键按键等;4、表单事件,例如当元素获得焦点时会触发focus()事件,失去焦点时会触发blur()事件,表单提交时会触发submit()事件。

Vue文档中的input框绑定事件详解Vue文档中的input框绑定事件详解Jun 21, 2023 am 08:12 AM

Vue.js是一种轻量级的JavaScript框架,具有易用、高效和灵活的特点,是目前广受欢迎的前端框架之一。在Vue.js中,input框绑定事件是一个十分常见的需求,本文将详细介绍Vue文档中的input框绑定事件。一、基础概念在Vue.js中,input框绑定事件指的是将输入框的值绑定到Vue实例的数据对象中,从而实现输入和响应的双向绑定。在Vue.j

深入研究jQuery中的关闭按钮事件深入研究jQuery中的关闭按钮事件Feb 24, 2024 pm 05:09 PM

深入理解jQuery中的关闭按钮事件在前端开发过程中,经常会遇到需要实现关闭按钮功能的情况,比如关闭弹窗、关闭提示框等。而在使用jQuery这个流行的JavaScript库时,实现关闭按钮事件也变得异常简单和方便。本文将深入探讨如何利用jQuery来实现关闭按钮事件,并提供具体的代码示例,帮助读者更好地理解和掌握这个技术。首先,我们需要了解在HTML中如何定

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool