简体   繁体   English

Javascript-canvas drawImage

[英]Javascript - canvas drawImage

Is it better for performance to: 性能是否更好:

  1. drawImage(img, ...) where img is an Image() or img is a canvas? drawImage(img,...)其中img是Image()或img是画布?

  2. drawImage 2x with scaling (nearest neighbor), or clear an additional canvas, draw 2x to it without scaling, then draw the result 1x with scaling? drawImage 2x具有缩放比例(最近邻居),或清除其他画布,在不缩放的情况下向其绘制2x,然后以缩放比例绘制结果1x?

  3. Round the px coordinates, width, height of the images being drawn, or leave them as floating points? 舍入要绘制的图像的px坐标,宽度,高度,还是将其保留为浮点数?

Why not do some jsPerfs and report your results to us here! 为什么不做一些jsPerfs并在这里向我们报告您的结果!

Until then, here's some highly speculative and anecdotal thoughts: 在此之前,这里有一些高度投机和轶事的想法:

(#1) I used to recommend drawImage(image... but I saw a jsPerf comparing drawImage(image with drawImage(canvas that were very close when using GPU acceleration (can't remember where, but it was a response comment from someone in one of my SO answers). Probably because the GPU can blit the canvas context.getImageData about as fast a it can blit an Images data. (#1)我曾经建议使用drawImage(image ...),但是我看到一个jsPerf,将drawImage(image和drawImage(canvas)在使用GPU加速时非常接近(不记得在哪里,但这是某人的回应评论)在我的一个SO答案中)。可能是因为GPU可以对画布context.getImageData进行刷新的速度尽可能快,它可以对Images数据进行刷新。

(#2) I have no idea...jsPerf with incremental scaling vs imageSmoothingEnabled==false (nearest-neighbor). (#2)我不知道...具有增量缩放功能的jsPerf vs imageSmoothingEnabled == false(最近邻居)。

(#3) The browser casts to float anyway, but a Mozilla performance article suggests "Avoid floating-point coordinates and use integers instead.". (#3)浏览器仍然强制转换为浮点数,但是Mozilla的性能文章建议“避免浮点坐标,而改用整数”。 Of course, pre-rounding takes some cpu cyles, so a jsPerf using actual design specific code is needed to determine which gives more performance in a specific situation. 当然,四舍五入需要一些cpu循环,因此需要使用实际设计特定代码的jsPerf来确定在特定情况下哪种代码性能更高。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM