Canvas Get Canvas Image 获取画布上的图像 - caleb531/jcanvas-docs GitHub Wiki

Get Canvas Image 获取画布上图像

The getCanvasImage() method retrieves the canvas's image data as a string. The string is a base64-encoded image URL, which can be used as a valid image URL for any purpose.
getCanvasImage()方法将画布上的图像作为字符串来访问。这个字符串是图像URL的base64编码,此种编码可以作为用于任何意图的可用图像URL。

$('canvas').getCanvasImage();

The default image type is PNG, although you can specify PNG, or JPEG.
默认的图片类型是PNG,然而你也可以指定PNG或JPEG。

$('canvas').getCanvasImage('png');
$('canvas').getCanvasImage('jpeg');

Setting Quality 设置图像质量

If you choose to retrieve the canvas image as a JPEG, you may also set the JPEG's quality (which ranges from 0 to 1).
如果你选择了将图像作为JPEG形式访问,你需要设置JPEG的质量(值为0-1)。

$('canvas').getCanvasImage('jpeg', 0.5);

Notes 注意

This method does not work on Google Chrome if the page is served from a file URL (file://). This is a limitation of Google Chrome's sandboxing architecture, and therefore cannot be fixed. However, this method still works when the page is served from a server (either online or running locally via localhost://).
在谷歌浏览器上,如果页面是以文件URL形式(file://)访问的话,本方法无效。这是谷歌浏览器沙盒结构的限制,因此无法被修复。但当页面使用服务器访问时仍然有效(无论是在线上访问,还是本地通过localhost://访问)