Skip to content

Commit 8402ef1

Browse files
committed
feat: allow download images
1 parent bbd9ded commit 8402ef1

File tree

6 files changed

+114
-7
lines changed

6 files changed

+114
-7
lines changed

index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ <h1 class="select-none">Automata Simulation</h1>
8585
>
8686
<i class="fa-solid fa-trash-can text-[10px]"></i>
8787
</div>
88+
89+
<div
90+
class="bg-white cursor-pointer select-none rounded-full border-2 w-8 h-8 text-center leading-6 text-xs"
91+
title="Clear all"
92+
id="btn-download"
93+
>
94+
<i class="fa-solid fa-cloud-arrow-down text-[10px]"></i>
95+
</div>
8896
</div>
8997

9098
<div class="flex gap-4">

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"author": "",
1616
"license": "ISC",
1717
"dependencies": {
18+
"html2canvas": "^1.4.1",
1819
"micromodal": "^0.4.10",
1920
"split.js": "^1.6.5"
2021
}

src/graph.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@ function initGraph() {
117117

118118
paper.on("element:pointerdblclick", function (elementView, evt) {
119119
const currentElement = elementView.model;
120-
// const label = prompt("Label name?");
121-
// currentElement.attr("label/text", label);
122-
console.log(currentElement);
123120
const inputEl = document.querySelector("#input-state-name");
124121
MicroModal.show("modal-state-name");
125122
inputEl.setAttribute("state-id", currentElement.id);

src/main.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
import MicroModal from "micromodal";
12
import Split from "split.js";
23
import { verifyAFD } from "./afd.js";
34
import { renderError, renderOut, renderOutString } from "./animateNode.js";
4-
import { createAutomata, clearAutomata } from "./automata.js";
5+
import { clearAutomata, createAutomata } from "./automata.js";
56
import { startDragTools } from "./dragTools.js";
6-
import { CANVAS_HEIGHT, initGraph } from "./graph.js";
7-
import { CircleShape, FILL_NODE_FINAL, NODE_WIDTH } from "./shapes.js";
8-
import MicroModal from "micromodal"; // es6 module
7+
import { initGraph } from "./graph.js";
8+
import { CircleShape, FILL_NODE_FINAL } from "./shapes.js";
9+
import download from "./utils/download.js";
910

1011
const { graph, paper } = initGraph();
1112
const inputString = document.querySelector("#input-string");
1213
const inputEl = document.querySelector("#input-label-name");
1314
const inputLabel = document.querySelector("#input-label-name");
1415
const inputState = document.querySelector("#input-state-name");
1516
const btnClearAll = document.querySelector("#btn-clear-all");
17+
const btnDownload = document.querySelector("#btn-download");
1618

1719
const automata = createAutomata();
1820

@@ -151,3 +153,6 @@ btnClearAll.addEventListener("click", () => {
151153
clearAutomata(automata);
152154
graph.clear();
153155
});
156+
157+
// Download png
158+
btnDownload.addEventListener("click", download);

src/utils/download.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import html2canvas from "html2canvas";
2+
3+
function download() {
4+
const canvas = document.querySelector("#paper");
5+
html2canvas(canvas).then((result) => {
6+
const a = document.createElement("a");
7+
a.href = result.toDataURL("image/png");
8+
a.download = `automata-${Date.now()}.png`;
9+
a.click();
10+
});
11+
}
12+
13+
export default download;

0 commit comments

Comments
 (0)