[英]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()
,将执行以下操作:
When setting a data-uri as source (asynchronously): 将data-uri设置为源时(异步):
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.