Created
March 18, 2014 02:32
-
-
Save jtyjty99999/9612547 to your computer and use it in GitHub Desktop.
保存一个外部域的图片
This file contains 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
var img = new Image, | |
canvas = document.createElement("canvas"), | |
ctx = canvas.getContext("2d"), | |
src = "http://example.com/image"; // 外部域中的图片url | |
img.crossOrigin = "Anonymous"; | |
img.onload = function() { | |
canvas.width = img.width; | |
canvas.height = img.height; | |
ctx.drawImage( img, 0, 0 ); | |
localStorage.setItem( "savedImageData", canvas.toDataURL("image/png") ); | |
} | |
img.src = src; | |
// 在src图片已被缓存的情况下,确保load事件发生. | |
if ( img.complete || img.complete === undefined ) { | |
img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="; | |
img.src = src; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment