Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
package.lib.composables.touch.mjs.map Maven / Gradle / Ivy
{"version":3,"file":"touch.mjs","names":["CircularBuffer","HORIZON","HISTORY","kineticEnergyToVelocity","work","sqrt2","Math","sqrt","abs","calculateImpulseVelocity","samples","length","t","d","i","vprev","vcurr","useVelocity","touches","addMovement","e","Array","from","changedTouches","forEach","touch","identifier","push","timeStamp","endTouch","getVelocity","id","values","reverse","Error","newest","x","y","val","clientX","clientY","direction","absX","absY","oops"],"sources":["../../src/composables/touch.ts"],"sourcesContent":["// Utilities\nimport { CircularBuffer } from '@/util'\n\nconst HORIZON = 100 // ms\nconst HISTORY = 20 // number of samples to keep\n\nexport interface Sample {\n t: number\n d: number\n}\n\n/** @see https://android.googlesource.com/platform/frameworks/native/+/master/libs/input/VelocityTracker.cpp */\nfunction kineticEnergyToVelocity (work: number) {\n const sqrt2 = 1.41421356237\n return (work < 0 ? -1.0 : 1.0) * Math.sqrt(Math.abs(work)) * sqrt2\n}\n\n/**\n * Returns pointer velocity in px/s\n */\nexport function calculateImpulseVelocity (samples: Sample[]) {\n // The input should be in reversed time order (most recent sample at index i=0)\n if (samples.length < 2) {\n // if 0 or 1 points, velocity is zero\n return 0\n }\n // if (samples[1].t > samples[0].t) {\n // // Algorithm will still work, but not perfectly\n // consoleWarn('Samples provided to calculateImpulseVelocity in the wrong order')\n // }\n if (samples.length === 2) {\n // if 2 points, basic linear calculation\n if (samples[1].t === samples[0].t) {\n // consoleWarn(`Events have identical time stamps t=${samples[0].t}, setting velocity = 0`)\n return 0\n }\n return (samples[1].d - samples[0].d) / (samples[1].t - samples[0].t)\n }\n // Guaranteed to have at least 3 points here\n // start with the oldest sample and go forward in time\n let work = 0\n for (let i = samples.length - 1; i > 0; i--) {\n if (samples[i].t === samples[i - 1].t) {\n // consoleWarn(`Events have identical time stamps t=${samples[i].t}, skipping sample`)\n continue\n }\n const vprev = kineticEnergyToVelocity(work) // v[i-1]\n const vcurr = (samples[i].d - samples[i - 1].d) / (samples[i].t - samples[i - 1].t) // v[i]\n work += (vcurr - vprev) * Math.abs(vcurr)\n if (i === samples.length - 1) {\n work *= 0.5\n }\n }\n return kineticEnergyToVelocity(work) * 1000\n}\n\nexport function useVelocity () {\n const touches: Record | undefined> = {}\n\n function addMovement (e: TouchEvent) {\n Array.from(e.changedTouches).forEach(touch => {\n const samples = touches[touch.identifier] ?? (touches[touch.identifier] = new CircularBuffer(HISTORY))\n samples.push([e.timeStamp, touch])\n })\n }\n\n function endTouch (e: TouchEvent) {\n Array.from(e.changedTouches).forEach(touch => {\n delete touches[touch.identifier]\n })\n }\n\n function getVelocity (id: number) {\n const samples = touches[id]?.values().reverse()\n\n if (!samples) {\n throw new Error(`No samples for touch id ${id}`)\n }\n\n const newest = samples[0]\n const x: Sample[] = []\n const y: Sample[] = []\n for (const val of samples) {\n if (newest[0] - val[0] > HORIZON) break\n\n x.push({ t: val[0], d: val[1].clientX })\n y.push({ t: val[0], d: val[1].clientY })\n }\n\n return {\n x: calculateImpulseVelocity(x),\n y: calculateImpulseVelocity(y),\n get direction () {\n const { x, y } = this\n const [absX, absY] = [Math.abs(x), Math.abs(y)]\n\n return absX > absY && x >= 0 ? 'right'\n : absX > absY && x <= 0 ? 'left'\n : absY > absX && y >= 0 ? 'down'\n : absY > absX && y <= 0 ? 'up'\n : oops()\n },\n }\n }\n\n return { addMovement, endTouch, getVelocity }\n}\n\nfunction oops (): never {\n throw new Error()\n}\n"],"mappings":"AAAA;AAAA,SACSA,cAAc;AAEvB,MAAMC,OAAO,GAAG,GAAG,EAAC;AACpB,MAAMC,OAAO,GAAG,EAAE,EAAC;;AAOnB;AACA,SAASC,uBAAuBA,CAAEC,IAAY,EAAE;EAC9C,MAAMC,KAAK,GAAG,aAAa;EAC3B,OAAO,CAACD,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,IAAIE,IAA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