Skip to content

Commit 7c96f68

Browse files
committed
add a screenshot button
1 parent e572c07 commit 7c96f68

File tree

4 files changed

+303
-257
lines changed

4 files changed

+303
-257
lines changed

public/app.js

Lines changed: 256 additions & 256 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/index.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,33 @@ <h1>Litecanvas</h1>
171171
/>
172172
</svg>
173173
</a>
174+
<button
175+
type="button"
176+
id="screenshot"
177+
title="Take a screenshot"
178+
aria-label="Take a screenshot"
179+
style="margin-left: auto; order: 3"
180+
>
181+
<svg
182+
xmlns="http://www.w3.org/2000/svg"
183+
fill="none"
184+
width="24"
185+
height="24"
186+
stroke-width="1.5"
187+
stroke="currentColor"
188+
>
189+
<path
190+
stroke-linecap="round"
191+
stroke-linejoin="round"
192+
d="M6.827 6.175A2.31 2.31 0 0 1 5.186 7.23c-.38.054-.757.112-1.134.175C2.999 7.58 2.25 8.507 2.25 9.574V18a2.25 2.25 0 0 0 2.25 2.25h15A2.25 2.25 0 0 0 21.75 18V9.574c0-1.067-.75-1.994-1.802-2.169a47.865 47.865 0 0 0-1.134-.175 2.31 2.31 0 0 1-1.64-1.055l-.822-1.316a2.192 2.192 0 0 0-1.736-1.039 48.774 48.774 0 0 0-5.232 0 2.192 2.192 0 0 0-1.736 1.039l-.821 1.316Z"
193+
/>
194+
<path
195+
stroke-linecap="round"
196+
stroke-linejoin="round"
197+
d="M16.5 12.75a4.5 4.5 0 1 1-9 0 4.5 4.5 0 0 1 9 0ZM18.75 10.5h.008v.008h-.008V10.5Z"
198+
/>
199+
</svg>
200+
</button>
174201
</div>
175202
<div class="editor">
176203
<div class="code">

public/sw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const cacheName = "luizbills.litecanvas-editor-v1";
2-
const version = "2025.7.18.1";
2+
const version = "2025.7.18.2";
33

44
const precacheResources = [
55
"/",

src/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const desktopExtensions = [];
6363
const code = $(".code");
6464
const game = $(".game");
6565
const playButton = $("#play");
66+
const screenshotButton = $("#screenshot");
6667
const stopButton = $("#stop");
6768
const shareButton = $("#share");
6869
const copyButton = $("#copy");
@@ -76,6 +77,7 @@ playButton.addEventListener("click", () => {
7677
show(game);
7778
hide(playButton);
7879
show(stopButton);
80+
show(screenshotButton);
7981
runCode();
8082
if (smallScreen) {
8183
getIframe().focus();
@@ -90,6 +92,7 @@ function stopGame(evt) {
9092
hide(game);
9193
show(playButton);
9294
hide(stopButton);
95+
hide(screenshotButton);
9396
getIframe().remove();
9497
}
9598

@@ -147,6 +150,21 @@ hideEditor.addEventListener("click", (evt) => {
147150
getIframe().focus();
148151
});
149152

153+
screenshotButton.addEventListener("click", () => {
154+
var link = document.createElement("a");
155+
link.download = "litecanvas-colors.png";
156+
const iframe = getIframe();
157+
158+
if (!iframe) return;
159+
160+
const iframeDocument = iframe.contentDocument;
161+
if (iframeDocument) {
162+
const canvas = iframeDocument.querySelector("canvas");
163+
link.href = canvas.toDataURL();
164+
link.click();
165+
}
166+
});
167+
150168
function runCode() {
151169
const iframe = getIframe();
152170
iframe.src = "preview.html"; // reload the iframe
@@ -329,6 +347,7 @@ if (!smallScreen) {
329347
} else {
330348
show(playButton);
331349
hide(game);
350+
hide(screenshotButton);
332351
}
333352

334353
window.isUpdateAvailable = new Promise(function (resolve) {

0 commit comments

Comments
 (0)