-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
147 lines (127 loc) · 4.27 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Restaline - Vector Fills for Pen Plotters</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<meta content="utf-8" http-equiv="encoding" />
<script src="paper-core.js"></script>
<script src="paperjs-offset.min.js"></script>
<link
href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet"
/>
<style>
body {
display: flex;
flex-direction: column;
background: aliceblue;
margin: 2em;
}
canvas {
margin: 1em;
display: none;
}
#canvas-container {
width: 1000px;
}
div {
display: flex;
}
a {
font-family: Arial Black, Helvetica, sans-serif;
padding: 0.5em 0.5em 0.3em;
margin: 0.5em;
border-radius: 0.7em;
text-decoration: none;
text-align: center;
}
</style>
<script type="module">
import { occultAndFill } from "./src/main.mjs";
window.onload = function () {
const input = document.getElementById("upload");
var mock = document.getElementById("mock");
var filter = /^image\/(?:svg\+xml)$/i;
input.addEventListener(
"change",
function () {
const file = this.files[0];
const fileName = file.name.slice(0, -4);
if (!filter.test(file.type)) {
alert("bad file type");
return null;
}
var reader = new FileReader();
reader.onloadend = async function (e) {
mock.innerHTML = e.target.result;
var svg = mock.querySelector("svg");
const canvasContainer = document.querySelector(
"#canvas-container"
);
const size = svg.viewBox.baseVal;
const canvas = document.querySelector("canvas");
canvas.width = document.body.clientWidth;
canvas.height = `${
document.body.clientWidth * (size.height / size.width)
}`;
paper.setup(canvas);
paper.autoUpdate = false;
const item = paper.project.importSVG(svg, {
insert: false,
expandShapes: true,
});
const options = {
spacing: 4,
inset: true,
autoClosePaths: true,
colorGroups: true,
union: false,
occult: false,
fillType: "snake",
};
const groups = await occultAndFill(item, size, options);
groups.forEach((p, i) => {
paper.project.clear();
paper.project.activeLayer.addChild(p);
const svgAsString = paper.project.exportSVG({
asString: true,
});
createLink(p, `${fileName}-${i + 1}.svg`, svgAsString);
});
paper.project.clear();
paper.project.activeLayer.addChildren(groups);
const svgAsString = paper.project.exportSVG({ asString: true });
mock.innerHTML = svgAsString;
};
reader.readAsText(file);
},
true
);
};
function createLink(path, name, dataString) {
const c =
path.strokeColor || path.fillColor || new paper.Color("black");
const contrastColor = getContrastYIQ(c);
const link = document.createElement("a");
link.download = name;
link.innerHTML = "<span class=material-icons>file_download</span>";
link.style = `background: ${c.toCSS(true)}; color: ${contrastColor}`;
link.href =
"data:image/svg+xml;charset=utf-8," + encodeURIComponent(dataString);
document.querySelector("#container").appendChild(link);
}
function getContrastYIQ(c) {
const yiq = (c.red * 299 + c.green * 587 + c.blue * 114) / 1000;
return yiq >= 128 ? "black" : "white";
}
</script>
</head>
<body>
<h1>Restaline - Vector Fills for Pen Plotters</h1>
<div id="container">
<input id="upload" type="file" />
</div>
<div id="mock"></div>
<canvas resize></canvas>
</body>
</html>