@@ -10,18 +10,16 @@ const repulsionStrength = 20;
1010let isMouseInsideBrowserWindow = false ;
1111
1212function setup ( ) {
13- // ページの全体の高さを取得
14- let pageHeight = Math . max (
15- document . body . scrollHeight ,
16- document . documentElement . scrollHeight ,
17- document . body . offsetHeight ,
18- document . documentElement . offsetHeight ,
19- document . body . clientHeight ,
20- document . documentElement . clientHeight ,
21- ) ;
13+ // スクロールバーを含まないビューポートの幅を取得
14+ let w = document . documentElement . clientWidth ;
15+ // 背景固定 (fixed) なので、ページ全体の高さではなくウィンドウの高さを使用
16+ let h = windowHeight ;
2217
23- animationCanvas = createCanvas ( windowWidth , pageHeight ) ;
18+ animationCanvas = createCanvas ( w , h ) ;
2419 animationCanvas . position ( 0 , 0 ) ;
20+
21+ // position: fixed を明示的に設定(p5.jsのデフォルト動作による上書き防止)
22+ animationCanvas . style ( "position" , "fixed" ) ;
2523 animationCanvas . style ( "z-index" , "-1" ) ;
2624 animationCanvas . style ( "pointer-events" , "none" ) ;
2725
@@ -40,15 +38,10 @@ function draw() {
4038}
4139
4240function windowResized ( ) {
43- let pageHeight = Math . max (
44- document . body . scrollHeight ,
45- document . documentElement . scrollHeight ,
46- document . body . offsetHeight ,
47- document . documentElement . offsetHeight ,
48- document . body . clientHeight ,
49- document . documentElement . clientHeight ,
50- ) ;
51- resizeCanvas ( windowWidth , pageHeight ) ;
41+ // リサイズ時もスクロールバーを含まない幅で計算
42+ let w = document . documentElement . clientWidth ;
43+ let h = windowHeight ;
44+ resizeCanvas ( w , h ) ;
5245}
5346
5447class Particle {
@@ -88,25 +81,29 @@ class Particle {
8881 this . pos . add ( this . vel ) ;
8982 this . acc . mult ( 0 ) ;
9083
84+ // 画面端の判定と位置補正
85+ // (ウィンドウリサイズでキャンバスが小さくなった場合にパーティクルが外に残らないよう constrain で補正)
9186 if (
9287 this . pos . x > width - this . size / 2 ||
9388 this . pos . x < this . size / 2
9489 ) {
9590 this . vel . x *= - 1 ;
96- if ( this . pos . x > width - this . size / 2 )
97- this . pos . x = width - this . size / 2 ;
98- if ( this . pos . x < this . size / 2 )
99- this . pos . x = this . size / 2 ;
91+ this . pos . x = constrain (
92+ this . pos . x ,
93+ this . size / 2 ,
94+ width - this . size / 2 ,
95+ ) ;
10096 }
10197 if (
10298 this . pos . y > height - this . size / 2 ||
10399 this . pos . y < this . size / 2
104100 ) {
105101 this . vel . y *= - 1 ;
106- if ( this . pos . y > height - this . size / 2 )
107- this . pos . y = height - this . size / 2 ;
108- if ( this . pos . y < this . size / 2 )
109- this . pos . y = this . size / 2 ;
102+ this . pos . y = constrain (
103+ this . pos . y ,
104+ this . size / 2 ,
105+ height - this . size / 2 ,
106+ ) ;
110107 }
111108 }
112109
0 commit comments