@supermousejs/pointer 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @supermousejs/pointer
2
+
3
+ ## 2.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - Initial v2.0.0 release
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies
12
+ - @supermousejs/utils@2.0.0
13
+ - @supermousejs/core@2.0.0
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Sijibomi Olusunmbola
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,19 @@
1
+ import { SupermousePlugin } from '../../core/src/index.ts';
2
+ import { ValueOrGetter } from '../../core/src/index.ts';
3
+
4
+ export declare const Pointer: (options?: PointerOptions) => SupermousePlugin;
5
+
6
+ export declare interface PointerOptions {
7
+ name?: string;
8
+ isEnabled?: boolean;
9
+ size?: ValueOrGetter<number>;
10
+ color?: ValueOrGetter<string>;
11
+ svg?: string;
12
+ rotationSmoothing?: number;
13
+ restingAngle?: ValueOrGetter<number>;
14
+ returnToRest?: ValueOrGetter<boolean>;
15
+ restDelay?: ValueOrGetter<number>;
16
+ opacity?: ValueOrGetter<number>;
17
+ }
18
+
19
+ export { }
package/dist/index.mjs ADDED
@@ -0,0 +1,35 @@
1
+ import { normalize as o, definePlugin as M, dom as r, math as u, Layers as P } from "@supermousejs/utils";
2
+ const U = `
3
+ <svg viewBox="0 0 100 100" fill="currentColor" style="display: block; width: 100%; height: 100%;">
4
+ <path d="M10 20 L90 50 L10 80 L25 50 Z" />
5
+ </svg>
6
+ `, B = (s = {}) => {
7
+ const f = s.rotationSmoothing || 0.15, h = s.svg || U, S = o(s.size, 32), y = o(s.restingAngle, -45), v = o(s.returnToRest, !0), x = o(s.restDelay, 200), z = o(s.opacity, 1);
8
+ let c = 0, l = 0, a = 0;
9
+ return M({
10
+ name: "pointer",
11
+ create: (t) => {
12
+ const e = r.createActor("div");
13
+ e.style.zIndex = P.CURSOR, e.style.transformOrigin = "center center";
14
+ const n = y(t.state);
15
+ return c = n, l = n, e.innerHTML = h, e;
16
+ },
17
+ styles: {
18
+ color: "color"
19
+ },
20
+ update: (t, e) => {
21
+ const n = S(t.state), A = y(t.state), d = v(t.state), m = x(t.state), T = z(t.state);
22
+ r.setStyle(e, "width", `${n}px`), r.setStyle(e, "height", `${n}px`), r.setStyle(e, "opacity", String(T));
23
+ const { x: L, y: w } = t.state.velocity, R = u.dist(L, w), g = performance.now();
24
+ let i = l;
25
+ R > 1 ? (i = t.state.angle, l = i, a = g) : d && g - a > m && (i = A);
26
+ const D = R <= 1 && d && g - a > m ? 0.05 : f;
27
+ c = u.lerpAngle(c, i, D);
28
+ const { x: C, y: O } = t.state.smooth;
29
+ r.setTransform(e, C, O, c);
30
+ }
31
+ }, s);
32
+ };
33
+ export {
34
+ B as Pointer
35
+ };
@@ -0,0 +1,5 @@
1
+ (function(s,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("@supermousejs/utils")):typeof define=="function"&&define.amd?define(["exports","@supermousejs/utils"],e):(s=typeof globalThis<"u"?globalThis:s||self,e(s.SupermousePointer={},s.SupermouseUtils))})(this,(function(s,e){"use strict";const u=`
2
+ <svg viewBox="0 0 100 100" fill="currentColor" style="display: block; width: 100%; height: 100%;">
3
+ <path d="M10 20 L90 50 L10 80 L25 50 Z" />
4
+ </svg>
5
+ `,h=(o={})=>{const S=o.rotationSmoothing||.15,R=o.svg||u,z=e.normalize(o.size,32),l=e.normalize(o.restingAngle,-45),v=e.normalize(o.returnToRest,!0),T=e.normalize(o.restDelay,200),x=e.normalize(o.opacity,1);let i=0,a=0,g=0;return e.definePlugin({name:"pointer",create:t=>{const n=e.dom.createActor("div");n.style.zIndex=e.Layers.CURSOR,n.style.transformOrigin="center center";const r=l(t.state);return i=r,a=r,n.innerHTML=R,n},styles:{color:"color"},update:(t,n)=>{const r=z(t.state),A=l(t.state),m=v(t.state),y=T(t.state),L=x(t.state);e.dom.setStyle(n,"width",`${r}px`),e.dom.setStyle(n,"height",`${r}px`),e.dom.setStyle(n,"opacity",String(L));const{x:w,y:P}=t.state.velocity,f=e.math.dist(w,P),d=performance.now();let c=a;f>1?(c=t.state.angle,a=c,g=d):m&&d-g>y&&(c=A);const j=f<=1&&m&&d-g>y?.05:S;i=e.math.lerpAngle(i,c,j);const{x:p,y:D}=t.state.smooth;e.dom.setTransform(n,p,D,i)}},o)};s.Pointer=h,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}));
package/meta.json ADDED
@@ -0,0 +1,20 @@
1
+
2
+ {
3
+ "id": "pointer",
4
+ "name": "Pointer",
5
+ "package": "@supermousejs/pointer",
6
+ "description": "A 'Vehicle' style arrow that rotates based on velocity vectors.",
7
+ "code": "app.use(Pointer({ size: 32, color: '#000' }));",
8
+ "recipeId": "vehicle-pointer",
9
+ "icon": "pointer",
10
+ "options": [
11
+ { "name": "size", "type": "number", "default": "32", "description": "Size of the pointer in pixels.", "reactive": true },
12
+ { "name": "color", "type": "string", "default": "'black'", "description": "Fill color of the SVG.", "reactive": true },
13
+ { "name": "svg", "type": "string", "default": "Default Arrow", "description": "Custom SVG content string.", "reactive": false },
14
+ { "name": "restingAngle", "type": "number", "default": "-45", "description": "Angle when stationary (degrees).", "reactive": true },
15
+ { "name": "returnToRest", "type": "boolean", "default": "true", "description": "Snap back to resting angle when stopped.", "reactive": true },
16
+ { "name": "restDelay", "type": "number", "default": "200", "description": "Milliseconds to wait before returning to rest.", "reactive": true },
17
+ { "name": "opacity", "type": "number", "default": "1", "description": "Opacity level (0-1).", "reactive": true },
18
+ { "name": "rotationSmoothing", "type": "number", "default": "0.15", "description": "Lerp factor for rotation.", "reactive": false }
19
+ ]
20
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@supermousejs/pointer",
3
+ "version": "2.0.0",
4
+ "main": "dist/index.umd.js",
5
+ "module": "dist/index.mjs",
6
+ "types": "dist/index.d.ts",
7
+ "dependencies": {
8
+ "@supermousejs/core": "2.0.0",
9
+ "@supermousejs/utils": "2.0.0"
10
+ },
11
+ "devDependencies": {},
12
+ "peerDependencies": {},
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.mjs",
17
+ "require": "./dist/index.umd.js"
18
+ }
19
+ },
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "scripts": {
24
+ "build": "vite build"
25
+ }
26
+ }
package/src/index.ts ADDED
@@ -0,0 +1,96 @@
1
+ import type { ValueOrGetter } from '@supermousejs/core';
2
+ import { definePlugin, normalize, dom, math, Layers } from '@supermousejs/utils';
3
+
4
+ export interface PointerOptions {
5
+ name?: string;
6
+ isEnabled?: boolean;
7
+ size?: ValueOrGetter<number>;
8
+ color?: ValueOrGetter<string>;
9
+ svg?: string;
10
+ rotationSmoothing?: number;
11
+ restingAngle?: ValueOrGetter<number>;
12
+ returnToRest?: ValueOrGetter<boolean>;
13
+ restDelay?: ValueOrGetter<number>;
14
+ opacity?: ValueOrGetter<number>;
15
+ }
16
+
17
+ const DEFAULT_SVG = `
18
+ <svg viewBox="0 0 100 100" fill="currentColor" style="display: block; width: 100%; height: 100%;">
19
+ <path d="M10 20 L90 50 L10 80 L25 50 Z" />
20
+ </svg>
21
+ `;
22
+
23
+ export const Pointer = (options: PointerOptions = {}) => {
24
+ const defSize = 32;
25
+ const smoothing = options.rotationSmoothing || 0.15;
26
+ const svgContent = options.svg || DEFAULT_SVG;
27
+
28
+ const getSize = normalize(options.size, defSize);
29
+ const getRestingAngle = normalize(options.restingAngle, -45);
30
+ const getReturnToRest = normalize(options.returnToRest, true);
31
+ const getRestDelay = normalize(options.restDelay, 200);
32
+ const getOpacity = normalize(options.opacity, 1);
33
+
34
+ let currentRotation = 0;
35
+ let lastRotation = 0;
36
+ let stopTime = 0;
37
+
38
+ return definePlugin<HTMLDivElement, PointerOptions>({
39
+ name: 'pointer',
40
+
41
+ create: (app) => {
42
+ const el = dom.createActor('div') as HTMLDivElement;
43
+ el.style.zIndex = Layers.CURSOR;
44
+ el.style.transformOrigin = 'center center';
45
+
46
+ const restAngle = getRestingAngle(app.state);
47
+ currentRotation = restAngle;
48
+ lastRotation = restAngle;
49
+
50
+ el.innerHTML = svgContent;
51
+
52
+ return el;
53
+ },
54
+
55
+ styles: {
56
+ color: 'color'
57
+ },
58
+
59
+ update: (app, el) => {
60
+ const size = getSize(app.state);
61
+ const restingAngle = getRestingAngle(app.state);
62
+ const returnToRest = getReturnToRest(app.state);
63
+ const restDelay = getRestDelay(app.state);
64
+ const opacity = getOpacity(app.state);
65
+
66
+ dom.setStyle(el, 'width', `${size}px`);
67
+ dom.setStyle(el, 'height', `${size}px`);
68
+ dom.setStyle(el, 'opacity', String(opacity));
69
+
70
+ const { x: vx, y: vy } = app.state.velocity;
71
+ const speed = math.dist(vx, vy);
72
+ const now = performance.now();
73
+
74
+ let targetRotation = lastRotation;
75
+
76
+ if (speed > 1) {
77
+ targetRotation = app.state.angle;
78
+ lastRotation = targetRotation;
79
+ stopTime = now;
80
+ } else {
81
+ if (returnToRest && (now - stopTime > restDelay)) {
82
+ targetRotation = restingAngle;
83
+ }
84
+ }
85
+
86
+ // Use core interpolation with shortest-path logic
87
+ const isReturning = speed <= 1 && returnToRest && (now - stopTime > restDelay);
88
+ const factor = isReturning ? 0.05 : smoothing;
89
+
90
+ currentRotation = math.lerpAngle(currentRotation, targetRotation, factor);
91
+
92
+ const { x, y } = app.state.smooth;
93
+ dom.setTransform(el, x, y, currentRotation);
94
+ }
95
+ }, options);
96
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "include": [
4
+ "src"
5
+ ],
6
+ "compilerOptions": {
7
+ "outDir": "dist",
8
+ "baseUrl": ".",
9
+ "paths": {
10
+ "@supermousejs/core": [
11
+ "../core/src/index.ts"
12
+ ],
13
+ "@supermousejs/utils": [
14
+ "../utils/src/index.ts"
15
+ ]
16
+ }
17
+ }
18
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { defineConfig } from 'vite';
2
+ import dts from 'vite-plugin-dts';
3
+ import path from 'path';
4
+
5
+ export default defineConfig({
6
+ build: {
7
+ lib: {
8
+ entry: path.resolve(__dirname, 'src/index.ts'),
9
+ name: 'SupermousePointer',
10
+ fileName: (format) => format === 'es' ? 'index.mjs' : 'index.umd.js',
11
+ },
12
+ rollupOptions: {
13
+ external: ['@supermousejs/core', '@supermousejs/utils'],
14
+ output: {
15
+ globals: {
16
+ '@supermousejs/core': 'SupermouseCore'
17
+ , '@supermousejs/utils': 'SupermouseUtils'}
18
+ }
19
+ }
20
+ },
21
+ plugins: [dts({ rollupTypes: true })]
22
+ });