Closed jwmerrill closed 9 years ago
That is a great find! I can imagine that some saves were placed accidentally, or lived through a refactor that they should not have. Are you interested in taking a swing at putting them in the right places? Based on what I recall about the architecture of the rendering code, there should be distinct transitions between levels:
group
group
I imagine all saves should be on the stepping in and all restores should be on the stepping out. Overall, this sounds like a very plausible improvement!
This might be a little premature. I'm doing some experiments with different ways of clearing the canvas, and they don't totally bear out what I asserted above. I'm going to close this until I collect a little more evidence.
Here's my inconclusive test: http://jsbin.com/vicawitefi/2/edit?js,output
In this version, you switch between strategies by commenting/uncommenting. I'm not seeing a huge difference between them.
This issue is part of trying to make animations run more smoothly in Firefox.
Currently, on each draw of a collage, the following is executed:
https://github.com/elm-lang/core/blob/9687580e6f7b012cdee712c2f14471b272c85488/src/Native/Graphics/Collage.js#L324
Resetting the canvas node's height and width serves two purposes:
Unfortunately, it appears that resetting the canvas this way also produces a canvas-sized amount of garbage on every frame in Firefox.
Clearing the canvas can also be accomplished by calling
ctx.clearRect(0, 0, w, h)
. However, this does not reset the transforms. Transforms can be reset through disciplined use ofctx.save()
andctx.restore()
, but there are currently saves that are never restored:https://github.com/elm-lang/core/blob/9687580e6f7b012cdee712c2f14471b272c85488/src/Native/Graphics/Collage.js#L313
There are two problems here:
translate
andscale
in the first two lines of this functionsave
inside the for loop is not matched anywhere by a correspondingrestore
.I think there is a potentially large win to be had by using transforms in a more disciplined way so that it is possible to avoid resetting the canvas width and height on every draw if they have not changed.