@playcanvas/web-components 0.1.1 → 0.1.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 CHANGED
@@ -24,7 +24,242 @@ UMD:
24
24
  <script src="https://cdn.jsdelivr.net/npm/@playcanvas/web-components@0.1.0"></script>
25
25
  ```
26
26
 
27
- ## Example 📖
27
+ ## Tag Reference 📖
28
+
29
+ Table of contents:
30
+
31
+ - [pc-app](#pc-app)
32
+ - [pc-asset](#pc-asset)
33
+ - [pc-camera](#pc-camera)
34
+ - [pc-collision](#pc-collision)
35
+ - [pc-element](#pc-element)
36
+ - [pc-entity](#pc-entity)
37
+ - [pc-light](#pc-light)
38
+ - [pc-listener](#pc-listener)
39
+ - [pc-module](#pc-module)
40
+ - [pc-render](#pc-render)
41
+ - [pc-rigidbody](#pc-rigidbody)
42
+ - [pc-scene](#pc-scene)
43
+ - [pc-script](#pc-script)
44
+ - [pc-scripts](#pc-scripts)
45
+ - [pc-sky](#pc-sky)
46
+ - [pc-sound](#pc-sound)
47
+ - [pc-sounds](#pc-sounds)
48
+ - [pc-splat](#pc-splat)
49
+
50
+ ### pc-app
51
+
52
+ The `pc-app` tag is the root element for your PlayCanvas application. It is used to initialize the PlayCanvas application and provide a container for your scene.
53
+
54
+ | Attribute | Description |
55
+ | --- | --- |
56
+ | `high-resolution` | Valueless attribute. If present, enables high-resolution mode. |
57
+
58
+ ### pc-asset
59
+
60
+ The `pc-asset` tag is used to define an asset. It must be a direct child of `pc-app`.
61
+
62
+ | Attribute | Description |
63
+ | --- | --- |
64
+ | `id` | The ID of the asset. This is used to reference the asset in scripts. |
65
+ | `src` | The path to the asset. |
66
+ | `preload` | Valueless attribute. If present, the asset is loaded immediately. |
67
+ | `type` | The type of asset. If not specified, the type is inferred from the file extension. Can be: `audio`, `binary`, `css`, `container`, `gsplat`, `html`, `json`, `script`, `shader`, `text`, `texture`. |
68
+
69
+ ### pc-camera
70
+
71
+ The `pc-camera` tag is used to define a camera component. It must be a direct child of an `pc-entity`.
72
+
73
+ | Attribute | Description |
74
+ | --- | --- |
75
+ | `clear-color` | The background color of the camera. Can be a comma-separated list of R, G, B, and A values or a hex color code. If not specified, `1,1,1,1` is used. |
76
+ | `far-clip` | The far clipping plane of the camera. If not specified, `1000` is used. |
77
+ | `fov` | The field of view of the camera. If not specified, `45` is used. |
78
+ | `near-clip` | The near clipping plane of the camera. If not specified, `0.1` is used. |
79
+ | `orthographic` | Valueless attribute. If present, the camera uses an orthographic projection. |
80
+ | `ortho-height` | The height of the orthographic projection. If not specified, `10` is used. |
81
+
82
+ ### pc-collision
83
+
84
+ The `pc-collision` tag is used to define a collision component. It must be a direct child of a `pc-entity`.
85
+
86
+ | Attribute | Description |
87
+ | --- | --- |
88
+ | `axis` | The axis of the collision component. If not specified, `1` is used (Y-axis). |
89
+ | `convex-hull` | Valueless attribute. If present, the collision component uses a convex hull. |
90
+ | `enabled` | Enabled state of the collision component. If not specified, `true` is used. |
91
+ | `half-extents` | The half-extents of the collision component. Specified as a comma-separated list of X, Y, and Z values. If not specified, `0.5,0.5,0.5` is used. |
92
+ | `height` | The height of the collision component. If not specified, `2` is used. |
93
+ | `radius` | The radius of the collision component. If not specified, `0.5` is used. |
94
+ | `type` | The type of collision component. Can be `box`, `capsule`, `cone`, `cylinder` or `sphere`. |
95
+
96
+ ### pc-element
97
+
98
+ The `pc-element` tag is used to define an element component. It must be a direct child of a `pc-entity`.
99
+
100
+ | Attribute | Description |
101
+ | --- | --- |
102
+ | `anchor` | The anchor of the element component. Specified as a comma-separated list of X, Y, Z, and W values. If not specified, `0,0,0,1` is used. |
103
+ | `asset` | A string that should match the `id` of a `pc-asset` tag that has a type of `font`. |
104
+ | `auto-width` | Valueless attribute. If present, the element component automatically adjusts its width. |
105
+ | `color` | The color of the element component. Can be a comma-separated list of R, G, B, and A values or a hex color code. If not specified, `1,1,1,1` is used. |
106
+ | `font-size` | The font size of the element component. If not specified, `16` is used. |
107
+ | `line-height` | The line height of the element component. If not specified, `1.2` is used. |
108
+ | `pivot` | The pivot of the element component. Specified as a comma-separated list of X and Y values. If not specified, `0.5,0.5` is used. |
109
+ | `text` | The text of the element component. |
110
+ | `type` | The type of element component. Can be `group`, `image` or `text`. If not specified, `group` is used. |
111
+ | `width` | The width of the element component. If not specified, `0` is used. |
112
+ | `wrap-lines` | Valueless attribute. If present, the element component wraps lines. |
113
+
114
+ ### pc-entity
115
+
116
+ The `pc-entity` tag is used to define an entity. It must be a direct child of `pc-scene` or another `pc-entity`.
117
+
118
+ | Attribute | Description |
119
+ | --- | --- |
120
+ | `enabled` | Enabled state of the entity. If not specified, `true` is used. |
121
+ | `name` | The name of the entity. |
122
+ | `position` | The position of the entity. Specified as a comma-separated list of X, Y, and Z values. If not specified, `0,0,0` is used. |
123
+ | `rotation` | The rotation of the entity. Specified as a comma-separated list of X, Y, and Z Euler angles in degrees. If not specified, `0,0,0` is used. |
124
+ | `scale` | The scale of the entity. Specified as a comma-separated list of X, Y, and Z values. If not specified, `1,1,1` is used. |
125
+ | `tags` | A comma-separated list of tags for the entity. |
126
+
127
+ ### pc-light
128
+
129
+ The `pc-light` tag is used to define a light component. It must be a direct child of a `pc-entity`.
130
+
131
+ | Attribute | Description |
132
+ | --- | --- |
133
+ | `color` | The color of the light. Can be a comma-separated list of R, G, B values or a hex color code. If not specified, `1,1,1` is used. |
134
+ | `cast-shadows` | Valueless attribute. If present, the light casts shadows. |
135
+ | `inner-cone-angle` | The angle of the light's inner cone. If not specified, `40` is used. |
136
+ | `intensity` | The intensity of the light. If not specified, `1` is used. |
137
+ | `normal-offset-bias` | The bias of the light's normal offset. If not specified, `0.05` is used. |
138
+ | `outer-cone-angle` | The angle of the light's outer cone. If not specified, `45` is used. |
139
+ | `range` | The range of the light. If not specified, `10` is used. |
140
+ | `shadow-bias` | The bias of the light's shadows. If not specified, `0.2` is used. |
141
+ | `shadow-distance` | The distance at which the light's shadows are no longer rendered. If not specified, `16` is used. |
142
+ | `type` | The type of light. Can be `directional`, `point` or `omni`. |
143
+
144
+ ### pc-listener
145
+
146
+ The `pc-listener` tag is used to define a listener component. It must be a direct child of a `pc-entity`. It has no attributes.
147
+
148
+ ### pc-module
149
+
150
+ The `pc-module` tag is used to define a WASM module. It must be a direct child of `pc-app`.
151
+
152
+ | Attribute | Description |
153
+ | --- | --- |
154
+ | `name` | The name of the module. This is used to reference the module in scripts. |
155
+ | `glue` | The path to the glue code for the module. |
156
+ | `wasm` | The path to the WASM file for the module. |
157
+ | `fallback` | The path to the fallback (asm.js) code for the module. |
158
+
159
+ ### pc-render
160
+
161
+ The `pc-render` tag is used to define a render component that render a 3D primitive. It must be a direct child of a `pc-entity`.
162
+
163
+ | Attribute | Description |
164
+ | --- | --- |
165
+ | `type` | The type of render component. Can be `box`, `capsule`, `cone`, `cylinder`, `plane` or `sphere`. |
166
+ | `cast-shadows` | Valueless attribute. If present, the render component casts shadows. |
167
+ | `receive-shadows` | Valueless attribute. If present, the render component receives shadows. |
168
+
169
+ ### pc-rigidbody
170
+
171
+ The `pc-rigidbody` tag is used to define a rigidbody component. It must be a direct child of a `pc-entity`.
172
+
173
+ | Attribute | Description |
174
+ | --- | --- |
175
+ | `angular-damping` | The angular damping of the rigidbody. If not specified, `0` is used. |
176
+ | `angular-factor` | The angular factor of the rigidbody. Specified as a comma-separated list of X, Y, and Z values. If not specified, `1,1,1` is used. |
177
+ | `friction` | The friction of the rigidbody. If not specified, `0.5` is used. |
178
+ | `linear-damping` | The linear damping of the rigidbody. If not specified, `0` is used. |
179
+ | `linear-factor` | The linear factor of the rigidbody. Specified as a comma-separated list of X, Y, and Z values. If not specified, `1,1,1` is used. |
180
+ | `mass` | The mass of the rigidbody. If not specified, `1` is used. |
181
+ | `restitution` | The restitution of the rigidbody. If not specified, `0` is used. |
182
+ | `rolling-friction` | The rolling friction of the rigidbody. If not specified, `0` is used. |
183
+ | `type` | The type of rigidbody component. Can be `static`, `kinematic` or `dynamic`. |
184
+
185
+ ### pc-scene
186
+
187
+ The `pc-scene` tag is used to define the scene. It must be a direct child of `pc-app`.
188
+
189
+ | Attribute | Description |
190
+ | --- | --- |
191
+ | `fog` | The type of fog to use. Can be `linear`, `exp`, or `exp2`. |
192
+ | `fog-color` | The color of the fog. Can be a CSS color string or a hex color code. |
193
+ | `fog-start` | The start distance of the fog. |
194
+ | `fog-end` | The end distance of the fog. |
195
+ | `fog-density` | The density of the fog. |
196
+
197
+ ### pc-script
198
+
199
+ The `pc-script` tag is used to define a script. It must be a direct child of a `pc-scripts` component.
200
+
201
+ | Attribute | Description |
202
+ | --- | --- |
203
+ | `attributes` | A JSON string of attributes for the script. |
204
+ | `enabled` | Enabled state of the script. If not specified, `true` is used. |
205
+ | `name` | The name of the script. |
206
+
207
+ ### pc-scripts
208
+
209
+ The `pc-scripts` tag is used to define a scripts component. It must be a direct child of a `pc-entity`.
210
+
211
+ | Attribute | Description |
212
+ | --- | --- |
213
+ | `enabled` | Enabled state of the scripts component. If not specified, `true` is used. |
214
+
215
+ ### pc-sky
216
+
217
+ The `pc-sky` tag is used to define a sky component. It must be a direct child of a `pc-scene`.
218
+
219
+ | Attribute | Description |
220
+ | --- | --- |
221
+ | `asset` | A string that should match the `id` of a `pc-asset` tag that has a type of `texture`. |
222
+ | `intensity` | The intensity of the sky. If not specified, `1` is used. |
223
+ | `level` | The mipmap level used to render the sky. If not specified, `0` is used (base mip level). |
224
+ | `rotation` | The rotation of the sky. Specified as a comma-separated list of X, Y, and Z values. If not specified, `0,0,0` is used. |
225
+ | `solid-color` | Valueless attribute. If present, the skybox itself is not rendered but is still used for environment lighting. |
226
+
227
+ ### pc-sound
228
+
229
+ The `pc-sound` tag is used to define a sound. It must be a direct child of a `pc-sounds`.
230
+
231
+ | Attribute | Description |
232
+ | --- | --- |
233
+ | `asset` | A string that should match the `id` of a `pc-asset` tag that has a type of `audio`. |
234
+ | `auto-play` | Valueless attribute. If present, the sound slot plays automatically. |
235
+ | `duration` | The duration of the sound slot. |
236
+ | `loop` | Valueless attribute. If present, the sound slot loops. |
237
+ | `name` | The name of the sound slot. |
238
+ | `overlap` | Valueless attribute. If present, the sound slot overlaps. |
239
+ | `pitch` | The pitch of the sound slot. If not specified, `1` is used. |
240
+ | `start-time` | The start time of the sound slot. If not specified, `0` is used. |
241
+ | `volume` | The volume of the sound slot. If not specified, `1` is used. |
242
+
243
+ ### pc-sounds
244
+
245
+ The `pc-sounds` tag is used to define a sound component. It must be a direct child of a `pc-entity`.
246
+
247
+ | Attribute | Description |
248
+ | --- | --- |
249
+ | `pitch` | The pitch of the sound. If not specified, `1` is used. |
250
+ | `positional` | Valueless attribute. If present, the sound is positional. |
251
+ | `volume` | The volume of the sound. If not specified, `1` is used. |
252
+
253
+ ### pc-splat
254
+
255
+ The `pc-splat` tag is used to define a splat component. It must be a direct child of a `pc-entity`.
256
+
257
+ | Attribute | Description |
258
+ | --- | --- |
259
+ | `asset` | A string that should match the `id` of a `pc-asset` tag that has a type of `gsplat`. |
260
+ | `enabled` | Enabled state of the splat component. If not specified, `true` is used. |
261
+
262
+ ## Example
28
263
 
29
264
  Below is a basic example of how to use PlayCanvas Web Components to create a simple 3D scene (a spinning cube):
30
265
 
@@ -1,12 +1,37 @@
1
1
  import { ScriptComponent } from 'playcanvas';
2
2
  import { ComponentElement } from './component';
3
+ interface ScriptAttributesChangeEvent extends CustomEvent {
4
+ detail: {
5
+ attributes: any;
6
+ };
7
+ }
8
+ interface ScriptEnableChangeEvent extends CustomEvent {
9
+ detail: {
10
+ enabled: boolean;
11
+ };
12
+ }
13
+ declare global {
14
+ interface HTMLElementEventMap {
15
+ 'scriptattributeschange': ScriptAttributesChangeEvent;
16
+ 'scriptenablechange': ScriptEnableChangeEvent;
17
+ }
18
+ }
3
19
  /**
4
20
  * Represents a script component in the PlayCanvas engine.
5
21
  *
6
22
  * @category Components
7
23
  */
8
24
  declare class ScriptComponentElement extends ComponentElement {
25
+ private observer;
9
26
  constructor();
27
+ connectedCallback(): Promise<void>;
28
+ private applyAttributes;
29
+ private handleScriptAttributesChange;
30
+ private handleScriptEnableChange;
31
+ private createScript;
32
+ private destroyScript;
33
+ private handleMutations;
34
+ disconnectedCallback(): void;
10
35
  /**
11
36
  * Gets the script component.
12
37
  * @returns The script component.
@@ -1,4 +1,3 @@
1
- import { ScriptComponentElement } from './script-component';
2
1
  /**
3
2
  * Represents a script in the PlayCanvas engine.
4
3
  */
@@ -6,13 +5,20 @@ declare class ScriptElement extends HTMLElement {
6
5
  private _attributes;
7
6
  private _enabled;
8
7
  private _name;
9
- private _script;
10
- connectedCallback(): Promise<void>;
11
- disconnectedCallback(): void;
12
- refreshAttributes(): void;
13
- protected get scriptsElement(): ScriptComponentElement | null;
8
+ /**
9
+ * Sets the attributes of the script.
10
+ * @param value - The attributes of the script.
11
+ */
14
12
  set scriptAttributes(value: string);
13
+ /**
14
+ * Gets the attributes of the script.
15
+ * @returns The attributes of the script.
16
+ */
15
17
  get scriptAttributes(): string;
18
+ /**
19
+ * Sets the enabled state of the script.
20
+ * @param value - The enabled state of the script.
21
+ */
16
22
  set enabled(value: boolean);
17
23
  /**
18
24
  * Gets the enabled state of the script.
package/dist/pwc.cjs CHANGED
@@ -1665,6 +1665,96 @@ customElements.define('pc-rigidbody', RigidBodyComponentElement);
1665
1665
  class ScriptComponentElement extends ComponentElement {
1666
1666
  constructor() {
1667
1667
  super('script');
1668
+ // Create mutation observer to watch for child script elements
1669
+ this.observer = new MutationObserver(this.handleMutations.bind(this));
1670
+ this.observer.observe(this, {
1671
+ childList: true
1672
+ });
1673
+ // Listen for script attribute and enable changes
1674
+ this.addEventListener('scriptattributeschange', this.handleScriptAttributesChange.bind(this));
1675
+ this.addEventListener('scriptenablechange', this.handleScriptEnableChange.bind(this));
1676
+ }
1677
+ async connectedCallback() {
1678
+ await super.connectedCallback();
1679
+ // Handle initial script elements
1680
+ this.querySelectorAll(':scope > pc-script').forEach((scriptElement) => {
1681
+ const scriptName = scriptElement.getAttribute('name');
1682
+ const attributes = scriptElement.getAttribute('attributes');
1683
+ if (scriptName) {
1684
+ this.createScript(scriptName, attributes);
1685
+ }
1686
+ });
1687
+ }
1688
+ applyAttributes(script, attributes) {
1689
+ try {
1690
+ // Parse the attributes string into an object and set them on the script
1691
+ const attributesObject = attributes ? JSON.parse(attributes) : {};
1692
+ Object.assign(script, attributesObject);
1693
+ }
1694
+ catch (error) {
1695
+ console.error(`Error parsing attributes JSON string ${attributes}:`, error);
1696
+ }
1697
+ }
1698
+ handleScriptAttributesChange(event) {
1699
+ const scriptElement = event.target;
1700
+ const scriptName = scriptElement.getAttribute('name');
1701
+ if (!scriptName || !this.component)
1702
+ return;
1703
+ const script = this.component.get(scriptName);
1704
+ if (script) {
1705
+ this.applyAttributes(script, event.detail.attributes);
1706
+ }
1707
+ }
1708
+ handleScriptEnableChange(event) {
1709
+ const scriptElement = event.target;
1710
+ const scriptName = scriptElement.getAttribute('name');
1711
+ if (!scriptName || !this.component)
1712
+ return;
1713
+ const script = this.component.get(scriptName);
1714
+ if (script) {
1715
+ script.enabled = event.detail.enabled;
1716
+ }
1717
+ }
1718
+ createScript(name, attributes) {
1719
+ if (!this.component)
1720
+ return null;
1721
+ this.component.on(`create:${name}`, (script) => {
1722
+ this.applyAttributes(script, attributes);
1723
+ });
1724
+ return this.component.create(name);
1725
+ }
1726
+ destroyScript(name) {
1727
+ if (!this.component)
1728
+ return;
1729
+ this.component.destroy(name);
1730
+ }
1731
+ handleMutations(mutations) {
1732
+ for (const mutation of mutations) {
1733
+ // Handle added nodes
1734
+ mutation.addedNodes.forEach((node) => {
1735
+ if (node instanceof HTMLElement && node.tagName.toLowerCase() === 'pc-script') {
1736
+ const scriptName = node.getAttribute('name');
1737
+ const attributes = node.getAttribute('attributes');
1738
+ if (scriptName) {
1739
+ this.createScript(scriptName, attributes);
1740
+ }
1741
+ }
1742
+ });
1743
+ // Handle removed nodes
1744
+ mutation.removedNodes.forEach((node) => {
1745
+ if (node instanceof HTMLElement && node.tagName.toLowerCase() === 'pc-script') {
1746
+ const scriptName = node.getAttribute('name');
1747
+ if (scriptName) {
1748
+ this.destroyScript(scriptName);
1749
+ }
1750
+ }
1751
+ });
1752
+ }
1753
+ }
1754
+ disconnectedCallback() {
1755
+ var _a;
1756
+ this.observer.disconnect();
1757
+ (_a = super.disconnectedCallback) === null || _a === void 0 ? void 0 : _a.call(this);
1668
1758
  }
1669
1759
  /**
1670
1760
  * Gets the script component.
@@ -1682,67 +1772,38 @@ customElements.define('pc-scripts', ScriptComponentElement);
1682
1772
  class ScriptElement extends HTMLElement {
1683
1773
  constructor() {
1684
1774
  super(...arguments);
1685
- this._attributes = {};
1775
+ this._attributes = '{}';
1686
1776
  this._enabled = true;
1687
1777
  this._name = '';
1688
- this._script = null;
1689
- }
1690
- async connectedCallback() {
1691
- var _a, _b, _c;
1692
- const appElement = this.closest('pc-app');
1693
- if (!appElement) {
1694
- console.error(`${this.tagName.toLowerCase()} should be a descendant of pc-app`);
1695
- return;
1696
- }
1697
- await appElement.getApplication();
1698
- const scriptAttributes = this.getAttribute('attributes');
1699
- if (scriptAttributes) {
1700
- try {
1701
- this._attributes = JSON.parse(scriptAttributes);
1702
- }
1703
- catch (e) {
1704
- console.error('Failed to parse script attributes:', e);
1705
- }
1706
- }
1707
- // When the script is created, initialize it with the necessary attributes
1708
- (_a = this.scriptsElement) === null || _a === void 0 ? void 0 : _a.component.on(`create:${this._name}`, (scriptInstance) => {
1709
- Object.assign(scriptInstance, this._attributes);
1710
- });
1711
- this._script = (_c = (_b = this.scriptsElement) === null || _b === void 0 ? void 0 : _b.component.create(this._name, { preloading: false })) !== null && _c !== void 0 ? _c : null;
1712
- }
1713
- disconnectedCallback() {
1714
- var _a;
1715
- (_a = this.scriptsElement) === null || _a === void 0 ? void 0 : _a.component.destroy(this._name);
1716
- }
1717
- refreshAttributes() {
1718
- if (this._script) {
1719
- Object.entries(this._attributes).forEach(([name, value]) => {
1720
- if (this._script && name in this._script) {
1721
- this._script[name] = value;
1722
- }
1723
- });
1724
- }
1725
- }
1726
- get scriptsElement() {
1727
- const scriptsElement = this.parentElement;
1728
- if (!(scriptsElement instanceof ScriptComponentElement)) {
1729
- console.warn('pc-script must be a direct child of a pc-scripts element');
1730
- return null;
1731
- }
1732
- return scriptsElement;
1733
1778
  }
1779
+ /**
1780
+ * Sets the attributes of the script.
1781
+ * @param value - The attributes of the script.
1782
+ */
1734
1783
  set scriptAttributes(value) {
1735
- this._attributes = JSON.parse(value);
1736
- this.refreshAttributes();
1784
+ this._attributes = value;
1785
+ this.dispatchEvent(new CustomEvent('scriptattributeschange', {
1786
+ detail: { attributes: value },
1787
+ bubbles: true
1788
+ }));
1737
1789
  }
1790
+ /**
1791
+ * Gets the attributes of the script.
1792
+ * @returns The attributes of the script.
1793
+ */
1738
1794
  get scriptAttributes() {
1739
- return JSON.stringify(this._attributes);
1795
+ return this._attributes;
1740
1796
  }
1797
+ /**
1798
+ * Sets the enabled state of the script.
1799
+ * @param value - The enabled state of the script.
1800
+ */
1741
1801
  set enabled(value) {
1742
1802
  this._enabled = value;
1743
- if (this._script) {
1744
- this._script.enabled = value;
1745
- }
1803
+ this.dispatchEvent(new CustomEvent('scriptenablechange', {
1804
+ detail: { enabled: value },
1805
+ bubbles: true
1806
+ }));
1746
1807
  }
1747
1808
  /**
1748
1809
  * Gets the enabled state of the script.
@@ -1879,7 +1940,7 @@ class SoundComponentElement extends ComponentElement {
1879
1940
  }
1880
1941
  }
1881
1942
  }
1882
- customElements.define('pc-sound', SoundComponentElement);
1943
+ customElements.define('pc-sounds', SoundComponentElement);
1883
1944
 
1884
1945
  /**
1885
1946
  * Represents a sound slot in the PlayCanvas engine.
@@ -2127,7 +2188,7 @@ class SoundSlotElement extends HTMLElement {
2127
2188
  }
2128
2189
  }
2129
2190
  }
2130
- customElements.define('pc-sound-slot', SoundSlotElement);
2191
+ customElements.define('pc-sound', SoundSlotElement);
2131
2192
 
2132
2193
  /**
2133
2194
  * Represents a model in the PlayCanvas engine.