Created
October 12, 2013 03:58
-
-
Save sbussard/6945650 to your computer and use it in GitHub Desktop.
Converts all images on a page to canvas of images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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