@srsergio/taptapp-ar 1.0.95 → 1.0.97
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/README.md +53 -151
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/react/TaptappAR.js +20 -6
- package/dist/react/types.d.ts +1 -1
- package/dist/react/use-ar.d.ts +2 -1
- package/dist/react/use-ar.js +52 -45
- package/dist/runtime/index.d.ts +2 -1
- package/dist/runtime/index.js +2 -1
- package/dist/runtime/track.d.ts +170 -0
- package/dist/runtime/track.js +381 -0
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/react/TaptappAR.tsx +31 -6
- package/src/react/types.ts +1 -1
- package/src/react/use-ar.ts +59 -49
- package/src/runtime/index.ts +2 -1
- package/src/runtime/track.ts +623 -0
- package/dist/runtime/simple-ar.d.ts +0 -86
- package/dist/runtime/simple-ar.js +0 -323
- package/src/runtime/simple-ar.ts +0 -399
package/src/react/use-ar.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { useEffect, useRef, useState, useCallback } from "react";
|
|
2
2
|
import type { ARConfig } from "./types.js";
|
|
3
|
-
import type {
|
|
3
|
+
import type { Tracker } from "../runtime/track.js";
|
|
4
4
|
|
|
5
|
-
export type ARStatus = "scanning" | "tracking" | "error";
|
|
5
|
+
export type ARStatus = "compiling" | "scanning" | "tracking" | "error";
|
|
6
6
|
|
|
7
7
|
export interface TrackedPoint {
|
|
8
8
|
x: number;
|
|
@@ -18,6 +18,7 @@ export interface UseARReturn {
|
|
|
18
18
|
isPlaying: boolean;
|
|
19
19
|
toggleVideo: () => Promise<void>;
|
|
20
20
|
trackedPoints: TrackedPoint[];
|
|
21
|
+
error: string | null;
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
export const useAR = (config: ARConfig): UseARReturn => {
|
|
@@ -26,7 +27,8 @@ export const useAR = (config: ARConfig): UseARReturn => {
|
|
|
26
27
|
const [status, setStatus] = useState<ARStatus>("scanning");
|
|
27
28
|
const [isPlaying, setIsPlaying] = useState(false);
|
|
28
29
|
const [trackedPoints, setTrackedPoints] = useState<TrackedPoint[]>([]);
|
|
29
|
-
const
|
|
30
|
+
const [error, setError] = useState<string | null>(null);
|
|
31
|
+
const arInstanceRef = useRef<Tracker | null>(null);
|
|
30
32
|
|
|
31
33
|
const toggleVideo = useCallback(async () => {
|
|
32
34
|
const overlay = overlayRef.current;
|
|
@@ -53,63 +55,70 @@ export const useAR = (config: ARConfig): UseARReturn => {
|
|
|
53
55
|
const initAR = async () => {
|
|
54
56
|
try {
|
|
55
57
|
// Safe hybrid import for SSR + Speed
|
|
56
|
-
const {
|
|
58
|
+
const { createTracker } = await import("../runtime/track.js");
|
|
57
59
|
if (!isMounted) return;
|
|
58
60
|
|
|
59
|
-
|
|
61
|
+
setStatus("compiling");
|
|
62
|
+
|
|
63
|
+
const instance = await createTracker({
|
|
60
64
|
container: containerRef.current!,
|
|
61
|
-
targetSrc: config.targetTaarSrc,
|
|
65
|
+
targetSrc: config.targetTaarSrc || config.targetImageSrc,
|
|
62
66
|
overlay: overlayRef.current!,
|
|
63
67
|
scale: config.scale,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
68
|
+
debugMode: false,
|
|
69
|
+
callbacks: {
|
|
70
|
+
onUpdate: (data) => {
|
|
71
|
+
const { screenCoords, reliabilities, stabilities } = data;
|
|
72
|
+
if (screenCoords && reliabilities && stabilities) {
|
|
73
|
+
const points = screenCoords.map((p, i) => ({
|
|
74
|
+
x: p.x,
|
|
75
|
+
y: p.y,
|
|
76
|
+
reliability: reliabilities[i],
|
|
77
|
+
stability: stabilities[i]
|
|
78
|
+
}));
|
|
79
|
+
setTrackedPoints(points);
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
onFound: async ({ targetIndex }) => {
|
|
83
|
+
console.log(`🎯 Target ${targetIndex} detected!`);
|
|
84
|
+
if (!isMounted) return;
|
|
85
|
+
setStatus("tracking");
|
|
86
|
+
|
|
87
|
+
const overlay = overlayRef.current;
|
|
88
|
+
if (overlay instanceof HTMLVideoElement) {
|
|
89
|
+
try {
|
|
90
|
+
await overlay.play();
|
|
91
|
+
setIsPlaying(true);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
console.warn("Auto-play blocked:", err);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
onLost: ({ targetIndex }) => {
|
|
98
|
+
console.log(`👋 Target ${targetIndex} lost`);
|
|
99
|
+
if (!isMounted) return;
|
|
100
|
+
setStatus("scanning");
|
|
101
|
+
setTrackedPoints([]);
|
|
102
|
+
|
|
103
|
+
const overlay = overlayRef.current;
|
|
104
|
+
if (overlay instanceof HTMLVideoElement) {
|
|
105
|
+
overlay.pause();
|
|
106
|
+
setIsPlaying(false);
|
|
89
107
|
}
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
onLost: ({ targetIndex }) => {
|
|
93
|
-
console.log(`👋 Target ${targetIndex} lost`);
|
|
94
|
-
if (!isMounted) return;
|
|
95
|
-
setStatus("scanning");
|
|
96
|
-
setTrackedPoints([]);
|
|
97
|
-
|
|
98
|
-
const overlay = overlayRef.current;
|
|
99
|
-
if (overlay instanceof HTMLVideoElement) {
|
|
100
|
-
overlay.pause();
|
|
101
|
-
setIsPlaying(false);
|
|
102
108
|
}
|
|
103
109
|
}
|
|
104
110
|
});
|
|
105
111
|
|
|
106
112
|
arInstanceRef.current = instance;
|
|
107
|
-
await instance.
|
|
113
|
+
await instance.startCamera();
|
|
108
114
|
|
|
109
115
|
if (isMounted) setStatus("scanning");
|
|
110
|
-
} catch (err) {
|
|
111
|
-
console.error("
|
|
112
|
-
if (isMounted)
|
|
116
|
+
} catch (err: any) {
|
|
117
|
+
console.error("❌ [TapTapp AR] Error durante la inicialización:", err);
|
|
118
|
+
if (isMounted) {
|
|
119
|
+
setError(err.message || String(err));
|
|
120
|
+
setStatus("error");
|
|
121
|
+
}
|
|
113
122
|
}
|
|
114
123
|
};
|
|
115
124
|
|
|
@@ -120,7 +129,7 @@ export const useAR = (config: ARConfig): UseARReturn => {
|
|
|
120
129
|
arInstanceRef.current?.stop();
|
|
121
130
|
arInstanceRef.current = null;
|
|
122
131
|
};
|
|
123
|
-
}, [config.targetTaarSrc, config.scale]);
|
|
132
|
+
}, [config.targetTaarSrc, config.targetImageSrc, config.scale]);
|
|
124
133
|
|
|
125
134
|
return {
|
|
126
135
|
containerRef,
|
|
@@ -128,6 +137,7 @@ export const useAR = (config: ARConfig): UseARReturn => {
|
|
|
128
137
|
status,
|
|
129
138
|
isPlaying,
|
|
130
139
|
toggleVideo,
|
|
131
|
-
trackedPoints
|
|
140
|
+
trackedPoints,
|
|
141
|
+
error
|
|
132
142
|
};
|
|
133
143
|
};
|
package/src/runtime/index.ts
CHANGED