-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClipPathDragMaker.js
309 lines (250 loc) · 13.2 KB
/
ClipPathDragMaker.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
// https://github.com/ManolisMariakakis/Clip-path-Maker-by-Drag-Drop?tab=GPL-3.0-1-ov-file#readme
// Initialize global variables
var width;
var height;
var grid = [1, 1];
var point;
var coords = [];
// ClipPathMaker constructor function
class ClipPathMaker {
constructor(clipboxSelector, clippathSelector) {
this.clipbox = document.querySelector(clipboxSelector);
this.clippath = document.querySelector(clippathSelector);
this.init();
}
// Initialization function to set up the clip path maker
init() {
this.appendStyles(); // Append external styles for the clip path maker
this.createShape(); // Create the initial shape and handles
}
// Function to append styles dynamically
appendStyles() {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'ClipPathDragMaker.css'; // External CSS file
document.head.appendChild(link);
}
// Function to create the initial shape with handles and points
createShape() {
var clipbox = this.clipbox;
width = clipbox.offsetWidth - 20;
height = clipbox.offsetHeight - 20;
// Create clipshape and cliphandles elements and append to clipbox
var clipshape = document.createElement('div');
clipshape.classList.add('clipshape');
var cliphandles = document.createElement('div');
cliphandles.classList.add('cliphandles');
clipbox.appendChild(clipshape);
clipbox.appendChild(cliphandles);
var clippath = this.clippath;
var codeXYSpan = document.createElement('span');
codeXYSpan.classList.add('code_xy');
codeXYSpan.textContent = '';
clippath.innerHTML = 'clip-path: ';
clippath.appendChild(codeXYSpan);
clippath.innerHTML += ';';
}
// Function to create and update the shape based on coordinates
makeShape(coords) {
var cliphandles = document.querySelector('.cliphandles');
var codeXY = document.querySelector('.code_xy');
// Clear existing handles and points
cliphandles.innerHTML = '';
codeXY.innerHTML = '';
coords.forEach((coord, i) => {
var x = coord[0];
var y = coord[1];
var code_x = x + "%";
var code_y = y + "%";
var x_px = Math.round((x / 100) * width);
var y_px = Math.round((y / 100) * height);
// Create handle element
var handleDiv = document.createElement('div');
handleDiv.classList.add('handle');
handleDiv.setAttribute('data-handle', i);
handleDiv.style.top = `${y_px}px`;
handleDiv.style.left = `${x_px}px`;
cliphandles.appendChild(handleDiv);
// Create code (point) element
var codeElement = document.createElement('code');
codeElement.classList.add('point');
codeElement.setAttribute('data-point', i);
codeElement.textContent = `${code_x} ${code_y}`;
codeXY.appendChild(codeElement);
// Handle polygon points
if (i === coords.length - 1) {
codeXY.insertAdjacentText('afterbegin', 'polygon(');
codeXY.insertAdjacentText('beforeend', ')');
this.clipIt(); // Apply the clip-path to the shape
this.DragDrop(); // Enable dragging and deleting of handles
} else {
codeXY.insertAdjacentText('beforeend', ',');
}
});
}
// Function to handle dragging and deleting of points
DragDrop() {
var clipbox = this.clipbox;
var cliphandles = clipbox.querySelectorAll(".handle");
var code_xy = document.querySelector('.code_xy');
// Delegate event to handle mousedown on handles
document.addEventListener('mousedown', (e) => {
if (e.target.classList.contains('handle')) {
var handle = e.target;
// Handle left-click (e.button === 0)
if (e.button === 0) {
var count = document.querySelectorAll('.handle').length;
// Only show delete button if more than 3 handles exist
if (count > 3) {
var point = handle.getAttribute('data-handle');
// Remove the 'show-delete' class from all handles
document.querySelectorAll('.handle').forEach((h) => {
h.classList.remove('show-delete');
});
// Add 'show-delete' class to the clicked handle
handle.classList.add('show-delete');
// Add delete button functionality if it hasn't been attached already
var deletePoint = handle.querySelector('.delete-point');
if (!deletePoint.hasListenerAttached) {
// Define the delete function
const handleDelete = (e) => {
handle.remove(); // Remove handle
var pointElement = document.querySelector(`[data-point="${point}"]`);
if (pointElement) {
pointElement.remove(); // Remove corresponding point
}
// Clean up code_xy content and update shape
code_xy.innerHTML = code_xy.innerHTML.replace(/\(,/g, '(').replace(/,\)/g, ')').replace(/,,/g, ',');
this.clipIt(); // Update the shape
// Rebuild the shape after deletion
// Get the current clip-path as text from the clippath element
var clip_path = this.clippath.textContent;
//console.log(`clip_path ${clip_path}`); // Log the current clip-path for debugging
// Remove 'clip-path:' and 'polygon()' from the string and get the coordinate part
var coordString = clip_path.replace('clip-path:', '').replace('polygon(', '').replace(')', '');
// Split the string into individual coordinate pairs (e.g., "10% 20%" becomes ['10% 20%', '30% 40%', ...])
var coordPairs = coordString.split(',').map(pair => pair.trim());
// Map each coordinate pair into an array of numeric values
// Example: ['10% 20%'] -> [10, 20]
var coordsArray = coordPairs.map(pair => {
// Split the pair into x and y values, remove '%' symbol, and convert to numbers
var [x, y] = pair.split(' ').map(val => parseFloat(val.replace('%', '')));
return [x, y]; // Return the coordinate pair as a numeric array
});
// Log the final coordinates array for debugging
//console.log('coordsArray:', coordsArray);
// Rebuild and redraw the shape using the updated coordinates
this.makeShape(coordsArray);
// Remove the event listener after execution
deletePoint.removeEventListener('mousedown', handleDelete);
};
// Attach the event listener only once
deletePoint.addEventListener('mousedown', handleDelete);
deletePoint.hasListenerAttached = true; // Mark as listener attached
}
// Delay hiding the delete button after mouseup
handle.addEventListener('mouseup', () => {
setTimeout(() => {
handle.classList.remove('show-delete');
}, 2000);
});
}
}
}
});
// Right-click contextmenu event
const handleRightClick = (e) => {
// Check if the clicked element is a handle
if (e.target.classList.contains('handle')) {
e.preventDefault(); // Prevent default right-click menu
var handle = e.target;
var point = handle.getAttribute('data-handle');
if (!handle) return; // Exit if handle doesn't exist
// Check if the right-click listener is already attached
if (!handle.hasListenerAttached) {
// Get current coords from the clip-path
var clip_path = this.clippath.textContent;
var coordString = clip_path.replace('clip-path:', '').replace('polygon(', '').replace(')', '');
var coordPairs = coordString.split(',').map(pair => pair.trim());
// Map the coord pairs into usable arrays
var coordsArray = coordPairs.map(pair => {
var [x, y] = pair.split(' ').map(val => parseFloat(val.replace('%', '')));
return [x, y];
});
// Find current handle's coordinates (x, y)
var currentCoord = coordsArray[point];
// Insert a new handle after the current handle's coordinates (offset 10%)
var newCoord = [
(currentCoord[0] + 10) % 100, // X offset
(currentCoord[1] + 10) % 100 // Y offset
];
coordsArray.splice(parseInt(point) + 1, 0, newCoord);
this.makeShape(coordsArray); // Update the shape
// Mark as listener attached to prevent further binding
handle.hasListenerAttached = true;
}
// Remove the event listener after execution
//document.removeEventListener('contextmenu', handleRightClick);
}
};
// Add listener for right-click if not already attached
if (!document.hasListenerAttached) {
document.addEventListener('contextmenu', handleRightClick);
document.hasListenerAttached = true; // Flag to prevent multiple listeners
}
// Add delete buttons to handles (only once)
document.querySelectorAll('.handle').forEach((handle) => {
handle.innerHTML = '<div class="delete-point"></div>';
});
// Handle dragging of points
cliphandles.forEach((handle) => {
let isDragging = false;
let offsetX = 0;
let offsetY = 0;
// Mousedown event for dragging
handle.addEventListener('mousedown', (e) => {
isDragging = true;
handle.classList.add('is-dragging');
offsetX = e.clientX - handle.getBoundingClientRect().left;
offsetY = e.clientY - handle.getBoundingClientRect().top;
e.preventDefault(); // Prevent default behavior
});
// Mousemove event to handle dragging
document.addEventListener('mousemove', (e) => {
if (isDragging) {
let x = e.clientX - offsetX;
let y = e.clientY - offsetY;
// Snap to grid
if (grid && grid.length === 2) {
x = Math.round(x / grid[0]) * grid[0];
y = Math.round(y / grid[1]) * grid[1];
}
if (x < 0) { x = 0; }
if (x > width) { x = width; }
if (y < 0) { y = 0; }
if (y > height) { y = height; }
handle.style.left = `${x}px`;
handle.style.top = `${y}px`;
let i = handle.dataset.handle;
let point = document.querySelector(`[data-point="${i}"]`);
point.textContent = `${((x / width) * 100).toFixed(0)}% ${((y / height) * 100).toFixed(0)}%`;
this.clipIt(); // Update the shape
e.preventDefault();
}
});
// Mouseup event to stop dragging
document.addEventListener('mouseup', () => {
if (isDragging) {
handle.classList.remove('is-dragging');
isDragging = false;
}
});
});
}
// Function to apply the clip-path style to the shape
clipIt() {
var clip_path = this.clippath.textContent;
var clipshape = document.querySelector('.clipshape');
clipshape.setAttribute('style', clip_path); // Apply clip-path
}
}