-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag.html
243 lines (224 loc) · 6.31 KB
/
drag.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Transform3d</title>
<style>
html,body{margin: 0; padding: 0; user-select: none;}
.page{
display: flex;
align-items: center;
justify-content: center;
width: 100vw;
height: 100vh;
}
.box{
padding: 16px;
width: 302px;
height: 400px;
overflow: hidden;
background-color: white;
border: 1px solid #ccc;
}
img{
width: 100%;
}
.form-item{
margin-bottom: 10px;
}
.submit{
text-align: right;
}
.handle{
position: absolute;
left: 0;
top: 0;
z-index: 1;
width: 20px;
height: 20px;
border-radius: 100%;
background-color: rgba(255, 0, 0, .5);
}
</style>
<script src="./numeric.min.js"></script>
</head>
<body>
<div class="page">
<div class="box" id="box">
<form action="#">
<div class="form-item">
<label for="name">
姓名
<input type="text" id="name">
</label>
</div>
<div class="form-item">
<label for="address">
地址
<textarea id="address" cols="30" rows="10"></textarea>
</label>
</div>
<div class="form-item">
<label for="range">
年龄
<input type="range" min="10" max="100" step="1" value="18" id="range">
</label>
</div>
<div class="submit">
<button>submit</button>
</div>
</form>
</div>
<div class="handle"></div>
<div class="handle"></div>
<div class="handle"></div>
<div class="handle"></div>
</div>
<script>
function init(){
const box = document.querySelector('#box');
const handles = document.querySelectorAll('.handle')
function getFromPoints(points){
// 映射前四个点坐标
const result = [];
const len = points.length;
for (let k = 0; k < len; k++) {
let p = points[k];
result.push({
x: p[0] - points[0][0],
y: p[1] - points[0][1]
});
}
/**
result
[
{x1, y1},
{x2, y2},
{x3, y3},
{x4, y4},
]
*/
return result;
}
function getToPoints(origin, target){
// 映射后四个点坐标
const result = [];
for (let k = 0, len = target.length; k < len; k++) {
p = target[k];
result.push({
x: p[0] - origin[0][0],
y: p[1] - origin[0][1]
});
}
return result;
}
function getTransform(from, to) {
var A, H, b, h;
A = []; // 8x8
// 四个点的坐标
for (let i =0 ; i < 4; i++) {
A.push([from[i].x, from[i].y, 1, 0, 0, 0, -from[i].x * to[i].x, -from[i].y * to[i].x]);
A.push([0, 0, 0, from[i].x, from[i].y, 1, -from[i].x * to[i].y, -from[i].y * to[i].y]);
}
b = []; // 8x1
for (let i = 0; i < 4; i++) {
b.push(to[i].x);
b.push(to[i].y);
}
// Solve A * h = b for h
// 即矩阵中常见的 Ax=b
// numeric.solve eg:
// IN> numeric.solve([[1,2],[3,4]],[17,39])
// OUT> [5,6]
// https://ccc-js.github.io/numeric2/documentation.html
h = numeric.solve(A, b);
/**
解得: h matrix
[
h0, h1, 0 h2
h3, h4, 0 h5
0, 0, 0, 1
h6, h7, 0 h8
]
*/
H = [
[h[0], h[1], 0, h[2]],
[h[3], h[4], 0, h[5]],
[0, 0, 1, 0],
[h[6], h[7], 0, 1]
];
return H;
};
function getMatrixCSSParameters(H){
// 获取 css matrix3d(a, b, 0, 0, c, d, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1) 参数
const result = [];
for (let i =0; i < 4; i++) {
const result1 = [];
for (let j = 0; j < 4; j++) {
result1.push(H[j][i].toFixed(20));
}
result.push(result1);
}
return result.join(',');
}
function applyTransform(element, origin, target) {
const from = getFromPoints(origin);
const to = getToPoints(origin, target);
const H = getTransform(from, to);
element.style.transform = `matrix3d(${getMatrixCSSParameters(H)})`;
element.style.transformOrigin = '0 0';
};
function getPoints(element){
const rect = element.getBoundingClientRect();
return [
[rect.left, rect.top],
[rect.left, rect.bottom],
[rect.right, rect.top],
[rect.right, rect.bottom],
];
}
function getControlPoints(element){
const result = [];
for( let handle of handles){
const rect = handle.getBoundingClientRect()
result.push([rect.x + 10, rect.y + 10]);
}
return result;
}
const originalPoints = getPoints(box);
const initControlHandles = () => {
originalPoints.forEach( (p, i) => {
handles[i].style.left = `${p[0] - 10}px`
handles[i].style.top = `${p[1] - 10}px`
})
}
const initEvent = ()=> {
for(let i=0; i<4; i++){
handles[i].addEventListener('mousedown', (e) => {
const handle = e.target;
const startX = e.x;
const startY = e.y;
const rect = handle.getBoundingClientRect();
const mousemove = (e) => {
handle.style.left = `${e.x - (startX - rect.x)}px`;
handle.style.top = `${e.y - (startY - rect.y)}px`;
applyTransform(box, originalPoints, getControlPoints())
}
const mouseup = () => {
applyTransform(box, originalPoints, getControlPoints())
document.removeEventListener('mousemove', mousemove)
document.removeEventListener('mouseup', mouseup)
}
document.addEventListener('mousemove', mousemove)
document.addEventListener('mouseup', mouseup)
})
}
}
initEvent();
initControlHandles();
}
init();
</script>
</body>
</html>