-
Notifications
You must be signed in to change notification settings - Fork 58
/
main.ts
293 lines (243 loc) · 7.49 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import { pinterCreator } from './lib/PINTR';
import { generateSvg } from './lib/svg';
import { generateSmoothSvg } from './lib/smooth-svg';
import { debounce } from './lib/utils';
import type { Coord } from './lib/PINTR';
export type configType = {
contrast: number;
definition: number;
density: number;
makeSmoothSvg: true | false;
singleLine: true | false;
strokeWidth: number;
smoothingAmount: number;
};
const DEFAULT_IMG = '/pintr/test.jpg';
let CONFIG: configType = {
contrast: 50,
definition: 50,
density: 50,
makeSmoothSvg: false,
singleLine: true,
strokeWidth: 1,
smoothingAmount: 50,
};
let GLOBAL: {
currentImgSrc: string;
coords: [Coord, Coord][];
width: number;
height: number;
} = {
currentImgSrc: DEFAULT_IMG,
coords: [],
width: 512,
height: 512,
};
async function main(imgSrc: string) {
const canvasDrawEl: HTMLCanvasElement | null =
document.querySelector('canvas#draw');
if (!canvasDrawEl) {
throw new Error();
}
const PINTR = await pinterCreator(imgSrc, {
canvasDrawEl,
onDraw({ coords }) {
GLOBAL.coords = coords;
},
onLoad({ width, height }) {
document.documentElement.style.setProperty('--sizew', `${width / 2}px`);
document.documentElement.style.setProperty('--sizeh', `${height / 2}px`);
GLOBAL.width = width;
GLOBAL.height = height;
},
onFinish({ coords }) {
if (!CONFIG.makeSmoothSvg) return;
console.warn('MAKING');
const smoothSvgData = generateSmoothSvg(coords, {
...CONFIG,
size: [GLOBAL.width, GLOBAL.height],
});
const smoothSvgContainerEl = document.querySelector(
'.smooth-svg-container'
) as HTMLElement;
smoothSvgContainerEl.innerHTML = smoothSvgData;
},
});
const srcImgEl: HTMLImageElement | null = document.querySelector('#srcImg');
const loadingEl: HTMLElement | null = document.querySelector('.loading');
if (srcImgEl && loadingEl) {
srcImgEl.style.backgroundImage = `url("${imgSrc}")`;
loadingEl.style.display = 'none';
}
PINTR.render(CONFIG);
}
function readFile(evt: Event) {
evt.preventDefault();
evt.stopPropagation();
const target = evt.target as HTMLInputElement;
if (!target || !target.files) return;
const file = target.files[0] as File;
if (file) {
const FR = new FileReader();
FR.addEventListener('load', function (e) {
if (!e || !e.target) return;
GLOBAL.currentImgSrc = String(e.target.result);
main(String(e.target.result));
});
FR.readAsDataURL(file);
}
}
function getInputNumber(selector: string): number {
const inputEl: HTMLInputElement | null = document.querySelector(selector);
return inputEl ? Number(inputEl.value) : 0;
}
function getInputBoolean(selector: string): true | false {
const inputEl: HTMLInputElement | null = document.querySelector(selector);
return inputEl ? Boolean(Number(inputEl.value)) : false;
}
function startNewDrawing() {
const density = getInputNumber('#density');
const singleLine = getInputBoolean('#singleLine');
const contrast = getInputNumber('#contrast');
const definition = getInputNumber('#definition');
const strokeWidth = getInputNumber('#strokeWidth');
const makeSmoothSvg = getInputBoolean('#makeSmoothSvg');
const smoothingAmount = getInputNumber('#smoothingAmount');
CONFIG = {
density,
singleLine,
contrast,
definition,
strokeWidth,
makeSmoothSvg,
smoothingAmount,
};
const smoothSvgContainerEl = document.querySelector(
'.experimental--smoth-svg--container'
) as HTMLElement;
smoothSvgContainerEl.style.display = makeSmoothSvg ? 'block' : 'none';
const smooghSvgContainerWarningEl = document.querySelector(
'.experimental--smoth-svg--container--warning'
) as HTMLElement;
smooghSvgContainerWarningEl.style.display = singleLine ? 'none' : 'block';
main(GLOBAL.currentImgSrc);
}
let count = 0;
function onDrop(ev: DragEvent) {
// Prevent default behavior (Prevent file from being opened)
ev.preventDefault();
if (!ev.dataTransfer || !ev.dataTransfer.files || !ev.dataTransfer.files[0])
return;
const FR = new FileReader();
FR.addEventListener('load', function (e) {
if (!e || !e.target || !e.target.result) {
return;
}
GLOBAL.currentImgSrc = String(e.target.result);
main(String(e.target.result));
document.body.classList.remove('-dragging');
count = 0;
});
FR.readAsDataURL(ev.dataTransfer.files[0]);
}
function onDragOver(evt: DragEvent) {
evt.preventDefault();
evt.stopPropagation();
}
function onDragEnter(evt: DragEvent) {
evt.stopPropagation();
count++;
if (count) {
document.body.classList.add('-dragging');
} else {
document.body.classList.remove('-dragging');
}
}
function onDragLeave(evt: DragEvent) {
evt.stopPropagation();
count--;
if (count) {
document.body.classList.add('-dragging');
} else {
document.body.classList.remove('-dragging');
}
}
// ADD LISTENERS
const appEl = document.querySelector('.app') as HTMLElement;
appEl.addEventListener('drop', onDrop);
appEl.addEventListener('dragover', onDragOver);
appEl.addEventListener('dragenter', onDragEnter);
appEl.addEventListener('dragleave', onDragLeave);
const inputImageFileEl = document.querySelector(
'#inputImageFile'
) as HTMLInputElement;
inputImageFileEl.addEventListener('change', readFile);
const inputImageButtonEl = document.querySelector(
'#inputImageButton'
) as HTMLElement;
inputImageButtonEl.addEventListener('click', () => {
inputImageFileEl.click();
});
document.querySelectorAll('[data-start-drawing]').forEach((input) => {
input.addEventListener('change', debounce(startNewDrawing, 32));
});
const smoothingAmountEl = document.querySelector(
'#smoothingAmount'
) as HTMLInputElement;
smoothingAmountEl.addEventListener(
'change',
debounce(() => {
CONFIG.smoothingAmount = getInputNumber('#smoothingAmount');
const smoothSvgData = generateSmoothSvg(GLOBAL.coords, {
...CONFIG,
size: [GLOBAL.width, GLOBAL.height],
});
const smoothSvgContainerEl = document.querySelector(
'.smooth-svg-container'
) as HTMLElement;
smoothSvgContainerEl.innerHTML = smoothSvgData;
}, 128)
);
const downloadEl = document.querySelector('#download') as HTMLButtonElement;
downloadEl.addEventListener('click', () => {
const link = document.createElement('a');
const canvasDrawEl = document.querySelector(
'canvas#draw'
) as HTMLCanvasElement;
link.download = 'PINTR.png';
link.href = canvasDrawEl.toDataURL();
link.click();
});
const downloadSvgEl = document.querySelector(
'#downloadSvg'
) as HTMLButtonElement;
downloadSvgEl.addEventListener('click', () => {
const link = document.createElement('a');
link.download = 'PINTR.svg';
const svgData = generateSvg(GLOBAL.coords, {
...CONFIG,
size: [GLOBAL.width, GLOBAL.height],
});
const svgBlob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' });
const svgUrl = URL.createObjectURL(svgBlob);
link.href = svgUrl;
link.click();
});
const downloadSmoothSvgEl = document.querySelector(
'#downloadSmoothSvg'
) as HTMLButtonElement;
downloadSmoothSvgEl.addEventListener('click', () => {
const link = document.createElement('a');
link.download = 'PINTR.svg';
const smoothSvgData = generateSmoothSvg(GLOBAL.coords, {
...CONFIG,
size: [GLOBAL.width, GLOBAL.height],
});
const svgBlob = new Blob([smoothSvgData], {
type: 'image/svg+xml;charset=utf-8',
});
const svgUrl = URL.createObjectURL(svgBlob);
link.href = svgUrl;
link.click();
});
startNewDrawing();