-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplateHelpers.ts
58 lines (49 loc) · 1.9 KB
/
templateHelpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import html2canvas from 'html2canvas';
import { RefObject } from 'react';
import BackgroundRectangle from '@/assets/nft-templates/background-rectangle.svg';
import BackgroundRound from '@/assets/nft-templates/background-round.svg';
import Gem01 from '@/assets/nft-templates/Gem-01.png';
import Gem02 from '@/assets/nft-templates/Gem-02.png';
import Gem03 from '@/assets/nft-templates/Gem-03.png';
import Gem04 from '@/assets/nft-templates/Gem-04.png';
import Gem05 from '@/assets/nft-templates/Gem-05.png';
import Gem06 from '@/assets/nft-templates/Gem-06.png';
import Gem07 from '@/assets/nft-templates/Gem-07.png';
import Star01 from '@/assets/nft-templates/Star-01.svg';
import Star02 from '@/assets/nft-templates/Star-02.svg';
import Star03 from '@/assets/nft-templates/Star-03.svg';
export const backgrounds = [BackgroundRound.src, BackgroundRectangle.src];
export const gems = [
Gem01.src,
Gem02.src,
Gem03.src,
Gem04.src,
Gem05.src,
Gem06.src,
Gem07.src,
];
export const stars = [Star01.src, Star02.src, Star03.src];
export const backgroundNames = ['Circle', 'Square'];
export const gemNames = ['Jaka', 'Ines', 'Nina', 'Mojca', 'Nace', 'Eva', 'Lan'];
export const componentToPNG = async (node: RefObject<HTMLDivElement>) => {
if (!node.current) return '';
const canvas = await html2canvas(node.current, {
scrollY: -window.scrollY,
useCORS: true,
backgroundColor: null,
scale: 2,
});
const dataURI = canvas.toDataURL('image/png', 1.0);
return dataURI;
};
export const dataURItoFile = (dataURI: string, filename: string): File => {
const binary = window.atob(dataURI.split(',')[1]);
const array = [];
for (let i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
const mimeType = dataURI.split(',')[0].split(':')[1].split(';')[0];
const blob = new Blob([new Uint8Array(array)], { type: mimeType });
const file = new File([blob], filename);
return file;
};