-
Notifications
You must be signed in to change notification settings - Fork 1
/
image-edit.js
246 lines (214 loc) · 8.79 KB
/
image-edit.js
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
/*
* jQuery Basic HTML5 Image Plugin 0.0.1
* https://github.com/jimlyndon/jquery.html5image
* Copyright (c) 2011 Jim Lyndon
* Licensed under the MIT license:
* http://creativecommons.org/licenses/MIT/
*
*/
(function ($) {
'use strict';
$.widget('ui.html5image', {
version: "0.0.1",
options: {},
widget: function () {
return this.html5image;
},
_hasCanvas: function() {
var elem = document.createElement( 'canvas' );
return !!(elem.getContext && elem.getContext('2d'));
},
_create: function () {
var options = this.options,
element = this.element,
html5image = (this.html5image = $("<div>")).appendTo("body"),
canvasEl = (this._hasCanvas()) ? $("<canvas></canvas>").addClass("ui-widget ui-imageedit").appendTo(html5image) : $("<img>").appendTo(html5image),
src = element.attr("src"),
img = new Image();
this.html5image.canvas = canvasEl[0];
html5image.appendTo("body");
if (this._hasCanvas()) {
var self = this;
img.onload = function () {
var max = 400, aspectRatio = 1;
if (img.width > max || img.height > max) {
aspectRatio = max / Math.max(img.width, img.height);
}
var canvas = self.html5image.canvas;
canvas.width = element.width() * aspectRatio;
canvas.height = element.height() * aspectRatio;
canvas.context = canvas.getContext("2d");
canvas.context.save();
canvas.context.scale(aspectRatio, aspectRatio);
canvas.context.drawImage(img, 0, 0);
canvas.context.restore();
};
img.src = src;
} else {
var canvas = this.html5image.canvas;
var max = 400, aspectRatio = 1;
if (element.width() > max || element.height() > max) {
aspectRatio = max / Math.max(element.width(), element.height());
}
canvas.width = element.width() * aspectRatio;
canvas.height = element.height() * aspectRatio;
canvas.src = src;
}
// hide original image
element.hide();
// create editor buttons
this._createButtons();
},
_createButtons: function () {
var element = this.element,
editor = $("<table>")
.addClass("ui-widget icon-collection")
.attr({ cellpaddding: "0", cellspacing: "0" })
.insertBefore(this.html5image.canvas),
tbody = $("<tbody>")
.appendTo(editor),
rows=$("<tr>")
.appendTo(tbody),
cols = '',
button_flipVert = "ui-icon-arrowthick-2-n-s",
button_flipHorz = "ui-icon-arrowthick-2-e-w",
button_rotate_left = "ui-icon-arrowthick-1-w",
button_rotate_right = "ui-icon-arrowthick-1-e",
buttons = [button_flipVert, button_flipHorz, button_rotate_left, button_rotate_right];
$.each(buttons, function(idx, value) {
cols += '<td><div class="ui-state-default ui-corner-all" title="' + value + '"><span class="ui-icon ' + value + '"></span></div></td>';
});
rows.append(cols);
var self = this;
$('.' + button_flipVert)
.click(function () {
self._verticalFlip();
}).hover(
function () {
$(this).addClass("ui-state-hover");
},
function () {
$(this).removeClass("ui-state-hover");
}
);
$('.' + button_flipHorz)
.click(function () {
self._horizontalFlip();
}).hover(
function () {
$(this).addClass("ui-state-hover");
},
function () {
$(this).removeClass("ui-state-hover");
}
);
$('.' + button_rotate_left)
.click(function () {
self._rotate();
}).hover(
function () {
$(this).addClass("ui-state-hover");
},
function () {
$(this).removeClass("ui-state-hover");
}
);
$('.' + button_rotate_right)
.click(function () {
self._rotate(true);
}).hover(
function () {
$(this).addClass("ui-state-hover");
},
function () {
$(this).removeClass("ui-state-hover");
}
);
},
_verticalFlip: function () {
if (this._hasCanvas()) {
var canvas = this.html5image.canvas;
canvas.context.save();
canvas.context.scale(1, -1);
canvas.context.translate(0, -canvas.height);
canvas.context.drawImage(canvas, 0, 0);
canvas.context.restore();
} else {
var canvas = this.html5image.canvas;
if(isNaN(canvas.flipV)) {
canvas.flipV = 1;
canvas.rotation = 2;
} else {
if(canvas.flipV == 1) {
canvas.flipV += -1;
canvas.rotation += -2;
} else {
canvas.flipV += 1;
canvas.rotation += 2;
}
}
canvas.style.filter = "progid:DXImageTransform.Microsoft.BasicImage(rotation="+ canvas.rotation +", mirror="+ canvas.flipV +")"; //"progid:DXImageTransform.Microsoft.Matrix(M11="+canvas.costheta+",M12="+(-canvas.sintheta)+",M21="+canvas.sintheta+",M22="+canvas.costheta+",SizingMethod='auto expand')";
}
},
_horizontalFlip: function () {
if (this._hasCanvas()) {
var canvas = this.html5image.canvas;
canvas.context.save();
canvas.context.scale(-1, 1);
canvas.context.translate(-canvas.width, 0);
canvas.context.drawImage(canvas, 0, 0);
canvas.context.restore();
}
else {
var canvas = this.html5image.canvas;
if(isNaN(canvas.angle))
canvas.angle = 0;
canvas.angle = canvas.angle += 270;
var rotation = Math.PI * canvas.angle / 180;
var costheta = Math.cos(rotation);
var sintheta = Math.sin(rotation);
canvas.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11="+costheta+",M12="+(-sintheta)+",M21="+sintheta+",M22="+costheta+",SizingMethod='auto expand')";
}
},
_rotate: function (clockwise) {
if (this._hasCanvas()) {
var canvas = this.html5image.canvas,
$tmpCanvas = $("<canvas></canvas>"),
tmpCanvas = $tmpCanvas[0];
tmpCanvas.width = canvas.width;
tmpCanvas.height = canvas.height;
tmpCanvas.style.display = "none";
tmpCanvas.getContext("2d").drawImage(canvas, 0, 0);
$tmpCanvas.appendTo("body");
canvas.width = tmpCanvas.height;
canvas.height = tmpCanvas.width;
canvas.context.save();
var radius = Math.PI / 2;
if (clockwise)
canvas.context.translate(canvas.width, 0);
else {
canvas.context.translate(0, canvas.height);
radius *= -1;
}
canvas.context.rotate(radius);
canvas.context.drawImage(tmpCanvas, 0, 0);
canvas.context.restore();
$tmpCanvas.remove();
}
else {
var canvas = this.html5image.canvas;
if(isNaN(canvas.angle))
canvas.angle = 0;
canvas.angle = (clockwise) ? canvas.angle += 90 : canvas.angle -= 90;
var rotation = Math.PI * canvas.angle / 180;
var costheta = Math.cos(rotation);
var sintheta = Math.sin(rotation);
canvas.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11="+costheta+",M12="+(-sintheta)+",M21="+sintheta+",M22="+costheta+",SizingMethod='auto expand')";
}
},
destroy: function () {
this.html5image.remove();
this.element.show();
}
});
} (jQuery));