-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.html
More file actions
355 lines (344 loc) Β· 13.1 KB
/
temp.html
File metadata and controls
355 lines (344 loc) Β· 13.1 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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0" />
<title>Multi-object Drag / Resize / Rotate</title>
<style>
/* -------- page -------- */
html,
body {
margin: 0;
height: 100%;
background: #f5f5f5;
overflow: hidden;
font-family: sans-serif;
}
#toolbar {
position: fixed;
top: 10px;
left: 10px;
z-index: 1000;
}
#toolbar button {
padding: 0.4rem 0.8rem;
border: 0;
border-radius: 4px;
background: #1e88e5;
color: #fff;
cursor: pointer;
}
#toolbar button:hover {
background: #0d47a1;
}
/* -------- box & handles -------- */
.box-wrapper {
position: absolute;
transform-origin: top left;
user-select: none;
}
.box {
outline: 2px solid #1e88e5;
outline-offset: -2px;
position: relative;
cursor: grab;
transform: translate(-50%, -50%);
}
.box:active {
cursor: grabbing;
}
.box img {
pointer-events: none;
user-select: none;
width: 100%;
height: 100%;
}
/* text inside box */
.text {
padding: 30px;
width: calc(100% - 60px);
height: 100%;
position: absolute;
top: 0;
left: 0;
font-size: 20px;
outline: none;
}
.text:focus {
outline: none;
}
/* handles */
.dot {
position: absolute;
height: 10px;
width: 10px;
background: #1e88e5;
border: 1px solid #fff;
border-radius: 50%;
z-index: 10;
}
.dot:hover {
background: #0d47a1;
cursor: grab;
}
.dot:active {
cursor: grabbing;
}
.left-top {
top: -5px;
left: -5px;
}
.right-top {
top: -5px;
right: -5px;
}
.right-bottom {
bottom: -5px;
right: -5px;
}
.left-bottom {
bottom: -5px;
left: -5px;
}
.top-mid {
top: -5px;
left: calc(50% - 5px);
}
.bottom-mid {
bottom: -5px;
left: calc(50% - 5px);
}
.left-mid {
left: -5px;
top: calc(50% - 5px);
}
.right-mid {
right: -5px;
top: calc(50% - 5px);
}
.rotate {
top: -30px;
left: calc(50% - 5px);
cursor: url('https://findicons.com/files/icons/1620/crystal_project/16/rotate_ccw.png'),
auto;
}
.rotate-link {
position: absolute;
width: 1px;
height: 15px;
background: #1e88e5;
top: -20px;
left: calc(50% + 0.5px);
z-index: -1;
}
</style>
</head>
<body>
<div id="toolbar">
<button id="add-random">Add random object</button>
</div>
<script>
const MIN_W = 40,
MIN_H = 40
/* ---------- factory helpers ---------- */
function baseMarkup(innerHTML) {
return `
<div class="dot rotate"></div>
<div class="dot left-top"></div><div class="dot right-top"></div>
<div class="dot right-bottom"></div><div class="dot left-bottom"></div>
<div class="dot top-mid"></div><div class="dot bottom-mid"></div>
<div class="dot left-mid"></div><div class="dot right-mid"></div>
<div class="rotate-link"></div>
${innerHTML}
`
}
function createBoxWrapper(x, y, w, h, innerHTML) {
const wrap = document.createElement('div')
wrap.className = 'box-wrapper'
wrap.style.left = x + 'px'
wrap.style.top = y + 'px'
const box = document.createElement('div')
box.className = 'box'
box.style.width = w + 'px'
box.style.height = h + 'px'
box.insertAdjacentHTML('beforeend', baseMarkup(innerHTML))
wrap.appendChild(box)
document.body.appendChild(wrap)
initInteractions(wrap)
return wrap
}
function createImageBox(src, x, y, w, h) {
return createBoxWrapper(x, y, w, h, `<img src="${src}">`)
}
function createTextBox(text, x, y, w, h) {
return createBoxWrapper(
x,
y,
w,
h,
`<div class="text" contenteditable="true">${text}</div>`
)
}
/* ---------- interaction logic ---------- */
function initInteractions(wrapper) {
const box = wrapper.querySelector('.box')
const handles = {
lt: wrapper.querySelector('.left-top'),
rt: wrapper.querySelector('.right-top'),
rb: wrapper.querySelector('.right-bottom'),
lb: wrapper.querySelector('.left-bottom'),
lm: wrapper.querySelector('.left-mid'),
rm: wrapper.querySelector('.right-mid'),
tm: wrapper.querySelector('.top-mid'),
bm: wrapper.querySelector('.bottom-mid'),
rot: wrapper.querySelector('.rotate'),
}
let initX, initY, startX, startY, initW, initH, initRot
const reposition = (x, y) => {
wrapper.style.left = x + 'px'
wrapper.style.top = y + 'px'
}
const resize = (w, h) => {
box.style.width = w + 'px'
box.style.height = h + 'px'
}
const getRot = (el) => {
const t = getComputedStyle(el).transform
if (t === 'none') return 0
const [a, b] = t.match(/matrix.*\((.+)\)/)[1].split(',')
return (Math.atan2(b, a) * 180) / Math.PI
}
const setRot = (deg) => (wrapper.style.transform = `rotate(${deg}deg)`)
/* drag */
wrapper.addEventListener('mousedown', (e) => {
if (e.target.classList.contains('dot')) return
initX = wrapper.offsetLeft
initY = wrapper.offsetTop
startX = e.clientX
startY = e.clientY
const move = (e2) =>
reposition(initX + e2.clientX - startX, initY + e2.clientY - startY)
window.addEventListener('mousemove', move)
window.addEventListener(
'mouseup',
() => window.removeEventListener('mousemove', move),
{ once: true }
)
})
/* resize helper */
function attachResize(dot, left, top, xR, yR) {
dot.addEventListener('mousedown', (e) => {
e.stopPropagation()
initX = wrapper.offsetLeft
initY = wrapper.offsetTop
startX = e.clientX
startY = e.clientY
initW = box.offsetWidth
initH = box.offsetHeight
initRot = (getRot(wrapper) * Math.PI) / 180
const cos = Math.cos(initRot),
sin = Math.sin(initRot)
const move = (e2) => {
let wDiff = e2.clientX - startX,
hDiff = e2.clientY - startY
let rW = cos * wDiff + sin * hDiff,
rH = cos * hDiff - sin * wDiff
let newW = initW,
newH = initH,
newX = initX,
newY = initY
if (xR) {
if (left) {
newW = initW - rW
rW = initW - newW
} else {
newW = initW + rW
}
if (newW < MIN_W) {
newW = MIN_W
rW = left ? initW - MIN_W : MIN_W - initW
}
newX += 0.5 * rW * cos
newY += 0.5 * rW * sin
}
if (yR) {
if (top) {
newH = initH - rH
rH = initH - newH
} else {
newH = initH + rH
}
if (newH < MIN_H) {
newH = MIN_H
rH = top ? initH - MIN_H : MIN_H - initH
}
newX -= 0.5 * rH * sin
newY += 0.5 * rH * cos
}
resize(newW, newH)
reposition(newX, newY)
}
window.addEventListener('mousemove', move)
window.addEventListener(
'mouseup',
() => window.removeEventListener('mousemove', move),
{ once: true }
)
})
}
attachResize(handles.rm, false, false, true, false)
attachResize(handles.lm, true, false, true, false)
attachResize(handles.tm, false, true, false, true)
attachResize(handles.bm, false, false, false, true)
attachResize(handles.lt, true, true, true, true)
attachResize(handles.rt, false, true, true, true)
attachResize(handles.rb, false, false, true, true)
attachResize(handles.lb, true, false, true, true)
/* rotate */
handles.rot.addEventListener('mousedown', (e) => {
e.stopPropagation()
const r = box.getBoundingClientRect(),
cx = r.left + r.width / 2,
cy = r.top + r.height / 2
const move = (e2) =>
setRot((Math.atan2(e2.clientY - cy, e2.clientX - cx) * 180) / Math.PI + 90)
window.addEventListener('mousemove', move)
window.addEventListener(
'mouseup',
() => window.removeEventListener('mousemove', move),
{ once: true }
)
})
}
/* ---------- demo objects ---------- */
createImageBox(
'https://www.thelessonspace.com/_nuxt/logo.BpPnDxxn.svg',
400,
200,
500,
200
)
createTextBox('Edit me!', 250, 400, 400, 200)
/* ---------- random add button ---------- */
document.getElementById('add-random').addEventListener('click', () => {
const w = 300 + Math.random() * 200,
h = 150 + Math.random() * 150
const x = 100 + Math.random() * 600,
y = 100 + Math.random() * 400
if (Math.random() < 0.5) {
createImageBox(
`https://picsum.photos/${Math.round(w)}/${Math.round(h)}?rand=${Math.random()}`,
x,
y,
w,
h
)
} else {
createTextBox('Click to edit', x, y, w, h)
}
})
</script>
</body>
</html>