-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathangular-thumbnail.js
183 lines (138 loc) · 4.6 KB
/
angular-thumbnail.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
angular.module('ui.thumbnail', [])
.provider('ThumbnailService', function() {
this.defaults = {
width: 100,
height: 100
};
this.$get = ['$q', function($q) {
var defaults = this.defaults;
return {
generate: function generate(src, opts) {
var deferred = $q.defer();
opts = opts || {};
if (!opts.noDistortion) {
opts.noDistortion = false;
}
this.load(src, opts).loaded.then(
function success(canvas) {
if (opts.returnType === 'blob') {
if (typeof canvas.toBlob !== 'function') {
return deferred.reject('Your browser doesn\'t support canvas.toBlob yet. Please polyfill it.');
}
try {
canvas.toBlob(function(blob) {
// one may use blob-util to get a URL
deferred.resolve(blob);
}, opts.type, opts.encoderOptions);
} catch (ex) {
deferred.reject(ex);
}
} else {
if (typeof canvas.toDataURL !== 'function') {
return deferred.reject('Your browser doesn\'t support canvas.toDataURL yet. Please polyfill it.');
}
try {
var base64 = canvas.toDataURL(opts.type, opts.encoderOptions);
deferred.resolve(base64);
} catch (ex) {
deferred.reject(ex);
}
}
}
);
return deferred.promise;
},
load: function load(src, opts) {
var canvas = this.createCanvas(opts);
return {
// creation is done
created: $q.when(canvas),
// wait for it
loaded: this.draw(canvas, src, opts.noDistortion)
};
},
draw: function draw(canvas, src, noDistortion) {
var deferred = $q.defer();
var self = this;
var ctx = canvas.getContext('2d');
// it seems that we cannot reuse image instance for drawing
var img = new Image();
img.onload = function onload() {
var resized = canvas;
if (noDistortion ){
resized = self.resizeImage(img, canvas);
canvas = self.updateCanvas(canvas, resized);
ctx = canvas.getContext('2d');
}
// designated canvas dimensions should have been set
ctx.drawImage(img, 0, 0, resized.width, resized.height);
// loading is done
deferred.resolve(canvas);
};
img.src = src;
return deferred.promise;
},
resizeImage: function resizeImage(img, canvas) {
var imageRatio = img.width / img.height;
var newHeight = canvas.width / imageRatio;
var newWidth = canvas.height * imageRatio;
var heightDiff = newHeight - canvas.height;
var widthDiff = newWidth - canvas.width;
if (widthDiff >= heightDiff) {
return dimensions(canvas.width, canvas.width / imageRatio);
} else {
return dimensions(canvas.height * imageRatio, canvas.height);
}
function dimensions(width, height){
return {
width: width,
height: height
};
}
},
createCanvas: function createCanvas(opts) {
var canvas = angular.element('<canvas></canvas>')[0];
return this.updateCanvas(canvas, opts);
},
updateCanvas: function updateCanvas(canvas, opts) {
opts = opts || {};
var w = Number(opts.width) || defaults.width;
var h = Number(opts.height) || defaults.height;
canvas.width = w;
canvas.height = h;
return canvas;
}
};
}]; // $get
})
.directive('uiThumbnail', ['ThumbnailService', function(ThumbnailService) {
return {
restrict: 'E',
scope: {
src: '=',
opts: '='
},
link: function link(scope, el, attrs) {
var promises = ThumbnailService.load(scope.src, scope.opts);
promises.created.then(
function created(canvas) {
// can be appended at this point
el.append(canvas);
}
);
promises.loaded.then(
function loaded(canvas) {
// create watches
scope.$watch('src', function(newSrc) {
ThumbnailService.draw(canvas, newSrc);
});
scope.$watchCollection('opts', function(newOpts) {
ThumbnailService.updateCanvas(canvas, newOpts);
// need to redraw
ThumbnailService.draw(canvas, scope.src);
});
}
);
}
};
}]);