-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
132 lines (123 loc) · 3.24 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
</style>
</head>
<body>
<div style="width: 320px;overflow: hidden;">
<canvas id="canvas"></canvas>
<form>
<input type="file" id="file">
<button type="button" onclick="cut()" style="float: right;">裁 剪</button>
</form>
</div>
<canvas id="canvas2" width="100" height="100" ></canvas>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var canvas2 = document.getElementById('canvas2');
var ctx2 = canvas2.getContext('2d');
var width = 320;
var height = 480;
var cutWidth = 200;
var cutHeight = 200;
canvas.width = width;
canvas.height = height;
canvas2.width = cutWidth;
canvas2.height = cutHeight;
var avatarLeft = (width - cutWidth) / 2;
var avatarTop = (height - cutHeight) / 2;
var img = new Image();
var startX,startY,endX,endY
var _top = 0,
_left = 0,
_tmp_top = 0,
_tmp_left = 0;
// mask
function mask(){
ctx.beginPath();
ctx.rect(0, 0, width, avatarTop);
ctx.rect(0, avatarTop + cutHeight, width, avatarTop);
ctx.rect(0, avatarTop, avatarLeft, cutHeight);
ctx.rect(avatarLeft + cutWidth, avatarTop, avatarLeft, cutHeight);
ctx.fillStyle = 'rgba(0, 0, 0, .5)';
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.strokeStyle = 'rgba(255, 255, 255, 1)';
ctx.rect(avatarLeft, avatarTop, cutWidth, cutHeight);
ctx.closePath();
ctx.stroke();
}
// event
function eventListenr(argument) {
canvas.onmousewheel = function(event) {
clear()
var delta = event.wheelDelta > 0 ? true : false;
var _width,_height;
if(delta){
_width = width+=4
_height = height+=4
}else{
_width = width-=4
_height = height-=4
}
ctx.drawImage(img,_top,_left,_width,_height)
mask();
}
canvas.onmousedown = function(event){
startX = event.offsetX
startY = event.offsetY
canvas.onmousemove = function(e) {
endX = e.offsetX
endY = e.offsetY
_tmp_top = endX - startX
_tmp_left = endY - startY
clear()
ctx.drawImage(img,_top+_tmp_top,_left+_tmp_left,width,height)
mask();
}
}
document.onmouseup = function(e){
_top += _tmp_top
_left += _tmp_left
_tmp_top = 0;
_tmp_left = 0;
canvas.onmousemove = null;
}
document.getElementById('file').onchange = function(e) {
var reader = new FileReader();
var file = e.target.files[0];
document.forms[0].reset();
reader.onload=function(e) {
res = e.target.result;
img.src = res
img.onload = function () {
_top = 0
_left = 0
drawImg()
canvas.setAttribute('style', 'cursor:move;')
mask();
}
};
reader.readAsDataURL(file);
}
}
function clear(){
ctx.clearRect(0, 0, canvas.width, canvas.height)
}
mask();
eventListenr();
function drawImg(){
ctx.drawImage(img,0,0,width,height)
}
function cut(){
var dataImg = ctx.getImageData(avatarLeft, avatarTop, cutWidth, cutHeight)
ctx2.putImageData(dataImg,0,0)
g = canvas2.toDataURL("image/png");
}
</script>
</body>
</html>