Skip to content

Instantly share code, notes, and snippets.

@sbussard
Created October 12, 2013 03:58
Show Gist options
  • Save sbussard/6945650 to your computer and use it in GitHub Desktop.
Save sbussard/6945650 to your computer and use it in GitHub Desktop.
Converts all images on a page to canvas of images
// requires jQuery obviously
$(function(){
$('img').each(function(i, img){
var $img = $(img);
var url = $img.attr('src');
var w = $img.width();
var h = $img.height();
var canvas = $('<canvas data-image="'+i+'"></canvas>').attr({ width: w, height: h });
$(img).replaceWith(canvas);
canvas = $('canvas[data-image="'+i+'"]')[0];
var context = canvas.getContext('2d');
var imageObj = new Image();
imageObj.onload = function() {
context.drawImage(imageObj, 0, 0, w, h);
};
imageObj.src = url;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment