Skip to content

Commit 78f9de5

Browse files
authored
Merge pull request #25 from ut-code/drag_to_paint
add drag_to_paint function
2 parents 4db41d3 + 2f2b895 commit 78f9de5

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

src/iframe/life-game.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ let timer = "stop";
44
let timerId = 0;
55
let generationFigure = 0;
66
let timerTime = 1000;
7+
let isDragging = false;
8+
let dragMode = false; // true: 黒にする, false: 白にする
79

810
const DEFAULT_BOARD_SIZE = 20;
911
const DEFAULT_CELL_SIZE = 30;
@@ -44,11 +46,19 @@ function renderBoard() {
4446
button.style.height = `${cellSize}px`;
4547
button.style.padding = "0"; //cellSizeが小さいとき、セルが横長になることを防ぐ
4648
button.style.display = "block"; //cellSizeが小さいとき、行間が空きすぎるのを防ぐ
47-
button.onclick = () => {
49+
button.onmousedown = (e) => {
50+
e.preventDefault();
4851
if (timer === "stop") {
52+
isDragging = true;
4953
board[i][j] = !board[i][j];
50-
renderBoard();
51-
//クリックされたら色を反転して盤面を変更
54+
dragMode = board[i][j];
55+
button.style.backgroundColor = board[i][j] ? "black" : "white";
56+
}
57+
};
58+
button.onmouseenter = () => {
59+
if (isDragging && timer === "stop" && board[i][j] !== dragMode) {
60+
board[i][j] = dragMode;
61+
button.style.backgroundColor = board[i][j] ? "black" : "white";
5262
}
5363
};
5464
td.appendChild(button);
@@ -58,6 +68,10 @@ function renderBoard() {
5868
}
5969
}
6070

71+
document.addEventListener("mouseup", () => {
72+
isDragging = false;
73+
});
74+
6175
renderBoard();
6276
progressBoard();
6377

src/routes/+page.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@
302302
</button>
303303

304304
<div class="font-bold text-black ml-4">
305-
{isJapanese ? "" + generationFigure + "世代" : "Generation" + generationFigure}
305+
{isJapanese ? "" + generationFigure + "世代" : "Generation:" + generationFigure}
306306
</div>
307307
</div>
308308

@@ -319,7 +319,7 @@
319319
</button>
320320

321321
<button
322-
class="btn btn-ghost btn-circle hover:bg-[rgb(220,220,220)]"
322+
class="btn btn-ghost btn-circle text-black hover:bg-[rgb(220,220,220)]"
323323
onclick={() => {
324324
intervalMs = 1000;
325325
sendEvent("timer_change", intervalMs);

0 commit comments

Comments
 (0)