-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (87 loc) · 2.63 KB
/
index.js
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
const { SVGPathData } = require("svg-pathdata");
const cheerio = require("cheerio");
const quadratic = require("adaptive-quadratic-curve");
const robot = require("robotjs");
const fs = require("fs");
const potrace = require("potrace");
const express = require("express");
const multer = require("multer");
const port = process.env.PORT || 8080
var pathToFile
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads')
},
filename: (req, file, cb) => {
const { originalname } = file;
cb(null, originalname)
}
})
const upload = multer({ storage });
const app = express();
app.use(express.static('public'));
app.post("/upload" , upload.single('image'), async (req, res) => {
// return res.json({ status: "OK" })
await new Promise(resolve => setTimeout(resolve, 2000));
if (res) {
run()
}
})
app.listen(port, () => console.log(`App is listening at port ${port}`))
const fileSys = () => {
fs.unlink(pathToFile, function(err) {
if (err) {
throw err
} else {
// console.log("Successfully deleted the file.")
}
})
}
// setTimeout(fileSys(), 10000)
const offsetX = 100;
const offsetY = 180;
robot.setMouseDelay(2);
const trace = () => {
const dir = './uploads'
const files = fs.readdirSync(dir)
let x = files[0]
pathToFile = "./uploads/" + x;
potrace.posterize(pathToFile, { steps: 1 }, async (err, svg) => {
if (err) throw err;
fs.writeFileSync("./output.svg", svg);
const $ = cheerio.load(svg);
$("path").each((i, elm) => {
const d = elm.attribs.d;
const pathData = new SVGPathData(d);
let current = pathData.commands[0];
robot.moveMouse(current.x + offsetX, current.y + offsetY);
robot.mouseToggle("down");
for (let command of pathData.commands) {
if (command.type === SVGPathData.MOVE_TO) {
robot.mouseToggle("up");
robot.moveMouse(command.x + offsetX, command.y + offsetY);
robot.mouseToggle("down");
} else if (command.type === SVGPathData.CURVE_TO) {
robot.moveMouse(command.x1 + offsetX, command.y1 + offsetY);
const points = quadratic(
[command.x1, command.y1],
[command.x, command.y],
[command.x2, command.y2],
1
);
for (let [x, y] of points) {
robot.dragMouse(x + offsetX, y + offsetY);
}
} else if (command.type === SVGPathData.LINE_TO) {
robot.dragMouse(command.x + offsetX, command.y + offsetY);
}
}
robot.mouseToggle("up");
});
});
};
const run = () => {
trace();
fileSys();
};
// setTimeout(run, 15000);