This is a small library that lets you easily save a HTML5 canvas element as an imagefile.
Files needed: canvas2image.js, base64.js
Draw on the canvas with the pretty boxes below using the mouse, then click the "Convert" buttons to convert it to a downloadable image - or the "Save" buttons to download the image file directly.
Files needed: canvas2image.js, base64.js
Draw on the canvas with the pretty boxes below using the mouse, then click the "Convert" buttons to convert it to a downloadable image - or the "Save" buttons to download the image file directly.
Using the HTML5 canvas element, you can create all sorts of cool graphics client-side on the fly using Javascript. However, the canvas image cannot (in all browsers) simply be saved to disk as any other image.
Luckily there is a neat function on the the canvas object called toDataURL(). This functions encodes the image data as a base64 encoded PNG file and returns it as a data: URI.
Other formats than PNG are supported by some browsers by adding an argument to the toDataURL() call containing the mime type of the format you want.
It looks like Opera and Safari only supports PNG, while Firefox supports at least PNG and JPEG.
Now, Using that data string, we can stick it in an <img> element or we can serve it directly to the browser and force it to download the image by replacing the mimetype.
That is basically what this little library does. The following functions are provided:
So, you may have noticed the saveAsBMP function. Since BMP is not supported by any of the browsers (and I felt like wasting an afternoon), BMP support has been added by utilizing the getImageData() method, which enabled us to read raw pixel data from the canvas.
Using this data, we can set up a BMP file structure (which is really simple compared to most other image formats), base64 encode everything and create our own data: URI.
The getImageData() method is only available in Firefox, the latest Opera and Safari using the latest WebKit nightly. For larger images, it can take several seconds to encode the image to BMP, but for smaller canvases, it seems fine and fast.
Luckily there is a neat function on the the canvas object called toDataURL(). This functions encodes the image data as a base64 encoded PNG file and returns it as a data: URI.
- var strDataURI = oCanvas.toDataURL();
- // returns "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACt..."
- var strDataURI = oCanvas.toDataURL("image/jpeg");
- // returns "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA..."
Now, Using that data string, we can stick it in an <img> element or we can serve it directly to the browser and force it to download the image by replacing the mimetype.
That is basically what this little library does. The following functions are provided:
- /*
- * Canvas2Image.saveAsXXXX = function(oCanvasElement, bReturnImgElement, iWidth, iHeight) { ... }
- */
- var oCanvas = document.getElementById("thecanvas");
- Canvas2Image.saveAsPNG(oCanvas); // will prompt the user to save the image as PNG.
- Canvas2Image.saveAsJPEG(oCanvas); // will prompt the user to save the image as JPEG.
- // Only supported by Firefox.
- Canvas2Image.saveAsBMP(oCanvas); // will prompt the user to save the image as BMP.
- // returns an <img> element containing the converted PNG image
- var oImgPNG = Canvas2Image.saveAsPNG(oCanvas, true);
- // returns an <img> element containing the converted JPEG image (Only supported by Firefox)
- var oImgJPEG = Canvas2Image.saveAsJPEG(oCanvas, true);
- // returns an <img> element containing the converted BMP image
- var oImgBMP = Canvas2Image.saveAsBMP(oCanvas, true);
- // all the functions also takes width and height arguments.
- // These can be used to scale the resulting image:
- // saves a PNG image scaled to 100x100
- Canvas2Image.saveAsPNG(oCanvas, false, 100, 100);
Using this data, we can set up a BMP file structure (which is really simple compared to most other image formats), base64 encode everything and create our own data: URI.
The getImageData() method is only available in Firefox, the latest Opera and Safari using the latest WebKit nightly. For larger images, it can take several seconds to encode the image to BMP, but for smaller canvases, it seems fine and fast.
Issues and compatibility
- Only works in canvas-enabled browsers (that means no IE).
- With Safari, you might need use the convert option and right click the image and select "save as" in order to save it.
- It would be really neat if somehow a filename could be attached to the data, but I've found no way to do that. For now, you have to specify the filename yourself.
- Since btoa() is only supported in Firefox, I've implemented base64 encoding using these functions made by Masanao Izumo
Ukrainian translation courtesy of Mario Pozner.
http://www.nihilogic.dk/labs/canvas2image/
相关推荐
Canvas2Image.convertToImage(canvasObj, width, height, type) Canvas2Image.convertToPNG(canvasObj, widht, height) Canvas2Image.convertToJPEG(canvasObj, widht, height) Canvas2Image.convertToGIF...
Web开发canvas2image.js将canvas保存为图片,下载图片。
画布2图像 将画布保存或转换为图像的工具 演示版 代码 你可以像这样使用它 Canvas2Image....Canvas2Image.convertToImage(canvasObj, width, height, type) Canvas2Image.convertToPNG(canvasObj, width, heig
var img = Canvas2Image.convertToImage(canvas); // 或者 var imgDataUrl = canvas.toDataURL('image/png'); document.getElementById('myImg').src = imgDataUrl; ``` 在调整图片质量和格式方面,`html2canvas`...
然后,`Canvas2Image.saveAsImage`函数接收canvas元素和data URL作为参数,将canvas内容保存为图片,并提供下载选项。 值得注意的是,`canvas.toDataURL`方法返回的数据URL是基于base64编码的,包含了图像的MIME...
var img = Canvas2Image.convertToImage(canvas); // 保存为PNG Canvas2Image.saveAsPNG(img); ``` **结合使用html2canvas和canvas2image** 为了将整个网页保存为图片,我们可以先使用html2canvas将页面渲染到...
canvas2image.js 将canvas转化为image的js插件,可以将画布内容下载实现截图功能。
6. **保存图片**:"canvas2image"可能包含一个用于将Canvas内容转换为图片的脚本,这通常通过`toDataURL()`方法实现,该方法能将Canvas内容转换为一个data URL,然后可以用此URL创建一个新的`<img>`元素或下载图片。...
从提供的"canvas2image.js"文件名来看,这应该是一个JavaScript库,用于增强Canvas到data:image的转换功能。 通常,这样的扩展库可能会包含以下功能: 1. **设置图片质量**:默认情况下,`toDataURL()`方法生成的...
canvas2image.js, 将canvas画布转化为图片的js插件,canvas2image.js
低版本HTML2canvas 前端开发 生成海报 截图等功能 1.该脚本允许您直接在用户浏览器上对网页或其部分进行“截图”。屏幕截图基于 DOM,因此可能不是 100% 准确到真实表示,因为它不会制作实际的屏幕截图,而是根据...
第2章深入研究了如何使用Canvas的API进行绘制;第3章告诉读者如何绘制并操作Canvas中的文本;第4章专门讲解图像、图像的操作及视频处理;第5章介绍如何实现平滑的动画效果;第6章讲解如何用JavaScript语言来实现精灵...
- **创建`ol.source.ImageCanvas`**:首先,你需要创建一个`ol.source.ImageCanvas`实例,传入一个包含`createCanvasFunction`的配置对象。这个函数会在需要时被调用,用于生成canvas元素并渲染矢量数据。 - **...
本项目"基于html2canvas生成带二维码的活动海报"提供了一个纯前端解决方案,使得开发者无需后端支持也能快速实现此类功能。html2canvas是一个JavaScript库,它能够将网页的DOM(文档对象模型)渲染为图片,从而解决...