1
- // aspect_ratio = 16/9
2
1
scale = 1
3
2
print = console . log
4
3
@@ -85,56 +84,66 @@ scene._children = []
85
84
_game_window . appendChild ( scene )
86
85
87
86
// print('browser aspect_ratio:', browser_aspect_ratio)
88
- format = null
89
87
is_mobile = 'ontouchstart' in document . documentElement
90
88
fullscreen = false
91
89
camera = null
90
+ ASPECT_RATIO = 9 / 16
92
91
93
92
function set_orientation ( value ) {
93
+ if ( value == 'vertical' ) {
94
+ aspect_ratio = 9 / 16
95
+ }
96
+ if ( value == 'horizontal' ) {
97
+ aspect_ratio = 16 / 9
98
+ }
99
+ set_aspect_ratio ( aspect_ratio )
100
+ }
101
+
102
+ // set_orientation
103
+ function set_aspect_ratio ( aspect_ratio ) {
104
+ ASPECT_RATIO = aspect_ratio
105
+ print ( 'set aspect ratio to:' , aspect_ratio )
94
106
var width = window . innerWidth
95
107
var height = window . innerHeight
96
108
browser_aspect_ratio = width / height
97
109
// print('width:', width, 'height:', height, 'browser_aspect_ratio:', browser_aspect_ratio)
98
-
99
- format = value
100
- if ( format == 'vertical' ) {
101
- aspect_ratio = 16 / 9
110
+ if ( aspect_ratio < 1 ) {
102
111
// used for setting correct draggable limits
103
112
asp_x = 1
104
- asp_y = 9 / 16
113
+ asp_y = aspect_ratio
105
114
106
- if ( browser_aspect_ratio >= 9 / 16 ) { // if the screen is wider than the game, like a pc monitor.
107
- // print('vertical view desktop')
108
- _game_window . style . width = `${ width * scale / browser_aspect_ratio / ( 16 / 9 ) } px`
115
+ if ( browser_aspect_ratio < aspect_ratio ) { // if the screen is wider than the game, like a pc monitor.
116
+ print ( 'vertical view desktop' )
117
+ _game_window . style . width = `${ width * scale / browser_aspect_ratio * aspect_ratio } px`
109
118
_game_window . style . height = `${ height * scale } px`
110
119
}
111
120
else { // if the screen is taller than the game, like a phone screen.
112
- // print('vertical view mobile')
113
- _game_window . style . height = `${ width * scale * ( 16 / 9 ) } px`
114
- _game_window . style . width = `${ width * scale } px`
115
- }
116
- if ( camera ) { camera . ui . scale = [ 1 , 1 / aspect_ratio ] }
117
- top_left = [ - .5 , .5 * aspect_ratio ]
118
- top_right = [ .5 , .5 * aspect_ratio ]
119
- bottom_left = [ - .5 , - .5 * aspect_ratio ]
120
- bottom_right = [ .5 , - .5 * aspect_ratio ]
121
- top = [ 0 , .5 * aspect_ratio ]
122
- bottom = [ 0 , - .5 * aspect_ratio ]
121
+ print ( 'vertical view mobile' , width , width / aspect_ratio , height )
122
+ _game_window . style . height = `${ height * scale } px`
123
+ _game_window . style . width = `${ height * scale * aspect_ratio } px`
124
+ }
125
+ if ( camera ) { camera . ui . scale = [ 1 , 1 * aspect_ratio ] }
126
+ top_left = [ - .5 , .5 / aspect_ratio ]
127
+ top_right = [ .5 , .5 / aspect_ratio ]
128
+ bottom_left = [ - .5 , - .5 / aspect_ratio ]
129
+ bottom_right = [ .5 , - .5 / aspect_ratio ]
130
+ top = [ 0 , .5 / aspect_ratio ]
131
+ bottom = [ 0 , - .5 / aspect_ratio ]
123
132
left = [ - .5 , 0 ]
124
133
right = [ .5 , 0 ]
125
134
}
126
135
else {
127
- aspect_ratio = 16 / 9
128
- asp_x = 16 / 9
136
+ // aspect_ratio = 16/9
137
+ asp_x = aspect_ratio
129
138
asp_y = 1
130
139
scene . style . width = `${ 1 / asp_x * 100 } %`
131
140
scene . style . height = `${ 1 / asp_y * 100 } %`
132
- if ( browser_aspect_ratio > 16 / 9 ) { // if the screen is wider than 16/9, fit to height
141
+ if ( browser_aspect_ratio > aspect_ratio ) { // if the screen is wider than 16/9, fit to height
133
142
_game_window . style . height = `${ height * scale } px`
134
- _game_window . style . width = `${ width * scale / browser_aspect_ratio * 16 / 9 } px`
143
+ _game_window . style . width = `${ width * scale / browser_aspect_ratio * aspect_ratio } px`
135
144
}
136
145
else { // if the screen is taller than 16/9, fit to width
137
- _game_window . style . height = `${ height * scale * browser_aspect_ratio / ( 16 / 9 ) } px`
146
+ _game_window . style . height = `${ height * scale * browser_aspect_ratio / aspect_ratio } px`
138
147
_game_window . style . width = `${ width * scale } px`
139
148
}
140
149
if ( camera ) { camera . ui . scale = [ 1 / aspect_ratio , 1 ] }
@@ -253,7 +262,8 @@ function set_window_color(value) {_game_window.style.backgroundColor = value}
253
262
function set_background_color ( value ) { document . body . style . backgroundColor = value }
254
263
function set_scale ( value ) {
255
264
scale = value
256
- set_orientation ( format )
265
+ // set_orientation(format)
266
+ set_aspect_ratio ( ASPECT_RATIO )
257
267
}
258
268
259
269
function set_fullscreen ( value ) {
@@ -299,7 +309,7 @@ LAST_SCENE = null
299
309
DEFAULT_FONT = null
300
310
TEXT_SIZE_MULTIPLIER = 1
301
311
302
- class Entity {
312
+ class Entity {
303
313
constructor ( options = null ) {
304
314
if ( ! ( 'type' in options ) ) {
305
315
options [ 'type' ] = 'entity'
@@ -444,7 +454,7 @@ class Entity {
444
454
else {
445
455
this . el . style . visibility = 'hidden'
446
456
}
447
-
457
+
448
458
this . _enabled = value
449
459
450
460
if ( value && this . on_enable ) {
@@ -666,7 +676,7 @@ class Entity {
666
676
get text_size ( ) { return this . _text_size }
667
677
set text_size ( value ) {
668
678
this . _text_size = value
669
- if ( format == 'vertical' ) {
679
+ if ( aspect_ratio < 1 ) {
670
680
this . model . style . fontSize = `${ value * scale * 1 * TEXT_SIZE_MULTIPLIER } vh`
671
681
}
672
682
else {
@@ -776,7 +786,7 @@ class Entity {
776
786
}
777
787
else {
778
788
// fallback to linear
779
- // entity[variable_name] = lerp(start_value, target_value, t)
789
+ // entity[variable_name] = lerp(start_value, target_value, t)
780
790
}
781
791
} ,
782
792
1000 * i / 60
@@ -824,6 +834,9 @@ class Entity {
824
834
look_at ( target_pos ) {
825
835
this . rotation = - ( Math . atan2 ( target_pos [ 1 ] - this . y , target_pos [ 0 ] - this . x ) ) * ( 180 / Math . PI )
826
836
}
837
+ look_in_direction ( direction ) {
838
+ this . rotation = - ( Math . atan2 ( direction [ 1 ] , direction [ 0 ] ) ) * ( 180 / Math . PI )
839
+ }
827
840
828
841
destroy_children ( ) {
829
842
for ( let _entity of this . children ) {
@@ -842,7 +855,7 @@ class Entity {
842
855
async function check_image ( url ) {
843
856
const res = await fetch ( url ) ;
844
857
const buff = await res . blob ( ) ;
845
-
858
+
846
859
return buff . type . startsWith ( 'image/' )
847
860
}
848
861
@@ -912,7 +925,7 @@ class Camera {
912
925
get x ( ) { return this . _x }
913
926
set x ( value ) {
914
927
this . _x = value
915
- if ( format == 'vertical' ) {
928
+ if ( aspect_ratio < 1 ) {
916
929
scene . style . left = `${ 50 + ( - value * 100 / this . fov ) } %`
917
930
}
918
931
else {
@@ -922,7 +935,7 @@ class Camera {
922
935
get y ( ) { return this . _y }
923
936
set y ( value ) {
924
937
this . _y = value
925
- if ( format == 'vertical' ) {
938
+ if ( aspect_ratio < 1 ) {
926
939
scene . style . top = `${ 50 + ( value * 100 * asp_y / this . fov ) } %`
927
940
}
928
941
else {
@@ -955,7 +968,7 @@ class Camera {
955
968
this . _fov = value
956
969
scene . style . width = `${ 1 / value * 100 / asp_x } %`
957
970
958
- if ( format == 'vertical' ) {
971
+ if ( aspect_ratio < 1 ) {
959
972
scene . style . height = `${ 1 / value * 100 / asp_x * asp_y } %`
960
973
}
961
974
else {
@@ -978,7 +991,7 @@ class Camera {
978
991
after ( duration , ( ) => {
979
992
this . xy = original_xy
980
993
} )
981
-
994
+
982
995
}
983
996
}
984
997
}
@@ -1018,11 +1031,11 @@ function Scene(input) {
1018
1031
settings = { parent :scene , visible_self :false , on_enter :null , on_exit :null , enabled :false , texture :null }
1019
1032
_bg_color = input . color
1020
1033
input . color = null
1021
-
1034
+
1022
1035
for ( const [ key , value ] of Object . entries ( input ) ) {
1023
1036
settings [ key ] = value
1024
1037
}
1025
-
1038
+
1026
1039
_entity = new Entity ( settings )
1027
1040
1028
1041
if ( input . texture || _bg_color ) {
@@ -1377,7 +1390,7 @@ function _onmousemove(pageX, pageY, clientX, clientY, pressure, target) {
1377
1390
}
1378
1391
document . addEventListener ( 'mousemove' , ( event ) => {
1379
1392
_onmousemove ( event . pageX , event . pageY , event . clientX , event . clientY , event . pressure , event . target )
1380
-
1393
+
1381
1394
} )
1382
1395
document . addEventListener ( 'touchmove' , ( event ) => {
1383
1396
event = event . touches [ 0 ]
@@ -1432,7 +1445,7 @@ function Video(options) {
1432
1445
_entity = new Entity ( settings )
1433
1446
// let name = settings['name']
1434
1447
1435
- let video_entity = new Entity ( settings )
1448
+ let video_entity = new Entity ( settings )
1436
1449
video_entity . video = document . createElement ( 'video' )
1437
1450
video_entity . video . src = settings [ 'name' ]
1438
1451
video_entity . video . controls = false
@@ -1532,7 +1545,7 @@ function sample(population, k){
1532
1545
1533
1546
return result ;
1534
1547
}
1535
-
1548
+
1536
1549
function grid_layout ( l , options ) {
1537
1550
let settings = { spacing :[ 1.1 , 1.1 ] , offset :[ 0 , 0 ] , max_x :16 }
1538
1551
for ( const [ key , value ] of Object . entries ( options ) ) {
@@ -1642,7 +1655,7 @@ function _input(event) {
1642
1655
else if ( event . type == "keydown" ) { // prevent key repeat
1643
1656
return
1644
1657
}
1645
-
1658
+
1646
1659
if ( key == 'left mouse up' ) {
1647
1660
// drop draggables
1648
1661
for ( var e of entities ) {
@@ -1664,13 +1677,13 @@ function _input(event) {
1664
1677
input ( key )
1665
1678
}
1666
1679
1667
- // swipe input
1680
+ // swipe input
1668
1681
if ( key == 'left mouse up' ) {
1669
1682
mouse . click_end_position = mouse . position
1670
1683
if ( time - time_of_press < .15 ) {
1671
1684
diff_x = mouse . position [ 0 ] - mouse . swipe_start_position [ 0 ]
1672
1685
diff_y = mouse . position [ 1 ] - mouse . swipe_start_position [ 1 ]
1673
-
1686
+
1674
1687
if ( diff_x < - .05 && abs ( diff_y ) < .15 ) {
1675
1688
_input ( 'swipe left' )
1676
1689
}
@@ -1714,3 +1727,6 @@ function _fullscreenchange() {
1714
1727
document . addEventListener ( 'fullscreenchange' , _fullscreenchange )
1715
1728
1716
1729
set_orientation ( 'vertical' )
1730
+ // set_orientation('horizontal')
1731
+ // set_aspect_ratio(.5)
1732
+ set_scale ( .95 )
0 commit comments