HTML Canvas 图像转 Base64 问题
我在检索画布上显示的图像的 Base64 版本时遇到问题。我查看了其他问题,但包括 canvas2image 在内的解决方案似乎都不起作用。
这是我的代码:
<!DOCTYPE>
<html>
<head>
<style>#canvas {cursor: pointer;}</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script type="text/javascript">
var can = document.getElementById('canvas');
var ctx = can.getContext('2d');
var img = new Image();
img.onload = function(){
can.width = img.width;
can.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
}
img.src = 'http://sstatic.net/stackoverflow/img/newsletter-ad.png';
can.onclick = function() {
ctx.translate(img.width-1, img.height-1);
ctx.rotate(Math.PI);
ctx.drawImage(img, 0, 0, img.width, img.height);
};
document.write(can.toDataURL()); //isn't writing the correct base64 image
</script>
</body>
</html>
这是测试链接: http://jsfiddle.net/mrchief/hgTdM/1/< /a> 谢谢,Mrchief
我需要的是画布中图像的正确的 base64 输出。
问题是它不会输出正确的 base64 版本的图像。但是正如您所看到的,画布内的图像显示没有任何问题。
我已经在 chrome 和 chrome 上测试过它火狐浏览器
非常感谢..
I'm having a problem with retrieving the base64 version of an image displayed on a canvas. I've looked at the other questions but none of the solutions including canvas2image seem to work.
Here's my code:
<!DOCTYPE>
<html>
<head>
<style>#canvas {cursor: pointer;}</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script type="text/javascript">
var can = document.getElementById('canvas');
var ctx = can.getContext('2d');
var img = new Image();
img.onload = function(){
can.width = img.width;
can.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
}
img.src = 'http://sstatic.net/stackoverflow/img/newsletter-ad.png';
can.onclick = function() {
ctx.translate(img.width-1, img.height-1);
ctx.rotate(Math.PI);
ctx.drawImage(img, 0, 0, img.width, img.height);
};
document.write(can.toDataURL()); //isn't writing the correct base64 image
</script>
</body>
</html>
Here's the test link: http://jsfiddle.net/mrchief/hgTdM/1/ thanks, Mrchief
What I need is the correct base64 output of the image in the canvas.
The problem is that it won't output the correct base64 version of the image.. But as you can see, the image inside the canvas is displayed without any problems.
I've tested it on chrome & firefox
Thanks very much..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
document.write()
调用会在页面加载后立即发生,在您的img
加载和绘制之前以及您的onclick
处理程序运行之前。您需要等到画布上的所有事情完成后才能生成数据 URI。忽略
onclick
,下面是在图像加载并以旋转方式绘制到画布后写出数据 url 的内容:您可以在此处查看此演示的实际效果:http://phrogz.net/tmp/upside-down-image-url.html
请注意您加载的图像必须位于与您的脚本相同的域,否则您将无法创建数据 URL。
Your
document.write()
call occurs immediately upon page load, before yourimg
is loaded and drawn and before youronclick
handler can ever run. You need to wait to generate the data URI until after everything has finished happening to the canvas.Ignoring the
onclick
, here's something that writes out the data url after the image has loaded and been drawn to the canvas in a rotated fashion:You can see this demo in action here: http://phrogz.net/tmp/upside-down-image-url.html
Note that the image you load must be on the same domain as your script, or else you will not be allowed to create the data URL.