@ajiu9/gesture
Advanced tools
Comparing version 1.0.6-beta.0 to 1.1.0
@@ -1,97 +0,99 @@ | ||
'use strict'; | ||
'use strict' | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
Object.defineProperty(exports, '__esModule', { value: true }) | ||
var __defProp = Object.defineProperty; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __publicField = (obj, key, value) => { | ||
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); | ||
return value; | ||
}; | ||
const __defProp = Object.defineProperty | ||
const __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value | ||
const __publicField = (obj, key, value) => { | ||
__defNormalProp(obj, typeof key !== 'symbol' ? `${key}` : key, value) | ||
return value | ||
} | ||
class Listener { | ||
constructor(element, recognizer) { | ||
this.element = element; | ||
this.recognizer = recognizer; | ||
__publicField(this, "isListeningMouse", false); | ||
__publicField(this, "contexts", /* @__PURE__ */ new Map()); | ||
this.setupMouseListeners(); | ||
this.setupTouchListeners(); | ||
this.element = element | ||
this.recognizer = recognizer | ||
__publicField(this, 'isListeningMouse', false) | ||
__publicField(this, 'contexts', /* @__PURE__ */ new Map()) | ||
this.setupMouseListeners() | ||
this.setupTouchListeners() | ||
} | ||
setupMouseListeners() { | ||
const el = document.documentElement; | ||
this.element.addEventListener("mousedown", (event) => { | ||
const context = /* @__PURE__ */ Object.create(null); | ||
this.contexts.set(`mouse${1 << event.button}`, context); | ||
this.recognizer.start(event, context); | ||
const el = document.documentElement | ||
this.element.addEventListener('mousedown', (event) => { | ||
const context = /* @__PURE__ */ Object.create(null) | ||
this.contexts.set(`mouse${1 << event.button}`, context) | ||
this.recognizer.start(event, context) | ||
const mouseMove = (event2) => { | ||
let button = 1; | ||
let button = 1 | ||
while (button <= event2.buttons) { | ||
if (button & event2.buttons) { | ||
let key; | ||
let key | ||
if (button === 2) | ||
key = 4; | ||
key = 4 | ||
else if (button === 4) | ||
key = 2; | ||
key = 2 | ||
else | ||
key = button; | ||
const context2 = this.contexts.get(`mouse${key}`); | ||
key = button | ||
const context2 = this.contexts.get(`mouse${key}`) | ||
if (context2) | ||
this.recognizer.move(event2, context2); | ||
this.recognizer.move(event2, context2) | ||
} | ||
button = button << 1; | ||
button = button << 1 | ||
} | ||
}; | ||
} | ||
const mouseup = (event2) => { | ||
const context2 = this.contexts.get(`mouse${1 << event2.button}`); | ||
const context2 = this.contexts.get(`mouse${1 << event2.button}`) | ||
if (context2) | ||
this.recognizer.end(event2, context2); | ||
this.contexts.delete(`mouse${1 << event2.button}`); | ||
this.recognizer.end(event2, context2) | ||
this.contexts.delete(`mouse${1 << event2.button}`) | ||
if (event2.buttons === 0) { | ||
el.removeEventListener("mousemove", mouseMove); | ||
el.removeEventListener("mouseup", mouseup); | ||
this.isListeningMouse = false; | ||
el.removeEventListener('mousemove', mouseMove) | ||
el.removeEventListener('mouseup', mouseup) | ||
this.isListeningMouse = false | ||
} | ||
}; | ||
} | ||
if (!this.isListeningMouse) { | ||
el.addEventListener("mousemove", mouseMove); | ||
el.addEventListener("mouseup", mouseup); | ||
this.isListeningMouse = true; | ||
el.addEventListener('mousemove', mouseMove) | ||
el.addEventListener('mouseup', mouseup) | ||
this.isListeningMouse = true | ||
} | ||
}); | ||
}) | ||
} | ||
setupTouchListeners() { | ||
this.element.addEventListener("touchstart", (event) => { | ||
this.element.addEventListener('touchstart', (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = /* @__PURE__ */ Object.create(null); | ||
this.contexts.set(touch.identifier, context); | ||
this.recognizer.start(touch, context); | ||
const context = /* @__PURE__ */ Object.create(null) | ||
this.contexts.set(touch.identifier, context) | ||
this.recognizer.start(touch, context) | ||
} | ||
}); | ||
this.element.addEventListener("touchmove", (event) => { | ||
}) | ||
this.element.addEventListener('touchmove', (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = this.contexts.get(touch.identifier); | ||
const context = this.contexts.get(touch.identifier) | ||
if (context) { | ||
event.preventDefault(); | ||
this.recognizer.move(touch, context); | ||
event.preventDefault() | ||
this.recognizer.move(touch, context) | ||
} | ||
} | ||
}); | ||
this.element.addEventListener("touchend", (event) => { | ||
}) | ||
this.element.addEventListener('touchend', (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = this.contexts.get(touch.identifier); | ||
const context = this.contexts.get(touch.identifier) | ||
if (context) { | ||
this.recognizer.end(touch, context); | ||
this.contexts.delete(touch.identifier); | ||
this.recognizer.end(touch, context) | ||
this.contexts.delete(touch.identifier) | ||
} | ||
} | ||
}); | ||
this.element.addEventListener("touchcancel", (event) => { | ||
}) | ||
this.element.addEventListener('touchcancel', (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = this.contexts.get(touch.identifier); | ||
const context = this.contexts.get(touch.identifier) | ||
if (context) { | ||
this.recognizer.cancel(touch, context); | ||
this.contexts.delete(touch.identifier); | ||
this.recognizer.cancel(touch, context) | ||
this.contexts.delete(touch.identifier) | ||
} | ||
} | ||
}); | ||
}) | ||
} | ||
@@ -101,38 +103,40 @@ } | ||
constructor(dispatcher) { | ||
this.dispatcher = dispatcher; | ||
this.dispatcher = dispatcher | ||
} | ||
start(point, context) { | ||
context.startX = point.clientX; | ||
context.startY = point.clientY; | ||
this.dispatcher.dispatch("start", { | ||
context.startX = point.clientX | ||
context.startY = point.clientY | ||
this.dispatcher.dispatch('start', { | ||
clientX: point.clientX, | ||
clientY: point.clientY | ||
}); | ||
clientY: point.clientY, | ||
}) | ||
context.points = [{ | ||
t: Date.now(), | ||
x: point.clientX, | ||
y: point.clientY | ||
}]; | ||
context.isTap = true; | ||
context.isPan = false; | ||
context.isPress = false; | ||
y: point.clientY, | ||
}] | ||
context.isTap = true | ||
context.isPan = false | ||
context.isPress = false | ||
context.handler = setTimeout(() => { | ||
context.isTap = false; | ||
context.isPan = false; | ||
context.isPress = true; | ||
context.handler = null; | ||
this.dispatcher.dispatch("press", {}); | ||
}, 500); | ||
context.isTap = false | ||
context.isPan = false | ||
context.isPress = true | ||
context.handler = null | ||
this.dispatcher.dispatch('press', {}) | ||
}, 500) | ||
} | ||
move(point, context) { | ||
var _a; | ||
const dx = point.clientX - context.startX; | ||
const dy = point.clientY - context.startY; | ||
let _a | ||
const dx = point.clientX - context.startX | ||
const dy = point.clientY - context.startY | ||
if (!context.isPan && dx ** 2 + dy ** 2 > 100) { | ||
context.isPan = true; | ||
context.isTap = false; | ||
context.isPress = false; | ||
context.isVertical = Math.abs(dx) - Math.abs(dy) >= 0; | ||
clearTimeout((_a = context.handler) != null ? _a : void 0); | ||
this.dispatcher.dispatch("panStart", { | ||
context.isPan = true | ||
context.isTap = false | ||
context.isPress = false | ||
context.isVertical = Math.abs(dx) - Math.abs(dy) >= 0 | ||
clearTimeout((_a = context.handler) != null ? _a : void 0) | ||
this.dispatcher.dispatch('panStart', { | ||
startX: context.startX, | ||
@@ -142,7 +146,7 @@ startY: context.startY, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical | ||
}); | ||
isVertical: context.isVertical, | ||
}) | ||
} | ||
if (context.isPan) { | ||
this.dispatcher.dispatch("pan", { | ||
this.dispatcher.dispatch('pan', { | ||
startX: context.startX, | ||
@@ -152,34 +156,35 @@ startY: context.startY, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical | ||
}); | ||
isVertical: context.isVertical, | ||
}) | ||
} | ||
context.points = context.points.filter((point2) => Date.now() - point2.t < 500); | ||
context.points = context.points.filter(point2 => Date.now() - point2.t < 500) | ||
context.points.push({ | ||
t: Date.now(), | ||
x: point.clientX, | ||
y: point.clientY | ||
}); | ||
y: point.clientY, | ||
}) | ||
} | ||
end(point, context) { | ||
var _a; | ||
let _a | ||
if (context.isTap) { | ||
this.dispatcher.dispatch("tap", { | ||
this.dispatcher.dispatch('tap', { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY | ||
}); | ||
clearTimeout((_a = context.handler) != null ? _a : void 0); | ||
clientY: point.clientY, | ||
}) | ||
clearTimeout((_a = context.handler) != null ? _a : void 0) | ||
} | ||
if (context.isPress) | ||
this.dispatcher.dispatch("pressEnd", {}); | ||
context.points = context.points.filter((point2) => Date.now() - point2.t < 500); | ||
let v = 0; | ||
this.dispatcher.dispatch('pressEnd', {}) | ||
context.points = context.points.filter(point2 => Date.now() - point2.t < 500) | ||
let v = 0 | ||
if (context.points.length) { | ||
const d = Math.sqrt((point.clientX - context.points[0].x) ** 2 + (point.clientY - context.points[0].y) ** 2); | ||
v = d / (Date.now() - context.points[0].t); | ||
const d = Math.sqrt((point.clientX - context.points[0].x) ** 2 + (point.clientY - context.points[0].y) ** 2) | ||
v = d / (Date.now() - context.points[0].t) | ||
} | ||
if (v > 1.5) { | ||
context.isFlick = true; | ||
this.dispatcher.dispatch("flick", { | ||
context.isFlick = true | ||
this.dispatcher.dispatch('flick', { | ||
startX: context.startX, | ||
@@ -191,9 +196,10 @@ startY: context.startY, | ||
isFlick: context.isFlick, | ||
velocity: v | ||
}); | ||
} else { | ||
context.isFlick = false; | ||
velocity: v, | ||
}) | ||
} | ||
else | ||
context.isFlick = false | ||
if (context.isPan) { | ||
this.dispatcher.dispatch("panEnd", { | ||
this.dispatcher.dispatch('panEnd', { | ||
startX: context.startX, | ||
@@ -205,6 +211,6 @@ startY: context.startY, | ||
isFlick: context.isFlick, | ||
velocity: v | ||
}); | ||
velocity: v, | ||
}) | ||
} | ||
this.dispatcher.dispatch("end", { | ||
this.dispatcher.dispatch('end', { | ||
startX: context.startX, | ||
@@ -217,8 +223,9 @@ startY: context.startY, | ||
isPan: context.isPan, | ||
velocity: v | ||
}); | ||
velocity: v, | ||
}) | ||
} | ||
cancel(point, context) { | ||
clearTimeout(context.handler); | ||
this.dispatcher.dispatch("cancel", {}); | ||
clearTimeout(context.handler) | ||
this.dispatcher.dispatch('cancel', {}) | ||
} | ||
@@ -228,18 +235,19 @@ } | ||
constructor(element) { | ||
this.element = element; | ||
this.element = element | ||
} | ||
dispatch(type, properties) { | ||
const event = new Event(type); | ||
const event = new Event(type) | ||
for (const name in properties) | ||
event[name] = properties[name]; | ||
this.element.dispatchEvent(event); | ||
event[name] = properties[name] | ||
this.element.dispatchEvent(event) | ||
} | ||
} | ||
function enableGesture(element) { | ||
new Listener(element, new Recognizer(new Dispatcher(element))); | ||
new Listener(element, new Recognizer(new Dispatcher(element))) | ||
} | ||
exports.Dispatcher = Dispatcher; | ||
exports.Listener = Listener; | ||
exports.Recognizer = Recognizer; | ||
exports.enableGesture = enableGesture; | ||
exports.Dispatcher = Dispatcher | ||
exports.Listener = Listener | ||
exports.Recognizer = Recognizer | ||
exports.enableGesture = enableGesture |
@@ -1,94 +0,96 @@ | ||
var __defProp = Object.defineProperty; | ||
var __pow = Math.pow; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __publicField = (obj, key, value) => { | ||
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); | ||
return value; | ||
}; | ||
const __defProp = Object.defineProperty | ||
const __pow = Math.pow | ||
const __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value | ||
const __publicField = (obj, key, value) => { | ||
__defNormalProp(obj, typeof key !== 'symbol' ? `${key}` : key, value) | ||
return value | ||
} | ||
class Listener { | ||
constructor(element, recognizer) { | ||
this.element = element; | ||
this.recognizer = recognizer; | ||
__publicField(this, "isListeningMouse", false); | ||
__publicField(this, "contexts", /* @__PURE__ */ new Map()); | ||
this.setupMouseListeners(); | ||
this.setupTouchListeners(); | ||
this.element = element | ||
this.recognizer = recognizer | ||
__publicField(this, 'isListeningMouse', false) | ||
__publicField(this, 'contexts', /* @__PURE__ */ new Map()) | ||
this.setupMouseListeners() | ||
this.setupTouchListeners() | ||
} | ||
setupMouseListeners() { | ||
const el = document.documentElement; | ||
this.element.addEventListener("mousedown", (event) => { | ||
const context = /* @__PURE__ */ Object.create(null); | ||
this.contexts.set(`mouse${1 << event.button}`, context); | ||
this.recognizer.start(event, context); | ||
const el = document.documentElement | ||
this.element.addEventListener('mousedown', (event) => { | ||
const context = /* @__PURE__ */ Object.create(null) | ||
this.contexts.set(`mouse${1 << event.button}`, context) | ||
this.recognizer.start(event, context) | ||
const mouseMove = (event2) => { | ||
let button = 1; | ||
let button = 1 | ||
while (button <= event2.buttons) { | ||
if (button & event2.buttons) { | ||
let key; | ||
let key | ||
if (button === 2) | ||
key = 4; | ||
key = 4 | ||
else if (button === 4) | ||
key = 2; | ||
key = 2 | ||
else | ||
key = button; | ||
const context2 = this.contexts.get(`mouse${key}`); | ||
key = button | ||
const context2 = this.contexts.get(`mouse${key}`) | ||
if (context2) | ||
this.recognizer.move(event2, context2); | ||
this.recognizer.move(event2, context2) | ||
} | ||
button = button << 1; | ||
button = button << 1 | ||
} | ||
}; | ||
} | ||
const mouseup = (event2) => { | ||
const context2 = this.contexts.get(`mouse${1 << event2.button}`); | ||
const context2 = this.contexts.get(`mouse${1 << event2.button}`) | ||
if (context2) | ||
this.recognizer.end(event2, context2); | ||
this.contexts.delete(`mouse${1 << event2.button}`); | ||
this.recognizer.end(event2, context2) | ||
this.contexts.delete(`mouse${1 << event2.button}`) | ||
if (event2.buttons === 0) { | ||
el.removeEventListener("mousemove", mouseMove); | ||
el.removeEventListener("mouseup", mouseup); | ||
this.isListeningMouse = false; | ||
el.removeEventListener('mousemove', mouseMove) | ||
el.removeEventListener('mouseup', mouseup) | ||
this.isListeningMouse = false | ||
} | ||
}; | ||
} | ||
if (!this.isListeningMouse) { | ||
el.addEventListener("mousemove", mouseMove); | ||
el.addEventListener("mouseup", mouseup); | ||
this.isListeningMouse = true; | ||
el.addEventListener('mousemove', mouseMove) | ||
el.addEventListener('mouseup', mouseup) | ||
this.isListeningMouse = true | ||
} | ||
}); | ||
}) | ||
} | ||
setupTouchListeners() { | ||
this.element.addEventListener("touchstart", (event) => { | ||
this.element.addEventListener('touchstart', (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = /* @__PURE__ */ Object.create(null); | ||
this.contexts.set(touch.identifier, context); | ||
this.recognizer.start(touch, context); | ||
const context = /* @__PURE__ */ Object.create(null) | ||
this.contexts.set(touch.identifier, context) | ||
this.recognizer.start(touch, context) | ||
} | ||
}); | ||
this.element.addEventListener("touchmove", (event) => { | ||
}) | ||
this.element.addEventListener('touchmove', (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = this.contexts.get(touch.identifier); | ||
const context = this.contexts.get(touch.identifier) | ||
if (context) { | ||
event.preventDefault(); | ||
this.recognizer.move(touch, context); | ||
event.preventDefault() | ||
this.recognizer.move(touch, context) | ||
} | ||
} | ||
}); | ||
this.element.addEventListener("touchend", (event) => { | ||
}) | ||
this.element.addEventListener('touchend', (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = this.contexts.get(touch.identifier); | ||
const context = this.contexts.get(touch.identifier) | ||
if (context) { | ||
this.recognizer.end(touch, context); | ||
this.contexts.delete(touch.identifier); | ||
this.recognizer.end(touch, context) | ||
this.contexts.delete(touch.identifier) | ||
} | ||
} | ||
}); | ||
this.element.addEventListener("touchcancel", (event) => { | ||
}) | ||
this.element.addEventListener('touchcancel', (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = this.contexts.get(touch.identifier); | ||
const context = this.contexts.get(touch.identifier) | ||
if (context) { | ||
this.recognizer.cancel(touch, context); | ||
this.contexts.delete(touch.identifier); | ||
this.recognizer.cancel(touch, context) | ||
this.contexts.delete(touch.identifier) | ||
} | ||
} | ||
}); | ||
}) | ||
} | ||
@@ -98,38 +100,40 @@ } | ||
constructor(dispatcher) { | ||
this.dispatcher = dispatcher; | ||
this.dispatcher = dispatcher | ||
} | ||
start(point, context) { | ||
context.startX = point.clientX; | ||
context.startY = point.clientY; | ||
this.dispatcher.dispatch("start", { | ||
context.startX = point.clientX | ||
context.startY = point.clientY | ||
this.dispatcher.dispatch('start', { | ||
clientX: point.clientX, | ||
clientY: point.clientY | ||
}); | ||
clientY: point.clientY, | ||
}) | ||
context.points = [{ | ||
t: Date.now(), | ||
x: point.clientX, | ||
y: point.clientY | ||
}]; | ||
context.isTap = true; | ||
context.isPan = false; | ||
context.isPress = false; | ||
y: point.clientY, | ||
}] | ||
context.isTap = true | ||
context.isPan = false | ||
context.isPress = false | ||
context.handler = setTimeout(() => { | ||
context.isTap = false; | ||
context.isPan = false; | ||
context.isPress = true; | ||
context.handler = null; | ||
this.dispatcher.dispatch("press", {}); | ||
}, 500); | ||
context.isTap = false | ||
context.isPan = false | ||
context.isPress = true | ||
context.handler = null | ||
this.dispatcher.dispatch('press', {}) | ||
}, 500) | ||
} | ||
move(point, context) { | ||
var _a; | ||
const dx = point.clientX - context.startX; | ||
const dy = point.clientY - context.startY; | ||
if (!context.isPan && __pow(dx, 2) + __pow(dy, 2) > 100) { | ||
context.isPan = true; | ||
context.isTap = false; | ||
context.isPress = false; | ||
context.isVertical = Math.abs(dx) - Math.abs(dy) >= 0; | ||
clearTimeout((_a = context.handler) != null ? _a : void 0); | ||
this.dispatcher.dispatch("panStart", { | ||
let _a | ||
const dx = point.clientX - context.startX | ||
const dy = point.clientY - context.startY | ||
if (!context.isPan && dx ** 2 + dy ** 2 > 100) { | ||
context.isPan = true | ||
context.isTap = false | ||
context.isPress = false | ||
context.isVertical = Math.abs(dx) - Math.abs(dy) >= 0 | ||
clearTimeout((_a = context.handler) != null ? _a : void 0) | ||
this.dispatcher.dispatch('panStart', { | ||
startX: context.startX, | ||
@@ -139,7 +143,7 @@ startY: context.startY, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical | ||
}); | ||
isVertical: context.isVertical, | ||
}) | ||
} | ||
if (context.isPan) { | ||
this.dispatcher.dispatch("pan", { | ||
this.dispatcher.dispatch('pan', { | ||
startX: context.startX, | ||
@@ -149,34 +153,35 @@ startY: context.startY, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical | ||
}); | ||
isVertical: context.isVertical, | ||
}) | ||
} | ||
context.points = context.points.filter((point2) => Date.now() - point2.t < 500); | ||
context.points = context.points.filter(point2 => Date.now() - point2.t < 500) | ||
context.points.push({ | ||
t: Date.now(), | ||
x: point.clientX, | ||
y: point.clientY | ||
}); | ||
y: point.clientY, | ||
}) | ||
} | ||
end(point, context) { | ||
var _a; | ||
let _a | ||
if (context.isTap) { | ||
this.dispatcher.dispatch("tap", { | ||
this.dispatcher.dispatch('tap', { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY | ||
}); | ||
clearTimeout((_a = context.handler) != null ? _a : void 0); | ||
clientY: point.clientY, | ||
}) | ||
clearTimeout((_a = context.handler) != null ? _a : void 0) | ||
} | ||
if (context.isPress) | ||
this.dispatcher.dispatch("pressEnd", {}); | ||
context.points = context.points.filter((point2) => Date.now() - point2.t < 500); | ||
let v = 0; | ||
this.dispatcher.dispatch('pressEnd', {}) | ||
context.points = context.points.filter(point2 => Date.now() - point2.t < 500) | ||
let v = 0 | ||
if (context.points.length) { | ||
const d = Math.sqrt(__pow(point.clientX - context.points[0].x, 2) + __pow(point.clientY - context.points[0].y, 2)); | ||
v = d / (Date.now() - context.points[0].t); | ||
const d = Math.sqrt((point.clientX - context.points[0].x) ** 2 + (point.clientY - context.points[0].y) ** 2) | ||
v = d / (Date.now() - context.points[0].t) | ||
} | ||
if (v > 1.5) { | ||
context.isFlick = true; | ||
this.dispatcher.dispatch("flick", { | ||
context.isFlick = true | ||
this.dispatcher.dispatch('flick', { | ||
startX: context.startX, | ||
@@ -188,9 +193,10 @@ startY: context.startY, | ||
isFlick: context.isFlick, | ||
velocity: v | ||
}); | ||
} else { | ||
context.isFlick = false; | ||
velocity: v, | ||
}) | ||
} | ||
else | ||
context.isFlick = false | ||
if (context.isPan) { | ||
this.dispatcher.dispatch("panEnd", { | ||
this.dispatcher.dispatch('panEnd', { | ||
startX: context.startX, | ||
@@ -202,6 +208,6 @@ startY: context.startY, | ||
isFlick: context.isFlick, | ||
velocity: v | ||
}); | ||
velocity: v, | ||
}) | ||
} | ||
this.dispatcher.dispatch("end", { | ||
this.dispatcher.dispatch('end', { | ||
startX: context.startX, | ||
@@ -214,8 +220,9 @@ startY: context.startY, | ||
isPan: context.isPan, | ||
velocity: v | ||
}); | ||
velocity: v, | ||
}) | ||
} | ||
cancel(point, context) { | ||
clearTimeout(context.handler); | ||
this.dispatcher.dispatch("cancel", {}); | ||
clearTimeout(context.handler) | ||
this.dispatcher.dispatch('cancel', {}) | ||
} | ||
@@ -225,15 +232,16 @@ } | ||
constructor(element) { | ||
this.element = element; | ||
this.element = element | ||
} | ||
dispatch(type, properties) { | ||
const event = new Event(type); | ||
const event = new Event(type) | ||
for (const name in properties) | ||
event[name] = properties[name]; | ||
this.element.dispatchEvent(event); | ||
event[name] = properties[name] | ||
this.element.dispatchEvent(event) | ||
} | ||
} | ||
function enableGesture(element) { | ||
new Listener(element, new Recognizer(new Dispatcher(element))); | ||
new Listener(element, new Recognizer(new Dispatcher(element))) | ||
} | ||
export { Dispatcher, Listener, Recognizer, enableGesture }; | ||
export { Dispatcher, Listener, Recognizer, enableGesture } |
@@ -1,97 +0,99 @@ | ||
var Gesture = (function (exports) { | ||
'use strict'; | ||
const Gesture = (function (exports) { | ||
'use strict' | ||
var __defProp = Object.defineProperty; | ||
var __pow = Math.pow; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __publicField = (obj, key, value) => { | ||
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); | ||
return value; | ||
}; | ||
const __defProp = Object.defineProperty | ||
const __pow = Math.pow | ||
const __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value | ||
const __publicField = (obj, key, value) => { | ||
__defNormalProp(obj, typeof key !== 'symbol' ? `${key}` : key, value) | ||
return value | ||
} | ||
class Listener { | ||
constructor(element, recognizer) { | ||
this.element = element; | ||
this.recognizer = recognizer; | ||
__publicField(this, "isListeningMouse", false); | ||
__publicField(this, "contexts", /* @__PURE__ */ new Map()); | ||
this.setupMouseListeners(); | ||
this.setupTouchListeners(); | ||
this.element = element | ||
this.recognizer = recognizer | ||
__publicField(this, 'isListeningMouse', false) | ||
__publicField(this, 'contexts', /* @__PURE__ */ new Map()) | ||
this.setupMouseListeners() | ||
this.setupTouchListeners() | ||
} | ||
setupMouseListeners() { | ||
const el = document.documentElement; | ||
this.element.addEventListener("mousedown", (event) => { | ||
const context = /* @__PURE__ */ Object.create(null); | ||
this.contexts.set(`mouse${1 << event.button}`, context); | ||
this.recognizer.start(event, context); | ||
const el = document.documentElement | ||
this.element.addEventListener('mousedown', (event) => { | ||
const context = /* @__PURE__ */ Object.create(null) | ||
this.contexts.set(`mouse${1 << event.button}`, context) | ||
this.recognizer.start(event, context) | ||
const mouseMove = (event2) => { | ||
let button = 1; | ||
let button = 1 | ||
while (button <= event2.buttons) { | ||
if (button & event2.buttons) { | ||
let key; | ||
let key | ||
if (button === 2) | ||
key = 4; | ||
key = 4 | ||
else if (button === 4) | ||
key = 2; | ||
key = 2 | ||
else | ||
key = button; | ||
const context2 = this.contexts.get(`mouse${key}`); | ||
key = button | ||
const context2 = this.contexts.get(`mouse${key}`) | ||
if (context2) | ||
this.recognizer.move(event2, context2); | ||
this.recognizer.move(event2, context2) | ||
} | ||
button = button << 1; | ||
button = button << 1 | ||
} | ||
}; | ||
} | ||
const mouseup = (event2) => { | ||
const context2 = this.contexts.get(`mouse${1 << event2.button}`); | ||
const context2 = this.contexts.get(`mouse${1 << event2.button}`) | ||
if (context2) | ||
this.recognizer.end(event2, context2); | ||
this.contexts.delete(`mouse${1 << event2.button}`); | ||
this.recognizer.end(event2, context2) | ||
this.contexts.delete(`mouse${1 << event2.button}`) | ||
if (event2.buttons === 0) { | ||
el.removeEventListener("mousemove", mouseMove); | ||
el.removeEventListener("mouseup", mouseup); | ||
this.isListeningMouse = false; | ||
el.removeEventListener('mousemove', mouseMove) | ||
el.removeEventListener('mouseup', mouseup) | ||
this.isListeningMouse = false | ||
} | ||
}; | ||
} | ||
if (!this.isListeningMouse) { | ||
el.addEventListener("mousemove", mouseMove); | ||
el.addEventListener("mouseup", mouseup); | ||
this.isListeningMouse = true; | ||
el.addEventListener('mousemove', mouseMove) | ||
el.addEventListener('mouseup', mouseup) | ||
this.isListeningMouse = true | ||
} | ||
}); | ||
}) | ||
} | ||
setupTouchListeners() { | ||
this.element.addEventListener("touchstart", (event) => { | ||
this.element.addEventListener('touchstart', (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = /* @__PURE__ */ Object.create(null); | ||
this.contexts.set(touch.identifier, context); | ||
this.recognizer.start(touch, context); | ||
const context = /* @__PURE__ */ Object.create(null) | ||
this.contexts.set(touch.identifier, context) | ||
this.recognizer.start(touch, context) | ||
} | ||
}); | ||
this.element.addEventListener("touchmove", (event) => { | ||
}) | ||
this.element.addEventListener('touchmove', (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = this.contexts.get(touch.identifier); | ||
const context = this.contexts.get(touch.identifier) | ||
if (context) { | ||
event.preventDefault(); | ||
this.recognizer.move(touch, context); | ||
event.preventDefault() | ||
this.recognizer.move(touch, context) | ||
} | ||
} | ||
}); | ||
this.element.addEventListener("touchend", (event) => { | ||
}) | ||
this.element.addEventListener('touchend', (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = this.contexts.get(touch.identifier); | ||
const context = this.contexts.get(touch.identifier) | ||
if (context) { | ||
this.recognizer.end(touch, context); | ||
this.contexts.delete(touch.identifier); | ||
this.recognizer.end(touch, context) | ||
this.contexts.delete(touch.identifier) | ||
} | ||
} | ||
}); | ||
this.element.addEventListener("touchcancel", (event) => { | ||
}) | ||
this.element.addEventListener('touchcancel', (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = this.contexts.get(touch.identifier); | ||
const context = this.contexts.get(touch.identifier) | ||
if (context) { | ||
this.recognizer.cancel(touch, context); | ||
this.contexts.delete(touch.identifier); | ||
this.recognizer.cancel(touch, context) | ||
this.contexts.delete(touch.identifier) | ||
} | ||
} | ||
}); | ||
}) | ||
} | ||
@@ -101,38 +103,40 @@ } | ||
constructor(dispatcher) { | ||
this.dispatcher = dispatcher; | ||
this.dispatcher = dispatcher | ||
} | ||
start(point, context) { | ||
context.startX = point.clientX; | ||
context.startY = point.clientY; | ||
this.dispatcher.dispatch("start", { | ||
context.startX = point.clientX | ||
context.startY = point.clientY | ||
this.dispatcher.dispatch('start', { | ||
clientX: point.clientX, | ||
clientY: point.clientY | ||
}); | ||
clientY: point.clientY, | ||
}) | ||
context.points = [{ | ||
t: Date.now(), | ||
x: point.clientX, | ||
y: point.clientY | ||
}]; | ||
context.isTap = true; | ||
context.isPan = false; | ||
context.isPress = false; | ||
y: point.clientY, | ||
}] | ||
context.isTap = true | ||
context.isPan = false | ||
context.isPress = false | ||
context.handler = setTimeout(() => { | ||
context.isTap = false; | ||
context.isPan = false; | ||
context.isPress = true; | ||
context.handler = null; | ||
this.dispatcher.dispatch("press", {}); | ||
}, 500); | ||
context.isTap = false | ||
context.isPan = false | ||
context.isPress = true | ||
context.handler = null | ||
this.dispatcher.dispatch('press', {}) | ||
}, 500) | ||
} | ||
move(point, context) { | ||
var _a; | ||
const dx = point.clientX - context.startX; | ||
const dy = point.clientY - context.startY; | ||
if (!context.isPan && __pow(dx, 2) + __pow(dy, 2) > 100) { | ||
context.isPan = true; | ||
context.isTap = false; | ||
context.isPress = false; | ||
context.isVertical = Math.abs(dx) - Math.abs(dy) >= 0; | ||
clearTimeout((_a = context.handler) != null ? _a : void 0); | ||
this.dispatcher.dispatch("panStart", { | ||
let _a | ||
const dx = point.clientX - context.startX | ||
const dy = point.clientY - context.startY | ||
if (!context.isPan && dx ** 2 + dy ** 2 > 100) { | ||
context.isPan = true | ||
context.isTap = false | ||
context.isPress = false | ||
context.isVertical = Math.abs(dx) - Math.abs(dy) >= 0 | ||
clearTimeout((_a = context.handler) != null ? _a : void 0) | ||
this.dispatcher.dispatch('panStart', { | ||
startX: context.startX, | ||
@@ -142,7 +146,7 @@ startY: context.startY, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical | ||
}); | ||
isVertical: context.isVertical, | ||
}) | ||
} | ||
if (context.isPan) { | ||
this.dispatcher.dispatch("pan", { | ||
this.dispatcher.dispatch('pan', { | ||
startX: context.startX, | ||
@@ -152,34 +156,35 @@ startY: context.startY, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical | ||
}); | ||
isVertical: context.isVertical, | ||
}) | ||
} | ||
context.points = context.points.filter((point2) => Date.now() - point2.t < 500); | ||
context.points = context.points.filter(point2 => Date.now() - point2.t < 500) | ||
context.points.push({ | ||
t: Date.now(), | ||
x: point.clientX, | ||
y: point.clientY | ||
}); | ||
y: point.clientY, | ||
}) | ||
} | ||
end(point, context) { | ||
var _a; | ||
let _a | ||
if (context.isTap) { | ||
this.dispatcher.dispatch("tap", { | ||
this.dispatcher.dispatch('tap', { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY | ||
}); | ||
clearTimeout((_a = context.handler) != null ? _a : void 0); | ||
clientY: point.clientY, | ||
}) | ||
clearTimeout((_a = context.handler) != null ? _a : void 0) | ||
} | ||
if (context.isPress) | ||
this.dispatcher.dispatch("pressEnd", {}); | ||
context.points = context.points.filter((point2) => Date.now() - point2.t < 500); | ||
let v = 0; | ||
this.dispatcher.dispatch('pressEnd', {}) | ||
context.points = context.points.filter(point2 => Date.now() - point2.t < 500) | ||
let v = 0 | ||
if (context.points.length) { | ||
const d = Math.sqrt(__pow(point.clientX - context.points[0].x, 2) + __pow(point.clientY - context.points[0].y, 2)); | ||
v = d / (Date.now() - context.points[0].t); | ||
const d = Math.sqrt((point.clientX - context.points[0].x) ** 2 + (point.clientY - context.points[0].y) ** 2) | ||
v = d / (Date.now() - context.points[0].t) | ||
} | ||
if (v > 1.5) { | ||
context.isFlick = true; | ||
this.dispatcher.dispatch("flick", { | ||
context.isFlick = true | ||
this.dispatcher.dispatch('flick', { | ||
startX: context.startX, | ||
@@ -191,9 +196,10 @@ startY: context.startY, | ||
isFlick: context.isFlick, | ||
velocity: v | ||
}); | ||
} else { | ||
context.isFlick = false; | ||
velocity: v, | ||
}) | ||
} | ||
else | ||
context.isFlick = false | ||
if (context.isPan) { | ||
this.dispatcher.dispatch("panEnd", { | ||
this.dispatcher.dispatch('panEnd', { | ||
startX: context.startX, | ||
@@ -205,6 +211,6 @@ startY: context.startY, | ||
isFlick: context.isFlick, | ||
velocity: v | ||
}); | ||
velocity: v, | ||
}) | ||
} | ||
this.dispatcher.dispatch("end", { | ||
this.dispatcher.dispatch('end', { | ||
startX: context.startX, | ||
@@ -217,8 +223,9 @@ startY: context.startY, | ||
isPan: context.isPan, | ||
velocity: v | ||
}); | ||
velocity: v, | ||
}) | ||
} | ||
cancel(point, context) { | ||
clearTimeout(context.handler); | ||
this.dispatcher.dispatch("cancel", {}); | ||
clearTimeout(context.handler) | ||
this.dispatcher.dispatch('cancel', {}) | ||
} | ||
@@ -228,22 +235,22 @@ } | ||
constructor(element) { | ||
this.element = element; | ||
this.element = element | ||
} | ||
dispatch(type, properties) { | ||
const event = new Event(type); | ||
const event = new Event(type) | ||
for (const name in properties) | ||
event[name] = properties[name]; | ||
this.element.dispatchEvent(event); | ||
event[name] = properties[name] | ||
this.element.dispatchEvent(event) | ||
} | ||
} | ||
function enableGesture(element) { | ||
new Listener(element, new Recognizer(new Dispatcher(element))); | ||
new Listener(element, new Recognizer(new Dispatcher(element))) | ||
} | ||
exports.Dispatcher = Dispatcher; | ||
exports.Listener = Listener; | ||
exports.Recognizer = Recognizer; | ||
exports.enableGesture = enableGesture; | ||
exports.Dispatcher = Dispatcher | ||
exports.Listener = Listener | ||
exports.Recognizer = Recognizer | ||
exports.enableGesture = enableGesture | ||
return exports; | ||
})({}); | ||
return exports | ||
})({}) |
{ | ||
"name": "@ajiu9/gesture", | ||
"version": "1.0.6-beta.0", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -20,4 +20,4 @@ "author": "ajiu9 <615944323@qq.com> (https://github.com/ajiu9/)", | ||
"files": [ | ||
"index.js", | ||
"dist" | ||
"dist", | ||
"index.js" | ||
], | ||
@@ -24,0 +24,0 @@ "buildOptions": { |
@@ -1,1 +0,1 @@ | ||
# @ajiu9/gesture | ||
# @ajiu9/gesture |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
2
24274
1