Skip to content

Commit 0926872

Browse files
authored
Merge pull request #10 from ut-code/separate_user-doc
Separate user doc
2 parents 49cf043 + 0ae8beb commit 0926872

4 files changed

Lines changed: 22 additions & 42 deletions

File tree

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
let timerId = 0;
2+
13
window.addEventListener("message", (event) => {
2-
if (event.data.type === "play-pause") {
3-
console.log("Play/Pause toggled");
4+
if (event.data.type === "play") {
5+
timer = "start";
6+
timerId = setInterval(progressBoard, 1000);
7+
} else if (event.data.type === "pause") {
8+
timer = "stop";
9+
clearInterval(timerId);
410
}
511
});

src/life-game/life-game.html

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@
66
<body>
77
<h1>
88
<div id="generation">第0世代</div>
9-
<div id="timer">停止中</div>
109
</h1>
1110
ボードのサイズ<span id="sizeLabel"></span>:
1211
<input type="number" id="sizeInput" style="width: 50px" />
1312
<button id="sizeChangeButton">サイズ変更</button>
1413

1514
<table id="game-board" style="border-collapse: collapse"></table>
16-
<button id="startbutton">START</button>      <button id="stopbutton">STOP</button
17-
>      <button id="randombutton">RANDOM</button>      <button id="resetbutton">
18-
RESET
19-
</button>
15+
<button id="randombutton">RANDOM</button>      
16+
<button id="resetbutton">RESET</button>
2017
<div id="pattern-button-container"></div>
2118
<script src="./life-game.js"></script>
2219
</body>

src/life-game/life-game.js

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use strict";
2-
let timerId = 0;
2+
33
let timer = "stop";
44
let generationFigure = 0;
55

@@ -40,10 +40,7 @@ function deadCellJudge(around) {
4040
}
4141

4242
const generation = document.getElementById("generation"); //世代を表す文(第+数字+世代)
43-
const isProgress = document.getElementById("timer"); //進行中かを表す文(再生中/停止中)
4443
//BUTTON
45-
const startButton = document.getElementById("startbutton");
46-
const stopButton = document.getElementById("stopbutton");
4744
const randomButton = document.getElementById("randombutton");
4845
const resetButton = document.getElementById("resetbutton");
4946
const sizeChangeButton = document.getElementById("sizeChangeButton");
@@ -88,7 +85,9 @@ function renderBoard() {
8885
table.appendChild(tr);
8986
}
9087
}
88+
9189
renderBoard();
90+
progressBoard();
9291

9392
randomButton.onclick = () => {
9493
//白黒ランダムにBoardを変更
@@ -108,35 +107,12 @@ resetButton.onclick = () => {
108107
stop();
109108
};
110109

111-
startButton.onclick = start;
112-
stopButton.onclick = stop;
113-
114-
function timerChange(sentence) {
115-
//現在再生中かを表すtimer変数を変更し、文章も変更
116-
timer = sentence;
117-
isProgress.textContent = timer === "start" ? "再生中" : "停止中";
118-
}
119-
120110
function generationChange(num) {
121111
//現在の世代を表すgenerationFigureを変更し、文章も変更
122112
generationFigure = num;
123113
generation.textContent = "第" + generationFigure + "世代";
124114
}
125115

126-
function start() {
127-
if (timer === "stop") {
128-
timerId = setInterval(progressBoard, 1000);
129-
timerChange("start");
130-
}
131-
}
132-
133-
function stop() {
134-
if (timer === "start") {
135-
clearInterval(timerId);
136-
timerChange("stop");
137-
}
138-
}
139-
140116
function progressBoard() {
141117
const newBoard = structuredClone(board);
142118
for (let i = 0; i < boardSize; i++) {

src/routes/+page.svelte

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import lgjs from "../life-game/life-game.js?raw";
55
import PlayandPause from "../life-game/LifeGameFunctions/PlayAndPause.js?raw";
66
7-
let code = $state(
7+
let code = $state(lgjs);
8+
9+
let previewDoc = $derived(
810
lghtml.replace(
911
/<script src="\.\/life-game\.js"><\/script>/,
1012
`<script>
@@ -14,10 +16,9 @@
1416
),
1517
);
1618
17-
let previewDoc = $derived(code);
1819
let showEditor = $state(true);
1920
let preview_iframe: HTMLIFrameElement | undefined = $state();
20-
let PlayAndPause = $state(true);
21+
let isProgress = $state(false);
2122
</script>
2223

2324
<div class="navbar bg-[#E0E0E0] shadow-sm">
@@ -34,13 +35,13 @@
3435
<button
3536
class="btn btn-ghost btn-circle hover:bg-[rgb(220,220,220)] swap ml-5"
3637
onclick={() => {
37-
PlayAndPause = !PlayAndPause;
38-
preview_iframe?.contentWindow?.postMessage({ type: "play-pause" }, "*");
38+
preview_iframe?.contentWindow?.postMessage({ type: isProgress ? "pause" : "play" }, "*");
39+
isProgress = !isProgress;
3940
}}
4041
>
41-
<input type="checkbox" bind:checked={PlayAndPause} />
42-
<img class="size-6 swap-on" src={icons.Play} alt="Play" />
43-
<img class="size-6 swap-off" src={icons.Pause} alt="Pause" />
42+
<input type="checkbox" bind:checked={isProgress} />
43+
<img class="size-6 swap-on" src={icons.Pause} alt="Pause" />
44+
<img class="size-6 swap-off" src={icons.Play} alt="Play" />
4445
</button>
4546

4647
<div class="btn btn-ghost btn-circle hover:bg-[rgb(220,220,220)] swap ml-5">

0 commit comments

Comments
 (0)