@ajiu9/gesture
Advanced tools
Comparing version 1.0.3-beta.8 to 1.0.3-beta.11
@@ -1,1 +0,220 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=Object.defineProperty,e=(e,s,i)=>(((e,s,i)=>{s in e?t(e,s,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[s]=i})(e,"symbol"!=typeof s?s+"":s,i),i);class s{constructor(t,e){let s=!1;const i=new Map,n=document.documentElement;t.addEventListener("mousedown",(t=>{const c=Object.create(null);i.set("mouse"+(1<<t.button),c),e.start(t,c);const a=t=>{let s=1;for(;s<=t.buttons;){if(s&t.buttons){let n;n=2===s?4:4===s?2:s;const c=i.get(`mouse${n}`);e.move(t,c)}s<<=1}},r=t=>{const c=i.get("mouse"+(1<<t.button));e.end(t,c),i.delete("mouse"+(1<<t.button)),0===t.buttons&&(n.removeEventListener("mousemove",a),n.removeEventListener("mouseup",r),s=!1)};s||(n.addEventListener("mousemove",a),n.addEventListener("mouseup",r),s=!0)})),t.addEventListener("touchstart",(t=>{for(const s of t.changedTouches){const t=Object.create(null);i.set(s.identifier,t),e.start(s,t)}})),t.addEventListener("touchmove",(t=>{for(const s of t.changedTouches){const n=i.get(s.identifier);t.preventDefault(),e.move(s,n)}})),t.addEventListener("touchend",(t=>{for(const s of t.changedTouches){const t=i.get(s.identifier);e.end(s,t),i.delete(s.identifier)}})),t.addEventListener("touchcancel",(t=>{for(const s of t.changedTouches){const t=i.get(s.identifier);e.cancel(s,t),i.delete(s.identifier)}}))}}class i{constructor(t){e(this,"cancel",((t,e)=>{clearTimeout(e.handler),this.dispatcher.dispatch("cancel",{})})),this.dispatcher=t}start(t,e){e.startX=t.clientX,e.startY=t.clientY,this.dispatcher.dispatch("start",{clientX:t.clientX,clientY:t.clientY}),e.points=[{t:Date.now(),x:t.clientX,y:t.clientY}],e.isTap=!0,e.isPan=!1,e.isPress=!1,e.handler=setTimeout((()=>{e.isTap=!1,e.isPan=!1,e.isPress=!0,e.handler=null,this.dispatcher.dispatch("press",{})}),500)}move(t,e){const s=t.clientX-e.startX,i=t.clientY-e.startY;!e.isPan&&s**2+i**2>100&&(e.isPan=!0,e.isTap=!1,e.isPress=!1,e.isVertical=Math.abs(s)-Math.abs(i)>=0,clearTimeout(e.handler),this.dispatcher.dispatch("panStart",{startX:e.startX,startY:e.startY,clientX:t.clientX,clientY:t.clientY,isVertical:e.isVertical})),e.isPan&&this.dispatcher.dispatch("pan",{startX:e.startX,startY:e.startY,clientX:t.clientX,clientY:t.clientY,isVertical:e.isVertical}),e.points=e.points.filter((t=>Date.now()-t.t<500)),e.points.push({t:Date.now(),x:t.clientX,y:t.clientY})}end(t,e){e.isTap&&(this.dispatcher.dispatch("tap",{startX:e.startX,startY:e.startY,clientX:t.clientX,clientY:t.clientY}),clearTimeout(e.handler)),e.isPress&&this.dispatcher.dispatch("pressEnd",{}),e.points=e.points.filter((t=>Date.now()-t.t<500));let s=0;if(e.points.length){s=Math.sqrt((t.clientX-e.points[0].x)**2+(t.clientY-e.points[0].y)**2)/(Date.now()-e.points[0].t)}s>1.5?(e.isFlick=!0,this.dispatcher.dispatch("flick",{startX:e.startX,startY:e.startY,clientX:t.clientX,clientY:t.clientY,isVertical:e.isVertical,isFlick:e.isFlick,velocity:s})):e.isFlick=!1,e.isPan&&this.dispatcher.dispatch("panEnd",{startX:e.startX,startY:e.startY,clientX:t.clientX,clientY:t.clientY,isVertical:e.isVertical,isFlick:e.isFlick,velocity:s}),this.dispatcher.dispatch("end",{startX:e.startX,startY:e.startY,clientX:t.clientX,clientY:t.clientY,isVertical:e.isVertical,isFlick:e.isFlick,isPan:e.isPan,velocity:s})}}class n{constructor(t){this.element=t}dispatch(t,e){const s=new Event(t);for(const i in e)s[i]=e[i];this.element.dispatchEvent(s)}}exports.Dispatcher=n,exports.Listener=s,exports.Recognizer=i,exports.enableGesture=function(t){new s(t,new i(new n(t)))}; | ||
'use strict'; | ||
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; | ||
}; | ||
class Listener { | ||
constructor(element, recognizer) { | ||
let isListeningMouse = false; | ||
const contexts = /* @__PURE__ */ new Map(); | ||
const el = document.documentElement; | ||
element.addEventListener("mousedown", (event) => { | ||
const context = /* @__PURE__ */ Object.create(null); | ||
contexts.set(`mouse${1 << event.button}`, context); | ||
recognizer.start(event, context); | ||
const mouseMove = (event2) => { | ||
let button = 1; | ||
while (button <= event2.buttons) { | ||
if (button & event2.buttons) { | ||
let key; | ||
if (button === 2) | ||
key = 4; | ||
else if (button === 4) | ||
key = 2; | ||
else | ||
key = button; | ||
const context2 = contexts.get(`mouse${key}`); | ||
recognizer.move(event2, context2); | ||
} | ||
button = button << 1; | ||
} | ||
}; | ||
const mouseup = (event2) => { | ||
const context2 = contexts.get(`mouse${1 << event2.button}`); | ||
recognizer.end(event2, context2); | ||
contexts.delete(`mouse${1 << event2.button}`); | ||
if (event2.buttons === 0) { | ||
el.removeEventListener("mousemove", mouseMove); | ||
el.removeEventListener("mouseup", mouseup); | ||
isListeningMouse = false; | ||
} | ||
}; | ||
if (!isListeningMouse) { | ||
el.addEventListener("mousemove", mouseMove); | ||
el.addEventListener("mouseup", mouseup); | ||
isListeningMouse = true; | ||
} | ||
}); | ||
element.addEventListener("touchstart", (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = /* @__PURE__ */ Object.create(null); | ||
contexts.set(touch.identifier, context); | ||
recognizer.start(touch, context); | ||
} | ||
}); | ||
element.addEventListener("touchmove", (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = contexts.get(touch.identifier); | ||
event.preventDefault(); | ||
recognizer.move(touch, context); | ||
} | ||
}); | ||
element.addEventListener("touchend", (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = contexts.get(touch.identifier); | ||
recognizer.end(touch, context); | ||
contexts.delete(touch.identifier); | ||
} | ||
}); | ||
element.addEventListener("touchcancel", (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = contexts.get(touch.identifier); | ||
recognizer.cancel(touch, context); | ||
contexts.delete(touch.identifier); | ||
} | ||
}); | ||
} | ||
} | ||
class Recognizer { | ||
constructor(dispatcher) { | ||
__publicField(this, "cancel", (point, context) => { | ||
clearTimeout(context.handler); | ||
this.dispatcher.dispatch("cancel", {}); | ||
}); | ||
this.dispatcher = dispatcher; | ||
} | ||
start(point, context) { | ||
context.startX = point.clientX; | ||
context.startY = point.clientY; | ||
this.dispatcher.dispatch("start", { | ||
clientX: point.clientX, | ||
clientY: point.clientY | ||
}); | ||
context.points = [{ | ||
t: Date.now(), | ||
x: point.clientX, | ||
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); | ||
} | ||
move(point, context) { | ||
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(context.handler); | ||
this.dispatcher.dispatch("panStart", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical | ||
}); | ||
} | ||
if (context.isPan) { | ||
this.dispatcher.dispatch("pan", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical | ||
}); | ||
} | ||
context.points = context.points.filter((point2) => Date.now() - point2.t < 500); | ||
context.points.push({ | ||
t: Date.now(), | ||
x: point.clientX, | ||
y: point.clientY | ||
}); | ||
} | ||
end(point, context) { | ||
if (context.isTap) { | ||
this.dispatcher.dispatch("tap", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY | ||
}); | ||
clearTimeout(context.handler); | ||
} | ||
if (context.isPress) | ||
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); | ||
} | ||
if (v > 1.5) { | ||
context.isFlick = true; | ||
this.dispatcher.dispatch("flick", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical, | ||
isFlick: context.isFlick, | ||
velocity: v | ||
}); | ||
} else { | ||
context.isFlick = false; | ||
} | ||
if (context.isPan) { | ||
this.dispatcher.dispatch("panEnd", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical, | ||
isFlick: context.isFlick, | ||
velocity: v | ||
}); | ||
} | ||
this.dispatcher.dispatch("end", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical, | ||
isFlick: context.isFlick, | ||
isPan: context.isPan, | ||
velocity: v | ||
}); | ||
} | ||
} | ||
class Dispatcher { | ||
constructor(element) { | ||
this.element = element; | ||
} | ||
dispatch(type, properties) { | ||
const event = new Event(type); | ||
for (const name in properties) | ||
event[name] = properties[name]; | ||
this.element.dispatchEvent(event); | ||
} | ||
} | ||
function enableGesture(element) { | ||
new Listener(element, new Recognizer(new Dispatcher(element))); | ||
} | ||
exports.Dispatcher = Dispatcher; | ||
exports.Listener = Listener; | ||
exports.Recognizer = Recognizer; | ||
exports.enableGesture = enableGesture; |
@@ -1,1 +0,214 @@ | ||
var t=Object.defineProperty,e=Math.pow,s=(e,s,i)=>(((e,s,i)=>{s in e?t(e,s,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[s]=i})(e,"symbol"!=typeof s?s+"":s,i),i);class i{constructor(t,e){let s=!1;const i=new Map,n=document.documentElement;t.addEventListener("mousedown",(t=>{const c=Object.create(null);i.set("mouse"+(1<<t.button),c),e.start(t,c);const a=t=>{let s=1;for(;s<=t.buttons;){if(s&t.buttons){let n;n=2===s?4:4===s?2:s;const c=i.get(`mouse${n}`);e.move(t,c)}s<<=1}},r=t=>{const c=i.get("mouse"+(1<<t.button));e.end(t,c),i.delete("mouse"+(1<<t.button)),0===t.buttons&&(n.removeEventListener("mousemove",a),n.removeEventListener("mouseup",r),s=!1)};s||(n.addEventListener("mousemove",a),n.addEventListener("mouseup",r),s=!0)})),t.addEventListener("touchstart",(t=>{for(const s of t.changedTouches){const t=Object.create(null);i.set(s.identifier,t),e.start(s,t)}})),t.addEventListener("touchmove",(t=>{for(const s of t.changedTouches){const n=i.get(s.identifier);t.preventDefault(),e.move(s,n)}})),t.addEventListener("touchend",(t=>{for(const s of t.changedTouches){const t=i.get(s.identifier);e.end(s,t),i.delete(s.identifier)}})),t.addEventListener("touchcancel",(t=>{for(const s of t.changedTouches){const t=i.get(s.identifier);e.cancel(s,t),i.delete(s.identifier)}}))}}class n{constructor(t){s(this,"cancel",((t,e)=>{clearTimeout(e.handler),this.dispatcher.dispatch("cancel",{})})),this.dispatcher=t}start(t,e){e.startX=t.clientX,e.startY=t.clientY,this.dispatcher.dispatch("start",{clientX:t.clientX,clientY:t.clientY}),e.points=[{t:Date.now(),x:t.clientX,y:t.clientY}],e.isTap=!0,e.isPan=!1,e.isPress=!1,e.handler=setTimeout((()=>{e.isTap=!1,e.isPan=!1,e.isPress=!0,e.handler=null,this.dispatcher.dispatch("press",{})}),500)}move(t,s){const i=t.clientX-s.startX,n=t.clientY-s.startY;!s.isPan&&e(i,2)+e(n,2)>100&&(s.isPan=!0,s.isTap=!1,s.isPress=!1,s.isVertical=Math.abs(i)-Math.abs(n)>=0,clearTimeout(s.handler),this.dispatcher.dispatch("panStart",{startX:s.startX,startY:s.startY,clientX:t.clientX,clientY:t.clientY,isVertical:s.isVertical})),s.isPan&&this.dispatcher.dispatch("pan",{startX:s.startX,startY:s.startY,clientX:t.clientX,clientY:t.clientY,isVertical:s.isVertical}),s.points=s.points.filter((t=>Date.now()-t.t<500)),s.points.push({t:Date.now(),x:t.clientX,y:t.clientY})}end(t,s){s.isTap&&(this.dispatcher.dispatch("tap",{startX:s.startX,startY:s.startY,clientX:t.clientX,clientY:t.clientY}),clearTimeout(s.handler)),s.isPress&&this.dispatcher.dispatch("pressEnd",{}),s.points=s.points.filter((t=>Date.now()-t.t<500));let i=0;if(s.points.length){i=Math.sqrt(e(t.clientX-s.points[0].x,2)+e(t.clientY-s.points[0].y,2))/(Date.now()-s.points[0].t)}i>1.5?(s.isFlick=!0,this.dispatcher.dispatch("flick",{startX:s.startX,startY:s.startY,clientX:t.clientX,clientY:t.clientY,isVertical:s.isVertical,isFlick:s.isFlick,velocity:i})):s.isFlick=!1,s.isPan&&this.dispatcher.dispatch("panEnd",{startX:s.startX,startY:s.startY,clientX:t.clientX,clientY:t.clientY,isVertical:s.isVertical,isFlick:s.isFlick,velocity:i}),this.dispatcher.dispatch("end",{startX:s.startX,startY:s.startY,clientX:t.clientX,clientY:t.clientY,isVertical:s.isVertical,isFlick:s.isFlick,isPan:s.isPan,velocity:i})}}class c{constructor(t){this.element=t}dispatch(t,e){const s=new Event(t);for(const i in e)s[i]=e[i];this.element.dispatchEvent(s)}}function a(t){new i(t,new n(new c(t)))}export{c as Dispatcher,i as Listener,n as Recognizer,a as enableGesture}; | ||
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; | ||
}; | ||
class Listener { | ||
constructor(element, recognizer) { | ||
let isListeningMouse = false; | ||
const contexts = /* @__PURE__ */ new Map(); | ||
const el = document.documentElement; | ||
element.addEventListener("mousedown", (event) => { | ||
const context = /* @__PURE__ */ Object.create(null); | ||
contexts.set(`mouse${1 << event.button}`, context); | ||
recognizer.start(event, context); | ||
const mouseMove = (event2) => { | ||
let button = 1; | ||
while (button <= event2.buttons) { | ||
if (button & event2.buttons) { | ||
let key; | ||
if (button === 2) | ||
key = 4; | ||
else if (button === 4) | ||
key = 2; | ||
else | ||
key = button; | ||
const context2 = contexts.get(`mouse${key}`); | ||
recognizer.move(event2, context2); | ||
} | ||
button = button << 1; | ||
} | ||
}; | ||
const mouseup = (event2) => { | ||
const context2 = contexts.get(`mouse${1 << event2.button}`); | ||
recognizer.end(event2, context2); | ||
contexts.delete(`mouse${1 << event2.button}`); | ||
if (event2.buttons === 0) { | ||
el.removeEventListener("mousemove", mouseMove); | ||
el.removeEventListener("mouseup", mouseup); | ||
isListeningMouse = false; | ||
} | ||
}; | ||
if (!isListeningMouse) { | ||
el.addEventListener("mousemove", mouseMove); | ||
el.addEventListener("mouseup", mouseup); | ||
isListeningMouse = true; | ||
} | ||
}); | ||
element.addEventListener("touchstart", (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = /* @__PURE__ */ Object.create(null); | ||
contexts.set(touch.identifier, context); | ||
recognizer.start(touch, context); | ||
} | ||
}); | ||
element.addEventListener("touchmove", (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = contexts.get(touch.identifier); | ||
event.preventDefault(); | ||
recognizer.move(touch, context); | ||
} | ||
}); | ||
element.addEventListener("touchend", (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = contexts.get(touch.identifier); | ||
recognizer.end(touch, context); | ||
contexts.delete(touch.identifier); | ||
} | ||
}); | ||
element.addEventListener("touchcancel", (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = contexts.get(touch.identifier); | ||
recognizer.cancel(touch, context); | ||
contexts.delete(touch.identifier); | ||
} | ||
}); | ||
} | ||
} | ||
class Recognizer { | ||
constructor(dispatcher) { | ||
__publicField(this, "cancel", (point, context) => { | ||
clearTimeout(context.handler); | ||
this.dispatcher.dispatch("cancel", {}); | ||
}); | ||
this.dispatcher = dispatcher; | ||
} | ||
start(point, context) { | ||
context.startX = point.clientX; | ||
context.startY = point.clientY; | ||
this.dispatcher.dispatch("start", { | ||
clientX: point.clientX, | ||
clientY: point.clientY | ||
}); | ||
context.points = [{ | ||
t: Date.now(), | ||
x: point.clientX, | ||
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); | ||
} | ||
move(point, context) { | ||
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(context.handler); | ||
this.dispatcher.dispatch("panStart", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical | ||
}); | ||
} | ||
if (context.isPan) { | ||
this.dispatcher.dispatch("pan", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical | ||
}); | ||
} | ||
context.points = context.points.filter((point2) => Date.now() - point2.t < 500); | ||
context.points.push({ | ||
t: Date.now(), | ||
x: point.clientX, | ||
y: point.clientY | ||
}); | ||
} | ||
end(point, context) { | ||
if (context.isTap) { | ||
this.dispatcher.dispatch("tap", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY | ||
}); | ||
clearTimeout(context.handler); | ||
} | ||
if (context.isPress) | ||
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); | ||
} | ||
if (v > 1.5) { | ||
context.isFlick = true; | ||
this.dispatcher.dispatch("flick", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical, | ||
isFlick: context.isFlick, | ||
velocity: v | ||
}); | ||
} else { | ||
context.isFlick = false; | ||
} | ||
if (context.isPan) { | ||
this.dispatcher.dispatch("panEnd", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical, | ||
isFlick: context.isFlick, | ||
velocity: v | ||
}); | ||
} | ||
this.dispatcher.dispatch("end", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical, | ||
isFlick: context.isFlick, | ||
isPan: context.isPan, | ||
velocity: v | ||
}); | ||
} | ||
} | ||
class Dispatcher { | ||
constructor(element) { | ||
this.element = element; | ||
} | ||
dispatch(type, properties) { | ||
const event = new Event(type); | ||
for (const name in properties) | ||
event[name] = properties[name]; | ||
this.element.dispatchEvent(event); | ||
} | ||
} | ||
function enableGesture(element) { | ||
new Listener(element, new Recognizer(new Dispatcher(element))); | ||
} | ||
export { Dispatcher, Listener, Recognizer, enableGesture }; |
@@ -1,1 +0,224 @@ | ||
var Gesture=function(t){"use strict";var e=Object.defineProperty,s=Math.pow,i=(t,s,i)=>(((t,s,i)=>{s in t?e(t,s,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[s]=i})(t,"symbol"!=typeof s?s+"":s,i),i);class n{constructor(t,e){let s=!1;const i=new Map,n=document.documentElement;t.addEventListener("mousedown",(t=>{const c=Object.create(null);i.set("mouse"+(1<<t.button),c),e.start(t,c);const a=t=>{let s=1;for(;s<=t.buttons;){if(s&t.buttons){let n;n=2===s?4:4===s?2:s;const c=i.get(`mouse${n}`);e.move(t,c)}s<<=1}},r=t=>{const c=i.get("mouse"+(1<<t.button));e.end(t,c),i.delete("mouse"+(1<<t.button)),0===t.buttons&&(n.removeEventListener("mousemove",a),n.removeEventListener("mouseup",r),s=!1)};s||(n.addEventListener("mousemove",a),n.addEventListener("mouseup",r),s=!0)})),t.addEventListener("touchstart",(t=>{for(const s of t.changedTouches){const t=Object.create(null);i.set(s.identifier,t),e.start(s,t)}})),t.addEventListener("touchmove",(t=>{for(const s of t.changedTouches){const n=i.get(s.identifier);t.preventDefault(),e.move(s,n)}})),t.addEventListener("touchend",(t=>{for(const s of t.changedTouches){const t=i.get(s.identifier);e.end(s,t),i.delete(s.identifier)}})),t.addEventListener("touchcancel",(t=>{for(const s of t.changedTouches){const t=i.get(s.identifier);e.cancel(s,t),i.delete(s.identifier)}}))}}class c{constructor(t){i(this,"cancel",((t,e)=>{clearTimeout(e.handler),this.dispatcher.dispatch("cancel",{})})),this.dispatcher=t}start(t,e){e.startX=t.clientX,e.startY=t.clientY,this.dispatcher.dispatch("start",{clientX:t.clientX,clientY:t.clientY}),e.points=[{t:Date.now(),x:t.clientX,y:t.clientY}],e.isTap=!0,e.isPan=!1,e.isPress=!1,e.handler=setTimeout((()=>{e.isTap=!1,e.isPan=!1,e.isPress=!0,e.handler=null,this.dispatcher.dispatch("press",{})}),500)}move(t,e){const i=t.clientX-e.startX,n=t.clientY-e.startY;!e.isPan&&s(i,2)+s(n,2)>100&&(e.isPan=!0,e.isTap=!1,e.isPress=!1,e.isVertical=Math.abs(i)-Math.abs(n)>=0,clearTimeout(e.handler),this.dispatcher.dispatch("panStart",{startX:e.startX,startY:e.startY,clientX:t.clientX,clientY:t.clientY,isVertical:e.isVertical})),e.isPan&&this.dispatcher.dispatch("pan",{startX:e.startX,startY:e.startY,clientX:t.clientX,clientY:t.clientY,isVertical:e.isVertical}),e.points=e.points.filter((t=>Date.now()-t.t<500)),e.points.push({t:Date.now(),x:t.clientX,y:t.clientY})}end(t,e){e.isTap&&(this.dispatcher.dispatch("tap",{startX:e.startX,startY:e.startY,clientX:t.clientX,clientY:t.clientY}),clearTimeout(e.handler)),e.isPress&&this.dispatcher.dispatch("pressEnd",{}),e.points=e.points.filter((t=>Date.now()-t.t<500));let i=0;if(e.points.length){i=Math.sqrt(s(t.clientX-e.points[0].x,2)+s(t.clientY-e.points[0].y,2))/(Date.now()-e.points[0].t)}i>1.5?(e.isFlick=!0,this.dispatcher.dispatch("flick",{startX:e.startX,startY:e.startY,clientX:t.clientX,clientY:t.clientY,isVertical:e.isVertical,isFlick:e.isFlick,velocity:i})):e.isFlick=!1,e.isPan&&this.dispatcher.dispatch("panEnd",{startX:e.startX,startY:e.startY,clientX:t.clientX,clientY:t.clientY,isVertical:e.isVertical,isFlick:e.isFlick,velocity:i}),this.dispatcher.dispatch("end",{startX:e.startX,startY:e.startY,clientX:t.clientX,clientY:t.clientY,isVertical:e.isVertical,isFlick:e.isFlick,isPan:e.isPan,velocity:i})}}class a{constructor(t){this.element=t}dispatch(t,e){const s=new Event(t);for(const i in e)s[i]=e[i];this.element.dispatchEvent(s)}}return t.Dispatcher=a,t.Listener=n,t.Recognizer=c,t.enableGesture=function(t){new n(t,new c(new a(t)))},t}({}); | ||
var 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; | ||
}; | ||
class Listener { | ||
constructor(element, recognizer) { | ||
let isListeningMouse = false; | ||
const contexts = /* @__PURE__ */ new Map(); | ||
const el = document.documentElement; | ||
element.addEventListener("mousedown", (event) => { | ||
const context = /* @__PURE__ */ Object.create(null); | ||
contexts.set(`mouse${1 << event.button}`, context); | ||
recognizer.start(event, context); | ||
const mouseMove = (event2) => { | ||
let button = 1; | ||
while (button <= event2.buttons) { | ||
if (button & event2.buttons) { | ||
let key; | ||
if (button === 2) | ||
key = 4; | ||
else if (button === 4) | ||
key = 2; | ||
else | ||
key = button; | ||
const context2 = contexts.get(`mouse${key}`); | ||
recognizer.move(event2, context2); | ||
} | ||
button = button << 1; | ||
} | ||
}; | ||
const mouseup = (event2) => { | ||
const context2 = contexts.get(`mouse${1 << event2.button}`); | ||
recognizer.end(event2, context2); | ||
contexts.delete(`mouse${1 << event2.button}`); | ||
if (event2.buttons === 0) { | ||
el.removeEventListener("mousemove", mouseMove); | ||
el.removeEventListener("mouseup", mouseup); | ||
isListeningMouse = false; | ||
} | ||
}; | ||
if (!isListeningMouse) { | ||
el.addEventListener("mousemove", mouseMove); | ||
el.addEventListener("mouseup", mouseup); | ||
isListeningMouse = true; | ||
} | ||
}); | ||
element.addEventListener("touchstart", (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = /* @__PURE__ */ Object.create(null); | ||
contexts.set(touch.identifier, context); | ||
recognizer.start(touch, context); | ||
} | ||
}); | ||
element.addEventListener("touchmove", (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = contexts.get(touch.identifier); | ||
event.preventDefault(); | ||
recognizer.move(touch, context); | ||
} | ||
}); | ||
element.addEventListener("touchend", (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = contexts.get(touch.identifier); | ||
recognizer.end(touch, context); | ||
contexts.delete(touch.identifier); | ||
} | ||
}); | ||
element.addEventListener("touchcancel", (event) => { | ||
for (const touch of event.changedTouches) { | ||
const context = contexts.get(touch.identifier); | ||
recognizer.cancel(touch, context); | ||
contexts.delete(touch.identifier); | ||
} | ||
}); | ||
} | ||
} | ||
class Recognizer { | ||
constructor(dispatcher) { | ||
__publicField(this, "cancel", (point, context) => { | ||
clearTimeout(context.handler); | ||
this.dispatcher.dispatch("cancel", {}); | ||
}); | ||
this.dispatcher = dispatcher; | ||
} | ||
start(point, context) { | ||
context.startX = point.clientX; | ||
context.startY = point.clientY; | ||
this.dispatcher.dispatch("start", { | ||
clientX: point.clientX, | ||
clientY: point.clientY | ||
}); | ||
context.points = [{ | ||
t: Date.now(), | ||
x: point.clientX, | ||
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); | ||
} | ||
move(point, context) { | ||
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(context.handler); | ||
this.dispatcher.dispatch("panStart", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical | ||
}); | ||
} | ||
if (context.isPan) { | ||
this.dispatcher.dispatch("pan", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical | ||
}); | ||
} | ||
context.points = context.points.filter((point2) => Date.now() - point2.t < 500); | ||
context.points.push({ | ||
t: Date.now(), | ||
x: point.clientX, | ||
y: point.clientY | ||
}); | ||
} | ||
end(point, context) { | ||
if (context.isTap) { | ||
this.dispatcher.dispatch("tap", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY | ||
}); | ||
clearTimeout(context.handler); | ||
} | ||
if (context.isPress) | ||
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); | ||
} | ||
if (v > 1.5) { | ||
context.isFlick = true; | ||
this.dispatcher.dispatch("flick", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical, | ||
isFlick: context.isFlick, | ||
velocity: v | ||
}); | ||
} else { | ||
context.isFlick = false; | ||
} | ||
if (context.isPan) { | ||
this.dispatcher.dispatch("panEnd", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical, | ||
isFlick: context.isFlick, | ||
velocity: v | ||
}); | ||
} | ||
this.dispatcher.dispatch("end", { | ||
startX: context.startX, | ||
startY: context.startY, | ||
clientX: point.clientX, | ||
clientY: point.clientY, | ||
isVertical: context.isVertical, | ||
isFlick: context.isFlick, | ||
isPan: context.isPan, | ||
velocity: v | ||
}); | ||
} | ||
} | ||
class Dispatcher { | ||
constructor(element) { | ||
this.element = element; | ||
} | ||
dispatch(type, properties) { | ||
const event = new Event(type); | ||
for (const name in properties) | ||
event[name] = properties[name]; | ||
this.element.dispatchEvent(event); | ||
} | ||
} | ||
function enableGesture(element) { | ||
new Listener(element, new Recognizer(new Dispatcher(element))); | ||
} | ||
exports.Dispatcher = Dispatcher; | ||
exports.Listener = Listener; | ||
exports.Recognizer = Recognizer; | ||
exports.enableGesture = enableGesture; | ||
return exports; | ||
})({}); |
{ | ||
"name": "@ajiu9/gesture", | ||
"version": "1.0.3-beta.8", | ||
"version": "1.0.3-beta.11", | ||
"description": "", | ||
@@ -17,4 +17,4 @@ "author": "ajiu9 <615944323@qq.com> (https://github.com/ajiu9/)", | ||
], | ||
"sideEffects": false, | ||
"main": "index.js", | ||
"module": "dist/gesture.esm-bundler.js", | ||
"files": [ | ||
@@ -21,0 +21,0 @@ "index.js", |
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
22875
652
1