OzoneAsai commited on
Commit
7dfd210
·
verified ·
1 Parent(s): 28988dd

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +48 -48
script.js CHANGED
@@ -1,45 +1,26 @@
1
  // モジュールの設定
2
  const { Engine, Render, Runner, Bodies, World, Events } = Matter;
3
 
4
- let engine, world, render, runner;
5
-
6
- // エンジンとレンダラの初期化
7
- function initializeEngine() {
8
- // 既存のエンジンとレンダラがあればキャンセル
9
- if (engine) {
10
- Matter.Render.stop(render);
11
- Matter.Runner.stop(runner);
12
- World.clear(world);
13
- Engine.clear(engine);
14
- render.canvas.remove();
15
- render.canvas = null;
16
- render.context = null;
17
- render.textures = {};
18
  }
 
19
 
20
- // 新しいエンジンとワールドを作成
21
- engine = Engine.create();
22
- world = engine.world;
23
-
24
- // 新しいレンダラを作成
25
- render = Render.create({
26
- element: document.body,
27
- engine: engine,
28
- canvas: document.getElementById('world'),
29
- options: {
30
- width: window.innerWidth,
31
- height: window.innerHeight,
32
- wireframes: false
33
- }
34
- });
35
-
36
- Render.run(render);
37
- runner = Runner.create();
38
- Runner.run(runner, engine);
39
- }
40
-
41
- // 初期化
42
- initializeEngine();
43
 
44
  // シミュレーションに必要な変数
45
  let ball = null;
@@ -48,18 +29,27 @@ let vyData = [];
48
  let xData = [];
49
  let tData = [];
50
  let vxyData = [];
 
51
  let startTime = null;
52
 
 
 
 
53
  // クリックイベントで小球を生成
54
  document.addEventListener('click', (event) => {
 
 
 
 
 
 
 
55
  if (ball) {
56
- World.remove(world, ball);
57
- plotGraphs();
58
- initializeEngine(); // エンジンをリセットして再初期化
59
  }
60
 
61
- const x = event.clientX;
62
- const y = event.clientY;
63
  const velocityX = parseFloat(document.getElementById('vx').value); // ユーザー入力の初速度X
64
  const velocityY = parseFloat(document.getElementById('vy').value); // ユーザー入力の初速度Y
65
 
@@ -68,11 +58,6 @@ document.addEventListener('click', (event) => {
68
  World.add(world, ball);
69
 
70
  // データの初期化
71
- vxData = [];
72
- vyData = [];
73
- xData = [];
74
- tData = [];
75
- vxyData = [];
76
  startTime = new Date().getTime();
77
  });
78
 
@@ -86,17 +71,32 @@ Events.on(engine, 'beforeUpdate', () => {
86
  tData.push(elapsedTime);
87
  vxyData.push({ x: ball.velocity.x, y: ball.velocity.y });
88
 
 
 
 
 
89
  // 画面外に出た場合、ボールを削除
90
  if (ball.position.x < 0 || ball.position.x > window.innerWidth ||
91
  ball.position.y < 0 || ball.position.y > window.innerHeight) {
92
  World.remove(world, ball);
93
  ball = null;
94
  plotGraphs();
95
- initializeEngine(); // エンジンをリセットして再初期化
96
  }
97
  }
98
  });
99
 
 
 
 
 
 
 
 
 
 
 
 
100
  // グラフ描画の関数
101
  function plotGraphs() {
102
  plotChart('vxChart', 'Velocity-X vs Time', tData, vxData);
 
1
  // モジュールの設定
2
  const { Engine, Render, Runner, Bodies, World, Events } = Matter;
3
 
4
+ // エンジンとワールドの作成
5
+ const engine = Engine.create();
6
+ const world = engine.world;
7
+
8
+ // レンダラの作成
9
+ const render = Render.create({
10
+ element: document.body,
11
+ engine: engine,
12
+ canvas: document.getElementById('world'),
13
+ options: {
14
+ width: window.innerWidth,
15
+ height: window.innerHeight,
16
+ wireframes: false,
17
+ background: '#ffffff'
18
  }
19
+ });
20
 
21
+ Render.run(render);
22
+ const runner = Runner.create();
23
+ Runner.run(runner, engine);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  // シミュレーションに必要な変数
26
  let ball = null;
 
29
  let xData = [];
30
  let tData = [];
31
  let vxyData = [];
32
+ let breadcrumbs = [];
33
  let startTime = null;
34
 
35
+ // キャンバスの境界を取得
36
+ const canvasBounds = render.canvas.getBoundingClientRect();
37
+
38
  // クリックイベントで小球を生成
39
  document.addEventListener('click', (event) => {
40
+ // キャンバス内のクリックのみ反応
41
+ if (event.clientX < canvasBounds.left || event.clientX > canvasBounds.right ||
42
+ event.clientY < canvasBounds.top || event.clientY > canvasBounds.bottom) {
43
+ return;
44
+ }
45
+
46
+ // 既存のボールがあれば新しいボールを生成しない
47
  if (ball) {
48
+ return;
 
 
49
  }
50
 
51
+ const x = event.clientX - canvasBounds.left;
52
+ const y = event.clientY - canvasBounds.top;
53
  const velocityX = parseFloat(document.getElementById('vx').value); // ユーザー入力の初速度X
54
  const velocityY = parseFloat(document.getElementById('vy').value); // ユーザー入力の初速度Y
55
 
 
58
  World.add(world, ball);
59
 
60
  // データの初期化
 
 
 
 
 
61
  startTime = new Date().getTime();
62
  });
63
 
 
71
  tData.push(elapsedTime);
72
  vxyData.push({ x: ball.velocity.x, y: ball.velocity.y });
73
 
74
+ // BreadCrumbsの追加
75
+ breadcrumbs.push(Bodies.circle(ball.position.x, ball.position.y, 2, { isStatic: true }));
76
+ World.add(world, breadcrumbs[breadcrumbs.length - 1]);
77
+
78
  // 画面外に出た場合、ボールを削除
79
  if (ball.position.x < 0 || ball.position.x > window.innerWidth ||
80
  ball.position.y < 0 || ball.position.y > window.innerHeight) {
81
  World.remove(world, ball);
82
  ball = null;
83
  plotGraphs();
84
+ resetVariables();
85
  }
86
  }
87
  });
88
 
89
+ // 変数をリセットする関数
90
+ function resetVariables() {
91
+ vxData = [];
92
+ vyData = [];
93
+ xData = [];
94
+ tData = [];
95
+ vxyData = [];
96
+ breadcrumbs.forEach(breadcrumb => World.remove(world, breadcrumb));
97
+ breadcrumbs = [];
98
+ }
99
+
100
  // グラフ描画の関数
101
  function plotGraphs() {
102
  plotChart('vxChart', 'Velocity-X vs Time', tData, vxData);