从一块空白的画布开始:
<canvas id='myCanvas' width='800' height='600'></canvas>
接下来要初始化画布:
function init_canvas(){
var canvas = document.getElementById('myCanvas');
context = canvas.getContext('2d');
context.lineCap = 'round';
// fill it with white background
context.save();
context.fillStyle = '#fff';
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
context.restore();
return;
}
接下来在画布上进行一系列的绘制。
然后尝试使用后端的ajax和PHP将其保存到服务器上。
在客户端:
var img = canvas.toDataURL('image/png');
// strip the leading garbage.
img.substr(img.indexOf(',')+1).toString();
将生成的字符串直接带入PHP并使用base64_decode()解密该字符串即可... 图像总是正确的大小,但它没有任何绘图 - 只有一个透明的图像。这似乎不是PHP中base64_decode()的问题,它似乎是安全问题或其他什么原因导致的。这在Firefox 4和最新的Chrome浏览器都会失败。
使用"image/png"头将结果png图像转储到Firefox时,在错误控制台中显示如下内容:
Error: Image corrupt or truncated: http://somewhere.com/showimage.php
Source file: http://somewhere.com/showimage.php
但是...就我所知,该图像并未损坏或截断,除非toDataURL()在所有地方都无法正常工作,因为php部分只是对toDataURL()的结果进行base64_decode()。
有什么想法吗?
谢谢!