@3dsource/metabox-front-api 1.2.2-dev.4 → 1.2.2
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 +1715 -164
- package/fesm2022/3dsource-metabox-front-api.mjs +48 -50
- package/fesm2022/3dsource-metabox-front-api.mjs.map +1 -1
- package/index.d.ts +126 -70
- package/package.json +1 -4
package/README.md
CHANGED
|
@@ -1,272 +1,1823 @@
|
|
|
1
|
-
# Metabox Basic
|
|
1
|
+
# Metabox Basic Configurator API
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A powerful TypeScript API for seamlessly integrating and controlling the Metabox Basic Configurator in your
|
|
4
|
+
web applications. This API provides full programmatic control over 3D product visualization, products,
|
|
5
|
+
their material slots, and materials, environments.
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
## Table of Contents
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
- [Features](#features)
|
|
10
|
+
- [Prerequisites](#prerequisites)
|
|
11
|
+
- [Installation](#installation)
|
|
12
|
+
- [Quick Start](#quick-start)
|
|
13
|
+
- [API Reference](#api-reference)
|
|
14
|
+
- [Core Functions](#core-functions)
|
|
15
|
+
- [Command Classes](#command-classes)
|
|
16
|
+
- [Event Handling](#event-handling)
|
|
17
|
+
- [Utility Functions](#utility-functions)
|
|
18
|
+
- [Examples](#examples)
|
|
19
|
+
- [Best Practices](#best-practices)
|
|
20
|
+
- [Troubleshooting](#troubleshooting)
|
|
21
|
+
- [Browser Compatibility](#browser-compatibility)
|
|
22
|
+
- [Contributing](#contributing)
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
- 🚀 Easy integration with any web framework (Angular, React, Vue, etc.)
|
|
27
|
+
- 📱 Responsive design support
|
|
28
|
+
- 🎨 Full control over materials, environments, and products
|
|
29
|
+
- 📸 Screenshot and PDF generation
|
|
30
|
+
- 🎬 Showcase management
|
|
31
|
+
- 📦 TypeScript support with full type definitions
|
|
32
|
+
- 🌐 CDN support for quick prototyping
|
|
33
|
+
|
|
34
|
+
## Prerequisites
|
|
35
|
+
|
|
36
|
+
- Modern web browser with ES6 module support
|
|
37
|
+
- HTTPS connection (required for unreal engine)
|
|
38
|
+
- Valid Metabox basic configurator URL
|
|
39
|
+
|
|
40
|
+
## Installation
|
|
41
|
+
|
|
42
|
+
### Method 1: NPM Package (Recommended)
|
|
43
|
+
|
|
44
|
+
For use with frameworks like Angular, React, Vue, etc.:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npm install @3dsource/metabox-front-api@^1.2.3 --save
|
|
9
48
|
```
|
|
10
49
|
|
|
11
|
-
|
|
50
|
+
Then import the required components:
|
|
12
51
|
|
|
13
52
|
```typescript
|
|
14
|
-
import {
|
|
53
|
+
import { integrateMetaBox, Communicator, ConfiguratorEnvelope, SetProduct, SetMaterial, SetEnvironment, GetScreenshot, GetPdf, saveImage } from '@3dsource/metabox-front-api';
|
|
15
54
|
```
|
|
16
55
|
|
|
17
|
-
|
|
18
|
-
|
|
56
|
+
### Method 2: CDN Import
|
|
57
|
+
|
|
58
|
+
Alternative CDN options:
|
|
59
|
+
|
|
60
|
+
- **jsDelivr**: `https://cdn.jsdelivr.net/npm/@3dsource/metabox-front-api@^1.2.3/+esm`
|
|
61
|
+
|
|
62
|
+
For quick prototyping or vanilla JavaScript projects:
|
|
19
63
|
|
|
20
64
|
```javascript
|
|
21
|
-
import {
|
|
65
|
+
import { integrateMetaBox, Communicator, ConfiguratorEnvelope, SetProduct, SetMaterial, SetEnvironment, GetScreenshot, GetPdf, saveImage } from 'https://cdn.jsdelivr.net/npm/@3dsource/metabox-front-api';
|
|
22
66
|
```
|
|
23
67
|
|
|
24
|
-
##
|
|
68
|
+
## Quick Start
|
|
25
69
|
|
|
26
|
-
### 1.
|
|
70
|
+
### 1. HTML Setup
|
|
27
71
|
|
|
28
|
-
|
|
29
|
-
You **MUST** control CSS style of `embed3DSource` element to make the configurator responsive and fit your needs on your
|
|
30
|
-
own.
|
|
72
|
+
Create a container element where the Metabox Configurator will be embedded:
|
|
31
73
|
|
|
32
74
|
```html
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
75
|
+
<!DOCTYPE html>
|
|
76
|
+
<html lang="en">
|
|
77
|
+
<head>
|
|
78
|
+
<meta charset="UTF-8" />
|
|
79
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
80
|
+
<title>Metabox Configurator</title>
|
|
81
|
+
<style>
|
|
82
|
+
#embed3DSource {
|
|
83
|
+
width: 100%;
|
|
84
|
+
height: 600px;
|
|
85
|
+
border: 1px solid #ccc;
|
|
86
|
+
border-radius: 8px;
|
|
87
|
+
}
|
|
88
|
+
</style>
|
|
89
|
+
</head>
|
|
90
|
+
<body>
|
|
91
|
+
<div id="embed3DSource">
|
|
92
|
+
<!-- Metabox Configurator will be embedded here -->
|
|
93
|
+
</div>
|
|
94
|
+
</body>
|
|
95
|
+
</html>
|
|
36
96
|
```
|
|
37
97
|
|
|
38
|
-
### 2.
|
|
98
|
+
### 2. Basic Integration
|
|
39
99
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
Insert this code into your HTML file just before the closing `</body>` tag:
|
|
43
|
-
|
|
44
|
-
**EXAMPLE:**
|
|
45
|
-
|
|
46
|
-
HTML:
|
|
100
|
+
#### JavaScript Example
|
|
47
101
|
|
|
48
102
|
```html
|
|
49
103
|
<script type="module">
|
|
50
|
-
import { integrateMetaBox,
|
|
104
|
+
import { integrateMetaBox, SetProduct, SetMaterial, SetEnvironment, GetScreenshot, saveImage } from 'https://cdn.jsdelivr.net/npm/@3dsource/metabox-front-api@1.2.3';
|
|
51
105
|
|
|
52
|
-
class
|
|
106
|
+
class MetaboxIntegrator {
|
|
53
107
|
constructor() {
|
|
54
|
-
|
|
55
|
-
|
|
108
|
+
// Replace 'your-configurator-id' with your actual basic configurator ID
|
|
109
|
+
const configuratorUrl = 'https://metabox.3dsource.com/metabox-configurator/basic/your-configurator-id?sidebar=false';
|
|
110
|
+
|
|
111
|
+
// Initialize the configurator
|
|
112
|
+
integrateMetaBox(configuratorUrl, 'embed3DSource', (api) => {
|
|
113
|
+
this.onApiReady(api);
|
|
114
|
+
});
|
|
56
115
|
}
|
|
57
116
|
|
|
58
|
-
|
|
117
|
+
onApiReady(api) {
|
|
118
|
+
console.log('Metabox API is ready!');
|
|
119
|
+
|
|
120
|
+
// Listen for configurator data updates
|
|
59
121
|
api.addEventListener('basicConfiguratorDataWithState', (data) => {
|
|
60
|
-
|
|
122
|
+
console.log('Configurator data updated:', data);
|
|
123
|
+
this.handleConfiguratorUpdate(data);
|
|
61
124
|
});
|
|
62
|
-
|
|
63
|
-
|
|
125
|
+
|
|
126
|
+
// Listen for screenshot/render completion
|
|
127
|
+
api.addEventListener('fastRender', (imageData) => {
|
|
128
|
+
console.log('Screenshot ready');
|
|
129
|
+
saveImage(imageData, 'configurator-screenshot.png');
|
|
64
130
|
});
|
|
65
|
-
|
|
131
|
+
|
|
132
|
+
// Set up the initial configuration
|
|
133
|
+
this.configureInitialState(api);
|
|
66
134
|
}
|
|
67
135
|
|
|
68
|
-
|
|
69
|
-
//
|
|
136
|
+
handleConfiguratorUpdate(data) {
|
|
137
|
+
// Handle configurator state changes
|
|
138
|
+
// You can update your UI, save state, etc.
|
|
70
139
|
}
|
|
71
140
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
api.sendCommandToMetaBox(new
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
api.sendCommandToMetaBox(new
|
|
78
|
-
|
|
79
|
-
|
|
141
|
+
configureInitialState(api) {
|
|
142
|
+
// Set the initial product
|
|
143
|
+
api.sendCommandToMetaBox(new SetProduct('your-product-id'));
|
|
144
|
+
|
|
145
|
+
// Set initial environment
|
|
146
|
+
api.sendCommandToMetaBox(new SetEnvironment('your-environment-id'));
|
|
147
|
+
|
|
148
|
+
// Apply material to a specific slot
|
|
149
|
+
api.sendCommandToMetaBox(new SetMaterial('slot-id', 'material-id'));
|
|
150
|
+
|
|
151
|
+
// Take a screenshot
|
|
80
152
|
api.sendCommandToMetaBox(new GetScreenshot('image/png', { x: 1024, y: 1024 }));
|
|
81
153
|
}
|
|
82
154
|
}
|
|
83
155
|
|
|
84
|
-
|
|
156
|
+
// Initialize the integrator
|
|
157
|
+
new MetaboxIntegrator();
|
|
85
158
|
</script>
|
|
86
159
|
```
|
|
87
160
|
|
|
88
|
-
TypeScript
|
|
161
|
+
#### TypeScript Example
|
|
89
162
|
|
|
90
163
|
```typescript
|
|
91
|
-
import {
|
|
164
|
+
import { integrateMetaBox, Communicator, ConfiguratorEnvelope, SetProduct, SetMaterial, SetEnvironment, GetScreenshot, GetPdf, ShowEmbeddedMenu, ShowOverlayInterface, saveImage } from '@3dsource/metabox-front-api';
|
|
165
|
+
|
|
166
|
+
class MetaboxIntegrator {
|
|
167
|
+
private api: Communicator | null = null;
|
|
92
168
|
|
|
93
|
-
class TsIntegrator {
|
|
94
169
|
constructor() {
|
|
95
|
-
|
|
96
|
-
integrateMetaBox(url, 'embed3DSource', (api) => this.apiReady(api));
|
|
170
|
+
this.initialize();
|
|
97
171
|
}
|
|
98
172
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
173
|
+
private async initialize(): Promise<void> {
|
|
174
|
+
try {
|
|
175
|
+
// Replace 'your-configurator-id' with your actual configurator ID
|
|
176
|
+
const configuratorUrl = 'https://metabox.3dsource.com/metabox-configurator/basic/your-configurator-id?sidebar=false';
|
|
177
|
+
|
|
178
|
+
// Initialize the configurator with type safety
|
|
179
|
+
integrateMetaBox(configuratorUrl, 'embed3DSource', (api: Communicator) => {
|
|
180
|
+
this.onApiReady(api);
|
|
181
|
+
});
|
|
182
|
+
} catch (error) {
|
|
183
|
+
console.error('Failed to initialize Metabox:', error);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private onApiReady(api: Communicator): void {
|
|
188
|
+
this.api = api;
|
|
189
|
+
console.log('Metabox API is ready!');
|
|
190
|
+
|
|
191
|
+
// Set up event listeners with proper typing
|
|
192
|
+
this.setupEventListeners();
|
|
193
|
+
|
|
194
|
+
// Configure initial state
|
|
195
|
+
this.configureInitialState();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private setupEventListeners(): void {
|
|
199
|
+
if (!this.api) return;
|
|
200
|
+
|
|
201
|
+
// Listen for configurator data updates
|
|
202
|
+
this.api.addEventListener('basicConfiguratorDataWithState', (data: ConfiguratorEnvelope) => {
|
|
203
|
+
console.log('Configurator data updated:', data);
|
|
204
|
+
this.handleConfiguratorUpdate(data);
|
|
102
205
|
});
|
|
103
|
-
|
|
104
|
-
|
|
206
|
+
|
|
207
|
+
// Listen for screenshot/render completion
|
|
208
|
+
this.api.addEventListener('fastRender', (imageData: string) => {
|
|
209
|
+
console.log('Screenshot ready');
|
|
210
|
+
saveImage(imageData, 'configurator-screenshot.png');
|
|
105
211
|
});
|
|
106
|
-
this.setupInitialState(api);
|
|
107
212
|
}
|
|
108
213
|
|
|
109
|
-
|
|
110
|
-
//
|
|
214
|
+
private handleConfiguratorUpdate(data: ConfiguratorEnvelope): void {
|
|
215
|
+
// Handle configurator state changes with full type safety
|
|
216
|
+
// Access data properties like data.product, data.materials, etc.
|
|
217
|
+
|
|
218
|
+
// Example: Update UI based on current product
|
|
219
|
+
if (data.product) {
|
|
220
|
+
console.log('Current product:', data.product);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private configureInitialState(): void {
|
|
225
|
+
if (!this.api) return;
|
|
226
|
+
|
|
227
|
+
// Set initial product
|
|
228
|
+
this.api.sendCommandToMetaBox(new SetProduct('your-product-id'));
|
|
229
|
+
|
|
230
|
+
// Set initial environment
|
|
231
|
+
this.api.sendCommandToMetaBox(new SetEnvironment('your-environment-id'));
|
|
232
|
+
|
|
233
|
+
// Apply material to a specific slot
|
|
234
|
+
this.api.sendCommandToMetaBox(new SetMaterial('slot-id', 'material-id'));
|
|
235
|
+
|
|
236
|
+
// Configure UI visibility
|
|
237
|
+
this.api.sendCommandToMetaBox(new ShowEmbeddedMenu(false));
|
|
238
|
+
this.api.sendCommandToMetaBox(new ShowOverlayInterface(false));
|
|
239
|
+
|
|
240
|
+
// Generate initial screenshot
|
|
241
|
+
this.api.sendCommandToMetaBox(new GetScreenshot('image/png', { x: 1024, y: 1024 }));
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Public method to change product
|
|
245
|
+
public changeProduct(productId: string): void {
|
|
246
|
+
if (this.api) {
|
|
247
|
+
this.api.sendCommandToMetaBox(new SetProduct(productId));
|
|
248
|
+
}
|
|
111
249
|
}
|
|
112
250
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
251
|
+
// Public method to apply material
|
|
252
|
+
public applyMaterial(slotId: string, materialId: string): void {
|
|
253
|
+
if (this.api) {
|
|
254
|
+
this.api.sendCommandToMetaBox(new SetMaterial(slotId, materialId));
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Public method to take screenshot
|
|
259
|
+
public takeScreenshot(width: number = 1024, height: number = 1024): void {
|
|
260
|
+
if (this.api) {
|
|
261
|
+
this.api.sendCommandToMetaBox(new GetScreenshot('image/png', { x: width, y: height }));
|
|
262
|
+
}
|
|
122
263
|
}
|
|
123
264
|
}
|
|
124
265
|
|
|
125
|
-
|
|
266
|
+
// Initialize the integrator
|
|
267
|
+
const metaboxIntegrator = new MetaboxIntegrator();
|
|
268
|
+
|
|
269
|
+
// Example usage:
|
|
270
|
+
// metaboxIntegrator.changeProduct('new-product-id');
|
|
271
|
+
// metaboxIntegrator.applyMaterial('slot-1', 'material-2');
|
|
272
|
+
// metaboxIntegrator.takeScreenshot(1920, 1080);
|
|
126
273
|
```
|
|
127
274
|
|
|
128
|
-
|
|
275
|
+
## API Reference
|
|
276
|
+
|
|
277
|
+
### Core Functions
|
|
129
278
|
|
|
130
279
|
#### `integrateMetaBox`
|
|
131
280
|
|
|
132
|
-
|
|
133
|
-
|
|
281
|
+
Integrates the Metabox configurator into the specified container element.
|
|
282
|
+
|
|
283
|
+
**TypeScript Signature:**
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
function integrateMetaBox(configuratorUrl: string, containerId: string, apiReadyCallback: (api: Communicator) => void): void;
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
**Parameters:**
|
|
290
|
+
|
|
291
|
+
- `configuratorUrl` (string): The URL of your Metabox basic configurator
|
|
292
|
+
- `containerId` (string): The ID of the HTML element where the configurator will be embedded
|
|
293
|
+
- `apiReadyCallback` (function): Callback function that receives the Communicator API instance
|
|
294
|
+
|
|
295
|
+
**Example:**
|
|
296
|
+
|
|
297
|
+
```typescript
|
|
298
|
+
integrateMetaBox('https://metabox.3dsource.com/metabox-configurator/basic/your-id', 'embed3DSource', (api) => {
|
|
299
|
+
console.log('API ready!', api);
|
|
300
|
+
});
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### Command Classes
|
|
304
|
+
|
|
305
|
+
All commands are sent to the configurator using the `api.sendCommandToMetaBox()` method.
|
|
306
|
+
|
|
307
|
+
#### Product Configuration
|
|
134
308
|
|
|
135
|
-
|
|
136
|
-
integrateMetaBox(configuratorUrl: string, containerId: string, apiReadyCallback: (api: Communicator) => void);
|
|
137
|
-
```
|
|
309
|
+
##### `SetProduct`
|
|
138
310
|
|
|
139
|
-
|
|
311
|
+
Sets the active product by ID.
|
|
140
312
|
|
|
141
|
-
|
|
142
|
-
|
|
313
|
+
```typescript
|
|
314
|
+
api.sendCommandToMetaBox(new SetProduct(productId));
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
##### `SetMaterial`
|
|
143
318
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
319
|
+
Applies a material to a specific product slot.
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
api.sendCommandToMetaBox(new SetMaterial(slotId, materialId));
|
|
323
|
+
```
|
|
147
324
|
|
|
148
|
-
####
|
|
325
|
+
#### Environment Configuration
|
|
326
|
+
|
|
327
|
+
##### `SetEnvironment`
|
|
328
|
+
|
|
329
|
+
Changes the environment/background by ID.
|
|
330
|
+
|
|
331
|
+
```typescript
|
|
332
|
+
api.sendCommandToMetaBox(new SetEnvironment(environmentId));
|
|
333
|
+
```
|
|
149
334
|
|
|
150
|
-
|
|
151
|
-
- **Usage**:
|
|
335
|
+
##### `SetEnvironmentMaterial`
|
|
152
336
|
|
|
153
|
-
|
|
154
|
-
api.sendCommandToMetaBox(new SetMaterial(slotId: string, materialId: string));
|
|
155
|
-
```
|
|
337
|
+
Applies a material to a specific environment slot.
|
|
156
338
|
|
|
157
|
-
|
|
339
|
+
```typescript
|
|
340
|
+
api.sendCommandToMetaBox(new SetEnvironmentMaterial(slotId, materialId));
|
|
341
|
+
```
|
|
158
342
|
|
|
159
|
-
|
|
160
|
-
- **Usage**:
|
|
343
|
+
#### Camera Controls
|
|
161
344
|
|
|
162
|
-
|
|
163
|
-
api.sendCommandToMetaBox(new SetEnvironmentMaterial(slotId: string, materialId: string));
|
|
164
|
-
```
|
|
345
|
+
##### `ResetCamera`
|
|
165
346
|
|
|
166
|
-
|
|
347
|
+
Resets the camera to its default position and rotation.
|
|
167
348
|
|
|
168
|
-
|
|
169
|
-
|
|
349
|
+
```typescript
|
|
350
|
+
api.sendCommandToMetaBox(new ResetCamera());
|
|
351
|
+
```
|
|
170
352
|
|
|
171
|
-
|
|
172
|
-
api.sendCommandToMetaBox(new SetProduct(productId: string));
|
|
173
|
-
```
|
|
353
|
+
##### `ApplyZoom`
|
|
174
354
|
|
|
175
|
-
|
|
355
|
+
Applies zoom to the camera view.
|
|
176
356
|
|
|
177
|
-
|
|
178
|
-
|
|
357
|
+
```typescript
|
|
358
|
+
api.sendCommandToMetaBox(new ApplyZoom(zoom));
|
|
359
|
+
```
|
|
179
360
|
|
|
180
|
-
|
|
181
|
-
api.sendCommandToMetaBox(new ShowEmbeddedMenu(visible: boolean));
|
|
182
|
-
```
|
|
361
|
+
#### Interface Controls
|
|
183
362
|
|
|
184
|
-
|
|
363
|
+
##### `ShowEmbeddedMenu`
|
|
185
364
|
|
|
186
|
-
|
|
187
|
-
- **Usage**:
|
|
365
|
+
Toggles the visibility of the embedded menu in the configurator.
|
|
188
366
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
367
|
+
```typescript
|
|
368
|
+
api.sendCommandToMetaBox(new ShowEmbeddedMenu(visible));
|
|
369
|
+
```
|
|
192
370
|
|
|
193
|
-
|
|
371
|
+
##### `ShowOverlayInterface`
|
|
194
372
|
|
|
195
|
-
|
|
196
|
-
- **Usage**:
|
|
373
|
+
Toggles the overlay interface over the Unreal viewport.
|
|
197
374
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
375
|
+
```typescript
|
|
376
|
+
api.sendCommandToMetaBox(new ShowOverlayInterface(visible));
|
|
377
|
+
```
|
|
201
378
|
|
|
202
|
-
####
|
|
379
|
+
#### Media Generation
|
|
203
380
|
|
|
204
|
-
|
|
205
|
-
- **Usage**:
|
|
381
|
+
##### `GetScreenshot`
|
|
206
382
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
383
|
+
Captures a screenshot/render of the current view.
|
|
384
|
+
|
|
385
|
+
```typescript
|
|
386
|
+
api.sendCommandToMetaBox(new GetScreenshot(mimeType, { x: 1920, y: 1080 }));
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
**Parameters:**
|
|
390
|
+
|
|
391
|
+
- `mimeType`: Image format ('image/png', 'image/jpeg', etc.)
|
|
392
|
+
- `size`: Optional dimensions object with x and y properties
|
|
393
|
+
|
|
394
|
+
##### `GetFastRender`
|
|
395
|
+
|
|
396
|
+
Captures a fast render of the current view (optimized for speed).
|
|
397
|
+
|
|
398
|
+
```typescript
|
|
399
|
+
api.sendCommandToMetaBox(new GetFastRender());
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
##### `GetPdf`
|
|
403
|
+
|
|
404
|
+
Generates a PDF of the current view.
|
|
405
|
+
|
|
406
|
+
```typescript
|
|
407
|
+
api.sendCommandToMetaBox(new GetPdf());
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
#### Showcase Management
|
|
411
|
+
|
|
412
|
+
##### `InitShowcase`
|
|
413
|
+
|
|
414
|
+
Initializes a showcase with the given showcase ID.
|
|
415
|
+
|
|
416
|
+
```typescript
|
|
417
|
+
api.sendCommandToMetaBox(new InitShowcase(showcaseId));
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
##### `PlayShowcase`
|
|
421
|
+
|
|
422
|
+
Plays the currently loaded showcase.
|
|
423
|
+
|
|
424
|
+
```typescript
|
|
425
|
+
api.sendCommandToMetaBox(new PlayShowcase());
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
##### `PauseShowcase`
|
|
429
|
+
|
|
430
|
+
Pauses the currently playing showcase.
|
|
431
|
+
|
|
432
|
+
```typescript
|
|
433
|
+
api.sendCommandToMetaBox(new PauseShowcase());
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
##### `StopShowcase`
|
|
437
|
+
|
|
438
|
+
Stops the currently playing showcase and returns to the initial state.
|
|
439
|
+
|
|
440
|
+
```typescript
|
|
441
|
+
api.sendCommandToMetaBox(new StopShowcase());
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
### Event Handling
|
|
445
|
+
|
|
446
|
+
The API uses an event-driven architecture. Listen for events using the `addEventListener` method:
|
|
447
|
+
|
|
448
|
+
#### Available Events
|
|
449
|
+
|
|
450
|
+
##### `basicConfiguratorDataWithState`
|
|
451
|
+
|
|
452
|
+
Fired when the configurator state changes (product, materials, environment, etc.).
|
|
453
|
+
|
|
454
|
+
```typescript
|
|
455
|
+
api.addEventListener('basicConfiguratorDataWithState', (data: ConfiguratorEnvelope) => {
|
|
456
|
+
console.log('Configurator state updated:', data);
|
|
457
|
+
// Access data.product, data.materials, data.environment, etc.
|
|
458
|
+
});
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
##### `fastRender`
|
|
462
|
+
|
|
463
|
+
Fired when a screenshot or fast render is completed.
|
|
464
|
+
|
|
465
|
+
```typescript
|
|
466
|
+
api.addEventListener('fastRender', (imageData: string) => {
|
|
467
|
+
console.log('Render completed');
|
|
468
|
+
// imageData contains the base64 encoded image
|
|
469
|
+
saveImage(imageData, 'screenshot.png');
|
|
470
|
+
});
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
##### `pdfGenerated`
|
|
474
|
+
|
|
475
|
+
Fired when PDF generation is completed.
|
|
476
|
+
|
|
477
|
+
```typescript
|
|
478
|
+
api.addEventListener('pdfGenerated', (pdfData: string) => {
|
|
479
|
+
console.log('PDF generated');
|
|
480
|
+
// Handle PDF data
|
|
481
|
+
});
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
### Utility Functions
|
|
210
485
|
|
|
211
486
|
#### `saveImage`
|
|
212
487
|
|
|
213
|
-
|
|
214
|
-
|
|
488
|
+
Utility function to save rendered images to a file.
|
|
489
|
+
|
|
490
|
+
```typescript
|
|
491
|
+
function saveImage(imageUrl: string, filename: string): void;
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
**Parameters:**
|
|
495
|
+
|
|
496
|
+
- `imageUrl`: The URL of the image to save (can be base64 encoded image data)
|
|
497
|
+
- `filename`: The name of the file to save
|
|
498
|
+
|
|
499
|
+
**Example:**
|
|
500
|
+
|
|
501
|
+
```typescript
|
|
502
|
+
api.addEventListener('fastRender', (imageData: string | null) => {
|
|
503
|
+
saveImage(imageData ?? '', 'my-render.png');
|
|
504
|
+
});
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
## Examples
|
|
508
|
+
|
|
509
|
+
### Basic Product Configurator
|
|
510
|
+
|
|
511
|
+
A complete HTML example showing how to integrate the Metabox configurator with vanilla JavaScript.
|
|
512
|
+
|
|
513
|
+
```html
|
|
514
|
+
<!DOCTYPE html>
|
|
515
|
+
<html lang="en">
|
|
516
|
+
<head>
|
|
517
|
+
<meta charset="UTF-8" />
|
|
518
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
519
|
+
<title>Product Configurator</title>
|
|
520
|
+
<style>
|
|
521
|
+
#metabox-container {
|
|
522
|
+
width: 100%;
|
|
523
|
+
height: 600px;
|
|
524
|
+
border: 1px solid #ddd;
|
|
525
|
+
border-radius: 8px;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
.controls {
|
|
529
|
+
margin: 20px 0;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
button {
|
|
533
|
+
margin: 5px;
|
|
534
|
+
padding: 10px 15px;
|
|
535
|
+
border: 1px solid #ccc;
|
|
536
|
+
border-radius: 4px;
|
|
537
|
+
background: #f5f5f5;
|
|
538
|
+
cursor: pointer;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
button:hover {
|
|
542
|
+
background: #e5e5e5;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
button:disabled {
|
|
546
|
+
opacity: 0.5;
|
|
547
|
+
cursor: not-allowed;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
.loading {
|
|
551
|
+
text-align: center;
|
|
552
|
+
padding: 20px;
|
|
553
|
+
color: #666;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
.error {
|
|
557
|
+
color: #d32f2f;
|
|
558
|
+
padding: 10px;
|
|
559
|
+
background: #ffebee;
|
|
560
|
+
border: 1px solid #ffcdd2;
|
|
561
|
+
border-radius: 4px;
|
|
562
|
+
margin: 10px 0;
|
|
563
|
+
}
|
|
564
|
+
</style>
|
|
565
|
+
</head>
|
|
566
|
+
<body>
|
|
567
|
+
<div id="loading" class="loading">Loading configurator...</div>
|
|
568
|
+
<div id="error" class="error" style="display: none;"></div>
|
|
569
|
+
<div id="metabox-container"></div>
|
|
570
|
+
<div class="controls">
|
|
571
|
+
<button id="product1-btn" onclick="changeProduct('product-1')" disabled>Product 1</button>
|
|
572
|
+
<button id="product2-btn" onclick="changeProduct('product-2')" disabled>Product 2</button>
|
|
573
|
+
<button id="red-material-btn" onclick="applyMaterial('slot-1', 'material-red')" disabled>Red Material</button>
|
|
574
|
+
<button id="blue-material-btn" onclick="applyMaterial('slot-1', 'material-blue')" disabled>Blue Material</button>
|
|
575
|
+
<button id="screenshot-btn" onclick="takeScreenshot()" disabled>Take Screenshot</button>
|
|
576
|
+
</div>
|
|
577
|
+
|
|
578
|
+
<script type="module">
|
|
579
|
+
import { integrateMetaBox, SetProduct, SetMaterial, GetScreenshot, saveImage } from 'https://cdn.jsdelivr.net/npm/@3dsource/metabox-front-api@latest';
|
|
580
|
+
|
|
581
|
+
let api = null;
|
|
582
|
+
let isApiReady = false;
|
|
583
|
+
|
|
584
|
+
// Helper function to show/hide loading state
|
|
585
|
+
function setLoadingState(loading) {
|
|
586
|
+
const loadingEl = document.getElementById('loading');
|
|
587
|
+
const buttons = document.querySelectorAll('button');
|
|
588
|
+
|
|
589
|
+
loadingEl.style.display = loading ? 'block' : 'none';
|
|
590
|
+
buttons.forEach((btn) => (btn.disabled = loading || !isApiReady));
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// Helper function to show error messages
|
|
594
|
+
function showError(message) {
|
|
595
|
+
const errorEl = document.getElementById('error');
|
|
596
|
+
errorEl.textContent = message;
|
|
597
|
+
errorEl.style.display = 'block';
|
|
598
|
+
setLoadingState(false);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// Initialize configurator with error handling
|
|
602
|
+
try {
|
|
603
|
+
// Replace 'your-configurator-id' with your actual configurator ID from 3DSource
|
|
604
|
+
const configuratorUrl = 'https://metabox.3dsource.com/metabox-configurator/basic/your-configurator-id';
|
|
605
|
+
|
|
606
|
+
integrateMetaBox(configuratorUrl, 'metabox-container', (apiInstance) => {
|
|
607
|
+
try {
|
|
608
|
+
api = apiInstance;
|
|
609
|
+
isApiReady = true;
|
|
610
|
+
setLoadingState(false);
|
|
611
|
+
console.log('Configurator ready!');
|
|
612
|
+
|
|
613
|
+
// Listen for configurator state changes
|
|
614
|
+
api.addEventListener('basicConfiguratorDataWithState', (data) => {
|
|
615
|
+
console.log('Configurator state updated:', data);
|
|
616
|
+
// You can update your UI based on the current state
|
|
617
|
+
// For example, update available materials, products, etc.
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
// Listen for screenshot completion
|
|
621
|
+
api.addEventListener('fastRender', (imageData) => {
|
|
622
|
+
console.log('Screenshot captured successfully');
|
|
623
|
+
// Save the image with a timestamp
|
|
624
|
+
saveImage(imageData, `configurator-screenshot-${Date.now()}.png`);
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
// Listen for errors
|
|
628
|
+
api.addEventListener('error', (error) => {
|
|
629
|
+
console.error('Configurator error:', error);
|
|
630
|
+
showError('An error occurred in the configurator: ' + error.message);
|
|
631
|
+
});
|
|
632
|
+
} catch (error) {
|
|
633
|
+
console.error('Error setting up API:', error);
|
|
634
|
+
showError('Failed to initialize configurator API: ' + error.message);
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
} catch (error) {
|
|
638
|
+
console.error('Error initializing configurator:', error);
|
|
639
|
+
showError('Failed to load configurator: ' + error.message);
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// Global functions for button interactions
|
|
643
|
+
window.changeProduct = (productId) => {
|
|
644
|
+
if (!isApiReady || !api) {
|
|
645
|
+
console.warn('API not ready yet');
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
try {
|
|
650
|
+
console.log(`Changing to product: ${productId}`);
|
|
651
|
+
api.sendCommandToMetaBox(new SetProduct(productId));
|
|
652
|
+
} catch (error) {
|
|
653
|
+
console.error('Error changing product:', error);
|
|
654
|
+
showError('Failed to change product: ' + error.message);
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
|
|
658
|
+
window.applyMaterial = (slotId, materialId) => {
|
|
659
|
+
if (!isApiReady || !api) {
|
|
660
|
+
console.warn('API not ready yet');
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
try {
|
|
665
|
+
console.log(`Applying material ${materialId} to slot ${slotId}`);
|
|
666
|
+
api.sendCommandToMetaBox(new SetMaterial(slotId, materialId));
|
|
667
|
+
} catch (error) {
|
|
668
|
+
console.error('Error applying material:', error);
|
|
669
|
+
showError('Failed to apply material: ' + error.message);
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
window.takeScreenshot = () => {
|
|
674
|
+
if (!isApiReady || !api) {
|
|
675
|
+
console.warn('API not ready yet');
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
try {
|
|
680
|
+
console.log('Taking screenshot...');
|
|
681
|
+
// Request high-quality screenshot in PNG format
|
|
682
|
+
api.sendCommandToMetaBox(new GetScreenshot('image/png', { x: 1920, y: 1080 }));
|
|
683
|
+
} catch (error) {
|
|
684
|
+
console.error('Error taking screenshot:', error);
|
|
685
|
+
showError('Failed to take screenshot: ' + error.message);
|
|
686
|
+
}
|
|
687
|
+
};
|
|
688
|
+
</script>
|
|
689
|
+
</body>
|
|
690
|
+
</html>
|
|
691
|
+
```
|
|
692
|
+
|
|
693
|
+
**Key Features of this Example:**
|
|
694
|
+
|
|
695
|
+
- **Error Handling**: Comprehensive error handling for all API operations
|
|
696
|
+
- **Loading States**: Visual feedback while the configurator loads
|
|
697
|
+
- **Button States**: Buttons are disabled until the API is ready
|
|
698
|
+
- **Console Logging**: Detailed logging for debugging
|
|
699
|
+
- **Responsive Design**: Clean, responsive button layout
|
|
700
|
+
- **Latest CDN**: Uses `@latest` for the most recent version
|
|
701
|
+
|
|
702
|
+
**To Use This Example:**
|
|
703
|
+
|
|
704
|
+
1. Replace `'your-configurator-id'` with your actual configurator ID
|
|
705
|
+
2. Replace placeholder product IDs (`'product-1'`, `'product-2'`) with real product IDs
|
|
706
|
+
3. Replace placeholder material IDs (`'material-red'`, `'material-blue'`) with real material IDs
|
|
707
|
+
4. Replace placeholder slot ID (`'slot-1'`) with the actual slot ID from your configurator
|
|
708
|
+
|
|
709
|
+
### React Integration Example
|
|
710
|
+
|
|
711
|
+
A complete React component with TypeScript showing proper integration, error handling, and cleanup.
|
|
712
|
+
|
|
713
|
+
```tsx
|
|
714
|
+
import React, { useEffect, useRef, useState, useCallback } from 'react';
|
|
715
|
+
import { integrateMetaBox, Communicator, SetProduct, SetMaterial, GetScreenshot, saveImage, ConfiguratorEnvelope } from '@3dsource/metabox-front-api';
|
|
716
|
+
|
|
717
|
+
interface MetaboxConfiguratorProps {
|
|
718
|
+
configuratorUrl: string;
|
|
719
|
+
onStateChange?: (data: ConfiguratorEnvelope) => void;
|
|
720
|
+
onError?: (error: string) => void;
|
|
721
|
+
className?: string;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
interface ConfiguratorState {
|
|
725
|
+
isLoading: boolean;
|
|
726
|
+
error: string | null;
|
|
727
|
+
isApiReady: boolean;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
const MetaboxConfigurator: React.FC<MetaboxConfiguratorProps> = ({ configuratorUrl, onStateChange, onError, className }) => {
|
|
731
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
732
|
+
const [api, setApi] = useState<Communicator | null>(null);
|
|
733
|
+
const [state, setState] = useState<ConfiguratorState>({
|
|
734
|
+
isLoading: true,
|
|
735
|
+
error: null,
|
|
736
|
+
isApiReady: false,
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
// Generate unique container ID to avoid conflicts
|
|
740
|
+
const containerId = useRef(`metabox-container-${Math.random().toString(36).substr(2, 9)}`);
|
|
741
|
+
|
|
742
|
+
// Error handler
|
|
743
|
+
const handleError = useCallback(
|
|
744
|
+
(error: string) => {
|
|
745
|
+
setState((prev) => ({ ...prev, error, isLoading: false }));
|
|
746
|
+
onError?.(error);
|
|
747
|
+
console.error('Metabox Configurator Error:', error);
|
|
748
|
+
},
|
|
749
|
+
[onError],
|
|
750
|
+
);
|
|
751
|
+
|
|
752
|
+
// Initialize configurator
|
|
753
|
+
useEffect(() => {
|
|
754
|
+
if (!containerRef.current) return;
|
|
755
|
+
|
|
756
|
+
let mounted = true;
|
|
757
|
+
|
|
758
|
+
const initializeConfigurator = async () => {
|
|
759
|
+
try {
|
|
760
|
+
setState((prev) => ({ ...prev, isLoading: true, error: null }));
|
|
761
|
+
|
|
762
|
+
// Set the container ID
|
|
763
|
+
containerRef.current!.id = containerId.current;
|
|
764
|
+
|
|
765
|
+
integrateMetaBox(configuratorUrl, containerId.current, (apiInstance) => {
|
|
766
|
+
if (!mounted) return; // Component was unmounted
|
|
767
|
+
|
|
768
|
+
try {
|
|
769
|
+
setApi(apiInstance);
|
|
770
|
+
setState((prev) => ({ ...prev, isLoading: false, isApiReady: true }));
|
|
771
|
+
|
|
772
|
+
// Set up event listeners with proper typing
|
|
773
|
+
apiInstance.addEventListener('basicConfiguratorDataWithState', (data: ConfiguratorEnvelope) => {
|
|
774
|
+
if (mounted) {
|
|
775
|
+
onStateChange?.(data);
|
|
776
|
+
}
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
apiInstance.addEventListener('fastRender', (imageData: string) => {
|
|
780
|
+
if (mounted) {
|
|
781
|
+
console.log('Screenshot captured successfully');
|
|
782
|
+
saveImage(imageData, `configurator-screenshot-${Date.now()}.png`);
|
|
783
|
+
}
|
|
784
|
+
});
|
|
785
|
+
|
|
786
|
+
// Listen for errors from the configurator
|
|
787
|
+
apiInstance.addEventListener('error', (error: any) => {
|
|
788
|
+
if (mounted) {
|
|
789
|
+
handleError(`Configurator error: ${error.message || error}`);
|
|
790
|
+
}
|
|
791
|
+
});
|
|
792
|
+
|
|
793
|
+
console.log('React Metabox Configurator initialized successfully');
|
|
794
|
+
} catch (error) {
|
|
795
|
+
if (mounted) {
|
|
796
|
+
handleError(`Failed to set up API: ${error instanceof Error ? error.message : String(error)}`);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
});
|
|
800
|
+
} catch (error) {
|
|
801
|
+
if (mounted) {
|
|
802
|
+
handleError(`Failed to initialize configurator: ${error instanceof Error ? error.message : String(error)}`);
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
};
|
|
806
|
+
|
|
807
|
+
initializeConfigurator();
|
|
808
|
+
|
|
809
|
+
// Cleanup function
|
|
810
|
+
return () => {
|
|
811
|
+
mounted = false;
|
|
812
|
+
if (api) {
|
|
813
|
+
// Clean up any event listeners if the API provides cleanup methods
|
|
814
|
+
console.log('Cleaning up Metabox Configurator');
|
|
815
|
+
}
|
|
816
|
+
};
|
|
817
|
+
}, [configuratorUrl, onStateChange, handleError, api]);
|
|
818
|
+
|
|
819
|
+
// Command methods with error handling
|
|
820
|
+
const changeProduct = useCallback(
|
|
821
|
+
(productId: string) => {
|
|
822
|
+
if (!state.isApiReady || !api) {
|
|
823
|
+
console.warn('API not ready yet');
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
try {
|
|
828
|
+
console.log(`Changing to product: ${productId}`);
|
|
829
|
+
api.sendCommandToMetaBox(new SetProduct(productId));
|
|
830
|
+
} catch (error) {
|
|
831
|
+
handleError(`Failed to change product: ${error instanceof Error ? error.message : String(error)}`);
|
|
832
|
+
}
|
|
833
|
+
},
|
|
834
|
+
[api, state.isApiReady, handleError],
|
|
835
|
+
);
|
|
836
|
+
|
|
837
|
+
const applyMaterial = useCallback(
|
|
838
|
+
(slotId: string, materialId: string) => {
|
|
839
|
+
if (!state.isApiReady || !api) {
|
|
840
|
+
console.warn('API not ready yet');
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
try {
|
|
845
|
+
console.log(`Applying material ${materialId} to slot ${slotId}`);
|
|
846
|
+
api.sendCommandToMetaBox(new SetMaterial(slotId, materialId));
|
|
847
|
+
} catch (error) {
|
|
848
|
+
handleError(`Failed to apply material: ${error instanceof Error ? error.message : String(error)}`);
|
|
849
|
+
}
|
|
850
|
+
},
|
|
851
|
+
[api, state.isApiReady, handleError],
|
|
852
|
+
);
|
|
853
|
+
|
|
854
|
+
const takeScreenshot = useCallback(() => {
|
|
855
|
+
if (!state.isApiReady || !api) {
|
|
856
|
+
console.warn('API not ready yet');
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
try {
|
|
861
|
+
console.log('Taking screenshot...');
|
|
862
|
+
api.sendCommandToMetaBox(new GetScreenshot('image/png', { x: 1920, y: 1080 }));
|
|
863
|
+
} catch (error) {
|
|
864
|
+
handleError(`Failed to take screenshot: ${error instanceof Error ? error.message : String(error)}`);
|
|
865
|
+
}
|
|
866
|
+
}, [api, state.isApiReady, handleError]);
|
|
867
|
+
|
|
868
|
+
// Render error state
|
|
869
|
+
if (state.error) {
|
|
870
|
+
return (
|
|
871
|
+
<div className={className}>
|
|
872
|
+
<div
|
|
873
|
+
style={{
|
|
874
|
+
color: '#d32f2f',
|
|
875
|
+
padding: '20px',
|
|
876
|
+
background: '#ffebee',
|
|
877
|
+
border: '1px solid #ffcdd2',
|
|
878
|
+
borderRadius: '4px',
|
|
879
|
+
textAlign: 'center',
|
|
880
|
+
}}
|
|
881
|
+
>
|
|
882
|
+
<h3>Configurator Error</h3>
|
|
883
|
+
<p>{state.error}</p>
|
|
884
|
+
<button
|
|
885
|
+
onClick={() => window.location.reload()}
|
|
886
|
+
style={{
|
|
887
|
+
padding: '10px 20px',
|
|
888
|
+
marginTop: '10px',
|
|
889
|
+
border: '1px solid #d32f2f',
|
|
890
|
+
background: '#fff',
|
|
891
|
+
color: '#d32f2f',
|
|
892
|
+
borderRadius: '4px',
|
|
893
|
+
cursor: 'pointer',
|
|
894
|
+
}}
|
|
895
|
+
>
|
|
896
|
+
Reload Page
|
|
897
|
+
</button>
|
|
898
|
+
</div>
|
|
899
|
+
</div>
|
|
900
|
+
);
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
return (
|
|
904
|
+
<div className={className}>
|
|
905
|
+
{/* Loading indicator */}
|
|
906
|
+
{state.isLoading && (
|
|
907
|
+
<div
|
|
908
|
+
style={{
|
|
909
|
+
textAlign: 'center',
|
|
910
|
+
padding: '40px',
|
|
911
|
+
color: '#666',
|
|
912
|
+
background: '#f5f5f5',
|
|
913
|
+
borderRadius: '8px',
|
|
914
|
+
}}
|
|
915
|
+
>
|
|
916
|
+
<div>Loading configurator...</div>
|
|
917
|
+
<div style={{ marginTop: '10px', fontSize: '14px' }}>Please wait while we initialize the 3D viewer</div>
|
|
918
|
+
</div>
|
|
919
|
+
)}
|
|
920
|
+
|
|
921
|
+
{/* Configurator container */}
|
|
922
|
+
<div
|
|
923
|
+
ref={containerRef}
|
|
924
|
+
style={{
|
|
925
|
+
width: '100%',
|
|
926
|
+
height: '600px',
|
|
927
|
+
border: '1px solid #ddd',
|
|
928
|
+
borderRadius: '8px',
|
|
929
|
+
display: state.isLoading ? 'none' : 'block',
|
|
930
|
+
}}
|
|
931
|
+
/>
|
|
932
|
+
|
|
933
|
+
{/* Controls */}
|
|
934
|
+
{state.isApiReady && (
|
|
935
|
+
<div style={{ marginTop: '20px' }}>
|
|
936
|
+
<div style={{ marginBottom: '10px', fontWeight: 'bold', color: '#333' }}>Configurator Controls:</div>
|
|
937
|
+
<div style={{ display: 'flex', flexWrap: 'wrap', gap: '10px' }}>
|
|
938
|
+
<button
|
|
939
|
+
onClick={() => changeProduct('product-1')}
|
|
940
|
+
disabled={!state.isApiReady}
|
|
941
|
+
style={{
|
|
942
|
+
padding: '10px 15px',
|
|
943
|
+
border: '1px solid #ccc',
|
|
944
|
+
borderRadius: '4px',
|
|
945
|
+
background: state.isApiReady ? '#f5f5f5' : '#e0e0e0',
|
|
946
|
+
cursor: state.isApiReady ? 'pointer' : 'not-allowed',
|
|
947
|
+
transition: 'background-color 0.2s',
|
|
948
|
+
}}
|
|
949
|
+
onMouseOver={(e) => {
|
|
950
|
+
if (state.isApiReady) e.currentTarget.style.background = '#e5e5e5';
|
|
951
|
+
}}
|
|
952
|
+
onMouseOut={(e) => {
|
|
953
|
+
if (state.isApiReady) e.currentTarget.style.background = '#f5f5f5';
|
|
954
|
+
}}
|
|
955
|
+
>
|
|
956
|
+
Product 1
|
|
957
|
+
</button>
|
|
958
|
+
<button
|
|
959
|
+
onClick={() => changeProduct('product-2')}
|
|
960
|
+
disabled={!state.isApiReady}
|
|
961
|
+
style={{
|
|
962
|
+
padding: '10px 15px',
|
|
963
|
+
border: '1px solid #ccc',
|
|
964
|
+
borderRadius: '4px',
|
|
965
|
+
background: state.isApiReady ? '#f5f5f5' : '#e0e0e0',
|
|
966
|
+
cursor: state.isApiReady ? 'pointer' : 'not-allowed',
|
|
967
|
+
}}
|
|
968
|
+
>
|
|
969
|
+
Product 2
|
|
970
|
+
</button>
|
|
971
|
+
<button
|
|
972
|
+
onClick={() => applyMaterial('slot-1', 'material-red')}
|
|
973
|
+
disabled={!state.isApiReady}
|
|
974
|
+
style={{
|
|
975
|
+
padding: '10px 15px',
|
|
976
|
+
border: '1px solid #ccc',
|
|
977
|
+
borderRadius: '4px',
|
|
978
|
+
background: state.isApiReady ? '#f5f5f5' : '#e0e0e0',
|
|
979
|
+
cursor: state.isApiReady ? 'pointer' : 'not-allowed',
|
|
980
|
+
}}
|
|
981
|
+
>
|
|
982
|
+
Red Material
|
|
983
|
+
</button>
|
|
984
|
+
<button
|
|
985
|
+
onClick={() => applyMaterial('slot-1', 'material-blue')}
|
|
986
|
+
disabled={!state.isApiReady}
|
|
987
|
+
style={{
|
|
988
|
+
padding: '10px 15px',
|
|
989
|
+
border: '1px solid #ccc',
|
|
990
|
+
borderRadius: '4px',
|
|
991
|
+
background: state.isApiReady ? '#f5f5f5' : '#e0e0e0',
|
|
992
|
+
cursor: state.isApiReady ? 'pointer' : 'not-allowed',
|
|
993
|
+
}}
|
|
994
|
+
>
|
|
995
|
+
Blue Material
|
|
996
|
+
</button>
|
|
997
|
+
<button
|
|
998
|
+
onClick={takeScreenshot}
|
|
999
|
+
disabled={!state.isApiReady}
|
|
1000
|
+
style={{
|
|
1001
|
+
padding: '10px 15px',
|
|
1002
|
+
border: '1px solid #007bff',
|
|
1003
|
+
borderRadius: '4px',
|
|
1004
|
+
background: state.isApiReady ? '#007bff' : '#6c757d',
|
|
1005
|
+
color: 'white',
|
|
1006
|
+
cursor: state.isApiReady ? 'pointer' : 'not-allowed',
|
|
1007
|
+
}}
|
|
1008
|
+
>
|
|
1009
|
+
📸 Take Screenshot
|
|
1010
|
+
</button>
|
|
1011
|
+
</div>
|
|
1012
|
+
</div>
|
|
1013
|
+
)}
|
|
1014
|
+
</div>
|
|
1015
|
+
);
|
|
1016
|
+
};
|
|
1017
|
+
|
|
1018
|
+
export default MetaboxConfigurator;
|
|
1019
|
+
```
|
|
1020
|
+
|
|
1021
|
+
**Key Features of this React Example:**
|
|
1022
|
+
|
|
1023
|
+
- **Proper TypeScript Integration**: Full type safety with proper interfaces
|
|
1024
|
+
- **Error Handling**: Comprehensive error states and user feedback
|
|
1025
|
+
- **Component Cleanup**: Proper unmounting and memory leak prevention
|
|
1026
|
+
- **Unique Container IDs**: Automatic ID generation to avoid conflicts
|
|
1027
|
+
- **Loading States**: Visual feedback during initialization
|
|
1028
|
+
- **Optimized Re-renders**: Uses useCallback to prevent unnecessary re-renders
|
|
1029
|
+
- **Responsive Design**: Flexible button layout with proper styling
|
|
1030
|
+
|
|
1031
|
+
**Usage Example:**
|
|
1032
|
+
|
|
1033
|
+
```tsx
|
|
1034
|
+
// App.tsx
|
|
1035
|
+
import React from 'react';
|
|
1036
|
+
import MetaboxConfigurator from './components/MetaboxConfigurator';
|
|
1037
|
+
import { ConfiguratorEnvelope } from '@3dsource/metabox-front-api';
|
|
1038
|
+
|
|
1039
|
+
function App() {
|
|
1040
|
+
const handleStateChange = (data: ConfiguratorEnvelope) => {
|
|
1041
|
+
console.log('Configurator state changed:', data);
|
|
1042
|
+
// Update your application state based on configurator changes
|
|
1043
|
+
};
|
|
1044
|
+
|
|
1045
|
+
const handleError = (error: string) => {
|
|
1046
|
+
console.error('Configurator error:', error);
|
|
1047
|
+
// Handle errors (show notifications, log to analytics, etc.)
|
|
1048
|
+
};
|
|
1049
|
+
|
|
1050
|
+
return (
|
|
1051
|
+
<div className="App">
|
|
1052
|
+
<h1>My Product Configurator</h1>
|
|
1053
|
+
<MetaboxConfigurator configuratorUrl="https://metabox.3dsource.com/metabox-configurator/basic/your-configurator-id" onStateChange={handleStateChange} onError={handleError} className="my-configurator" />
|
|
1054
|
+
</div>
|
|
1055
|
+
);
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
export default App;
|
|
1059
|
+
```
|
|
1060
|
+
|
|
1061
|
+
**To Use This Component:**
|
|
1062
|
+
|
|
1063
|
+
1. Install the package: `npm install @3dsource/metabox-front-api`
|
|
1064
|
+
2. Replace `'your-configurator-id'` with your actual configurator ID
|
|
1065
|
+
3. Replace placeholder IDs in the component with your real product, material, and slot IDs
|
|
1066
|
+
4. Customize the styling by passing a `className` prop or modifying the inline styles
|
|
1067
|
+
|
|
1068
|
+
### Angular Integration Example
|
|
1069
|
+
|
|
1070
|
+
A complete Angular component with TypeScript showing proper integration, error handling, and lifecycle management.
|
|
1071
|
+
|
|
1072
|
+
```typescript
|
|
1073
|
+
// metabox-configurator.component.ts
|
|
1074
|
+
import { Component, input, OnInit, output, signal } from '@angular/core';
|
|
1075
|
+
import { Communicator, ConfiguratorEnvelope, GetPdf, GetScreenshot, InitShowcase, integrateMetaBox, PauseShowcase, PlayShowcase, saveImage, SetMaterial, SetProduct, ShowEmbeddedMenu, ShowOverlayInterface, StopShowcase } from '@3dsource/metabox-front-api';
|
|
1076
|
+
|
|
1077
|
+
interface ConfiguratorState {
|
|
1078
|
+
isLoading: boolean;
|
|
1079
|
+
error: string | null;
|
|
1080
|
+
isApiReady: boolean;
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
@Component({
|
|
1084
|
+
selector: 'app-metabox-configurator',
|
|
1085
|
+
template: `
|
|
1086
|
+
@let _state = state();
|
|
1087
|
+
<div class="configurator-container">
|
|
1088
|
+
<!-- Loading State -->
|
|
1089
|
+
@if (_state.isLoading) {
|
|
1090
|
+
<div class="loading-container">
|
|
1091
|
+
<div class="loading-content">
|
|
1092
|
+
<div class="loading-spinner"></div>
|
|
1093
|
+
<div class="loading-text">Loading configurator...</div>
|
|
1094
|
+
<div class="loading-subtext">Please wait while we initialize the 3D viewer</div>
|
|
1095
|
+
</div>
|
|
1096
|
+
</div>
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
<!-- Error State -->
|
|
1100
|
+
@if (_state.error) {
|
|
1101
|
+
<div class="error-container">
|
|
1102
|
+
<h3>Configurator Error</h3>
|
|
1103
|
+
<p>{{ _state.error }}</p>
|
|
1104
|
+
<button (click)="retryInitialization()" class="retry-button">Retry</button>
|
|
1105
|
+
</div>
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
<!-- Configurator Container -->
|
|
1109
|
+
<div [id]="containerId()" class="configurator-viewport" [hidden]="_state.isLoading || _state.error"></div>
|
|
1110
|
+
|
|
1111
|
+
<!-- Controls -->
|
|
1112
|
+
@if (_state.isApiReady) {
|
|
1113
|
+
<div class="controls">
|
|
1114
|
+
<div class="controls-title">Configurator Controls:</div>
|
|
1115
|
+
<div class="controls-buttons">
|
|
1116
|
+
<button (click)="changeProduct('541f46ab-a86c-48e3-bcfa-f92341483db3')" [disabled]="!_state.isApiReady" class="control-button">Product Change</button>
|
|
1117
|
+
<button (click)="initShowcase('685e656f-32e4-4a63-a9f3-bf3c9e440906')" [disabled]="!_state.isApiReady" class="control-button">Init showcase</button>
|
|
1118
|
+
<button (click)="stopShowcase()" [disabled]="!_state.isApiReady" class="control-button">Stop showcase</button>
|
|
1119
|
+
<button (click)="playShowcase()" [disabled]="!_state.isApiReady" class="control-button">Play showcase</button>
|
|
1120
|
+
<button (click)="pauseShowcase()" [disabled]="!_state.isApiReady" class="control-button">Pause showcase</button>
|
|
1121
|
+
<button (click)="applyMaterial('slot-1', 'material-red')" [disabled]="!_state.isApiReady" class="control-button">Red Material</button>
|
|
1122
|
+
<button (click)="applyMaterial('slot-1', 'material-blue')" [disabled]="!_state.isApiReady" class="control-button">Blue Material</button>
|
|
1123
|
+
<button (click)="getPdf()" [disabled]="!_state.isApiReady" class="control-button">Get PDF</button>
|
|
1124
|
+
<button (click)="takeScreenshot()" [disabled]="!_state.isApiReady" class="control-button screenshot-button">📸 Take Screenshot</button>
|
|
1125
|
+
</div>
|
|
1126
|
+
</div>
|
|
1127
|
+
}
|
|
1128
|
+
</div>
|
|
1129
|
+
`,
|
|
1130
|
+
styles: [
|
|
1131
|
+
`
|
|
1132
|
+
.configurator-container {
|
|
1133
|
+
width: 100%;
|
|
1134
|
+
position: relative;
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
.configurator-viewport {
|
|
1138
|
+
width: 100%;
|
|
1139
|
+
height: 600px;
|
|
1140
|
+
border: 1px solid #ddd;
|
|
1141
|
+
border-radius: 8px;
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
.loading-container {
|
|
1145
|
+
display: flex;
|
|
1146
|
+
align-items: center;
|
|
1147
|
+
justify-content: center;
|
|
1148
|
+
height: 600px;
|
|
1149
|
+
background: #f5f5f5;
|
|
1150
|
+
border: 1px solid #ddd;
|
|
1151
|
+
border-radius: 8px;
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
.loading-content {
|
|
1155
|
+
text-align: center;
|
|
1156
|
+
color: #666;
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
.loading-spinner {
|
|
1160
|
+
width: 40px;
|
|
1161
|
+
height: 40px;
|
|
1162
|
+
border: 4px solid #f3f3f3;
|
|
1163
|
+
border-top: 4px solid #007bff;
|
|
1164
|
+
border-radius: 50%;
|
|
1165
|
+
animation: spin 1s linear infinite;
|
|
1166
|
+
margin: 0 auto 20px;
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
@keyframes spin {
|
|
1170
|
+
0% {
|
|
1171
|
+
transform: rotate(0deg);
|
|
1172
|
+
}
|
|
1173
|
+
100% {
|
|
1174
|
+
transform: rotate(360deg);
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
.loading-text {
|
|
1179
|
+
font-size: 16px;
|
|
1180
|
+
font-weight: 500;
|
|
1181
|
+
margin-bottom: 8px;
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
.loading-subtext {
|
|
1185
|
+
font-size: 14px;
|
|
1186
|
+
opacity: 0.8;
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
.error-container {
|
|
1190
|
+
padding: 40px;
|
|
1191
|
+
text-align: center;
|
|
1192
|
+
background: #ffebee;
|
|
1193
|
+
border: 1px solid #ffcdd2;
|
|
1194
|
+
border-radius: 8px;
|
|
1195
|
+
color: #d32f2f;
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
.error-container h3 {
|
|
1199
|
+
margin: 0 0 16px 0;
|
|
1200
|
+
font-size: 18px;
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
.error-container p {
|
|
1204
|
+
margin: 0 0 20px 0;
|
|
1205
|
+
font-size: 14px;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
.retry-button {
|
|
1209
|
+
padding: 10px 20px;
|
|
1210
|
+
border: 1px solid #d32f2f;
|
|
1211
|
+
background: #fff;
|
|
1212
|
+
color: #d32f2f;
|
|
1213
|
+
border-radius: 4px;
|
|
1214
|
+
cursor: pointer;
|
|
1215
|
+
font-size: 14px;
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
.retry-button:hover {
|
|
1219
|
+
background: #d32f2f;
|
|
1220
|
+
color: #fff;
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
.controls {
|
|
1224
|
+
margin-top: 20px;
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
.controls-title {
|
|
1228
|
+
font-weight: bold;
|
|
1229
|
+
color: #333;
|
|
1230
|
+
margin-bottom: 10px;
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
.controls-buttons {
|
|
1234
|
+
display: flex;
|
|
1235
|
+
flex-wrap: wrap;
|
|
1236
|
+
gap: 10px;
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
.control-button {
|
|
1240
|
+
padding: 10px 15px;
|
|
1241
|
+
border: 1px solid #ccc;
|
|
1242
|
+
border-radius: 4px;
|
|
1243
|
+
background: #f5f5f5;
|
|
1244
|
+
cursor: pointer;
|
|
1245
|
+
transition: all 0.2s;
|
|
1246
|
+
font-size: 14px;
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
.control-button:hover:not(:disabled) {
|
|
1250
|
+
background: #e5e5e5;
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
.control-button:disabled {
|
|
1254
|
+
opacity: 0.5;
|
|
1255
|
+
cursor: not-allowed;
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
.screenshot-button {
|
|
1259
|
+
border-color: #007bff;
|
|
1260
|
+
background: #007bff;
|
|
1261
|
+
color: white;
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
.screenshot-button:hover:not(:disabled) {
|
|
1265
|
+
background: #0056b3;
|
|
1266
|
+
}
|
|
1267
|
+
`,
|
|
1268
|
+
],
|
|
1269
|
+
})
|
|
1270
|
+
export class MetaboxConfiguratorComponent implements OnInit {
|
|
1271
|
+
configuratorUrl = input.required<string>();
|
|
1272
|
+
stateChange = output<ConfiguratorEnvelope>();
|
|
1273
|
+
errorFired = output<string>();
|
|
1274
|
+
state = signal<ConfiguratorState>({
|
|
1275
|
+
isLoading: true,
|
|
1276
|
+
error: null,
|
|
1277
|
+
isApiReady: false,
|
|
1278
|
+
});
|
|
1279
|
+
containerId = signal(`metabox-container-${Math.random().toString(36).substring(2, 9)}`);
|
|
1280
|
+
private api: Communicator | null = null;
|
|
1281
|
+
|
|
1282
|
+
ngOnInit(): void {
|
|
1283
|
+
this.initializeConfigurator();
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
// Public methods for external control
|
|
1287
|
+
changeProduct(productId: string): void {
|
|
1288
|
+
if (!this.state().isApiReady || !this.api) {
|
|
1289
|
+
console.warn('API not ready yet');
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
try {
|
|
1294
|
+
console.log(`Changing to product: ${productId}`);
|
|
1295
|
+
this.api.sendCommandToMetaBox(new SetProduct(productId));
|
|
1296
|
+
} catch (error) {
|
|
1297
|
+
this.handleError(`Failed to change product: ${this.getErrorMessage(error)}`);
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
initShowcase(productId: string): void {
|
|
1302
|
+
if (!this.state().isApiReady || !this.api) {
|
|
1303
|
+
console.warn('API not ready yet');
|
|
1304
|
+
return;
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
try {
|
|
1308
|
+
console.log(`Init showcase for product: ${productId}`);
|
|
1309
|
+
this.api.sendCommandToMetaBox(new InitShowcase(productId));
|
|
1310
|
+
} catch (error) {
|
|
1311
|
+
this.handleError(`Failed to init showcase for product: ${this.getErrorMessage(error)}`);
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
stopShowcase(): void {
|
|
1316
|
+
if (!this.state().isApiReady || !this.api) {
|
|
1317
|
+
console.warn('API not ready yet');
|
|
1318
|
+
return;
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
try {
|
|
1322
|
+
console.log(`Stop showcase`);
|
|
1323
|
+
this.api.sendCommandToMetaBox(new StopShowcase());
|
|
1324
|
+
} catch (error) {
|
|
1325
|
+
this.handleError(`Failed to init showcase for product: ${this.getErrorMessage(error)}`);
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
playShowcase(): void {
|
|
1330
|
+
if (!this.state().isApiReady || !this.api) {
|
|
1331
|
+
console.warn('API not ready yet');
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
try {
|
|
1336
|
+
console.log(`Play showcase`);
|
|
1337
|
+
this.api.sendCommandToMetaBox(new PlayShowcase());
|
|
1338
|
+
} catch {
|
|
1339
|
+
this.handleError(`Failed to play showcase`);
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
pauseShowcase(): void {
|
|
1344
|
+
if (!this.state().isApiReady || !this.api) {
|
|
1345
|
+
console.warn('API not ready yet');
|
|
1346
|
+
return;
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
try {
|
|
1350
|
+
console.log(`Pause showcase`);
|
|
1351
|
+
this.api.sendCommandToMetaBox(new PauseShowcase());
|
|
1352
|
+
} catch {
|
|
1353
|
+
this.handleError(`Failed to pause showcase`);
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
applyMaterial(slotId: string, materialId: string): void {
|
|
1358
|
+
if (!this.state().isApiReady || !this.api) {
|
|
1359
|
+
console.warn('API not ready yet');
|
|
1360
|
+
return;
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
try {
|
|
1364
|
+
console.log(`Applying material ${materialId} to slot ${slotId}`);
|
|
1365
|
+
this.api.sendCommandToMetaBox(new SetMaterial(slotId, materialId));
|
|
1366
|
+
} catch (error) {
|
|
1367
|
+
this.handleError(`Failed to apply material: ${this.getErrorMessage(error)}`);
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
takeScreenshot(): void {
|
|
1372
|
+
if (!this.state().isApiReady || !this.api) {
|
|
1373
|
+
console.warn('API not ready yet');
|
|
1374
|
+
return;
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
try {
|
|
1378
|
+
console.log('Taking screenshot...');
|
|
1379
|
+
this.api.sendCommandToMetaBox(new GetScreenshot('image/png', { x: 1920, y: 1080 }));
|
|
1380
|
+
} catch (error) {
|
|
1381
|
+
this.handleError(`Failed to take screenshot: ${this.getErrorMessage(error)}`);
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
getPdf(): void {
|
|
1386
|
+
if (!this.state().isApiReady || !this.api) {
|
|
1387
|
+
console.warn('API not ready yet');
|
|
1388
|
+
return;
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
try {
|
|
1392
|
+
console.log('Generating PDF...');
|
|
1393
|
+
this.api.sendCommandToMetaBox(new GetPdf());
|
|
1394
|
+
} catch (error) {
|
|
1395
|
+
this.handleError(`Failed to generating pdf: ${this.getErrorMessage(error)}`);
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
retryInitialization(): void {
|
|
1400
|
+
this.updateState({ error: null });
|
|
1401
|
+
this.initializeConfigurator();
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
sendInitCommands() {
|
|
1405
|
+
if (!this.api) {
|
|
1406
|
+
return;
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
this.api.sendCommandToMetaBox(new ShowEmbeddedMenu(true));
|
|
1410
|
+
this.api.sendCommandToMetaBox(new ShowOverlayInterface(true));
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
private initializeConfigurator() {
|
|
1414
|
+
try {
|
|
1415
|
+
this.updateState({ isLoading: true, error: null });
|
|
1416
|
+
|
|
1417
|
+
integrateMetaBox(this.configuratorUrl(), this.containerId(), (apiInstance) => {
|
|
1418
|
+
try {
|
|
1419
|
+
this.api = apiInstance;
|
|
1420
|
+
this.updateState({ isLoading: false, isApiReady: true });
|
|
1421
|
+
this.sendInitCommands();
|
|
1422
|
+
this.setupEventListeners();
|
|
1423
|
+
|
|
1424
|
+
console.log('Angular Metabox Configurator initialized successfully');
|
|
1425
|
+
} catch (error) {
|
|
1426
|
+
this.handleError(`Failed to set up API: ${this.getErrorMessage(error)}`);
|
|
1427
|
+
}
|
|
1428
|
+
});
|
|
1429
|
+
} catch (error) {
|
|
1430
|
+
this.handleError(`Failed to initialize configurator: ${this.getErrorMessage(error)}`);
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
private setupEventListeners(): void {
|
|
1435
|
+
if (!this.api) {
|
|
1436
|
+
return;
|
|
1437
|
+
}
|
|
1438
|
+
// Listen for configurator state changes
|
|
1439
|
+
this.api.addEventListener('basicConfiguratorDataWithState', (data: ConfiguratorEnvelope) => {
|
|
1440
|
+
console.log('Configurator state updated:', data);
|
|
1441
|
+
this.stateChange.emit(data);
|
|
1442
|
+
});
|
|
1443
|
+
|
|
1444
|
+
// Listen for screenshot completion
|
|
1445
|
+
this.api.addEventListener('screenshot', (imageData: string | null) => {
|
|
1446
|
+
console.log(`Screenshot captured successfully ${imageData ?? ''}`);
|
|
1447
|
+
saveImage(imageData ?? '', `configurator-screenshot-${Date.now()}.png`);
|
|
1448
|
+
});
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
private updateState(updates: Partial<ConfiguratorState>): void {
|
|
1452
|
+
this.state.set({ ...this.state(), ...updates });
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
private handleError(errorMessage: string): void {
|
|
1456
|
+
console.error('Metabox Configurator Error:', errorMessage);
|
|
1457
|
+
this.updateState({ error: errorMessage, isLoading: false });
|
|
1458
|
+
this.errorFired.emit(errorMessage);
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
private getErrorMessage(error: any): string {
|
|
1462
|
+
if (error instanceof Error) {
|
|
1463
|
+
return error.message;
|
|
1464
|
+
}
|
|
1465
|
+
return String(error);
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
```
|
|
1469
|
+
|
|
1470
|
+
**Key Features of this Angular Example:**
|
|
1471
|
+
|
|
1472
|
+
- **Comprehensive Error Handling**: Try-catch blocks and user-friendly error states
|
|
1473
|
+
- **Proper Lifecycle Management**: OnDestroy implementation with cleanup
|
|
1474
|
+
- **Loading States**: Visual feedback with spinner during initialization
|
|
1475
|
+
- **TypeScript Integration**: Full type safety with proper interfaces
|
|
1476
|
+
- **Unique Container IDs**: Automatic ID generation to avoid conflicts
|
|
1477
|
+
- **Event Outputs**: Emits state changes and errors to parent components
|
|
1478
|
+
- **Responsive Design**: Clean, accessible button layout with proper styling
|
|
1479
|
+
- **Component Cleanup**: Prevents memory leaks and handles component destruction
|
|
1480
|
+
|
|
1481
|
+
**Usage Example:**
|
|
1482
|
+
|
|
1483
|
+
```typescript
|
|
1484
|
+
// app.component.ts
|
|
1485
|
+
import { Component } from '@angular/core';
|
|
1486
|
+
import { ConfiguratorEnvelope } from '@3dsource/metabox-front-api';
|
|
1487
|
+
|
|
1488
|
+
@Component({
|
|
1489
|
+
selector: 'app-root',
|
|
1490
|
+
template: `
|
|
1491
|
+
<div class="app-container">
|
|
1492
|
+
<h1>My Product Configurator</h1>
|
|
1493
|
+
<app-metabox-configurator [configuratorUrl]="configuratorUrl" (stateChange)="onConfiguratorStateChange($event)" (errorFired)="onConfiguratorError($event)"></app-metabox-configurator>
|
|
1494
|
+
</div>
|
|
1495
|
+
`,
|
|
1496
|
+
styles: [
|
|
1497
|
+
`
|
|
1498
|
+
.app-container {
|
|
1499
|
+
max-width: 1200px;
|
|
1500
|
+
margin: 0 auto;
|
|
1501
|
+
padding: 20px;
|
|
1502
|
+
}
|
|
1503
|
+
`,
|
|
1504
|
+
],
|
|
1505
|
+
})
|
|
1506
|
+
export class AppComponent {
|
|
1507
|
+
configuratorUrl = 'https://metabox.3dsource.com/metabox-configurator/basic/your-configurator-id';
|
|
1508
|
+
|
|
1509
|
+
onConfiguratorStateChange(data: ConfiguratorEnvelope): void {
|
|
1510
|
+
console.log('Configurator state changed:', data);
|
|
1511
|
+
// Update your application state based on configurator changes
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
onConfiguratorError(error: string): void {
|
|
1515
|
+
console.error('Configurator error:', error);
|
|
1516
|
+
// Handle errors (show notifications, log to analytics, etc.)
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
```
|
|
1520
|
+
|
|
1521
|
+
**To Use This Component:**
|
|
1522
|
+
|
|
1523
|
+
1. Install the package: `npm install @3dsource/metabox-front-api`
|
|
1524
|
+
2. Import the component in your Angular module
|
|
1525
|
+
3. Replace `'your-configurator-id'` with your actual configurator ID
|
|
1526
|
+
4. Replace placeholder IDs with your real product, material, and slot IDs
|
|
1527
|
+
5. Customize the styling by modifying the component styles
|
|
1528
|
+
|
|
1529
|
+
## Best Practices
|
|
1530
|
+
|
|
1531
|
+
### 1. Error Handling
|
|
1532
|
+
|
|
1533
|
+
Always implement proper error handling when working with the API:
|
|
1534
|
+
|
|
1535
|
+
```typescript
|
|
1536
|
+
integrateMetaBox(configuratorUrl, containerId, (api) => {
|
|
1537
|
+
try {
|
|
1538
|
+
// Set up event listeners with error handling
|
|
1539
|
+
api.addEventListener('basicConfiguratorDataWithState', (data) => {
|
|
1540
|
+
try {
|
|
1541
|
+
handleConfiguratorUpdate(data);
|
|
1542
|
+
} catch (error) {
|
|
1543
|
+
console.error('Error handling configurator update:', error);
|
|
1544
|
+
}
|
|
1545
|
+
});
|
|
1546
|
+
|
|
1547
|
+
// Send commands with error handling
|
|
1548
|
+
api.sendCommandToMetaBox(new SetProduct(productId));
|
|
1549
|
+
} catch (error) {
|
|
1550
|
+
console.error('Error initializing configurator:', error);
|
|
1551
|
+
}
|
|
1552
|
+
});
|
|
1553
|
+
```
|
|
1554
|
+
|
|
1555
|
+
### 2. State Management
|
|
1556
|
+
|
|
1557
|
+
Keep track of the configurator state for a better user experience:
|
|
1558
|
+
|
|
1559
|
+
```typescript
|
|
1560
|
+
class ConfiguratorStateManager {
|
|
1561
|
+
private currentState = {
|
|
1562
|
+
product: null,
|
|
1563
|
+
materials: {},
|
|
1564
|
+
environment: null,
|
|
1565
|
+
};
|
|
1566
|
+
|
|
1567
|
+
updateState(data) {
|
|
1568
|
+
this.currentState = { ...this.currentState, ...data };
|
|
1569
|
+
this.saveStateToLocalStorage();
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
saveStateToLocalStorage() {
|
|
1573
|
+
localStorage.setItem('configuratorState', JSON.stringify(this.currentState));
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
loadStateFromLocalStorage() {
|
|
1577
|
+
const saved = localStorage.getItem('configuratorState');
|
|
1578
|
+
return saved ? JSON.parse(saved) : null;
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
```
|
|
1582
|
+
|
|
1583
|
+
### 3. Performance Optimization
|
|
1584
|
+
|
|
1585
|
+
- **Debounce rapid commands**: Avoid sending too many commands in quick succession
|
|
1586
|
+
|
|
1587
|
+
```typescript
|
|
1588
|
+
// Debounce example
|
|
1589
|
+
const debouncedMaterialChange = debounce((slotId, materialId) => {
|
|
1590
|
+
api.sendCommandToMetaBox(new SetMaterial(slotId, materialId));
|
|
1591
|
+
}, 300);
|
|
1592
|
+
```
|
|
1593
|
+
|
|
1594
|
+
### 4. Responsive Design
|
|
1595
|
+
|
|
1596
|
+
Ensure the configurator works well on different screen sizes:
|
|
1597
|
+
|
|
1598
|
+
```css
|
|
1599
|
+
#embed3DSource {
|
|
1600
|
+
width: 100%;
|
|
1601
|
+
height: 60vh;
|
|
1602
|
+
min-height: 400px;
|
|
1603
|
+
max-height: 800px;
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
@media (max-width: 768px) {
|
|
1607
|
+
#embed3DSource {
|
|
1608
|
+
height: 50vh;
|
|
1609
|
+
min-height: 300px;
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
```
|
|
1613
|
+
|
|
1614
|
+
### 5. Loading States
|
|
1615
|
+
|
|
1616
|
+
Provide feedback to users while the configurator loads:
|
|
1617
|
+
|
|
1618
|
+
```typescript
|
|
1619
|
+
const showLoadingState = () => {
|
|
1620
|
+
document.getElementById('loading').style.display = 'block';
|
|
1621
|
+
};
|
|
1622
|
+
|
|
1623
|
+
const hideLoadingState = () => {
|
|
1624
|
+
document.getElementById('loading').style.display = 'none';
|
|
1625
|
+
};
|
|
1626
|
+
|
|
1627
|
+
integrateMetaBox(configuratorUrl, containerId, (api) => {
|
|
1628
|
+
hideLoadingState();
|
|
1629
|
+
// Continue with initialization
|
|
1630
|
+
});
|
|
1631
|
+
```
|
|
1632
|
+
|
|
1633
|
+
## Troubleshooting
|
|
1634
|
+
|
|
1635
|
+
### Common Issues
|
|
1636
|
+
|
|
1637
|
+
#### 1. Configurator Not Loading
|
|
1638
|
+
|
|
1639
|
+
**Problem**: The configurator iframe doesn't appear or shows a blank screen.
|
|
1640
|
+
|
|
1641
|
+
**Solutions**:
|
|
1642
|
+
|
|
1643
|
+
- Verify the configurator URL is correct and accessible
|
|
1644
|
+
- Ensure the container element exists in the DOM before calling `integrateMetaBox`
|
|
1645
|
+
- Check that you're using HTTPS (required for Unreal Engine)
|
|
1646
|
+
- Verify your configurator ID is valid
|
|
1647
|
+
|
|
1648
|
+
```typescript
|
|
1649
|
+
// Check if container exists
|
|
1650
|
+
const container = document.getElementById('embed3DSource');
|
|
1651
|
+
if (!container) {
|
|
1652
|
+
console.error('Container element not found');
|
|
1653
|
+
return;
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1656
|
+
// Verify HTTPS
|
|
1657
|
+
if (location.protocol !== 'https:' && location.hostname !== 'localhost') {
|
|
1658
|
+
console.warn('HTTPS is required for Metabox configurator');
|
|
1659
|
+
}
|
|
1660
|
+
```
|
|
1661
|
+
|
|
1662
|
+
#### 2. Commands Not Working
|
|
1663
|
+
|
|
1664
|
+
**Problem**: Commands sent to the configurator don't have any effect.
|
|
1665
|
+
|
|
1666
|
+
**Solutions**:
|
|
1667
|
+
|
|
1668
|
+
- Ensure the API is fully initialized before sending commands
|
|
1669
|
+
- Check that product/material/environment IDs are correct
|
|
1670
|
+
- Verify the configurator supports the specific command
|
|
1671
|
+
|
|
1672
|
+
```typescript
|
|
1673
|
+
let apiReady = false;
|
|
1674
|
+
|
|
1675
|
+
integrateMetaBox(configuratorUrl, containerId, (api) => {
|
|
1676
|
+
apiReady = true;
|
|
1677
|
+
// Now it's safe to send commands
|
|
1678
|
+
});
|
|
1679
|
+
|
|
1680
|
+
// Wait for API to be ready
|
|
1681
|
+
const sendCommand = (command) => {
|
|
1682
|
+
if (apiReady && api) {
|
|
1683
|
+
api.sendCommandToMetaBox(command);
|
|
1684
|
+
} else {
|
|
1685
|
+
console.warn('API not ready yet');
|
|
1686
|
+
}
|
|
1687
|
+
};
|
|
1688
|
+
```
|
|
1689
|
+
|
|
1690
|
+
#### 3. Events Not Firing
|
|
1691
|
+
|
|
1692
|
+
**Problem**: Event listeners don't receive events from the configurator.
|
|
1693
|
+
|
|
1694
|
+
**Solutions**:
|
|
1695
|
+
|
|
1696
|
+
- Set up event listeners immediately after API initialization
|
|
1697
|
+
- Check event names for typos
|
|
1698
|
+
- Ensure the configurator is configured to send the expected events
|
|
1699
|
+
|
|
1700
|
+
```typescript
|
|
1701
|
+
integrateMetaBox(configuratorUrl, containerId, (api) => {
|
|
1702
|
+
// Set up listeners immediately
|
|
1703
|
+
api.addEventListener('basicConfiguratorDataWithState', (data) => {
|
|
1704
|
+
console.log('Event received:', data);
|
|
1705
|
+
});
|
|
1706
|
+
|
|
1707
|
+
// Test if events are working
|
|
1708
|
+
setTimeout(() => {
|
|
1709
|
+
console.log('Testing event system...');
|
|
1710
|
+
api.sendCommandToMetaBox(new SetProduct('test-product'));
|
|
1711
|
+
}, 1000);
|
|
1712
|
+
});
|
|
1713
|
+
```
|
|
1714
|
+
|
|
1715
|
+
#### 4. Screenshot/PDF Generation Issues
|
|
1716
|
+
|
|
1717
|
+
**Problem**: Screenshots or PDFs are not generated or are of poor quality.
|
|
1718
|
+
|
|
1719
|
+
**Solutions**:
|
|
1720
|
+
|
|
1721
|
+
- Wait for the scene to fully load before capturing
|
|
1722
|
+
- Use appropriate dimensions for your use case
|
|
1723
|
+
- Ensure the configurator viewport is visible (not hidden or zero-sized)
|
|
1724
|
+
|
|
1725
|
+
```typescript
|
|
1726
|
+
// Wait for scene to load before screenshot
|
|
1727
|
+
api.addEventListener('basicConfiguratorDataWithState', (data) => {
|
|
1728
|
+
// Scene is loaded, safe to take screenshot
|
|
1729
|
+
setTimeout(() => {
|
|
1730
|
+
api.sendCommandToMetaBox(new GetScreenshot('image/png', { x: 1920, y: 1080 }));
|
|
1731
|
+
}, 500);
|
|
1732
|
+
});
|
|
1733
|
+
```
|
|
1734
|
+
|
|
1735
|
+
### Debug Mode
|
|
1736
|
+
|
|
1737
|
+
Enable debug logging to troubleshoot issues:
|
|
1738
|
+
|
|
1739
|
+
```typescript
|
|
1740
|
+
// Enable debug mode (if available)
|
|
1741
|
+
window.METABOX_DEBUG = true;
|
|
1742
|
+
|
|
1743
|
+
integrateMetaBox(configuratorUrl, containerId, (api) => {
|
|
1744
|
+
console.log('API initialized:', api);
|
|
1745
|
+
|
|
1746
|
+
// Log all events
|
|
1747
|
+
api.addEventListener('basicConfiguratorDataWithState', (data) => {
|
|
1748
|
+
console.log('State update:', data);
|
|
1749
|
+
});
|
|
1750
|
+
|
|
1751
|
+
api.addEventListener('fastRender', (imageData) => {
|
|
1752
|
+
console.log('Render completed, data length:', imageData.length);
|
|
1753
|
+
});
|
|
1754
|
+
});
|
|
1755
|
+
```
|
|
1756
|
+
|
|
1757
|
+
### Getting Help
|
|
1758
|
+
|
|
1759
|
+
If you're still experiencing issues:
|
|
1760
|
+
|
|
1761
|
+
1. Check the browser console for error messages
|
|
1762
|
+
2. Verify your configurator URL in a separate browser tab
|
|
1763
|
+
3. Test with a minimal example to isolate the issue
|
|
1764
|
+
4. Contact support with:
|
|
1765
|
+
|
|
1766
|
+
- Browser version and type
|
|
1767
|
+
- Configurator URL (if possible)
|
|
1768
|
+
- Console error messages
|
|
1769
|
+
- Steps to reproduce the issue
|
|
215
1770
|
|
|
216
|
-
|
|
217
|
-
saveImage(renderData: string, fileName: string): void;
|
|
218
|
-
```
|
|
1771
|
+
## Browser Compatibility
|
|
219
1772
|
|
|
220
|
-
|
|
1773
|
+
### Supported Browsers
|
|
221
1774
|
|
|
222
|
-
|
|
223
|
-
- **Usage**:
|
|
1775
|
+
The Metabox Front API supports modern browsers with ES6 module support:
|
|
224
1776
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
1777
|
+
- **Chrome**: 61+ ✅
|
|
1778
|
+
- **Firefox**: 60+ ✅
|
|
1779
|
+
- **Safari**: 10.1+ ✅
|
|
1780
|
+
- **Edge**: 16+ ✅
|
|
1781
|
+
- **Opera**: 48+ ✅
|
|
228
1782
|
|
|
229
|
-
|
|
1783
|
+
### Requirements
|
|
230
1784
|
|
|
231
|
-
- **
|
|
232
|
-
- **
|
|
1785
|
+
- **ES6 Modules**: Required for importing the API
|
|
1786
|
+
- **WebGL**: Required for 3D rendering
|
|
1787
|
+
- **HTTPS**: Required for Unreal Engine integration
|
|
1788
|
+
- **Modern JavaScript**: ES2017+ features are used
|
|
233
1789
|
|
|
234
|
-
|
|
235
|
-
api.sendCommandToMetaBox(new ApplyZoom(zoom: number));
|
|
236
|
-
```
|
|
1790
|
+
## Contributing
|
|
237
1791
|
|
|
238
|
-
|
|
1792
|
+
We welcome contributions to improve the Metabox Front API! Here's how you can help:
|
|
239
1793
|
|
|
240
|
-
|
|
241
|
-
- **Usage**:
|
|
1794
|
+
### Development Setup
|
|
242
1795
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
1796
|
+
1. Clone the repository
|
|
1797
|
+
2. Install dependencies: `npm install`
|
|
1798
|
+
3. Build the project: `npm run build`
|
|
1799
|
+
4. Run tests: `npm test`
|
|
246
1800
|
|
|
247
|
-
|
|
1801
|
+
### Guidelines
|
|
248
1802
|
|
|
249
|
-
-
|
|
250
|
-
-
|
|
1803
|
+
- Follow TypeScript best practices
|
|
1804
|
+
- Add tests for new features
|
|
1805
|
+
- Update documentation for API changes
|
|
1806
|
+
- Use semantic commit messages
|
|
251
1807
|
|
|
252
|
-
|
|
253
|
-
api.sendCommandToMetaBox(new PlayShowcase());
|
|
254
|
-
```
|
|
1808
|
+
### Reporting Issues
|
|
255
1809
|
|
|
256
|
-
|
|
1810
|
+
When reporting issues, please include:
|
|
257
1811
|
|
|
258
|
-
-
|
|
259
|
-
-
|
|
1812
|
+
- Browser version and operating system
|
|
1813
|
+
- Minimal code example reproducing the issue
|
|
1814
|
+
- Expected vs actual behavior
|
|
1815
|
+
- Console error messages (if any)
|
|
260
1816
|
|
|
261
|
-
|
|
262
|
-
api.sendCommandToMetaBox(new PauseShowcase());
|
|
263
|
-
```
|
|
1817
|
+
### Feature Requests
|
|
264
1818
|
|
|
265
|
-
|
|
1819
|
+
We're always looking to improve the API. Feel free to suggest new features or enhancements through our issue tracker.
|
|
266
1820
|
|
|
267
|
-
|
|
268
|
-
- **Usage**:
|
|
1821
|
+
---
|
|
269
1822
|
|
|
270
|
-
|
|
271
|
-
api.sendCommandToMetaBox(new StopShowcase());
|
|
272
|
-
```
|
|
1823
|
+
**Need help?** Contact our support team or check our documentation for more detailed guides and tutorials.
|