Skip to content

Commit bfb334a

Browse files
committed
Add ability to hide debug UI, to be used for Cypress screenshot
Other Changes: - Also updated all the screenshots - Added some TODOs for further work - Made the readme screenshot 50% smaller
1 parent acb1798 commit bfb334a

13 files changed

+68
-11
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A clone of [2048](https://play2048.co/), and also a remake of my first side project on GitHub, [2048Clone](https://github.com/Coteh/2048Clone).
88

9-
![Browser Game Screenshot](cypress/screenshots/readme/screenshot.png "Browser Game Screenshot")
9+
![Browser Game Screenshot](screenshot.png "Browser Game Screenshot")
1010

1111
## Setup
1212

cypress.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default defineConfig({
1919
screenshotOnRunFailure: process.env.CI === undefined,
2020
video: process.env.CI === undefined,
2121
videoCompression: 0,
22+
trashAssetsBeforeRuns: false,
2223
blockHosts: ["*.posthog.com"],
2324
reporter: "junit",
2425
reporterOptions: {

cypress/e2e/misc/screenshot.cy.ts

+30
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe("misc", () => {
2828
};
2929
const preferences: Preferences = {
3030
theme: "standard",
31+
animations: "enabled",
3132
};
3233
window.localStorage.setItem("game-state", JSON.stringify(gameState));
3334
window.localStorage.setItem("persistent-state", JSON.stringify(persistentState));
@@ -39,9 +40,38 @@ describe("misc", () => {
3940
specify("gameplay screenshot", () => {
4041
cy.viewport("iphone-6");
4142

43+
// Hide debug elements from the screenshot
44+
cy.get("body").type("d");
45+
cy.get("#debug-overlay").should("be.visible").click({
46+
force: true,
47+
});
48+
49+
// TODO: Create a video screenshot for the readme.
50+
// What needs to be fixed:
51+
// - screenshot.sh needs to reposition the ffmpeg crop to where the game is located on the page
52+
// - Animations look very choppy on the video taken by Cypress
53+
54+
// After this delay, the video screenshot should start.
55+
cy.wait(1000);
56+
57+
// Static screenshot taken for now
4258
cy.screenshot("readme/screenshot", {
4359
capture: "viewport",
4460
overwrite: true,
4561
});
62+
63+
cy.get("body").type("{leftArrow}");
64+
65+
cy.wait(500);
66+
67+
cy.get("body").type("{downArrow}");
68+
69+
cy.wait(500);
70+
71+
cy.get("body").type("{leftArrow}");
72+
73+
cy.wait(500);
74+
75+
cy.get("body").type("{downArrow}");
4676
});
4777
});
17.2 KB
Loading
Loading
Loading
Loading
Loading
Loading
Loading

screenshot.png

57.8 KB
Loading

scripts/screenshot.sh

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ Y=80
99

1010
# Install Cypress first before running this script
1111

12-
./node_modules/.bin/cypress run --spec cypress/e2e/misc/screenshot.cy.js
12+
# TODO: Fix screenshots not being taken when run in headless (run in non-headless for now)
13+
./node_modules/.bin/cypress run --spec cypress/e2e/misc/screenshot.cy.ts
1314

1415
# Crop filter adapted from: https://video.stackexchange.com/a/4571
1516
# GIF filters adapted from: https://superuser.com/a/556031
1617

17-
ffmpeg -y -ss 5 -i cypress/videos/screenshot.cy.js.mp4 -filter:v "crop=$WIDTH:$HEIGHT:$X:$Y,fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 $OUTPUT_FILE
18+
# TODO: Add video screenshot
19+
# ffmpeg -y -ss 5 -i cypress/videos/screenshot.cy.ts.mp4 -filter:v "crop=$WIDTH:$HEIGHT:$X:$Y,fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 $OUTPUT_FILE
20+
21+
magick convert cypress/screenshots/readme/screenshot.png -resize 50% screenshot.png

src/index.ts

+30-8
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,17 @@ document.addEventListener("DOMContentLoaded", async () => {
284284
console.log("going up");
285285
move(DIRECTION_UP);
286286
break;
287+
case "d":
288+
if (import.meta.env.DEV) {
289+
if (debugButton.style.display === "") {
290+
debugButton.style.display = "none";
291+
undoButton.style.display = "none";
292+
} else {
293+
debugButton.style.display = "";
294+
undoButton.style.display = "";
295+
}
296+
}
297+
break;
287298
}
288299
};
289300

@@ -296,16 +307,17 @@ document.addEventListener("DOMContentLoaded", async () => {
296307
if (gameState.ended) {
297308
newGame();
298309
if (onNewGameStarted) {
299-
onNewGameStarted()
310+
onNewGameStarted();
300311
}
301312
return;
302313
}
303314
const dialogElem = createDialogContentFromTemplate("#prompt-dialog-content");
304-
(dialogElem.querySelector(".prompt-text") as HTMLSpanElement).innerText = "Are you sure you want to start a new game? All progress will be lost.";
315+
(dialogElem.querySelector(".prompt-text") as HTMLSpanElement).innerText =
316+
"Are you sure you want to start a new game? All progress will be lost.";
305317
renderPromptDialog(dialogElem, true, () => {
306318
newGame();
307319
if (onNewGameStarted) {
308-
onNewGameStarted()
320+
onNewGameStarted();
309321
}
310322
});
311323
};
@@ -543,7 +555,8 @@ document.addEventListener("DOMContentLoaded", async () => {
543555
}
544556
} else if (elem.classList.contains(BLOCK_STYLE_SETTING_NAME)) {
545557
const blockStyleIndex = selectableBlockStyles.indexOf(selectedBlockStyle);
546-
let nextBlockStyle = selectableBlockStyles[(blockStyleIndex + 1) % selectableBlockStyles.length];
558+
let nextBlockStyle =
559+
selectableBlockStyles[(blockStyleIndex + 1) % selectableBlockStyles.length];
547560
switchBlockStyle(nextBlockStyle);
548561
savePreferenceValue(BLOCK_STYLE_PREFERENCE_NAME, nextBlockStyle);
549562
toggle.innerText = nextBlockStyle;
@@ -593,12 +606,16 @@ document.addEventListener("DOMContentLoaded", async () => {
593606
}
594607
if (getPreferenceValue(ANIMATIONS_PREFERENCE_NAME) === SETTING_ENABLED) {
595608
isAnimationEnabled = true;
596-
const setting = document.querySelector(`.setting.${ANIMATIONS_SETTING_NAME}`) as HTMLElement;
609+
const setting = document.querySelector(
610+
`.setting.${ANIMATIONS_SETTING_NAME}`
611+
) as HTMLElement;
597612
const knob = setting.querySelector(".knob") as HTMLElement;
598613
knob.classList.add("enabled");
599614
}
600615
switchBlockStyle(getPreferenceValue(BLOCK_STYLE_PREFERENCE_NAME));
601-
const blockStyleSetting = document.querySelector(`.setting.${BLOCK_STYLE_SETTING_NAME}`) as HTMLElement;
616+
const blockStyleSetting = document.querySelector(
617+
`.setting.${BLOCK_STYLE_SETTING_NAME}`
618+
) as HTMLElement;
602619
(blockStyleSetting.querySelector(".toggle") as HTMLElement).innerText = selectedBlockStyle;
603620

604621
const generateShareText = (gameState: GameState) => {
@@ -827,9 +844,14 @@ document.addEventListener("DOMContentLoaded", async () => {
827844
});
828845

829846
if (import.meta.env.DEV) {
830-
undoButton.style.display = "";
831847
debugButton.style.display = "";
832-
(document.querySelector("#debug-overlay") as HTMLDivElement).style.display = "";
848+
undoButton.style.display = "";
849+
const debugOverlay = document.querySelector("#debug-overlay") as HTMLDivElement;
850+
debugOverlay.style.display = "";
851+
debugOverlay.addEventListener("click", (e) => {
852+
e.preventDefault();
853+
debugOverlay.style.opacity = debugOverlay.style.opacity !== "0" ? "0" : "1";
854+
});
833855
(document.querySelector("#swipeSensitivity") as HTMLSpanElement).innerText =
834856
swipeSensitivity.toString();
835857
}

0 commit comments

Comments
 (0)