@playcanvas/web-components 0.1.0 → 0.1.1

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.
@@ -0,0 +1,252 @@
1
+ import { Color, ElementComponent, Vec2, Vec4 } from 'playcanvas';
2
+
3
+ import { AssetElement } from '../asset';
4
+ import { ComponentElement } from './component';
5
+ import { parseColor, parseVec2, parseVec4 } from '../utils';
6
+
7
+
8
+ /**
9
+ * Represents an element component in the PlayCanvas engine.
10
+ *
11
+ * @category Components
12
+ */
13
+ class ElementComponentElement extends ComponentElement {
14
+ private _anchor: Vec4 = new Vec4(0.5, 0.5, 0.5, 0.5);
15
+
16
+ private _asset: string = '';
17
+
18
+ private _autoWidth: boolean = true;
19
+
20
+ private _color: Color = new Color(1, 1, 1, 1);
21
+
22
+ private _fontSize: number = 32;
23
+
24
+ private _lineHeight: number = 32;
25
+
26
+ private _pivot: Vec2 = new Vec2(0.5, 0.5);
27
+
28
+ private _text: string = '';
29
+
30
+ private _type: 'group' | 'image' | 'text' = 'group';
31
+
32
+ private _width: number = 0;
33
+
34
+ private _wrapLines: boolean = false;
35
+
36
+ constructor() {
37
+ super('element');
38
+ }
39
+
40
+ async connectedCallback() {
41
+ await super.connectedCallback();
42
+
43
+ this.component!._text._material.useFog = true;
44
+ }
45
+
46
+ getAsset() {
47
+ const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`) as AssetElement;
48
+ return assetElement!.asset;
49
+ }
50
+
51
+ getInitialComponentData() {
52
+ return {
53
+ anchor: this._anchor,
54
+ autoWidth: this._autoWidth,
55
+ color: this._color,
56
+ fontAsset: this.getAsset()!.id,
57
+ fontSize: this._fontSize,
58
+ lineHeight: this._lineHeight,
59
+ pivot: this._pivot,
60
+ type: this._type,
61
+ text: this._text,
62
+ width: this._width,
63
+ wrapLines: this._wrapLines
64
+ };
65
+ }
66
+
67
+ /**
68
+ * Gets the element component.
69
+ * @returns The element component.
70
+ */
71
+ get component(): ElementComponent | null {
72
+ return super.component as ElementComponent | null;
73
+ }
74
+
75
+ set anchor(value: Vec4) {
76
+ this._anchor = value;
77
+ if (this.component) {
78
+ this.component.anchor = value;
79
+ }
80
+ }
81
+
82
+ get anchor() {
83
+ return this._anchor;
84
+ }
85
+
86
+ set asset(value: string) {
87
+ this._asset = value;
88
+ const asset = this.getAsset();
89
+ if (this.component && asset) {
90
+ this.component.fontAsset = asset.id;
91
+ }
92
+ }
93
+
94
+ get asset() {
95
+ return this._asset;
96
+ }
97
+
98
+ set autoWidth(value: boolean) {
99
+ this._autoWidth = value;
100
+ if (this.component) {
101
+ this.component.autoWidth = value;
102
+ }
103
+ }
104
+
105
+ get autoWidth() {
106
+ return this._autoWidth;
107
+ }
108
+
109
+ set color(value: Color) {
110
+ this._color = value;
111
+ if (this.component) {
112
+ this.component.color = value;
113
+ }
114
+ }
115
+
116
+ get color() {
117
+ return this._color;
118
+ }
119
+
120
+ set fontSize(value: number) {
121
+ this._fontSize = value;
122
+ if (this.component) {
123
+ this.component.fontSize = value;
124
+ }
125
+ }
126
+
127
+ get fontSize() {
128
+ return this._fontSize;
129
+ }
130
+
131
+ set lineHeight(value: number) {
132
+ this._lineHeight = value;
133
+ if (this.component) {
134
+ this.component.lineHeight = value;
135
+ }
136
+ }
137
+
138
+ get lineHeight() {
139
+ return this._lineHeight;
140
+ }
141
+
142
+ set pivot(value: Vec2) {
143
+ this._pivot = value;
144
+ if (this.component) {
145
+ this.component.pivot = value;
146
+ }
147
+ }
148
+
149
+ get pivot() {
150
+ return this._pivot;
151
+ }
152
+
153
+ set text(value: string) {
154
+ this._text = value;
155
+ if (this.component) {
156
+ this.component.text = value;
157
+ }
158
+ }
159
+
160
+ get text() {
161
+ return this._text;
162
+ }
163
+
164
+ /**
165
+ * Sets the type of the element component.
166
+ * @param value - The type.
167
+ */
168
+ set type(value: 'group' | 'image' | 'text') {
169
+ this._type = value;
170
+ if (this.component) {
171
+ this.component.type = value;
172
+ }
173
+ }
174
+
175
+ /**
176
+ * Gets the type of the element component.
177
+ * @returns The type.
178
+ */
179
+ get type(): 'group' | 'image' | 'text' {
180
+ return this._type;
181
+ }
182
+
183
+ set width(value: number) {
184
+ this._width = value;
185
+ if (this.component) {
186
+ this.component.width = value;
187
+ }
188
+ }
189
+
190
+ get width() {
191
+ return this._width;
192
+ }
193
+
194
+ set wrapLines(value: boolean) {
195
+ this._wrapLines = value;
196
+ if (this.component) {
197
+ this.component.wrapLines = value;
198
+ }
199
+ }
200
+
201
+ get wrapLines() {
202
+ return this._wrapLines;
203
+ }
204
+
205
+ static get observedAttributes() {
206
+ return [...super.observedAttributes, 'anchor', 'asset', 'auto-width', 'color', 'font-size', 'line-height', 'pivot', 'text', 'type', 'width', 'wrap-lines'];
207
+ }
208
+
209
+ attributeChangedCallback(name: string, _oldValue: string, newValue: string) {
210
+ super.attributeChangedCallback(name, _oldValue, newValue);
211
+
212
+ switch (name) {
213
+ case 'anchor':
214
+ this.anchor = parseVec4(newValue);
215
+ break;
216
+ case 'asset':
217
+ this.asset = newValue;
218
+ break;
219
+ case 'auto-width':
220
+ this.autoWidth = newValue !== 'false';
221
+ break;
222
+ case 'color':
223
+ this.color = parseColor(newValue);
224
+ break;
225
+ case 'font-size':
226
+ this.fontSize = Number(newValue);
227
+ break;
228
+ case 'line-height':
229
+ this.lineHeight = Number(newValue);
230
+ break;
231
+ case 'pivot':
232
+ this.pivot = parseVec2(newValue);
233
+ break;
234
+ case 'text':
235
+ this.text = newValue;
236
+ break;
237
+ case 'type':
238
+ this.type = newValue as 'group' | 'image' | 'text';
239
+ break;
240
+ case 'width':
241
+ this.width = Number(newValue);
242
+ break;
243
+ case 'wrap-lines':
244
+ this.wrapLines = this.hasAttribute(name);
245
+ break;
246
+ }
247
+ }
248
+ }
249
+
250
+ customElements.define('pc-element', ElementComponentElement);
251
+
252
+ export { ElementComponentElement };
@@ -0,0 +1,66 @@
1
+ import { GSplatComponent } from 'playcanvas';
2
+
3
+ import { ComponentElement } from './component';
4
+ import { AssetElement } from '../asset';
5
+
6
+ /**
7
+ * Represents a gsplat component in the PlayCanvas engine.
8
+ *
9
+ * @category Components
10
+ */
11
+ class GSplatComponentElement extends ComponentElement {
12
+ private _asset: string = '';
13
+
14
+ constructor() {
15
+ super('gsplat');
16
+ }
17
+
18
+ getAsset() {
19
+ const assetElement = document.querySelector(`pc-asset[id="${this._asset}"]`) as AssetElement;
20
+ return assetElement!.asset;
21
+ }
22
+
23
+ getInitialComponentData() {
24
+ return {
25
+ asset: this.getAsset()
26
+ };
27
+ }
28
+
29
+ /**
30
+ * Gets the gsplat component.
31
+ * @returns The gsplat component.
32
+ */
33
+ get component(): GSplatComponent | null {
34
+ return super.component as GSplatComponent | null;
35
+ }
36
+
37
+ set asset(value: string) {
38
+ this._asset = value;
39
+ const asset = this.getAsset();
40
+ if (this.component && asset) {
41
+ this.component.asset = asset;
42
+ }
43
+ }
44
+
45
+ get asset() {
46
+ return this._asset;
47
+ }
48
+
49
+ static get observedAttributes() {
50
+ return [...super.observedAttributes, 'asset'];
51
+ }
52
+
53
+ attributeChangedCallback(name: string, _oldValue: string, newValue: string) {
54
+ super.attributeChangedCallback(name, _oldValue, newValue);
55
+
56
+ switch (name) {
57
+ case 'asset':
58
+ this.asset = newValue;
59
+ break;
60
+ }
61
+ }
62
+ }
63
+
64
+ customElements.define('pc-splat', GSplatComponentElement);
65
+
66
+ export { GSplatComponentElement };
@@ -0,0 +1,313 @@
1
+ import { Color, LightComponent } from 'playcanvas';
2
+
3
+ import { ComponentElement } from './component';
4
+ import { parseColor } from '../utils';
5
+
6
+ /**
7
+ * Represents a light component in the PlayCanvas engine.
8
+ *
9
+ * @category Components
10
+ */
11
+ class LightComponentElement extends ComponentElement {
12
+ private _castShadows = false;
13
+
14
+ private _color = new Color(1, 1, 1);
15
+
16
+ private _innerConeAngle = 40;
17
+
18
+ private _intensity = 1;
19
+
20
+ private _normalOffsetBias = 0.05;
21
+
22
+ private _outerConeAngle = 45;
23
+
24
+ private _range = 10;
25
+
26
+ private _shadowBias = 0.2;
27
+
28
+ private _shadowDistance = 16;
29
+
30
+ private _type = 'directional';
31
+
32
+ /**
33
+ * Creates a new LightComponentElement.
34
+ */
35
+ constructor() {
36
+ super('light');
37
+ }
38
+
39
+ getInitialComponentData() {
40
+ return {
41
+ castShadows: this._castShadows,
42
+ color: this._color,
43
+ innerConeAngle: this._innerConeAngle,
44
+ intensity: this._intensity,
45
+ normalOffsetBias: this._normalOffsetBias,
46
+ outerConeAngle: this._outerConeAngle,
47
+ range: this._range,
48
+ shadowBias: this._shadowBias,
49
+ shadowDistance: this._shadowDistance,
50
+ type: this._type
51
+ };
52
+ }
53
+
54
+ /**
55
+ * Gets the light component.
56
+ * @returns The light component.
57
+ */
58
+ get component(): LightComponent | null {
59
+ return super.component as LightComponent | null;
60
+ }
61
+
62
+ /**
63
+ * Sets the cast shadows flag of the light.
64
+ * @param value - The cast shadows flag.
65
+ */
66
+ set castShadows(value: boolean) {
67
+ this._castShadows = value;
68
+ if (this.component) {
69
+ this.component.castShadows = value;
70
+ }
71
+ }
72
+
73
+ /**
74
+ * Gets the cast shadows flag of the light.
75
+ * @returns The cast shadows flag.
76
+ */
77
+ get castShadows() {
78
+ return this._castShadows;
79
+ }
80
+
81
+ /**
82
+ * Sets the color of the light.
83
+ * @param value - The color.
84
+ */
85
+ set color(value: Color) {
86
+ this._color = value;
87
+ if (this.component) {
88
+ this.component.color = value;
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Gets the color of the light.
94
+ * @returns The color.
95
+ */
96
+ get color() {
97
+ return this._color;
98
+ }
99
+
100
+ /**
101
+ * Sets the inner cone angle of the light.
102
+ * @param value - The inner cone angle.
103
+ */
104
+ set innerConeAngle(value: number) {
105
+ this._innerConeAngle = value;
106
+ if (this.component) {
107
+ this.component.innerConeAngle = value;
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Gets the inner cone angle of the light.
113
+ * @returns The inner cone angle.
114
+ */
115
+ get innerConeAngle() {
116
+ return this._innerConeAngle;
117
+ }
118
+
119
+ /**
120
+ * Sets the intensity of the light.
121
+ * @param value - The intensity.
122
+ */
123
+ set intensity(value: number) {
124
+ this._intensity = value;
125
+ if (this.component) {
126
+ this.component.intensity = value;
127
+ }
128
+ }
129
+
130
+ /**
131
+ * Gets the intensity of the light.
132
+ * @returns The intensity.
133
+ */
134
+ get intensity() {
135
+ return this._intensity;
136
+ }
137
+
138
+ /**
139
+ * Sets the normal offset bias of the light.
140
+ * @param value - The normal offset bias.
141
+ */
142
+ set normalOffsetBias(value: number) {
143
+ this._normalOffsetBias = value;
144
+ if (this.component) {
145
+ this.component.normalOffsetBias = value;
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Gets the normal offset bias of the light.
151
+ * @returns The normal offset bias.
152
+ */
153
+ get normalOffsetBias() {
154
+ return this._normalOffsetBias;
155
+ }
156
+
157
+ /**
158
+ * Sets the outer cone angle of the light.
159
+ * @param value - The outer cone angle.
160
+ */
161
+ set outerConeAngle(value: number) {
162
+ this._outerConeAngle = value;
163
+ if (this.component) {
164
+ this.component.outerConeAngle = value;
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Gets the outer cone angle of the light.
170
+ * @returns The outer cone angle.
171
+ */
172
+ get outerConeAngle() {
173
+ return this._outerConeAngle;
174
+ }
175
+
176
+ /**
177
+ * Sets the range of the light.
178
+ * @param value - The range.
179
+ */
180
+ set range(value: number) {
181
+ this._range = value;
182
+ if (this.component) {
183
+ this.component.range = value;
184
+ }
185
+ }
186
+
187
+ /**
188
+ * Gets the range of the light.
189
+ * @returns The range.
190
+ */
191
+ get range() {
192
+ return this._range;
193
+ }
194
+
195
+ /**
196
+ * Sets the shadow bias of the light.
197
+ * @param value - The shadow bias.
198
+ */
199
+ set shadowBias(value: number) {
200
+ this._shadowBias = value;
201
+ if (this.component) {
202
+ this.component.shadowBias = value;
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Gets the shadow bias of the light.
208
+ * @returns The shadow bias.
209
+ */
210
+ get shadowBias() {
211
+ return this._shadowBias;
212
+ }
213
+
214
+ /**
215
+ * Sets the shadow distance of the light.
216
+ * @param value - The shadow distance.
217
+ */
218
+ set shadowDistance(value: number) {
219
+ this._shadowDistance = value;
220
+ if (this.component) {
221
+ this.component.shadowDistance = value;
222
+ }
223
+ }
224
+
225
+ /**
226
+ * Gets the shadow distance of the light.
227
+ * @returns The shadow distance.
228
+ */
229
+ get shadowDistance() {
230
+ return this._shadowDistance;
231
+ }
232
+
233
+ /**
234
+ * Sets the type of the light.
235
+ * @param value - The type.
236
+ */
237
+ set type(value: string) {
238
+ if (!['directional', 'omni', 'spot'].includes(value)) {
239
+ console.warn(`Invalid light type '${value}', using default type '${this._type}'.`);
240
+ return;
241
+ }
242
+
243
+ this._type = value;
244
+ if (this.component) {
245
+ this.component.type = value;
246
+ }
247
+ }
248
+
249
+ /**
250
+ * Gets the type of the light.
251
+ * @returns The type.
252
+ */
253
+ get type() {
254
+ return this._type;
255
+ }
256
+
257
+ static get observedAttributes() {
258
+ return [
259
+ ...super.observedAttributes,
260
+ 'color',
261
+ 'cast-shadows',
262
+ 'intensity',
263
+ 'inner-cone-angle',
264
+ 'normal-offset-bias',
265
+ 'outer-cone-angle',
266
+ 'range',
267
+ 'shadow-bias',
268
+ 'shadow-distance',
269
+ 'type'
270
+ ];
271
+ }
272
+
273
+ attributeChangedCallback(name: string, _oldValue: string, newValue: string) {
274
+ super.attributeChangedCallback(name, _oldValue, newValue);
275
+
276
+ switch (name) {
277
+ case 'color':
278
+ this.color = parseColor(newValue);
279
+ break;
280
+ case 'cast-shadows':
281
+ this.castShadows = this.hasAttribute('cast-shadows');
282
+ break;
283
+ case 'inner-cone-angle':
284
+ this.innerConeAngle = Number(newValue);
285
+ break;
286
+ case 'intensity':
287
+ this.intensity = Number(newValue);
288
+ break;
289
+ case 'normal-offset-bias':
290
+ this.normalOffsetBias = Number(newValue);
291
+ break;
292
+ case 'outer-cone-angle':
293
+ this.outerConeAngle = Number(newValue);
294
+ break;
295
+ case 'range':
296
+ this.range = Number(newValue);
297
+ break;
298
+ case 'shadow-bias':
299
+ this.shadowBias = Number(newValue);
300
+ break;
301
+ case 'shadow-distance':
302
+ this.shadowDistance = Number(newValue);
303
+ break;
304
+ case 'type':
305
+ this.type = newValue;
306
+ break;
307
+ }
308
+ }
309
+ }
310
+
311
+ customElements.define('pc-light', LightComponentElement);
312
+
313
+ export { LightComponentElement };
@@ -0,0 +1,26 @@
1
+ import { AudioListenerComponent } from 'playcanvas';
2
+
3
+ import { ComponentElement } from './component';
4
+
5
+ /**
6
+ * Represents a audio listener component in the PlayCanvas engine.
7
+ *
8
+ * @category Components
9
+ */
10
+ class ListenerComponentElement extends ComponentElement {
11
+ constructor() {
12
+ super('audiolistener');
13
+ }
14
+
15
+ /**
16
+ * Gets the audio listener component.
17
+ * @returns The audio listener component.
18
+ */
19
+ get component(): AudioListenerComponent | null {
20
+ return super.component as AudioListenerComponent | null;
21
+ }
22
+ }
23
+
24
+ customElements.define('pc-listener', ListenerComponentElement);
25
+
26
+ export { ListenerComponentElement };