forked from kennyki/angular-thumbnail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-thumbnail.min.js
1 lines (1 loc) · 1.49 KB
/
angular-thumbnail.min.js
1
angular.module("ui.thumbnail",[]).provider("ThumbnailService",function(){this.defaults={width:100,height:100},this.$get=["$q",function(t){var e=this.defaults;return{generate:function(e,n){var r=t.defer();return n=n||{},this.load(e,n).loaded.then(function(t){if("blob"===n.returnType){if("function"!=typeof t.toBlob)return r.reject("Your browser doesn't support canvas.toBlob yet. Please polyfill it.");try{t.toBlob(function(t){r.resolve(t)},n.type,n.encoderOptions)}catch(e){r.reject(e)}}else{if("function"!=typeof t.toDataURL)return r.reject("Your browser doesn't support canvas.toDataURL yet. Please polyfill it.");try{var a=t.toDataURL(n.type,n.encoderOptions);r.resolve(a)}catch(e){r.reject(e)}}}),r.promise},load:function(e,n){var r=this.createCanvas(n);return{created:t.when(r),loaded:this.draw(r,e)}},draw:function(e,n){var r=t.defer(),a=e.getContext("2d"),o=new Image;return o.onload=function(){a.drawImage(o,0,0,e.width,e.height),r.resolve(e)},o.src=n,r.promise},createCanvas:function(t){var e=angular.element("<canvas></canvas>")[0];return this.updateCanvas(e,t)},updateCanvas:function(t,n){n=n||{};var r=Number(n.width)||e.width,a=Number(n.height)||e.height;return t.width=r,t.height=a,t}}}]}).directive("uiThumbnail",function(t){return{restrict:"E",scope:{src:"=",opts:"="},link:function(e,n){var r=t.load(e.src,e.opts);r.created.then(function(t){n.append(t)}),r.loaded.then(function(n){e.$watch("src",function(e){t.draw(n,e)}),e.$watchCollection("opts",function(r){t.updateCanvas(n,r),t.draw(n,e.src)})})}}});