Using drawImage with Canvas in Chrome is very slow

I am trying to draw a large number of instances of an SVG file onto the canvas using drawImage. By creating one image element using SVG as the source, and then using drawImage for each instance on the canvas, I hoped that I could create a composite image on the canvas very quickly, even with a large number of copies.

In terms of performance, this works well in Firefox - I can attract 60,000 instances in about 300 ms. But on Chrome, it's terribly slow: 16,000 copies take more than 5 seconds. I put the version of the code on jsfiddle , which demonstrates the problem in Chrome.

I have an example of what I call drawImage below, where the canvas is filled with as many x-sized images as possible. I read a suggestion to try a second hidden canvas to buffer all instances, and then update the visible canvas with one call. But this did not affect performance, individual drawImage calls still seem tiring.

Any thoughts on what is going wrong and what can I do to fix it?

svgImg = new Image; can.width = 1800; can.height = 900; svgImg.onload = function () { if (internalSize == size) return; internalSize = size; var timeBefore = new Date().getTime(); var tot = 0; var canWidth = can.width; var canHeight = can.height; for (var x = 0; x < canWidth; x += size) { for (var y = 0; y < canHeight; y += size) { ctx.drawImage(svgImg, x, y, size, size); tot++; } } document.getElementById('count').innerHTML = "Total Count: " + tot; var timeAfter = new Date().getTime(); }; svgImg.src = "http://www.w3.org/Icons/SVG/svg-logo.svg"; svgImg.width = size; svgImg.height = size; 
+7
source share
1 answer

Slowdown 1: Occurs when either the source or target canvas is in RAM and the other canvas is on the GPU.

Slowdown 2: Occurs when src and dest canvas are of different sizes.


Corresponding error: http://code.google.com/p/chromium/issues/detail?id=170021

I noticed the same problem and simplified the case of drawing one empty canvas on another. This does not seem to be a problem when the canvases are the same size, but at some point the performance takes up the nose. Here is jspref and my results:

jspref chrome results

Pay attention to the difference in 255x255 to 100x100 and 260x260 to 100x100.

+5
source

All Articles