npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@any-touch/doubletap

v2.2.0

Published

any-touch的识别器, 用来识别双击手势, 是Tap识别器的一个特例

Downloads

563

Readme

@any-touch/doubletap

双击识别器. 基于@any-touch/tap实现, 是tap的一个特例.

安装

npm i -S @any-touch/doubletap

快速开始

// 只加载tap识别器(拖拽)
import Core from '@any-touch/core';
import doubletap from '@any-touch/doubletap';
const at = new Core(el);
at.use(doubletap)

// 双击
at.on('doubletap', (event) => {
    console.log(event) // 包含点击位置信息
});

源码解析

因为是基于@any-touch/tap实现的, 所以代码不多, 只是使用beforeEach拦截器.

export default function (at: Core) {
    at.use(tap, { name: 'doubletap', tapTimes: 2 });
    const doubleTapContext = at.get('doubletap')
    let timeID: number;
    at.beforeEach((type, next) => {
        if ('tap' === type) {
            clearTimeout(timeID);
            timeID = setTimeout(() => {
                if ([STATE.POSSIBLE, STATE.FAILED].includes(doubleTapContext.state)) {
                    next();
                }
            }, 300);
        } else {
            next();
        }
    });
    return doubleTapContext;
}

这里只需要多理解一个概念就是STATE(识别器的状态). |名称|解释| |---|---| |POSSIBLE|待识别| |RECOGNIZED|已识别| |FAILED|识别失败|

识别流程如下, 每次点击屏幕, STATE是"POSSIBLE状态, 识别成功后状态变成"RECOGNIZED", 识别失败变成"FAILED".

特别解释下"FAILED"的发生情况,用doubletap识别器举例, 当2次点击间隔时间过长, doubletap的状态就会变成"FAILED", 当第三次点击的时候状态又变回"POSSIBLE".

完整源码

N击

如果看懂了双击的逻辑, 那么稍作修改就可以实现"3击".

export default function (at: Core) {
    // ⭐只有这一行的tapTimes改成了3
    at.use(tap, { name: 'doubletap', tapTimes: 3 });

    // 未改动
    const doubleTapContext = at.get('doubletap')
    let timeID: number;
    at.beforeEach((type, next) => {
        if ('tap' === type) {
            clearTimeout(timeID);
            timeID = setTimeout(() => {
                if ([STATE.POSSIBLE, STATE.FAILED].includes(doubleTapContext.state)) {
                    next();
                }
            }, 300);
        } else {
            next();
        }
    });
    return doubleTapContext;
}