Skip to content

Instantly share code, notes, and snippets.

@yangxing-star
Created August 5, 2014 05:20
Show Gist options
  • Save yangxing-star/06346860a66b42691661 to your computer and use it in GitHub Desktop.
Save yangxing-star/06346860a66b42691661 to your computer and use it in GitHub Desktop.
图片转成base64
var img = new Image();
img.crossOrigin = "*";
img.src = "http://s3.steedos.com/users/5194c74a8e296a6cfe000019/admin/angel_galaxy-wallpaper-1440x900.jpg";
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.width;
var ctx = canvas.getContext("2d");
ctx.fillRect(50,50,50,50);
img.onload = function() {
ctx.drawImage(img, 0, 0);
var g = ctx.canvas.toDataURL("image/jpeg", 1);
document.getElementById('test').value = g;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment