Skip to content

Commit cf08bb5

Browse files
authored
Merge pull request #7 from TryShape/issue-6-save-as-png
Added the functionality to save a shape as png
2 parents 063c224 + a354e64 commit cf08bb5

4 files changed

Lines changed: 29 additions & 0 deletions

File tree

components/core/App.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { harperFetch } from "../../utils/HarperFetch";
66

77
// Dummy Shape Data
88
// import { shapes } from "../../data/shapes";
9+
910
// loader
1011
import Loader from "react-loader-spinner";
1112

components/utils/ShapeList.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ const Shape = dynamic(import("react-clip-path"), { ssr: false });
1010
// Switch
1111
import Switch from "react-switch";
1212

13+
// html-to-image
14+
import { toPng } from 'html-to-image';
15+
16+
// downloadjs
17+
import download from 'downloadjs';
18+
1319
// Shape Listing Styled-Componentns
1420
import {
1521
ShapeCards,
@@ -49,6 +55,16 @@ const ShapeList = ({ data }) => {
4955
return name.split(" ").join("-");
5056
};
5157

58+
const saveAsPng = (event, id, name) => {
59+
console.log('Save as Png');
60+
61+
toPng(document.getElementById(id)).then(function (dataUrl) {
62+
console.log(dataUrl);
63+
download(dataUrl, `${name}.png`);
64+
toast.success(`${name}.png has been exported sucessfully!`);
65+
});
66+
}
67+
5268
async function performCopy(event, formula) {
5369
event.preventDefault();
5470
try {

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"start": "next start"
99
},
1010
"dependencies": {
11+
"downloadjs": "^1.4.7",
12+
"html-to-image": "^1.6.2",
1113
"next": "^10.0.0",
1214
"react": "17.0.1",
1315
"react-clip-path": "^0.0.6",

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,11 @@ domain-browser@^1.1.1:
711711
resolved "https://registry.yarnpkg.com/domain-browser/-/domain-browser-1.2.0.tgz#3d31f50191a6749dd1375a7f522e823d42e54eda"
712712
integrity sha512-jnjyiM6eRyZl2H+W8Q/zLMA481hzi0eszAaBUzIVnmYVDBbnLxVNnfu1HgEBvCbL+71FrxMl3E6lpKH7Ge3OXA==
713713

714+
downloadjs@^1.4.7:
715+
version "1.4.7"
716+
resolved "https://registry.yarnpkg.com/downloadjs/-/downloadjs-1.4.7.tgz#f69f96f940e0d0553dac291139865a3cd0101e3c"
717+
integrity sha1-9p+W+UDg0FU9rCkROYZaPNAQHjw=
718+
714719
electron-to-chromium@^1.3.723:
715720
version "1.3.752"
716721
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.752.tgz#0728587f1b9b970ec9ffad932496429aef750d09"
@@ -957,6 +962,11 @@ hoist-non-react-statics@^3.0.0:
957962
dependencies:
958963
react-is "^16.7.0"
959964

965+
html-to-image@^1.6.2:
966+
version "1.6.2"
967+
resolved "https://registry.yarnpkg.com/html-to-image/-/html-to-image-1.6.2.tgz#9d444424cc638df13db5c7be810ac0d2962f5edd"
968+
integrity sha512-X6X7wJW2KQ+AGqMeBITdntCcQnxBgZY62MdGOi042Y70+0SMe8/iJCzUv8RNaUoXqUjWw5FPyoTDmdGoapTQIQ==
969+
960970
http-errors@1.7.3:
961971
version "1.7.3"
962972
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.7.3.tgz#6c619e4f9c60308c38519498c14fbb10aacebb06"

0 commit comments

Comments
 (0)