Base64编码的PNG数据,HTML5画布 [英] Base64 png data to html5 canvas
问题描述
日安程序员,
我要为Base64加载png图片到画布元素。我有这个code:
I want to load a png image 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>
在Chrome浏览器8我有这样的:未捕获类型错误:类错误
In Chrome 8 I have got this: Uncaught TypeError: Type error
而在Firefox的萤火虫这样的:类的一个对象是关联的对象code:参数的期望类型不兼容17
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
在这Base64是5x5px黑色方形PNG,我在GIMP制作并把它在GNU / Linux上的程序的base64为base64。
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.
非常感谢你的答案。
推荐答案
通过它您需要的长相实际传递的drawImage图像对象,像这样
By the looks of it you need to actually pass drawImage an image object like so
var canvas = document.getElementById("c");
var ctx = canvas.getContext("2d");
var image = new Image();
image.onload = function() {
ctx.drawImage(image, 0, 0);
};
image.src = "data:image/ png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oMCRUiMrIBQVkAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAADElEQVQI12NgoC4AAABQAAEiE+h1AAAAAElFTkSuQmCC";
我试过它在Chrome和它工作正常。
I've tried it in chrome and it works fine.
这篇关于Base64编码的PNG数据,HTML5画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!