@hkdigital/lib-core 0.5.1 → 0.5.3

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.
@@ -40,6 +40,15 @@
40
40
  * marginTop?: number,
41
41
  * marginBottom?: number,
42
42
  * center?: boolean,
43
+ * enableScaling?: boolean,
44
+ * designLandscape?: {width: number, height: number},
45
+ * designPortrait?: {width: number, height: number},
46
+ * clamping?: {
47
+ * ui: {min: number, max: number},
48
+ * textBase: {min: number, max: number},
49
+ * textHeading: {min: number, max: number},
50
+ * textUi: {min: number, max: number}
51
+ * },
43
52
  * snippetLandscape?:GameBoxSnippet,
44
53
  * snippetPortrait?: GameBoxSnippet,
45
54
  * snippetRequireFullscreen?: GameBoxSnippet,
@@ -66,6 +75,11 @@
66
75
 
67
76
  center,
68
77
 
78
+ enableScaling,
79
+ designLandscape,
80
+ designPortrait,
81
+ clamping,
82
+
69
83
  // > Snippets
70
84
  snippetLandscape,
71
85
  snippetPortrait,
@@ -232,6 +246,33 @@
232
246
  }
233
247
  });
234
248
 
249
+ // Set up scaling if enabled, with orientation awareness
250
+ $effect(() => {
251
+ if (!enableScaling || !gameContainer || !gameWidth || !gameHeight) {
252
+ return () => {}; // No-op cleanup if scaling not enabled or required elements missing
253
+ }
254
+
255
+ // Select the appropriate design based on orientation
256
+ const activeDesign = isLandscape ? designLandscape : designPortrait;
257
+
258
+ // console.debug(
259
+ // `GameBox scaling [${isLandscape ? 'landscape' : 'portrait'}]:`,
260
+ // `game: ${gameWidth}x${gameHeight}`,
261
+ // `design: ${activeDesign.width}x${activeDesign.height}`
262
+ // );
263
+
264
+ // Apply scaling with the current design based on orientation
265
+ return enableContainerScaling({
266
+ container: gameContainer,
267
+ design: activeDesign,
268
+ clamping,
269
+ getDimensions: () => ({
270
+ width: gameWidth,
271
+ height: gameHeight
272
+ })
273
+ });
274
+ });
275
+
235
276
  let show = $state(false);
236
277
 
237
278
  let isPwa = $state(false);
@@ -14,6 +14,33 @@ type GameBox = {
14
14
  marginTop?: number | undefined;
15
15
  marginBottom?: number | undefined;
16
16
  center?: boolean | undefined;
17
+ enableScaling?: boolean | undefined;
18
+ designLandscape?: {
19
+ width: number;
20
+ height: number;
21
+ } | undefined;
22
+ designPortrait?: {
23
+ width: number;
24
+ height: number;
25
+ } | undefined;
26
+ clamping?: {
27
+ ui: {
28
+ min: number;
29
+ max: number;
30
+ };
31
+ textBase: {
32
+ min: number;
33
+ max: number;
34
+ };
35
+ textHeading: {
36
+ min: number;
37
+ max: number;
38
+ };
39
+ textUi: {
40
+ min: number;
41
+ max: number;
42
+ };
43
+ } | undefined;
17
44
  snippetLandscape?: Snippet<[SnippetParams]> | undefined;
18
45
  snippetPortrait?: Snippet<[SnippetParams]> | undefined;
19
46
  snippetRequireFullscreen?: Snippet<[SnippetParams]> | undefined;
@@ -33,6 +60,33 @@ declare const GameBox: import("svelte").Component<{
33
60
  marginTop?: number;
34
61
  marginBottom?: number;
35
62
  center?: boolean;
63
+ enableScaling?: boolean;
64
+ designLandscape?: {
65
+ width: number;
66
+ height: number;
67
+ };
68
+ designPortrait?: {
69
+ width: number;
70
+ height: number;
71
+ };
72
+ clamping?: {
73
+ ui: {
74
+ min: number;
75
+ max: number;
76
+ };
77
+ textBase: {
78
+ min: number;
79
+ max: number;
80
+ };
81
+ textHeading: {
82
+ min: number;
83
+ max: number;
84
+ };
85
+ textUi: {
86
+ min: number;
87
+ max: number;
88
+ };
89
+ };
36
90
  snippetLandscape?: import("svelte").Snippet<[{
37
91
  isLandscape: boolean;
38
92
  isPortrait: boolean;
@@ -0,0 +1,577 @@
1
+ <script>
2
+ import { onMount } from 'svelte';
3
+
4
+ import {
5
+ getGameWidthOnLandscape,
6
+ getGameWidthOnPortrait
7
+ } from './gamebox.util.js';
8
+
9
+ import { enableContainerScaling } from '$lib/design/index.js';
10
+ // import { enableContainerScaling } from '@hkdigital/lib-core/design/index.js';
11
+
12
+ /**
13
+ * @typedef {{
14
+ * isMobile:boolean,
15
+ * os:'Android'|'iOS',
16
+ * isFullscreen:boolean,
17
+ * isDevMode:boolean,
18
+ * requestDevmode:function,
19
+ * requestFullscreen:function,
20
+ * gameWidth: number,
21
+ * gameHeight: number
22
+ * }} SnippetParams
23
+ */
24
+
25
+ /**
26
+ * @typedef {import('svelte').Snippet<[SnippetParams]>} GameBoxSnippet
27
+ */
28
+
29
+ /**
30
+ * @type {{
31
+ * base?: string,
32
+ * bg?: string,
33
+ * classes?: string,
34
+ * style?: string,
35
+ * aspectOnLandscape?: number,
36
+ * aspectOnPortrait?: number,
37
+ * marginLeft?: number,
38
+ * marginRight?: number,
39
+ * marginTop?: number,
40
+ * marginBottom?: number,
41
+ * center?: boolean,
42
+ * enableScaling?: boolean,
43
+ * designLandscape?: {width: number, height: number},
44
+ * designPortrait?: {width: number, height: number},
45
+ * clamping?: {
46
+ * ui: {min: number, max: number},
47
+ * textBase: {min: number, max: number},
48
+ * textHeading: {min: number, max: number},
49
+ * textUi: {min: number, max: number}
50
+ * },
51
+ * snippetLandscape?:GameBoxSnippet,
52
+ * snippetPortrait?: GameBoxSnippet,
53
+ * snippetRequireFullscreen?: GameBoxSnippet,
54
+ * snippetInstallOnHomeScreen?: GameBoxSnippet,
55
+ * [attr: string]: any
56
+ * }}
57
+ */
58
+ const {
59
+ // > Style
60
+ base = '',
61
+ bg = '',
62
+ classes = '',
63
+ style = '',
64
+
65
+ // > Functional properties
66
+ aspectOnLandscape,
67
+ aspectOnPortrait,
68
+
69
+ marginLeft = 0,
70
+ marginRight = 0,
71
+
72
+ marginTop = 0,
73
+ marginBottom = 0,
74
+
75
+ center,
76
+
77
+ // > Scaling options
78
+ enableScaling = false,
79
+ designLandscape = { width: 1920, height: 1080 },
80
+ designPortrait = { width: 1920, height: 1080 },
81
+ clamping = {
82
+ ui: { min: 0.3, max: 2 },
83
+ textBase: { min: 0.75, max: 1.5 },
84
+ textHeading: { min: 0.75, max: 2.25 },
85
+ textUi: { min: 0.5, max: 1.25 }
86
+ },
87
+
88
+ // > Snippets
89
+ snippetLandscape,
90
+ snippetPortrait,
91
+ snippetRequireFullscreen,
92
+ snippetInstallOnHomeScreen
93
+ } = $props();
94
+
95
+ // > Game dimensions and state
96
+ let windowWidth = $state();
97
+ let windowHeight = $state();
98
+
99
+ let gameWidth = $state();
100
+ let gameHeight = $state();
101
+
102
+ let iosWindowWidth = $state();
103
+ let iosWindowHeight = $state();
104
+
105
+ function getIsLandscape() {
106
+ if (isPwa && isAppleMobile) {
107
+ return iosWindowWidth > iosWindowHeight;
108
+ } else {
109
+ return windowWidth > windowHeight;
110
+ }
111
+ }
112
+
113
+ let isLandscape = $state();
114
+
115
+ // $derived.by(getIsLandscape);
116
+
117
+ $effect(() => {
118
+ isLandscape = getIsLandscape();
119
+ });
120
+
121
+ // Game container reference
122
+ let gameContainer = $state();
123
+
124
+ // Update game dimensions based on window size and orientation
125
+ $effect(() => {
126
+ const width = iosWindowWidth ?? windowWidth;
127
+ const height = iosWindowHeight ?? windowHeight;
128
+
129
+ if (!width || !height) return;
130
+
131
+ const availWidth = width - marginLeft - marginRight;
132
+ const availHeight = height - marginTop - marginBottom;
133
+
134
+ // console.debug('GameBox margins:', {
135
+ // marginLeft,
136
+ // marginRight,
137
+ // marginTop,
138
+ // marginBottom
139
+ // });
140
+
141
+ let gameAspect;
142
+
143
+ if (availWidth > availHeight) {
144
+ gameWidth = getGameWidthOnLandscape({
145
+ windowWidth: availWidth,
146
+ windowHeight: availHeight,
147
+ aspectOnLandscape
148
+ });
149
+ gameAspect = aspectOnLandscape;
150
+ } else {
151
+ gameWidth = getGameWidthOnPortrait({
152
+ windowWidth: availWidth,
153
+ windowHeight: availHeight,
154
+ aspectOnPortrait
155
+ });
156
+ gameAspect = aspectOnPortrait;
157
+ }
158
+
159
+ if (gameAspect) {
160
+ gameHeight = gameWidth / gameAspect;
161
+ } else {
162
+ gameHeight = availHeight;
163
+ }
164
+ });
165
+
166
+ // Set up scaling if enabled, with orientation awareness
167
+ $effect(() => {
168
+ if (!enableScaling || !gameContainer || !gameWidth || !gameHeight) {
169
+ return () => {}; // No-op cleanup if scaling not enabled or required elements missing
170
+ }
171
+
172
+ // Select the appropriate design based on orientation
173
+ const activeDesign = isLandscape ? designLandscape : designPortrait;
174
+
175
+ // console.debug(
176
+ // `GameBox scaling [${isLandscape ? 'landscape' : 'portrait'}]:`,
177
+ // `game: ${gameWidth}x${gameHeight}`,
178
+ // `design: ${activeDesign.width}x${activeDesign.height}`
179
+ // );
180
+
181
+ // Apply scaling with the current design based on orientation
182
+ return enableContainerScaling({
183
+ container: gameContainer,
184
+ design: activeDesign,
185
+ clamping,
186
+ getDimensions: () => ({
187
+ width: gameWidth,
188
+ height: gameHeight
189
+ })
190
+ });
191
+ });
192
+
193
+ let show = $state(false);
194
+
195
+ const isAppleMobile = /iPhone|iPod/.test(navigator.userAgent);
196
+
197
+ let isPwa = $state(false);
198
+
199
+ let os = $state();
200
+
201
+ let isMobile = $state(false);
202
+
203
+ let isDevMode = $state(false);
204
+
205
+ // Check: always true for home app?
206
+ let isFullscreen = $state(false);
207
+
208
+ let supportsFullscreen = $state(false);
209
+
210
+ onMount(() => {
211
+ supportsFullscreen = document.fullscreenEnabled;
212
+
213
+ isMobile = getIsMobile();
214
+
215
+ os = getOS();
216
+
217
+ // Run before show
218
+ isFullscreen = !!document.fullscreenElement;
219
+
220
+ isPwa = window.matchMedia(
221
+ '(display-mode: fullscreen) or (display-mode: standalone)'
222
+ ).matches;
223
+
224
+ isLandscape = getIsLandscape();
225
+
226
+ show = true;
227
+
228
+ function updateIosWidthHeight() {
229
+ // const isPwa = window.matchMedia(
230
+ // '(display-mode: fullscreen) or (display-mode: standalone)'
231
+ // ).matches;
232
+
233
+ if (isPwa && isAppleMobile) {
234
+ const angle = screen.orientation.angle;
235
+
236
+ if (angle === 90 || angle === 270) {
237
+ iosWindowWidth = screen.height;
238
+ iosWindowHeight = screen.width;
239
+ } else {
240
+ iosWindowWidth = screen.width;
241
+ iosWindowHeight = screen.height;
242
+ }
243
+ // console.debug( { iosWindowWidth, iosWindowHeight } );
244
+ }
245
+ }
246
+
247
+ updateIosWidthHeight();
248
+
249
+ function updateOrientation(event) {
250
+ // console.debug('updateOrientation');
251
+ const type = event.target.type;
252
+ const angle = event.target.angle;
253
+
254
+ // isPwa = window.matchMedia(
255
+ // '(display-mode: fullscreen) or (display-mode: standalone)'
256
+ // ).matches;
257
+
258
+ updateIosWidthHeight();
259
+
260
+ console.debug(
261
+ `ScreenOrientation change: ${type}, ${angle} degrees.`,
262
+ isPwa,
263
+ windowWidth,
264
+ windowHeight,
265
+ screen.width,
266
+ screen.height,
267
+ iosWindowWidth,
268
+ iosWindowHeight
269
+ );
270
+
271
+ // if( angle
272
+ }
273
+
274
+ $effect(() => {
275
+ screen.orientation.addEventListener('change', updateOrientation);
276
+
277
+ return () => {
278
+ screen.orientation.removeEventListener('change', updateOrientation);
279
+ };
280
+ });
281
+
282
+ //
283
+ });
284
+
285
+ onMount(() => {
286
+ const gameBoxNoScroll = 'game-box-no-scroll';
287
+ const html = document.documentElement;
288
+ html.classList.add(gameBoxNoScroll);
289
+
290
+ return () => {
291
+ html.classList.remove(gameBoxNoScroll);
292
+ };
293
+ });
294
+
295
+ function getOS() {
296
+ if (isAppleMobile) {
297
+ return 'iOS';
298
+ } else if (/Android/.test(navigator.userAgent)) {
299
+ return 'Android';
300
+ }
301
+ }
302
+
303
+ /**
304
+ * Returns true if a device is a mobile phone (or similar)
305
+ */
306
+ function getIsMobile() {
307
+ // @ts-ignore
308
+ if (navigator?.userAgentData?.mobile !== undefined) {
309
+ // Supports for mobile flag
310
+ // @ts-ignore
311
+ return navigator.userAgentData.mobile;
312
+ } else if (isAppleMobile) {
313
+ return true;
314
+ } else if (/Android/.test(navigator.userAgent)) {
315
+ return true;
316
+ }
317
+
318
+ return false;
319
+ }
320
+
321
+ /**
322
+ * Returns true if the window is in full screen
323
+ * - Checks if CSS thinks we're in fullscreen mode
324
+ * - Checks if there is a fullscreen element (for safari)
325
+ */
326
+ function getIsFullscreen() {
327
+ if (
328
+ window.matchMedia(
329
+ '(display-mode: fullscreen) or (display-mode: standalone)'
330
+ ).matches
331
+ ) {
332
+ return true;
333
+ } else if (document.fullscreenElement) {
334
+ // Safari
335
+ return true;
336
+ }
337
+
338
+ return false;
339
+ }
340
+
341
+ async function requestFullscreen() {
342
+ console.debug('Request full screen');
343
+ show = false;
344
+
345
+ await document.documentElement.requestFullscreen();
346
+ isFullscreen = true;
347
+
348
+ setTimeout(() => {
349
+ show = true;
350
+ }, 1000);
351
+ }
352
+
353
+ // async function exitFullscreen() {
354
+ // console.debug('Exit full screen');
355
+ // show = false;
356
+
357
+ // await document.exitFullscreen();
358
+ // isFullscreen = false;
359
+
360
+ // setTimeout( () => { show = true; }, 1000 );
361
+ // }
362
+
363
+ $effect(() => {
364
+ // Update isFullscreen if window width or height changes
365
+
366
+ windowWidth;
367
+ windowHeight;
368
+
369
+ isFullscreen = getIsFullscreen();
370
+
371
+ // if( !isFullscreen )
372
+ // {
373
+ // show = false;
374
+ // setTimeout( () => { show = true; }, 1000 );
375
+ // }
376
+
377
+ // console.debug('isFullscreen', isFullscreen);
378
+ });
379
+
380
+ isDevMode = false;
381
+
382
+ function requestDevmode() {
383
+ isDevMode = true;
384
+ console.debug(isDevMode);
385
+ }
386
+
387
+ $effect(() => {
388
+ if (location.hostname === 'localhost') {
389
+ isDevMode = true;
390
+ }
391
+ });
392
+
393
+ $effect(() => {
394
+ if (isFullscreen) {
395
+ const url = new URL(window.location.href);
396
+ url.searchParams.set('preset', 'cinema');
397
+ window.history.pushState({}, '', url);
398
+ }
399
+ });
400
+ </script>
401
+
402
+ <svelte:window bind:innerWidth={windowWidth} bind:innerHeight={windowHeight} />
403
+
404
+ {#if gameHeight}
405
+ <div class:center>
406
+ <div
407
+ data-component="game-box"
408
+ data-orientation={isLandscape ? 'landscape' : 'portrait'}
409
+ bind:this={gameContainer}
410
+ class="{base} {bg} {classes}"
411
+ class:isMobile
412
+ style:width="{gameWidth}px"
413
+ style:height="{gameHeight}px"
414
+ style:--game-width={gameWidth}
415
+ style:--game-height={gameHeight}
416
+ style:margin-left="{marginLeft}px"
417
+ style:margin-right="{marginRight}px"
418
+ style:margin-top="{marginTop}px"
419
+ style:margin-bottom="{marginBottom}px"
420
+ {style}
421
+ >
422
+ {#if show}
423
+ {#if isLandscape}
424
+ <!-- Landscape -->
425
+ {#if snippetRequireFullscreen}
426
+ <!-- Require fullscreen -->
427
+ {#if isFullscreen && !isDevMode}
428
+ {@render snippetLandscape({
429
+ isMobile,
430
+ os,
431
+ isFullscreen,
432
+ isDevMode,
433
+ requestDevmode,
434
+ requestFullscreen,
435
+ gameWidth,
436
+ gameHeight
437
+ })}
438
+ {:else if supportsFullscreen && !isDevMode}
439
+ <!-- Require fullscreen (on landscape) -->
440
+ {@render snippetRequireFullscreen({
441
+ isMobile,
442
+ os,
443
+ isFullscreen,
444
+ isDevMode,
445
+ requestDevmode,
446
+ requestFullscreen,
447
+ gameWidth,
448
+ gameHeight
449
+ })}
450
+ {:else if isMobile && snippetInstallOnHomeScreen && !isDevMode}
451
+ <!-- Require install on home screen on mobile -->
452
+ {@render snippetInstallOnHomeScreen({
453
+ isMobile,
454
+ os,
455
+ isFullscreen,
456
+ isDevMode,
457
+ requestDevmode,
458
+ requestFullscreen,
459
+ gameWidth,
460
+ gameHeight
461
+ })}
462
+ {:else}
463
+ {@render snippetLandscape({
464
+ isMobile,
465
+ os,
466
+ isFullscreen,
467
+ isDevMode,
468
+ requestDevmode,
469
+ requestFullscreen,
470
+ gameWidth,
471
+ gameHeight
472
+ })}
473
+ {/if}
474
+ {:else}
475
+ <!-- Do not require fullscreen -->
476
+ <!-- *we do not try install home app -->
477
+ {@render snippetLandscape({
478
+ isMobile,
479
+ os,
480
+ isFullscreen,
481
+ isDevMode,
482
+ requestDevmode,
483
+ requestFullscreen,
484
+ gameWidth,
485
+ gameHeight
486
+ })}
487
+ {/if}
488
+ {:else}
489
+ <!-- Portrait -->
490
+ {#if snippetRequireFullscreen}
491
+ <!-- Require fullscreen -->
492
+ {#if isFullscreen && !isDevMode}
493
+ {@render snippetPortrait({
494
+ isMobile,
495
+ os,
496
+ isFullscreen,
497
+ isDevMode,
498
+ requestDevmode,
499
+ requestFullscreen,
500
+ gameWidth,
501
+ gameHeight
502
+ })}
503
+ {:else if supportsFullscreen && !isDevMode}
504
+ <!-- Require fullscreen (on landscape) -->
505
+ {@render snippetRequireFullscreen({
506
+ isMobile,
507
+ os,
508
+ isFullscreen,
509
+ isDevMode,
510
+ requestDevmode,
511
+ requestFullscreen,
512
+ gameWidth,
513
+ gameHeight
514
+ })}
515
+ {:else if isMobile && snippetInstallOnHomeScreen && !isDevMode}
516
+ <!-- Require install on home screen on mobile -->
517
+ {@render snippetInstallOnHomeScreen({
518
+ isMobile,
519
+ os,
520
+ isFullscreen,
521
+ isDevMode,
522
+ requestDevmode,
523
+ requestFullscreen,
524
+ gameWidth,
525
+ gameHeight
526
+ })}
527
+ {:else}
528
+ {@render snippetPortrait({
529
+ isMobile,
530
+ os,
531
+ isFullscreen,
532
+ isDevMode,
533
+ requestDevmode,
534
+ requestFullscreen,
535
+ gameWidth,
536
+ gameHeight
537
+ })}
538
+ {/if}
539
+ {:else}
540
+ <!-- Do not require fullscreen -->
541
+ <!-- *we do not try install home app -->
542
+ {@render snippetPortrait({
543
+ isMobile,
544
+ os,
545
+ isFullscreen,
546
+ isDevMode,
547
+ requestDevmode,
548
+ requestFullscreen,
549
+ gameWidth,
550
+ gameHeight
551
+ })}
552
+ {/if}
553
+ {/if}
554
+ {/if}
555
+ </div>
556
+ </div>
557
+ {/if}
558
+
559
+ <style>
560
+ .center {
561
+ display: grid;
562
+ height: 100lvh;
563
+ display: grid;
564
+ justify-items: center;
565
+ align-items: center;
566
+ /* border: solid 1px red;*/
567
+ }
568
+
569
+ :global(html.game-box-no-scroll) {
570
+ overflow: clip;
571
+ scrollbar-width: none; /* Firefox */
572
+ -ms-overflow-style: none; /* IE and Edge */
573
+ }
574
+ :global(html.game-box-no-scroll::-webkit-scrollbar) {
575
+ display: none;
576
+ }
577
+ </style>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-core",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"