Skip to content

Commit ade3149

Browse files
authored
Merge pull request #745 from whatwareweb/dev2
Add features to SVG import
2 parents bf30801 + 8fa5f70 commit ade3149

File tree

2 files changed

+142
-28
lines changed

2 files changed

+142
-28
lines changed

src/components/DropBox.tsx

Lines changed: 91 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,51 +102,114 @@ function pauseEvent(e) {
102102
return false;
103103
}
104104

105-
function customAlert(polylines) {
105+
const dialogstyle = `
106+
z-index: 999999999999;
107+
width: 550px;
108+
min-height: 100px;
109+
position: absolute;
110+
left: 50%;
111+
top: 100px;
112+
transform: translate(-50%, 0%);
113+
background: #f4f4f4;
114+
border-radius: 10px;
115+
border: 1px solid black;
116+
padding: 8px;
117+
`
118+
119+
const polylinesStyle = `
120+
overflow-x: auto;
121+
background: #e2e2e2;
122+
border-radius: 5px;
123+
margin: auto;
124+
margin-top: 1em;
125+
margin-bottom: 1em;
126+
padding: .25rem;
127+
`
128+
129+
const buttonClasses = `class="mx-auto my-1 text-white p-2 rounded cursor-pointer bg-gray-700 hover:bg-gray-500"`
106130

131+
function customAlert(polylines) {
107132
const el = document.createElement("div");
108-
const style = `
109-
z-index: 999999999999;
110-
width: 550px;
111-
min-height: 100px;
112-
position: absolute;
113-
left: 50%;
114-
top: 100px;
115-
transform: translate(-50%, 0%);
116-
background: #f4f4f4;
117-
border-radius: 10px;
118-
border: 1px solid black;
119-
padding: 8px;
120-
`
121-
122-
const polylinesStyle = `
123-
overflow-x: auto;
124-
background: #e2e2e2;
125-
border-radius: 5px;
126-
margin: auto;
127-
margin-top: 1em;
128-
margin-bottom: 1em;
129-
padding: .25rem;
130-
`
131-
133+
132134
el.innerHTML = `
133-
<div style="${style}">
135+
<div style="${dialogstyle}">
134136
<div>
135137
<div>Here are the polylines of your SVG. Copy and paste it into the editor.</div>
136138
<pre style="${polylinesStyle}">${polylines}</pre>
137139
</div>
138140
139141
<span style="width: 100%; display: flex;">
140-
<button class="mx-auto my-1 text-white p-2 rounded cursor-pointer bg-gray-700 hover:bg-gray-500">
142+
<button id="closeButton"" ${buttonClasses}>
141143
close
142144
</button>
145+
<button id="scaleButton" ${buttonClasses}>
146+
scale
147+
</button>
143148
</span>
144149
</div>
145150
`
146151

147-
el.querySelector("button").addEventListener("click", () => {
152+
el.querySelector("#closeButton").addEventListener("click", () => {
148153
el.remove();
149154
})
155+
156+
el.querySelector("#scaleButton").addEventListener("click", () => {
157+
el.remove();
158+
scaleAlert(polylines);
159+
})
160+
161+
document.body.append(el);
162+
}
163+
164+
function scaleAlert(polylines) {
165+
const el = document.createElement("div");
166+
167+
el.innerHTML = `
168+
<div style="${dialogstyle}">
169+
<div>
170+
<div>Scale your polylines to specific dimensions.</div>
171+
<pre style="${polylinesStyle}">${polylines}</pre>
172+
</div>
173+
174+
<div style="width: 100%; display: flex;">
175+
<input type="number" id="newWidth" placeholder="Width" style="${polylinesStyle} width: 50%; margin: 5px; ">
176+
<input type="number" id="newHeight" placeholder="Height" style="${polylinesStyle} width: 50%; margin: 5px; ">
177+
</div>
150178
179+
<div style="width: 100%; text-align: center;">
180+
<input type="checkbox" id="paddingCheckbox" name="paddingCheckbox">
181+
<label for="paddingCheckbox"> Add Padding</label><br>
182+
</div>
183+
184+
<span style="width: 100%; display: flex;">
185+
<button id="scaleButton"" ${buttonClasses}>
186+
scale (keep aspect ratio)
187+
</button>
188+
<button id="cancelButton"" ${buttonClasses}>
189+
cancel
190+
</button>
191+
</span>
192+
</div>
193+
`
194+
195+
el.querySelector("#scaleButton").addEventListener("click", () => {
196+
const newWidth = el.querySelector("#newWidth").value;
197+
const newHeight = el.querySelector("#newHeight").value;
198+
const addPadding = el.querySelector("#paddingCheckbox").checked;
199+
200+
if (Number.isNaN(newWidth) || Number.isNaN(newHeight)|| newWidth == "" || newHeight == "") {
201+
alert("Invalid width/height provided ");
202+
} else {
203+
const newPolyLines = tk.scalePolylinesToDimension(polylines, newWidth, newHeight, addPadding);
204+
el.remove();
205+
customAlert(newPolyLines);
206+
}
207+
})
208+
209+
el.querySelector("#cancelButton").addEventListener("click", () => {
210+
el.remove();
211+
customAlert(polylines);
212+
})
213+
151214
document.body.append(el);
152215
}

src/drawingToolkit/toolkit.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,57 @@ export const toolkit = {
149149
}
150150

151151
},
152+
scalePolylinesToDimension(polylines, width, height, addPadding){
153+
polylines = JSON.parse(polylines);
154+
155+
let minXVal = Number.POSITIVE_INFINITY;
156+
let minYVal = Number.POSITIVE_INFINITY;
157+
polylines.forEach((obj) => {
158+
obj.forEach((coord) => {
159+
if (coord[0] < minXVal) {
160+
minXVal = coord[0];
161+
}
162+
if (coord[1] < minYVal) {
163+
minYVal = coord[1];
164+
}
165+
})
166+
})
167+
168+
translate(polylines, [-minXVal, -minYVal]);
169+
170+
let maxXVal = Number.NEGATIVE_INFINITY;
171+
let maxYVal = Number.NEGATIVE_INFINITY;
172+
polylines.forEach((obj) => {
173+
obj.forEach((coord) => {
174+
if (coord[0] > maxXVal) {
175+
maxXVal = coord[0];
176+
}
177+
if (coord[1] > maxYVal) {
178+
maxYVal = coord[1];
179+
}
180+
})
181+
})
182+
183+
var ratio = Math.min(width / maxXVal, height / maxYVal);
184+
185+
polylines.forEach((obj) => {
186+
obj.forEach((coord) => {
187+
coord[0] *= ratio;
188+
coord[1] *= ratio;
189+
})
190+
})
191+
192+
if (ratio == height / maxYVal) {
193+
translate(polylines, [width/2 - maxXVal * ratio / 2, 0]);
194+
} else if (ratio == width / maxXVal) {
195+
translate(polylines, [0, height/2 - maxYVal * ratio / 2]);
196+
}
197+
if (addPadding) {
198+
scale(polylines, 0.97);
199+
}
200+
201+
return JSON.stringify(polylines);
202+
},
152203
join() {
153204
const [first, ...rest] = arguments;
154205
if (!first) return [];

0 commit comments

Comments
 (0)