var imgBicubicInterpolation = new Class({ initialize: function(elements){ var _this=this; $$(elements).each(function(el) { if(el.get('tag') === "img" && !el.retrieve('bicubized')) { var src = el.src; if(src.split('.').pop()!='svg'){ var imgW = el.width; var imgH = el.height; var can=new Element('canvas',{width:imgW,height:imgH}).setStyle('display','none').inject(el,'after'); var callback = _this.drawHighResolutionImgThumbnail; _this.drawCanvas(can, imgW*8, imgH*8, src, callback, el, true); } } }); }, drawCanvas: function (can, imgW, imgH, src, callback, imgEl, crossOrigin) { var ctx = can.getContext('2d'); var img = new Image(); if(crossOrigin) { img.setAttribute('crossOrigin', 'anonymous'); //tainted canvases may not be exported chrome, ie will also throw security error } var w = imgW; var h = imgH; img.onload = function() { // Step it down several times var can2 = document.createElement('canvas'); can2.width = w; can2.height =h; var ctx2 = can2.getContext('2d'); // Draw it at 1/2 size 3 times (step down three times) ctx2.drawImage(img, 0, 0, w/2, h/2); ctx2.drawImage(can2, 0, 0, w/2, h/2, 0, 0, w/4, h/4); ctx2.drawImage(can2, 0, 0, w/4, h/4, 0, 0, w/8, h/8); //ctx.drawImage(img, 0, 0, w/6, h/6); ctx.drawImage(can2, 0, 0, w/8, h/8, 0, 0, w/8, h/8); if(callback) { callback(can, this.src, imgEl); can.destroy(); } }; img.src = src; }, drawHighResolutionImgThumbnail: function (can, attrSrc, imgEl) { $(imgEl).setProperty('src', can.toDataURL("image/png")); $(imgEl).setProperty('data-src', attrSrc); $(imgEl).store('bicubized',true); } });