11// Example: AR.js Core ECS + ArtoolkitPlugin with Start/Stop/Load buttons
2+ // Import everything from the bundled lib.
23
3- import { Engine } from '../../src/core/engine.js' ;
4- import { CaptureSystem } from '../../src/systems/capture-system.js' ;
5- import { SOURCE_TYPES } from '../../src/core/components.js' ;
6- import { FramePumpSystem } from '../../src/systems/frame-pump-system.js' ;
7-
8- import { webcamPlugin } from '../../plugins/source/webcam.js' ;
9- import { defaultProfilePlugin } from '../../plugins/profile/default-policy.js' ;
10- import { imagePlugin as artookit } from '../../plugins ' ;
4+ import {
5+ Engine ,
6+ CaptureSystem ,
7+ FramePumpSystem ,
8+ SOURCE_TYPES ,
9+ webcamPlugin ,
10+ defaultProfilePlugin ,
11+ } from '../../dist/arjs-core.mjs ' ;
1112
1213// UI
1314const statusEl = document . getElementById ( 'status' ) ;
@@ -26,21 +27,18 @@ function attachVideoToViewport(ctx) {
2627 if ( ! viewport ) return ;
2728
2829 try {
29- // Detach from body if plugin appended it
3030 if ( videoEl . parentNode && videoEl . parentNode !== viewport ) {
3131 videoEl . parentNode . removeChild ( videoEl ) ;
3232 }
3333 } catch { }
3434
35- // Ensure attributes optimal for inline playback
3635 try {
3736 videoEl . setAttribute ( 'playsinline' , '' ) ;
3837 videoEl . setAttribute ( 'autoplay' , '' ) ;
3938 videoEl . muted = true ;
4039 videoEl . controls = false ;
4140 } catch { }
4241
43- // Override any offscreen styles the source plugin applied
4442 Object . assign ( videoEl . style , {
4543 position : 'relative' ,
4644 top : '0px' ,
@@ -81,19 +79,19 @@ let cameraStarted = false;
8179async function bootstrap ( ) {
8280 engine = new Engine ( ) ;
8381
84- // Register core/source plugins
82+ // Register core/source plugins from the bundled lib
8583 engine . pluginManager . register ( defaultProfilePlugin . id , defaultProfilePlugin ) ;
8684 engine . pluginManager . register ( webcamPlugin . id , webcamPlugin ) ;
8785
88- // Load ArtoolkitPlugin ESM from bundled vendor (ensure assets folder is present and served )
86+ // Load ArtoolkitPlugin ESM (local vendor or CDN )
8987 const mod = await import ( './vendor/arjs-plugin-artoolkit/arjs-plugin-artoolkit.esm.js' ) ;
9088 const ArtoolkitPlugin = mod . ArtoolkitPlugin || mod . default ;
9189
9290 // Set up UI listeners BEFORE enable to avoid missing early 'ready'
9391 engine . eventBus . on ( 'ar:workerReady' , ( ) => {
9492 log ( 'Worker ready' ) ;
9593 setStatus ( 'Worker ready. You can start the webcam and load the marker.' , 'success' ) ;
96- loadBtn . disabled = false ; // allow loading even before camera (matches minimal example)
94+ loadBtn . disabled = false ;
9795 } ) ;
9896 engine . eventBus . on ( 'ar:workerError' , ( e ) => {
9997 log ( `workerError: ${ JSON . stringify ( e ) } ` ) ;
@@ -109,13 +107,11 @@ async function bootstrap() {
109107 await engine . pluginManager . enable ( defaultProfilePlugin . id , ctx ) ;
110108 await engine . pluginManager . enable ( webcamPlugin . id , ctx ) ;
111109
112- // Create ARToolKit plugin and wire it EXPLICITLY to this engine context
110+ // Create ARToolKit plugin and wire it to this engine context
113111 artoolkit = new ArtoolkitPlugin ( {
114112 cameraParametersUrl : '/examples/vite-artoolkit/data/camera_para.dat' ,
115113 minConfidence : 0.6 ,
116114 } ) ;
117-
118- // IMPORTANT: bind to engine context so events go to engine.eventBus
119115 await artoolkit . init ( ctx ) ;
120116 await artoolkit . enable ( ) ;
121117
@@ -129,17 +125,6 @@ async function bootstrap() {
129125 loadBtn . disabled = false ;
130126 } else {
131127 setStatus ( 'Plugin initialized. Waiting for worker…' , 'normal' ) ;
132- // Tiny watchdog in case the ready event is missed due to external wiring
133- const t0 = Date . now ( ) ;
134- const iv = setInterval ( ( ) => {
135- if ( artoolkit . workerReady ) {
136- loadBtn . disabled = false ;
137- setStatus ( 'Worker ready. You can start the webcam and load the marker.' , 'success' ) ;
138- clearInterval ( iv ) ;
139- } else if ( Date . now ( ) - t0 > 5000 ) {
140- clearInterval ( iv ) ;
141- }
142- } , 100 ) ;
143128 }
144129
145130 // UI initial state
@@ -223,7 +208,6 @@ async function loadMarker() {
223208 log ( 'loadMarker failed: ' + ( err ?. message || err ) ) ;
224209 setStatus ( 'Failed to load marker' , 'error' ) ;
225210 } finally {
226- // Re-enable for retry/replace
227211 loadBtn . disabled = false ;
228212 }
229213}
0 commit comments