-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-editor.html
More file actions
414 lines (366 loc) · 14.1 KB
/
Copy pathimage-editor.html
File metadata and controls
414 lines (366 loc) · 14.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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>图片编辑工具</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
text-align: center;
}
.upload-area {
border: 2px dashed #ccc;
border-radius: 10px;
padding: 20px;
margin-bottom: 20px;
cursor: pointer;
transition: all 0.3s ease;
}
.upload-area:hover {
border-color: #2c3e50;
background-color: #f8f9fa;
}
.upload-text {
color: #666;
margin-bottom: 10px;
}
.edit-button {
padding: 10px 20px;
background-color: #2c3e50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
margin-top: 20px;
}
.edit-button:hover {
background-color: #34495e;
}
.preview-image {
max-width: 100%;
height: auto;
margin-top: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.back-button {
padding: 8px 15px;
background-color: #2c3e50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
margin-bottom: 20px;
display: inline-block;
}
.back-button:hover {
background-color: #34495e;
}
.canvas-container {
position: relative;
display: inline-block;
background: white;
padding: 10px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
cursor: crosshair;
}
.crop-buttons {
position: absolute;
display: none;
gap: 10px;
padding: 10px;
background: rgba(255, 255, 255, 0.9);
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
.crop-button {
padding: 5px 15px;
border: none;
border-radius: 3px;
cursor: pointer;
font-size: 14px;
color: white;
}
.confirm-button {
background-color: #28a745;
}
.confirm-button:hover {
background-color: #218838;
}
.cancel-button {
background-color: #dc3545;
}
.cancel-button:hover {
background-color: #c82333;
}
canvas {
border: 1px solid #ccc;
border-radius: 10px;
max-width: 100%;
}
.controls {
margin-top: 20px;
}
.control-button {
padding: 8px 15px;
background-color: #2c3e50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
margin: 5px;
}
.control-button:hover {
background-color: #34495e;
}
.tip-text {
color: #666;
background-color: #f8f9fa;
padding: 10px;
border-radius: 5px;
margin-bottom: 15px;
border-left: 4px solid #2c3e50;
font-size: 14px;
}
</style>
</head>
<body>
<button class="back-button" onclick="window.location.href='aa.html'">← 返回主页面</button>
<div class="container">
<div class="tip-text">提示:对着图片右键,选择"图像另存为"即可保存图片</div>
<div class="upload-area" onclick="triggerFileInput()">
<div class="upload-text">点击或拖拽图片到此处</div>
</div>
<input type="file" id="fileInput" style="display: none;" accept="image/*" onchange="handleFileSelect(event)">
<div class="canvas-container">
<canvas id="canvas" style="display: none;"></canvas>
<div class="crop-buttons" id="cropButtons">
<button class="crop-button confirm-button" onclick="confirmCrop()">保留</button>
<button class="crop-button cancel-button" onclick="cancelCrop()">取消</button>
</div>
</div>
<div class="controls" style="display: none;">
<button class="control-button" onclick="rotateImage(-30)">左旋30°</button>
<button class="control-button" onclick="rotateImage(30)">右旋30°</button>
<button class="control-button" onclick="flipImage('horizontal')">左右对调</button>
<button class="control-button" onclick="flipImage('vertical')">上下对调</button>
<button class="control-button" onclick="cropImage()">裁剪选区</button>
</div>
</div>
<script>
let selectedFile = null;
let canvas, ctx;
let img = new Image();
let startX, startY, endX, endY;
let isDrawing = false;
let currentRotation = 0;
let originalImageData = null;
let tempCanvas = document.createElement('canvas');
let tempCtx = tempCanvas.getContext('2d');
function triggerFileInput() {
document.getElementById('fileInput').click();
}
function handleFileSelect(event) {
const file = event.target.files[0];
if (file && file.type.startsWith('image/')) {
selectedFile = file;
const reader = new FileReader();
reader.onload = function(e) {
img.src = e.target.result;
img.onload = function() {
initCanvas();
document.querySelector('.controls').style.display = 'block';
};
};
reader.readAsDataURL(file);
} else {
alert('请选择图片文件');
}
}
function initCanvas() {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
canvas.style.display = 'block';
currentRotation = 0;
originalImageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
// 初始化临时画布
tempCanvas.width = canvas.width;
tempCanvas.height = canvas.height;
tempCtx.drawImage(canvas, 0, 0);
bindCanvasEvents();
}
function bindCanvasEvents() {
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', drawSelection);
canvas.addEventListener('mouseup', endDrawing);
canvas.addEventListener('mouseleave', cancelDrawing);
}
function startDrawing(e) {
document.getElementById('cropButtons').style.display = 'none';
isDrawing = true;
const rect = canvas.getBoundingClientRect();
const scaleX = canvas.width / rect.width;
const scaleY = canvas.height / rect.height;
startX = (e.clientX - rect.left) * scaleX;
startY = (e.clientY - rect.top) * scaleY;
// 保存当前画布状态
tempCtx.clearRect(0, 0, tempCanvas.width, tempCanvas.height);
tempCtx.drawImage(canvas, 0, 0);
}
function drawSelection(e) {
if (!isDrawing) return;
const rect = canvas.getBoundingClientRect();
const scaleX = canvas.width / rect.width;
const scaleY = canvas.height / rect.height;
endX = (e.clientX - rect.left) * scaleX;
endY = (e.clientY - rect.top) * scaleY;
// 恢复原始图像
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(tempCanvas, 0, 0);
// 绘制选区边框
ctx.strokeStyle = '#ff0000';
ctx.lineWidth = 3;
ctx.setLineDash([]);
ctx.strokeRect(
Math.min(startX, endX),
Math.min(startY, endY),
Math.abs(endX - startX),
Math.abs(endY - startY)
);
}
function endDrawing() {
if (!isDrawing) return;
isDrawing = false;
ctx.setLineDash([]);
// 显示裁剪按钮,并设置正确的位置
const cropButtons = document.getElementById('cropButtons');
cropButtons.style.display = 'flex';
cropButtons.style.position = 'absolute';
// 确保按钮显示在选区附近且不超出画布范围
const rect = canvas.getBoundingClientRect();
const buttonX = Math.min(startX, endX);
const buttonY = Math.max(0, Math.min(startY, endY) - 40); // 确保不会超出顶部
cropButtons.style.left = `${buttonX}px`;
cropButtons.style.top = `${buttonY}px`;
}
function cancelDrawing() {
if (isDrawing) {
isDrawing = false;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(tempCanvas, 0, 0);
ctx.setLineDash([]);
}
}
function confirmCrop() {
const cropWidth = Math.abs(endX - startX);
const cropHeight = Math.abs(endY - startY);
const startXPos = Math.min(startX, endX);
const startYPos = Math.min(startY, endY);
const imageData = ctx.getImageData(startXPos, startYPos, cropWidth, cropHeight);
canvas.width = cropWidth;
canvas.height = cropHeight;
ctx.putImageData(imageData, 0, 0);
// 更新临时画布
tempCanvas.width = cropWidth;
tempCanvas.height = cropHeight;
tempCtx.putImageData(imageData, 0, 0);
// 隐藏裁剪按钮
document.getElementById('cropButtons').style.display = 'none';
// 重置选区坐标
startX = startY = endX = endY = undefined;
}
function cancelCrop() {
// 恢复原始图像
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(tempCanvas, 0, 0);
// 隐藏裁剪按钮
document.getElementById('cropButtons').style.display = 'none';
// 重置选区坐标
startX = startY = endX = endY = undefined;
}
function calculateRotatedSize(width, height, angle) {
const radians = (angle * Math.PI) / 180;
const sin = Math.abs(Math.sin(radians));
const cos = Math.abs(Math.cos(radians));
const newWidth = Math.floor(width * cos + height * sin);
const newHeight = Math.floor(width * sin + height * cos);
return { width: newWidth, height: newHeight };
}
function rotateImage(degrees) {
currentRotation = (currentRotation + degrees) % 360;
const radians = (currentRotation * Math.PI) / 180;
const { width: newWidth, height: newHeight } = calculateRotatedSize(img.width, img.height, currentRotation);
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d');
tempCanvas.width = newWidth;
tempCanvas.height = newHeight;
tempCtx.clearRect(0, 0, newWidth, newHeight);
tempCtx.translate(newWidth / 2, newHeight / 2);
tempCtx.rotate(radians);
tempCtx.drawImage(img, -img.width / 2, -img.height / 2);
canvas.width = newWidth;
canvas.height = newHeight;
ctx.clearRect(0, 0, newWidth, newHeight);
ctx.drawImage(tempCanvas, 0, 0);
}
function flipImage(direction) {
const width = canvas.width;
const height = canvas.height;
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d');
tempCanvas.width = width;
tempCanvas.height = height;
if (direction === 'horizontal') {
tempCtx.translate(width, 0);
tempCtx.scale(-1, 1);
} else if (direction === 'vertical') {
tempCtx.translate(0, height);
tempCtx.scale(1, -1);
}
tempCtx.drawImage(canvas, 0, 0);
ctx.clearRect(0, 0, width, height);
ctx.drawImage(tempCanvas, 0, 0);
}
function cropImage() {
if (startX !== undefined && startY !== undefined && endX !== undefined && endY !== undefined) {
const cropWidth = Math.abs(endX - startX);
const cropHeight = Math.abs(endY - startY);
const startXPos = Math.min(startX, endX);
const startYPos = Math.min(startY, endY);
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d');
tempCanvas.width = cropWidth;
tempCanvas.height = cropHeight;
tempCtx.drawImage(
canvas,
startXPos, startYPos, cropWidth, cropHeight,
0, 0, cropWidth, cropHeight
);
canvas.width = cropWidth;
canvas.height = cropHeight;
ctx.drawImage(tempCanvas, 0, 0);
originalImageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
startX = startY = endX = endY = undefined;
}
}
</script>
</body>
</html>