-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathresize.js
72 lines (62 loc) · 1.9 KB
/
resize.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
var Liip = Liip || {};
Liip.resizer = (function ($) {
var mainCanvas;
var init = function () {
$("#resize").click(startResize);
};
/*
* Creates a new image object from the src
* Uses the deferred pattern
*/
var createImage = function (src) {
var deferred = $.Deferred();
var img = new Image();
img.onload = function() {
deferred.resolve(img);
};
img.src = src;
return deferred.promise();
};
/*
* Create an Image, when loaded pass it on to the resizer
*/
var startResize = function () {
$.when(
createImage($("#inputImage").attr('src'))
).then(resize, function () {console.log('error')});
};
/*
* Draw the image object on a new canvas and half the size of the canvas
* until the darget size has been reached
* Afterwards put the base64 data into the target image
*/
var resize = function (image) {
mainCanvas = document.createElement("canvas");
mainCanvas.width = 1024;
mainCanvas.height = 768;
var ctx = mainCanvas.getContext("2d");
ctx.drawImage(image, 0, 0, mainCanvas.width, mainCanvas.height);
size = parseInt($('#size').get(0).value, 10);
while (mainCanvas.width > size) {
mainCanvas = halfSize(mainCanvas);
}
$('#outputImage').attr('src', mainCanvas.toDataURL("image/jpeg"));
};
/*
* Draw initial canvas on new canvas and half it's size
*/
var halfSize = function (i) {
var canvas = document.createElement("canvas");
canvas.width = i.width / 2;
canvas.height = i.height / 2;
var ctx = canvas.getContext("2d");
ctx.drawImage(i, 0, 0, canvas.width, canvas.height);
return canvas;
};
return {
init: init
};
})(jQuery);
jQuery(function($) {
Liip.resizer.init();
});