/
ScreenSpec.ts
946 lines (793 loc) · 30.8 KB
/
ScreenSpec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
import * as ex from '@excalibur';
import { ExcaliburMatchers } from 'excalibur-jasmine';
import { Camera } from '@excalibur';
describe('A Screen', () => {
let canvas: HTMLCanvasElement;
let context: ex.ExcaliburGraphicsContext;
let browser: ex.BrowserEvents;
beforeEach(() => {
jasmine.addMatchers(ExcaliburMatchers);
// It's important nothing else is hanging out in the dom
Array.from(document.body.children).forEach((element) => {
document.body.removeChild(element);
});
document.body.style.margin = '0';
canvas = document.createElement('canvas');
context = new ex.ExcaliburGraphicsContext2DCanvas({
canvasElement: canvas
});
document.body.appendChild(canvas);
browser = new ex.BrowserEvents(window, document);
});
afterEach(() => {
Array.from(document.body.children).forEach((element) => {
document.body.removeChild(element);
});
});
it('should exist', () => {
expect(ex.Screen).toBeDefined();
});
it('can be constructed', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 400, height: 400 }
});
expect(sut).toBeDefined();
});
it('can calculate the aspect ratio', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 }
});
expect(sut.aspectRatio).toBe(800 / 600);
});
it('can use fit screen display mode, the viewport will adjust to it width', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.FitScreen,
viewport: { width: 800, height: 600 }
});
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1000 });
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 800 });
window.dispatchEvent(new Event('resize'));
expect(sut.resolution.width).toBe(800);
expect(sut.resolution.height).toBe(600);
expect(sut.contentArea.width).toBe(800);
expect(sut.contentArea.height).toBe(600);
expect(sut.viewport.width).toBe(1000);
expect(sut.viewport.height).toBe(1000 / sut.aspectRatio);
});
it('can use fit screen display mode, the viewport will adjust to it height', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.FitScreen,
viewport: { width: 800, height: 600 }
});
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1300 });
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 800 });
window.dispatchEvent(new Event('resize'));
expect(sut.resolution.width).toBe(800);
expect(sut.resolution.height).toBe(600);
expect(sut.contentArea.width).toBe(800);
expect(sut.contentArea.height).toBe(600);
expect(sut.viewport.width).toBe(800 * sut.aspectRatio);
expect(sut.viewport.height).toBe(800);
});
it('can use the FitScreenAndFill display mode, screen aspectRatio > window aspect ratio', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.FitScreenAndFill,
viewport: { width: 800, height: 600 }
});
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1300 });
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 1300 });
window.dispatchEvent(new Event('resize'));
expect(sut.resolution.width).toBe(800);
expect(sut.resolution.height).toBe(800);
expect(sut.contentArea.width).toBe(800);
expect(sut.contentArea.height).toBe(600);
expect(sut.viewport.width).toBe(1300);
expect(sut.viewport.height).toBe(1300);
});
it('can use the FitScreenAndFill display mode, screen aspectRatio < window aspect ratio', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.FitScreenAndFill,
viewport: { width: 800, height: 600 }
});
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1300 });
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 800 });
window.dispatchEvent(new Event('resize'));
expect(sut.resolution.width).toBe(975);
expect(sut.resolution.height).toBe(600);
expect(sut.contentArea.width).toBe(800);
expect(sut.contentArea.height).toBe(600);
expect(sut.viewport.width).toBe(1300);
expect(sut.viewport.height).toBe(800);
});
it('can use the FitContainerAndFill display mode, screen aspectRatio > container aspect ratio', () => {
const parentEl = document.createElement('div');
document.body.removeChild(canvas);
parentEl.appendChild(canvas);
document.body.appendChild(parentEl);
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.FitContainerAndFill,
viewport: { width: 800, height: 600 }
});
parentEl.style.width = '1300px';
parentEl.style.height = '1300px';
parentEl.dispatchEvent(new Event('resize'));
expect(sut.parent).toBe(parentEl);
expect(sut.resolution.width).toBe(800);
expect(sut.resolution.height).toBe(800);
expect(sut.contentArea.width).toBe(800);
expect(sut.contentArea.height).toBe(600);
expect(sut.viewport.width).toBe(100);
expect(sut.viewport.widthUnit).toBe('percent');
expect(sut.viewport.height).toBe(100);
expect(sut.viewport.heightUnit).toBe('percent');
expect(sut.canvas.offsetWidth).toBe(1300);
expect(sut.canvas.offsetHeight).toBe(1300);
});
it('can use the FitContainerAndFill display mode, screen aspectRatio < container aspect ratio', () => {
const parentEl = document.createElement('div');
document.body.removeChild(canvas);
parentEl.appendChild(canvas);
document.body.appendChild(parentEl);
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.FitContainerAndFill,
viewport: { width: 800, height: 600 }
});
parentEl.style.width = '1300px';
parentEl.style.height = '800px';
parentEl.dispatchEvent(new Event('resize'));
expect(sut.parent).toBe(parentEl);
expect(sut.resolution.width).toBe(975);
expect(sut.resolution.height).toBe(600);
expect(sut.contentArea.width).toBe(800);
expect(sut.contentArea.height).toBe(600);
expect(sut.viewport.width).toBe(100);
expect(sut.viewport.widthUnit).toBe('percent');
expect(sut.viewport.height).toBe(100);
expect(sut.viewport.heightUnit).toBe('percent');
expect(sut.canvas.offsetWidth).toBe(1300);
expect(sut.canvas.offsetHeight).toBe(800);
});
it('can use the FitScreenAndZoom display mode, screen aspect ratio < window aspect ratio', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.FitScreenAndZoom,
viewport: { width: 800, height: 600 }
});
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1300 });
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 800 });
window.dispatchEvent(new Event('resize'));
expect(sut.resolution.width).toBe(800);
expect(sut.resolution.height).toBe(600);
expect(sut.contentArea.width).toBe(800);
expect(sut.contentArea.height).toBeCloseTo(492.3, 1);
expect(sut.viewport.width).toBe(1300);
expect(sut.viewport.height).toBeCloseTo(975);
});
it('can use the FitScreenAndZoom display mode, screen aspect ratio > window aspect ratio', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.FitScreenAndZoom,
viewport: { width: 800, height: 600 }
});
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1300 });
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 1300 });
window.dispatchEvent(new Event('resize'));
expect(sut.resolution.width).toBe(800);
expect(sut.resolution.height).toBe(600);
expect(sut.contentArea.width).toBe(600);
expect(sut.contentArea.height).toBe(600);
expect(sut.viewport.width).toBeCloseTo(1733.3, 1);
expect(sut.viewport.height).toBe(1300);
});
it('can use the FitContainerAndZoom display mode, screen aspect ratio < container aspect ratio', () => {
const parentEl = document.createElement('div');
document.body.removeChild(canvas);
parentEl.appendChild(canvas);
document.body.appendChild(parentEl);
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.FitContainerAndZoom,
viewport: { width: 800, height: 600 }
});
parentEl.style.width = '1300px';
parentEl.style.height = '800px';
parentEl.dispatchEvent(new Event('resize'));
expect(sut.resolution.width).toBe(800);
expect(sut.resolution.height).toBe(600);
expect(sut.contentArea.width).toBe(800);
expect(sut.contentArea.height).toBeCloseTo(492.3, 1);
expect(sut.viewport.width).toBe(1300);
expect(sut.viewport.height).toBeCloseTo(975);
});
it('can use the FitContainerAndZoom display mode, screen aspect ratio > container aspect ratio', () => {
const parentEl = document.createElement('div');
document.body.removeChild(canvas);
parentEl.appendChild(canvas);
document.body.appendChild(parentEl);
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.FitContainerAndZoom,
viewport: { width: 800, height: 600 }
});
parentEl.style.width = '1300px';
parentEl.style.height = '1300px';
parentEl.dispatchEvent(new Event('resize'));
expect(sut.resolution.width).toBe(800);
expect(sut.resolution.height).toBe(600);
expect(sut.contentArea.width).toBe(600);
expect(sut.contentArea.height).toBe(600);
expect(sut.viewport.width).toBeCloseTo(1733.3, 1);
expect(sut.viewport.height).toBe(1300);
});
describe('can use fit container display mode, the viewport', () => {
it('will adjust to height', () => {
const parentEl = document.createElement('div');
parentEl.appendChild(canvas);
document.body.appendChild(parentEl);
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.FitContainer,
viewport: { width: 800, height: 600 }
});
parentEl.style.width = '1300px';
parentEl.style.height = '800px';
parentEl.dispatchEvent(new Event('resize'));
expect(sut.parent).toBe(parentEl);
expect(sut.resolution.width).toBe(800);
expect(sut.resolution.height).toBe(600);
expect(sut.viewport.width).toBe(800 * sut.aspectRatio);
expect(sut.viewport.height).toBe(100);
expect(sut.viewport.heightUnit).toBe('percent');
expect(sut.canvas.offsetHeight).toBe(800);
expect(sut.canvas.offsetWidth).toBe(Math.ceil(800 * sut.aspectRatio));
});
it('will adjust to width', () => {
const parentEl = document.createElement('div');
parentEl.appendChild(canvas);
document.body.appendChild(parentEl);
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.FitContainer,
viewport: { width: 800, height: 600 }
});
parentEl.style.width = '1000px';
parentEl.style.height = '800px';
parentEl.dispatchEvent(new Event('resize'));
expect(sut.parent).toBe(parentEl);
expect(sut.resolution.width).toBe(800);
expect(sut.resolution.height).toBe(600);
expect(sut.viewport.width).toBe(100);
expect(sut.viewport.widthUnit).toBe('percent');
expect(sut.viewport.height).toBe(1000 / sut.aspectRatio);
expect(sut.canvas.offsetHeight).toBe(1000 / sut.aspectRatio);
expect(sut.canvas.offsetWidth).toBe(1000);
});
});
it('can use fill screen display mode, the viewport and resolution adjust to match', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.FillScreen,
viewport: { width: 800, height: 600 }
});
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1300 });
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 800 });
window.dispatchEvent(new Event('resize'));
expect(sut.resolution.width).toBe(1300);
expect(sut.resolution.height).toBe(800);
expect(sut.viewport.width).toBe(1300);
expect(sut.viewport.height).toBe(800);
});
it('adjusts coordinates by height when using fullscreen api', () => {
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1000 });
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 800 });
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.Fixed,
viewport: { width: 800, height: 600 }
});
expect(sut.isFullScreen).toBe(false);
const nonFullScreenPage = sut.screenToPageCoordinates(ex.vec(800, 600));
expect(nonFullScreenPage).toBeVector(ex.vec(800, 600));
const nonFullScreenScreen = sut.pageToScreenCoordinates(nonFullScreenPage);
expect(nonFullScreenScreen).toBeVector(ex.vec(800, 600));
canvas.dispatchEvent(new Event('fullscreenchange'));
expect(sut.isFullScreen).toBe(true);
const page = sut.screenToPageCoordinates(ex.vec(800, 600));
expect(page).toBeVector(ex.vec(1000, 775));
const screen = sut.pageToScreenCoordinates(page);
expect(screen).toBeVector(ex.vec(800, 600));
});
it('adjusts coordinates by width when using fullscreen api', () => {
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1300 });
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 800 });
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.Fixed,
viewport: { width: 800, height: 600 }
});
expect(sut.isFullScreen).toBe(false);
const nonFullScreenPage = sut.screenToPageCoordinates(ex.vec(800, 600));
expect(nonFullScreenPage).toBeVector(ex.vec(800, 600));
const nonFullScreenScreen = sut.pageToScreenCoordinates(nonFullScreenPage);
expect(nonFullScreenScreen).toBeVector(ex.vec(800, 600));
canvas.dispatchEvent(new Event('fullscreenchange'));
expect(sut.isFullScreen).toBe(true);
const page = sut.screenToPageCoordinates(ex.vec(800, 600));
expect(page).toBeVector(ex.vec(1183.33, 800));
const screen = sut.pageToScreenCoordinates(page);
expect(screen).toBeVector(ex.vec(800, 600));
});
it('will go fullscreen with the canvas element by default', () => {
const mockCanvas = jasmine.createSpyObj('canvas', ['addEventListener', 'removeEventListener', 'requestFullscreen']);
mockCanvas.style = {};
const sut = new ex.Screen({
canvas: mockCanvas,
context,
browser,
displayMode: ex.DisplayMode.Fixed,
viewport: { width: 800, height: 600 }
});
sut.goFullScreen();
expect(mockCanvas.requestFullscreen).toHaveBeenCalled();
});
it('will go fullscreen given an element id', () => {
const container = document.createElement('div');
container.id = 'some-id';
container.appendChild(canvas);
document.body.appendChild(container);
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.Fixed,
viewport: { width: 800, height: 600 }
});
const fakeElement = jasmine.createSpyObj('element', ['requestFullscreen', 'getAttribute', 'setAttribute', 'addEventListener']);
spyOn(document, 'getElementById').and.returnValue(fakeElement);
sut.goFullScreen('some-id');
expect(document.getElementById).toHaveBeenCalledWith('some-id');
expect(fakeElement.getAttribute).toHaveBeenCalledWith('ex-fullscreen-listener');
expect(fakeElement.setAttribute).toHaveBeenCalledWith('ex-fullscreen-listener', 'true');
expect(fakeElement.addEventListener).toHaveBeenCalled();
expect(fakeElement.requestFullscreen).toHaveBeenCalled();
});
it('can round trip convert coordinates', () => {
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1300 });
Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: 800 });
const sut = new ex.Screen({
canvas,
context,
browser,
displayMode: ex.DisplayMode.FitScreen,
viewport: { width: 800, height: 600 }
});
const page = new ex.Vector(100, 200);
const screen = sut.pageToScreenCoordinates(page);
const world = sut.screenToWorldCoordinates(screen);
const screen2 = sut.worldToScreenCoordinates(world);
const page2 = sut.screenToPageCoordinates(screen2);
const world2 = sut.pageToWorldCoordinates(page);
const page3 = sut.worldToPageCoordinates(world2);
expect(page).toBeVector(page2);
expect(page).toBeVector(page3);
expect(screen).toBeVector(screen2);
expect(world).toBeVector(world2);
});
it('will use the current pixel ratio', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 400, height: 400 }
});
expect(sut.pixelRatio).toEqual(window.devicePixelRatio);
});
it('can override pixel ratio', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 400, height: 400 },
pixelRatio: 10
});
expect(sut.pixelRatio).not.toEqual(window.devicePixelRatio);
});
it('can specify a non-scaled resolution', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 },
pixelRatio: 1
});
sut.applyResolutionAndViewport();
expect(sut.canvas.width).toBe(800);
expect(sut.canvas.height).toBe(600);
expect(sut.scaledWidth).toBe(800);
expect(sut.scaledHeight).toBe(600);
expect(sut.canvas.style.width).toBe('800px');
expect(sut.canvas.style.height).toBe('600px');
expect(sut.viewport.width).toBe(800);
expect(sut.viewport.height).toBe(600);
});
it('can specify a scaled resolution', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 },
pixelRatio: 2
});
sut.applyResolutionAndViewport();
// Internal resolution is scaled for hi dpi
expect(sut.canvas.width).toBe(1600);
expect(sut.canvas.height).toBe(1200);
expect(sut.scaledWidth).toBe(1600);
expect(sut.scaledHeight).toBe(1200);
// Viewport remains the same
expect(sut.canvas.style.width).toBe('800px');
expect(sut.canvas.style.height).toBe('600px');
expect(sut.viewport.width).toBe(800);
expect(sut.viewport.height).toBe(600);
});
it('can specify anti-aliasing off', () => {
const sut = new ex.Screen({
canvas,
context,
canvasImageRendering: 'pixelated',
browser,
viewport: { width: 800, height: 600 },
pixelRatio: 2,
antialiasing: false
});
sut.applyResolutionAndViewport();
expect(context.smoothing).toBeFalse();
expect(canvas.style.imageRendering).toBe('pixelated');
});
it('can specify anti-aliasing off will fall back to crisp-edges if pixelated not supported', () => {
// Using some proxy trickery we can simulate the behavior of firefox not supporting pixelated
const styleProxy = new Proxy(
{},
{
set: function (object, property, value) {
if (property === 'imageRendering' && value === 'pixelated') {
object[property] = '';
return true;
}
object[property] = value;
return true;
}
}
);
const canvasStub = { ...canvas, style: styleProxy } as HTMLCanvasElement;
canvasStub.addEventListener = () => {
/* nothing */
};
const sut = new ex.Screen({
canvas: canvasStub,
context,
browser,
canvasImageRendering: 'pixelated',
viewport: { width: 800, height: 600 },
pixelRatio: 2,
antialiasing: false
});
sut.applyResolutionAndViewport();
expect(context.smoothing).toBeFalse();
expect(canvasStub.style.imageRendering).toBe('crisp-edges');
});
it('can specify anti-aliasing on', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 },
pixelRatio: 2,
antialiasing: true
});
sut.applyResolutionAndViewport();
expect(context.smoothing).toBeTrue();
expect(canvas.style.imageRendering).toBe('auto');
});
it('can push and pop screen resolution', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 },
pixelRatio: 2
});
sut.pushResolutionAndViewport();
sut.resolution = { width: 200, height: 100 };
sut.applyResolutionAndViewport();
expect(sut.resolution.width).toBe(200);
expect(sut.resolution.height).toBe(100);
expect(sut.scaledWidth).toBe(400);
expect(sut.scaledHeight).toBe(200);
sut.popResolutionAndViewport();
sut.applyResolutionAndViewport();
expect(sut.resolution.width).toBe(800);
expect(sut.resolution.height).toBe(600);
expect(sut.scaledWidth).toBe(1600);
expect(sut.scaledHeight).toBe(1200);
});
it('can calculate world coordinates from screen coordinates without a camera', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 },
pixelRatio: 2
});
sut.applyResolutionAndViewport();
// The camera is always center screen
// The absence of a camera is treated like a camera at (0, 0) in world space
expect(sut.screenToWorldCoordinates(ex.vec(400, 300))).toBeVector(ex.vec(0, 0));
expect(sut.screenToWorldCoordinates(ex.vec(0, 0))).toBeVector(ex.vec(-400, -300));
expect(sut.screenToWorldCoordinates(ex.vec(800, 0))).toBeVector(ex.vec(400, -300));
expect(sut.screenToWorldCoordinates(ex.vec(0, 600))).toBeVector(ex.vec(-400, 300));
expect(sut.screenToWorldCoordinates(ex.vec(800, 600))).toBeVector(ex.vec(400, 300));
});
it('can calculate screen coordinates from world coordinates without a camera', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 },
pixelRatio: 2
});
sut.applyResolutionAndViewport();
// The camera is always center screen
// The absence of a camera is treated like a camera at (0, 0) in world space
expect(sut.worldToScreenCoordinates(ex.vec(0, 0))).toBeVector(ex.vec(400, 300));
expect(sut.worldToScreenCoordinates(ex.vec(-400, -300))).toBeVector(ex.vec(0, 0));
expect(sut.worldToScreenCoordinates(ex.vec(400, -300))).toBeVector(ex.vec(800, 0));
expect(sut.worldToScreenCoordinates(ex.vec(-400, 300))).toBeVector(ex.vec(0, 600));
expect(sut.worldToScreenCoordinates(ex.vec(400, 300))).toBeVector(ex.vec(800, 600));
});
it('can calculate world coordinates from screen coordinates with a camera', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 },
pixelRatio: 2
});
const camera = new Camera();
camera.x = 400;
camera.y = 300;
camera.zoom = 2;
sut.setCurrentCamera(camera);
sut.applyResolutionAndViewport();
camera._initialize({screen: sut, clock: { elapsed: () => 16}} as ex.Engine);
// The camera is always center screen
// The absence of a camera is treated like a camera at (0, 0) in world space
expect(sut.screenToWorldCoordinates(ex.vec(400, 300))).toBeVector(ex.vec(400, 300));
expect(sut.screenToWorldCoordinates(ex.vec(0, 0))).toBeVector(ex.vec(200, 150));
expect(sut.screenToWorldCoordinates(ex.vec(800, 0))).toBeVector(ex.vec(600, 150));
expect(sut.screenToWorldCoordinates(ex.vec(0, 600))).toBeVector(ex.vec(200, 450));
expect(sut.screenToWorldCoordinates(ex.vec(800, 600))).toBeVector(ex.vec(600, 450));
});
it('can calculate screen coordinates from world coordinates with a camera', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 },
pixelRatio: 2
});
const camera = new Camera();
camera.x = 400;
camera.y = 300;
camera.zoom = 2;
sut.setCurrentCamera(camera);
sut.applyResolutionAndViewport();
camera._initialize({screen: sut, clock: { elapsed: () => 16}} as ex.Engine);
// The camera is always center screen
// The absence of a camera is treated like a camera at (0, 0) in world space
expect(sut.worldToScreenCoordinates(ex.vec(400, 300))).toBeVector(ex.vec(400, 300));
expect(sut.worldToScreenCoordinates(ex.vec(200, 150))).toBeVector(ex.vec(0, 0));
expect(sut.worldToScreenCoordinates(ex.vec(600, 150))).toBeVector(ex.vec(800, 0));
expect(sut.worldToScreenCoordinates(ex.vec(200, 450))).toBeVector(ex.vec(0, 600));
expect(sut.worldToScreenCoordinates(ex.vec(600, 450))).toBeVector(ex.vec(800, 600));
});
it('can return world bounds', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 },
pixelRatio: 2
});
const camera = new Camera();
camera.x = 400;
camera.y = 300;
camera.zoom = 2;
sut.setCurrentCamera(camera);
sut.applyResolutionAndViewport();
camera._initialize({screen: sut, clock: { elapsed: () => 16}} as ex.Engine);
const bounds = sut.getWorldBounds();
expect(bounds.left).toBe(200);
expect(bounds.right).toBe(600);
expect(bounds.bottom).toBe(450);
expect(bounds.top).toBe(150);
});
it('can return world bounds with camera rotation', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 },
pixelRatio: 2
});
const camera = new Camera();
camera.x = 400;
camera.y = 300;
camera.rotation = Math.PI / 2;
camera.zoom = 2;
sut.setCurrentCamera(camera);
sut.applyResolutionAndViewport();
camera._initialize({screen: sut, clock: { elapsed: () => 16}} as ex.Engine);
const bounds = sut.getWorldBounds();
expect(bounds.left).toBe(250);
expect(bounds.right).toBe(550);
expect(bounds.bottom).toBe(500);
expect(bounds.top).toBe(100);
});
it('can calculate screen center without a camera and no relevant pixel ratio', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 }
});
expect(sut.center).toBeVector(ex.vec(400, 300));
});
it('can calculate screen center with a camera with zoom and no relevant pixel ratio', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 }
});
const camera = new Camera();
camera.x = 400;
camera.y = 300;
camera.zoom = 2;
sut.setCurrentCamera(camera);
expect(sut.center).toBeVector(ex.vec(200, 150));
});
it('can calculate screen center without a camera and relevant pixel ratio', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 },
pixelRatio: 2
});
expect(sut.center).toBeVector(ex.vec(400, 300));
});
it('can calculate screen center with a camera with zoom and relevant pixel ratio', () => {
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 },
pixelRatio: 2
});
const camera = new Camera();
camera.x = 400;
camera.y = 300;
camera.zoom = 2;
sut.setCurrentCamera(camera);
expect(sut.center).toBeVector(ex.vec(200, 150));
});
it('will warn if the resolution is too large', () => {
const logger = ex.Logger.getInstance();
spyOn(logger, 'warnOnce');
const canvasElement = document.createElement('canvas');
canvasElement.width = 100;
canvasElement.height = 100;
const context = new ex.ExcaliburGraphicsContextWebGL({
canvasElement: canvasElement,
enableTransparency: false,
snapToPixel: true,
backgroundColor: ex.Color.White
});
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 },
pixelRatio: 2
});
spyOn(context, 'checkIfResolutionSupported').and.returnValue(false);
sut.resolution = { width: 3000, height: 3000 };
sut.applyResolutionAndViewport();
expect(context.checkIfResolutionSupported).toHaveBeenCalled();
expect(logger.warnOnce).toHaveBeenCalledOnceWith(
`The currently configured resolution (${sut.resolution.width}x${sut.resolution.height}) and pixel ratio (${sut.pixelRatio})` +
' are too large for the platform WebGL implementation, this may work but cause WebGL rendering to behave oddly.' +
' Try reducing the resolution or disabling Hi DPI scaling to avoid this' +
' (read more here https://excaliburjs.com/docs/screens#understanding-viewport--resolution).'
);
});
it('will warn if the resolution is too large and attempt to recover', () => {
const logger = ex.Logger.getInstance();
const warnOnce = spyOn(logger, 'warnOnce');
const canvasElement = document.createElement('canvas');
canvasElement.width = 100;
canvasElement.height = 100;
const context = new ex.ExcaliburGraphicsContextWebGL({
canvasElement: canvasElement,
enableTransparency: false,
snapToPixel: true,
backgroundColor: ex.Color.White
});
const sut = new ex.Screen({
canvas,
context,
browser,
viewport: { width: 800, height: 600 }
});
spyOn(context, 'checkIfResolutionSupported').and.callThrough();
sut.resolution = { width: 2000, height: 2000 };
(sut as any)._devicePixelRatio = 3;
sut.applyResolutionAndViewport();
expect(context.checkIfResolutionSupported).toHaveBeenCalled();
expect(warnOnce.calls.argsFor(0)).toEqual([
`The currently configured resolution (${sut.resolution.width}x${sut.resolution.height}) and pixel ratio (3)` +
' are too large for the platform WebGL implementation, this may work but cause WebGL rendering to behave oddly.' +
' Try reducing the resolution or disabling Hi DPI scaling to avoid this' +
' (read more here https://excaliburjs.com/docs/screens#understanding-viewport--resolution).'
]);
expect(warnOnce.calls.argsFor(1)).toEqual([
'Scaled resolution too big attempted recovery!' +
` Pixel ratio was automatically reduced to (2) to avoid 4k texture limit.` +
' Setting `ex.Engine({pixelRatio: ...}) will override any automatic recalculation, do so at your own risk.` ' +
' (read more here https://excaliburjs.com/docs/screens#understanding-viewport--resolution).'
]);
});
});