Jump to content

Preventing on('pointertap') if scrolling


Recommended Posts

When natively scrolling the div which the pixi resides in on a touch device, on('pointertap') on an interactive Sprite will fire even if the starting "tap-down" location is far away from the ending "tap-up".

The desired effect is that nothing happen with the button event (the user is intending to scroll, not tap on the button).

How do I prevent the tap to happen, is there a way to detect distance (and/or time) between the press down location and press up without rewriting the entire pointertap event from scratch?

Link to comment
Share on other sites

Couldn't find any built-in way to do this, so I used a manual approach:

const threshold = 40;
const ctx = this;

const onButtonTapUp = function(_e) {
	// We don't want mouse events from right-click, middle-click, and back/forward buttons on mouse
	if (_e.data.pointerType === 'mouse' && _e.data.button !== 0) return;
	// ...we just want touch-taps and left mouse clicks

	const oldX = _e.target.startTapX;
	const oldY = _e.target.startTapY;
	const newX = _e.data.global.x;
	const newY = _e.data.global.y-ctx.myDiv.scrollTop;

	if (newX-threshold <= oldX && newX+threshold >= oldX && newY-threshold <= oldY && newY+threshold >= oldY) {
		// Do action

const onButtonTapDown = function(_e) {
	if (_e.data.pointerType === 'mouse' && _e.data.button !== 0) return;
	_e.target.startTapX = _e.data.global.x;
	_e.target.startTapY = _e.data.global.y-ctx.myDiv.scrollTop;

interactiveSprite.on('pointertap', onButtonTapUp);
interactiveSprite.on('pointerdown', onButtonTapDown);


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...