-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.js
399 lines (343 loc) · 12.6 KB
/
tools.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Get DOM elements
const colorPicker = document.getElementById('color-input');
const colorExpand = document.getElementById('color-picker');
const colorContainer = document.getElementById('color-container');
const colorIcon = document.getElementById('color');
const trash = document.getElementById('trash-container');
const trashChildren = Array.from(trash.getElementsByTagName("*"));
const colorChildren = Array.from(colorContainer.getElementsByTagName("*"));
const alignContainer = document.getElementById('align-container');
const alignChildren = Array.from(alignContainer.getElementsByTagName("*"));
// UI Controls
function ToggleHide(element, fn, hideAll) {
let elementToHide = document.getElementById(element);
elementToHide.classList.toggle('hidden');
if (hideAll) {
let elementsToHide = document.getElementsByClassName('ui-expand');
for (element of elementsToHide) {
if (element.id != elementToHide.id) {
element.classList.add('hidden');
}
}
}
if (fn) {
fn();
}
}
function Unhide(element, fn, hideAll) {
let elementToHide = document.getElementById(element);
elementToHide.classList.remove('hidden');
if (hideAll) {
let elementsToHide = document.getElementsByClassName('ui-expand');
for (element of elementsToHide) {
if (element.id != elementToHide.id) {
element.classList.add('hidden');
}
}
}
if (fn) {
fn();
}
}
function Hide(element, fn, hideAll) {
let elementToHide = document.getElementById(element);
elementToHide.classList.add('hidden');
if (hideAll) {
let elementsToHide = document.getElementsByClassName('ui-expand');
for (element of elementsToHide) {
if (element.id != elementToHide.id) {
element.classList.add('hidden');
}
}
}
if (fn) {
fn();
}
}
// Alwan color picker
let swatches = [
'#000000', '#333333', '#666666', '#999999', '#CCCCCC', '#FFFFFF',
'#3d0000', '#7a0000', '#ff0000', '#b85000', '#ff6f00', '#e5ff00',
'#115201', '#219e02', '#33ff00', '#00ffa2', '#02dcf0', '#00aade',
'#6800de', '#3800b0', '#2b0173', '#001b52', '#003882', '#00568f',
];
const alwan = new Alwan('#color', {
id: 'alwan-color-picker',
theme: 'light',
toggle: true,
popover: true,
position: 'right',
margin: 25,
preset: false, // Use custom button
color: '#000',
default: '#000',
target: 'color', // On which element the picker will be appended
disabled: false,
format: 'hex', // Output format of color
singleInput: false,
inputs: {}, // Allow Hex, RGB, HSL inputs
opacity: false, // Show/Hide opacity slider
preview: true, // Show a preview of the color
copy: false, // Copy the color to clipboard
swatches: swatches,
toggleSwatches: false,
shared: false, // Shared between multiple instances
});
const alwanDOM = document.getElementsByClassName('alwan')[0];
const alwanChildren = Array.from(alwanDOM.getElementsByTagName("*"));
// Load images from server using AJAX
let activeImages = [];
function LoadImagesFromServer(target, buttonClass, imageClass, sticker, sessionId) {
let xhr = new XMLHttpRequest();
if (sticker) {
xhr.open("GET", "utils/refresh-images.php?sticker=" + true);
} else {
xhr.open("GET", "utils/refresh-images.php?id=" + sessionId);
}
xhr.onload = function () {
if (xhr.status === 200) {
let images = JSON.parse(xhr.responseText); // Parse the JSON string into an object
if (images.length != 0 && !images.every((val, i) => val === activeImages[i])) {
let imagesContainer = document.getElementById(target);
if (imagesContainer.classList.contains("images-container")) { qrcode_container.classList.add("hidden") };
images.forEach(image => { // Create DOM elements
if (activeImages.includes(image)) { return; }
if (sticker) {
image += "&sticker=true";
} else {
image += "&id=" + sessionId;
}
let button = document.createElement("button");
button.classList.add(buttonClass, 'clickable');
button.addEventListener("click", function () { AddImage(image); });
let img = document.createElement("img");
img.classList.add(imageClass);
img.src = image;
button.appendChild(img);
imagesContainer.appendChild(button);
});
activeImages = images;
}
} else {
alert("Error " + xhr.status + ": " + xhr.statusText);
}
};
xhr.send();
}
// Load stickers from server
LoadImagesFromServer('sticker-container', 'image-button', 'sticker-single', true);
// Align objects
function AlignObject(direction) {
let selectedObjects = canvas.getActiveObjects();
selectedObjects.forEach((object) => {
let bounds = object.getBoundingRect();
switch (direction) {
case 'left':
object.set('left', 0 + bounds.width / 2);
break;
case 'center':
object.set('left', canvas.width / 2);
break;
case 'right':
object.set('left', canvas.width - bounds.width / 2);
break;
case 'top':
object.set('top', 0 + bounds.height / 2);
break;
case 'middle':
object.set('top', canvas.height / 2);
break;
case 'bottom':
object.set('top', canvas.height - bounds.height / 2);
break;
}
object.setCoords();
});
canvas.renderAll();
}
// Add objects to canvas
function AddSquare() {
let rect = new fabric.Rect({
width: 200,
height: 200,
originX: 'center',
originY: 'center',
top: canvas.height / 2,
left: canvas.width / 2,
fill: `${currentColor}`,
})
canvas.add(rect);
canvas.setActiveObject(canvas.item(canvas.getObjects().length - 1));
canvas.renderAll();
Hide('shape-dropdown');
}
function AddCircle() {
let circle = new fabric.Circle({
radius: 100,
originX: 'center',
originY: 'center',
top: canvas.height / 2,
left: canvas.width / 2,
fill: `${currentColor}`,
})
canvas.add(circle);
canvas.setActiveObject(canvas.item(canvas.getObjects().length - 1));
canvas.renderAll();
Hide('shape-dropdown');
}
function AddTriangle() {
let rect = new fabric.Triangle({
width: 200,
height: 200,
originX: 'center',
originY: 'center',
top: canvas.height / 2,
left: canvas.width / 2,
fill: `${currentColor}`,
})
canvas.add(rect);
canvas.setActiveObject(canvas.item(canvas.getObjects().length - 1));
canvas.renderAll();
Hide('shape-dropdown');
}
function AddText() {
canvas.add(new fabric.IText('Dubbelklik om te typen', {
fontFamily: 'Helvetica',
originX: 'center',
originY: 'center',
left: canvas.width / 2,
top: canvas.height / 2,
fill: `${currentColor}`,
fontSize: 90,
}));
canvas.setActiveObject(canvas.item(canvas.getObjects().length - 1));
canvas.renderAll();
}
function AddImage(url) {
fabric.Image.fromURL(url, function (img) {
let imgWidth = img.width;
let imgHeight = img.height;
let aspectRatio = imgWidth / imgHeight;
if (aspectRatio >= 1) {
// Image is wider than it is tall
img.scaleToWidth(canvas.width / 2);
} else {
// Image is taller than it is wide
img.scaleToHeight(canvas.height / 2);
}
img.set({
originX: 'center',
originY: 'center',
top: canvas.height / 2,
left: canvas.width / 2,
});
canvas.add(img);
canvas.setActiveObject(canvas.item(canvas.getObjects().length - 1));
canvas.renderAll();
});
Hide('image-dropdown');
Hide('sticker-dropdown');
}
// Delete objects
function DeleteObject() {
Hide('confirm-delete');
let selectedObjects = canvas.getActiveObjects();
selectedObjects.forEach(object => {
if (object.constructor === fabric.IText && object.isEditing) {
return;
}
canvas.remove(object);
canvas.discardActiveObject(); // Deselect objects
});
canvas.renderAll();
}
// Set color
let currentColor = 'black';
colorIcon.style.color = currentColor;
function SetColor(color) {
currentColor = color;
colorIcon.style.color = color;
let activeObjects = canvas.getActiveObjects();
activeObjects.forEach(object => {
object.set({ fill: `${currentColor}` });
});
canvas.renderAll();
}
alwan.on('color', function (color) {
SetColor(color.hex());
});
// Keyboard controls
document.addEventListener('keydown', (event) => {
if (event.key == 'Delete' || event.key == 'Backspace') {
DeleteObject();
}
if (event.key == 'ArrowUp') {
canvas.bringForward(canvas.getActiveObject());
} else if (event.key == 'ArrowDown') {
canvas.sendBackwards(canvas.getActiveObject());
}
});
// Custom object selection
fabric.Object.prototype.transparentCorners = false;
fabric.Object.prototype.cornerColor = 'blue';
fabric.Object.prototype.cornerStyle = 'circle';
fabric.Object.prototype.borderColor = 'blue';
fabric.Object.prototype.cornerSize = 20;
//fabric.Object.prototype.borderDashArray = [10, 5];
// Create layer icons
let layerUpIcon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Pro 6.3.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path fill="${fabric.Object.prototype.cornerColor}" d="M256 512A256 256 0 1 0 256 0a256 256 0 1 0 0 512zM377 271c9.4 9.4 9.4 24.6 0 33.9s-24.6 9.4-33.9 0l-87-87-87 87c-9.4 9.4-24.6 9.4-33.9 0s-9.4-24.6 0-33.9L239 167c9.4-9.4 24.6-9.4 33.9 0L377 271z"/></svg>`;
let layerUpBlob = new Blob([layerUpIcon], { type: 'image/svg+xml;charset=utf-8' });
let layerUpImg = new Image();
layerUpImg.src = URL.createObjectURL(layerUpBlob);
let layerDownIcon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Pro 6.3.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path fill="${fabric.Object.prototype.cornerColor}" d="M256 0a256 256 0 1 0 0 512A256 256 0 1 0 256 0zM135 241c-9.4-9.4-9.4-24.6 0-33.9s24.6-9.4 33.9 0l87 87 87-87c9.4-9.4 24.6-9.4 33.9 0s9.4 24.6 0 33.9L273 345c-9.4 9.4-24.6 9.4-33.9 0L135 241z"/></svg>`;
let layerDownBlob = new Blob([layerDownIcon], { type: 'image/svg+xml;charset=utf-8' });
let layerDownImg = new Image();
layerDownImg.src = URL.createObjectURL(layerDownBlob);
// Move layer up or down
function moveLayer(transform, forward) {
let target = transform.target;
let canvas = target.canvas;
if (forward) {
canvas.bringForward(target);
} else {
canvas.sendBackwards(target);
}
}
// Create custom controls
fabric.Object.prototype.controls.layerUp = new fabric.Control({
x: 0.5,
y: 0,
offsetX: 30,
offsetY: -22,
cursorStyle: 'pointer',
mouseUpHandler: function(eventData, transform) { moveLayer(transform, true); },
render: renderIcon(layerUpImg),
cornerSize: 28
});
fabric.Object.prototype.controls.layerDown = new fabric.Control({
x: 0.5,
y: 0,
offsetX: 30,
offsetY: 22,
cursorStyle: 'pointer',
mouseUpHandler: function (eventData, transform) { moveLayer(transform, false); },
render: renderIcon(layerDownImg),
cornerSize: 28
});
function renderIcon(icon) {
return function renderIcon(ctx, left, top, fabricObject) {
let size = this.cornerSize;
ctx.save();
ctx.translate(left, top);
ctx.rotate(fabric.util.degreesToRadians(fabricObject.angle));
ctx.drawImage(icon, -size / 2, -size / 2, size, size);
ctx.restore();
}
}
// Deselect object when clicking outside of canvas, except trash or color
document.addEventListener('mousedown', function(event) {
if(event.target != canvas.upperCanvasEl && !trashChildren.includes(event.target) && !colorChildren.includes(event.target) && !alignChildren.includes(event.target) && !alwanChildren.includes(event.target)) {
canvas.discardActiveObject();
canvas.renderAll();
}
});