简体   繁体   English

canvas.toDataURL(“ image / png”)-如何工作以及如何对其进行优化

[英]canvas.toDataURL(“image/png”) - how does it work and how to optimize it

I wanted to know if there was anyone out there that knows how 我想知道是否有人知道如何

canvas.toDataURL("image/png"); 

works? 作品? I want to understand better because at times it seems to really slow my computer down. 我想更好地理解,因为有时它似乎确实使我的计算机运行缓慢。 Is there a way to optimize the base64 image before during or after to get better performance ? 有没有一种方法可以在优化之前或之后优化base64映像以获得更好的性能?

function base64(url) {
    var dataURL;
    var img = new Image(),
        canvas = document.createElement("canvas"),
        ctx = canvas.getContext("2d"),
        src = url; 
    img.crossOrigin = "Anonymous";
    img.onload = function () {
        canvas.height = img.height;
        canvas.width = img.width;
        ctx.drawImage(img, 0, 0);
        var dataURL = canvas.toDataURL('image/png');
        preload(dataURL);
        canvas = null;
    };
    img.src = url;
}

Basically this is my function but I wanted to see if there was a way to make this process perform better or if there was an alternative to canvas.toDataURL('image/png'); 基本上,这是我的功能,但是我想看看是否有一种方法可以使此过程更好地执行,或者是否有替代canvas.toDataURL('image / png');的方法。

thanks 谢谢

toDataURL() does the following when called (synchronously): 当(同步)调用toDataURL() ,将执行以下操作:

  • Creates a file header based on the file type requested or supported (defaults to PNG) 根据请求或支持的文件类型创建文件头(默认为PNG)
  • Compresses the bitmap data based on file format 根据文件格式压缩位图数据
  • Encodes the resulting binary file to Base-64 string 将生成的二进制文件编码为Base-64字符串
  • Prepends the data-uri header and returns the result 准备data-uri标头并返回结果

When setting a data-uri as source (asynchronously): 将data-uri设置为源时(异步):

  • String is verified 字符串已验证
  • Base-64 part is separated and decoded to binary format Base-64部分被分离并解码为二进制格式
  • Binary file verified then parsed and uncompressed 验证二进制文件,然后解析并解压缩
  • Resulting bitmap set to Image element and proper callbacks invoked 结果位图设置为Image元素,并调用了正确的回调

These are time-consuming steps and as they are internal we cannot tap into them for any reason. 这些步骤很耗时,由于它们是内部的,因此我们不能出于任何原因使用它们。 As they are pretty optimized as they are given the context they work in there is little we can do to optimize them. 由于在给它们提供上下文的基础上对其进行了非常优化,因此我们无法对其进行优化。

You can experiment with different compression schemes by using JPEG versus PNG. 您可以通过使用JPEG和PNG来尝试不同的压缩方案。 They are using very different compression techniques and depending on the image size and content one can be better than the other in various situations. 他们使用的压缩技术非常不同,根据图像的大小和内容,一种在各种情况下都可能比另一种更好。

My 2 cents.. 我的2美分

The high performance alternative is canvas.toBlob . 高性能的替代品是canvas.toBlob It is extremely fast, asynchronous, and produces a blob which can also be swapped to disk, and is subjectly speaking simply far more useful. 它非常快速,异步,并且产生了一个Blob,该Blob也可以交换到磁盘上,从主题上讲,它的作用要大得多。
Unfortunately it is implemented in Firefox, but not in chrome . 不幸的是,它是在Firefox中实现的, 而不是在chrome中实现的

Having carefully bench-marked this, there is no way around because canvas.toDataURL itself is the bottleneck by orders of magnitude. 仔细地进行基准测试之后,就没有办法解决了,因为canvas.toDataURL本身就是数量级的瓶颈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 canvas.toDataURL() 将 Canvas 保存为图像? - How To Save Canvas As An Image With canvas.toDataURL()? canvas.toDataURL()不会改变图像质量。怎么会? - canvas.toDataURL() does not alter image quality. How comes? Canvas.toDataUrl(“ image / png”)无法正常工作 - Canvas.toDataUrl(“image/png”) is not working properly JavaScript中“ canvas.toDataURL(” image / png“);”的替代形式? - Alternatives to “canvas.toDataURL(”image/png“);” in JavaScript? 如何裁剪 canvas.toDataURL - How to crop canvas.toDataURL image.src = canvas.toDataURL(“image/png”); 保存问题 - image.src = canvas.toDataURL(“image/png”); Saving issue “canvas.toDataURL(”image / png“)”在firefox中无法正常工作 - “canvas.toDataURL(”image/png“)” not working properly in firefox 如何在src中添加canvas.toDataURL - How add canvas.toDataURL in src 如何在HTML5 canvas,canvas.toDataURL()中保存大小为a * a的图像,并稍后以不同的大小a * a / 2a * 2a绘制它? - How to save an image of size a*a in HTML5 canvas, canvas.toDataURL() and later draw it in different sizes, a*a / 2a*2a? 创建快照时,Chrome 版本 87 不支持 HTML2CANVAS 的 function canvas.toDataURL("image/png") - Chrome version 87 not supporting function canvas.toDataURL("image/png") of HTML2CANVAS when creating snapshot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM