-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex copy.html
More file actions
330 lines (288 loc) · 12.6 KB
/
index copy.html
File metadata and controls
330 lines (288 loc) · 12.6 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Clip Path Shape Generator</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background: grey;
}
.shape-container {
position: relative;
width: 300px;
height: 300px;
margin: 20px auto;
background-color: lightblue;
overflow: visible;
clip-path: path("M 50,50 L 250,50 L 150,250 Z");
}
.point {
position: absolute;
width: 15px;
height: 15px;
background: red;
border-radius: 50%;
transform: translate(-50%, -50%);
cursor: grab;
}
.point:hover {
border: 1px solid blue;
}
.point-label {
display: none;
position: absolute;
font-size: 12px;
background: white;
padding: 2px 4px;
border-radius: 4px;
pointer-events: none;
}
.slider-container {
margin: 10px 0;
}
.slider-container label {
display: inline-block;
width: 80px;
}
.slider-value {
display: inline-block;
width: 40px;
text-align: center;
}
.hidden {
display: none;
}
.controls {
margin: 10px 0;
}
.controls button {
margin: 0 5px;
padding: 5px 10px;
font-size: 14px;
cursor: pointer;
}
</style>
</head>
<body>
<h2>Dynamic Clip Path Shape Generator</h2>
<button onclick="togglePoints()">Toggle Points</button>
<div class="controls">
<button onclick="undo()" id="undoBtn" disabled>Undo</button>
<button onclick="redo()" id="redoBtn" disabled>Redo</button>
</div>
<div class="shape-container" id="shape"></div>
<div class="code"><p>clip-path: <code>path(<span id="code"></span>)</code></p></div>
<div id="sliders"></div>
<script>
let points = [];
let draggedPoint = null;
let offsetX, offsetY;
let showPoints = true; // Toggle state for points visibility
let history = []; // Array to store history of states
let currentStateIndex = -1; // Index of the current state in history
function generatePoints() {
// Always start with 3 points that form a triangle
points = [
{ x: 50, y: 50 }, // Point 1
{ x: 250, y: 50 }, // Point 2
{ x: 150, y: 250 } // Point 3
];
document.getElementById('sliders').innerHTML = '';
const shape = document.getElementById('shape');
shape.innerHTML = '';
// Create sliders for the initial 3 points
points.forEach((point, index) => {
createSlider(index, point.x, point.y);
});
// Save the initial state to history
saveState();
updateShape();
}
function createSlider(index, x, y) {
const sliders = document.getElementById('sliders');
sliders.innerHTML += `
<div class="slider-container">
<label>Point-${index + 1} X:</label>
<input type="range" min="0" max="300" value="${x}" oninput="updatePoint(${index}, this.value, 'x')">
<span class="slider-value">${x}</span>
<label>Y:</label>
<input type="range" min="0" max="300" value="${y}" oninput="updatePoint(${index}, this.value, 'y')">
<span class="slider-value">${y}</span>
</div>`;
}
function updatePoint(index, value, axis) {
points[index][axis] = Math.round(parseFloat(value)); // Round to nearest integer
updateShape();
// Update the slider value display
const sliderContainer = document.querySelectorAll('.slider-container')[index];
if (axis === 'x') {
sliderContainer.querySelectorAll('.slider-value')[0].textContent = value;
} else {
sliderContainer.querySelectorAll('.slider-value')[1].textContent = value;
}
// Save the state after updating a point
saveState();
}
function updateShape() {
const shape = document.getElementById('shape');
shape.innerHTML = '';
let pathString = 'M ' + points.map(p => `${p.x},${p.y}`).join(' L ') + ' Z';
shape.style.clipPath = `path("${pathString}")`;
document.getElementById("code").innerText = pathString;
if (showPoints) {
points.forEach((p, index) => {
let pointElem = document.createElement('div');
pointElem.className = 'point';
pointElem.style.left = p.x + 'px';
pointElem.style.top = p.y + 'px';
pointElem.setAttribute('data-index', index);
pointElem.addEventListener('mousedown', startDrag);
shape.appendChild(pointElem);
let label = document.createElement('div');
label.className = 'point-label';
label.style.left = (p.x + 10) + 'px';
label.style.top = (p.y - 10) + 'px';
label.innerText = `P${index + 1} (${p.x},${p.y})`; // Use integer coordinates
shape.appendChild(label);
});
}
}
function startDrag(event) {
draggedPoint = event.target;
let index = draggedPoint.getAttribute('data-index');
offsetX = event.clientX - points[index].x;
offsetY = event.clientY - points[index].y;
document.addEventListener('mousemove', drag);
document.addEventListener('mouseup', stopDrag);
}
function drag(event) {
if (draggedPoint) {
let index = draggedPoint.getAttribute('data-index');
points[index].x = Math.min(Math.max(Math.round(event.clientX - offsetX), 0), 300); // Round to nearest integer
points[index].y = Math.min(Math.max(Math.round(event.clientY - offsetY), 0), 300); // Round to nearest integer
updateShape();
// Update the slider values when dragging
const sliderContainer = document.querySelectorAll('.slider-container')[index];
sliderContainer.querySelectorAll('.slider-value')[0].textContent = points[index].x;
sliderContainer.querySelectorAll('.slider-value')[1].textContent = points[index].y;
}
}
function stopDrag() {
document.removeEventListener('mousemove', drag);
document.removeEventListener('mouseup', stopDrag);
draggedPoint = null;
// Save the state after dragging
saveState();
}
// 💡 TOGGLE POINTS VISIBILITY 💡
function togglePoints() {
showPoints = !showPoints; // Toggle the state
updateShape(); // Redraw the shape with or without points
}
// 💡 SAVE STATE TO HISTORY 💡
function saveState() {
// Remove all future states if we're not at the latest state
if (currentStateIndex < history.length - 1) {
history.splice(currentStateIndex + 1);
}
// Save the current state
history.push(JSON.parse(JSON.stringify(points))); // Deep copy of points
currentStateIndex = history.length - 1;
// Enable/disable Undo and Redo buttons
document.getElementById('undoBtn').disabled = currentStateIndex === 0;
document.getElementById('redoBtn').disabled = currentStateIndex === history.length - 1;
}
// 💡 UNDO FUNCTIONALITY 💡
function undo() {
if (currentStateIndex > 0) {
currentStateIndex--;
points = JSON.parse(JSON.stringify(history[currentStateIndex])); // Deep copy of state
updateShape();
updateSliders();
// Enable/disable Undo and Redo buttons
document.getElementById('undoBtn').disabled = currentStateIndex === 0;
document.getElementById('redoBtn').disabled = false;
}
}
// 💡 REDO FUNCTIONALITY 💡
function redo() {
if (currentStateIndex < history.length - 1) {
currentStateIndex++;
points = JSON.parse(JSON.stringify(history[currentStateIndex])); // Deep copy of state
updateShape();
updateSliders();
// Enable/disable Undo and Redo buttons
document.getElementById('undoBtn').disabled = false;
document.getElementById('redoBtn').disabled = currentStateIndex === history.length - 1;
}
}
// 💡 UPDATE SLIDERS AFTER UNDO/REDO 💡
function updateSliders() {
const sliders = document.getElementById('sliders');
sliders.innerHTML = ''; // Clear existing sliders
// Recreate sliders for all points
points.forEach((point, index) => {
createSlider(index, point.x, point.y);
});
}
// 💡 ADD NEW POINT BETWEEN TWO CLOSEST POINTS 💡
document.getElementById('shape').addEventListener('dblclick', function (event) {
let rect = this.getBoundingClientRect();
let x = event.clientX - rect.left;
let y = event.clientY - rect.top;
// Find the two closest points
let closestPair = findClosestPair(x, y);
// Calculate the new point between them
let newPoint = {
x: (closestPair[0].x + closestPair[1].x) / 2,
y: (closestPair[0].y + closestPair[1].y) / 2
};
// Insert the new point between the two closest points
let insertIndex = closestPair[2] + 1; // Insert after the first point in the closest pair
points.splice(insertIndex, 0, newPoint);
// Update slider names and re-create sliders
updateSlidersAfterInsertion(insertIndex);
// Save the state after adding a new point
saveState();
// Update the shape
updateShape();
});
// Function to update sliders after inserting a new point
function updateSlidersAfterInsertion(insertIndex) {
const sliders = document.getElementById('sliders');
sliders.innerHTML = ''; // Clear existing sliders
// Recreate sliders for all points
points.forEach((point, index) => {
createSlider(index, point.x, point.y);
});
}
// Function to find the two closest points to the new point
function findClosestPair(x, y) {
let closestDistance = Infinity;
let closestPoints = [];
let index = -1;
// Check distance between the new point and each pair of points
for (let i = 0; i < points.length; i++) {
let nextIndex = (i + 1) % points.length; // Connect to the next point (circular)
let midX = (points[i].x + points[nextIndex].x) / 2;
let midY = (points[i].y + points[nextIndex].y) / 2;
let dist = calculateDistance(x, y, midX, midY);
if (dist < closestDistance) {
closestDistance = dist;
closestPoints = [points[i], points[nextIndex]];
index = i; // Store index of the first point in the closest pair
}
}
return [closestPoints[0], closestPoints[1], index]; // Return the two closest points and the index of the first point
}
// Function to calculate the distance between two points
function calculateDistance(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
generatePoints();
</script>
</body>
</html>