Base64 PNG 数据到 HTML5 画布
我想将 Base64 编码的 PNG 图像加载到 canvas 元素。我有这样的代码:
<html>
<head>
</head>
<body>
<canvas id="c"></canvas>
<script type="text/javascript">
var canvas = document.getElementById("c");
var ctx = canvas.getContext("2d");
data = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oMCRUiMrIBQVkAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAADElEQVQI12NgoC4AAABQAAEiE+h1AAAAAElFTkSuQmCC";
ctx.drawImage(data, 0, 0);
</script>
</body>
</html>
在 Chrome 8 中,我收到错误:Uncaught TypeError: Type error
在 Firefox 的 Firebug 中:“对象的类型与与该对象关联的参数的预期类型不兼容" code: "17"
其中,base64 是我在 GIMP 中制作的 5x5px 黑色 PNG 正方形,并在 GNU/Linux 的程序 base64 中将其转换为 base64。
I want to load a PNG image encoded in Base64 to canvas element. I have this code:
<html>
<head>
</head>
<body>
<canvas id="c"></canvas>
<script type="text/javascript">
var canvas = document.getElementById("c");
var ctx = canvas.getContext("2d");
data = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oMCRUiMrIBQVkAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAADElEQVQI12NgoC4AAABQAAEiE+h1AAAAAElFTkSuQmCC";
ctx.drawImage(data, 0, 0);
</script>
</body>
</html>
In Chrome 8 I get the error: Uncaught TypeError: Type error
And in Firefox's Firebug this: "The type of an object is incompatible with the expected type of the parameter associated to the object" code: "17"
In that base64 is 5x5px black PNG square that I have made in GIMP and turn it to base64 in GNU/Linux's program base64.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从它的外观来看,您实际上需要向 drawImage 传递一个图像对象,如下所示
我已经在 chrome 中尝试过了,效果很好。
By the looks of it you need to actually pass drawImage an image object like so
I've tried it in chrome and it works fine.
杰里夫的回答很好,除了一个缺陷。
(就像 Totty.js 指出的那样。)
......
Jerryf's answer is fine, except for one flaw.
(Like Totty.js pointed out.)
....
更现代的版本。所提供的解决方案的问题之一是您的内容安全策略必须允许在图像 src 中设置 data: 协议。
避免使用 image.src。使用 Blob 和 createImageBitmap 代替,例如
More modern version. One of the problems with the provided solutions is that your Content-Security-Policy must allow setting data: protocol in the image src.
To avoid using image.src. Use the Blob and createImageBitmap instead e.g.