js: Simplify drawing context management

There's not much point in re-requesting the drawing context from the
offscreen canvas at the start of each drawing operation.  The canvas
keeps the context around and returns it on every call to getContext(),
so we may as well just keep our reference to it too.  This does mean
that the front-end won't detect puzzles drawing outside of a redraw
operation, but I think it's the mid-end's job to assert things like
that.

Rumours that I'm doing this because I had a mysterious bug whereby ctx
was unexpectedly null are entirely true.
This commit is contained in:
Ben Harris
2022-12-02 23:55:28 +00:00
parent f0d4705364
commit d3ef8e65dc
2 changed files with 2 additions and 3 deletions

View File

@ -224,7 +224,6 @@ mergeInto(LibraryManager.library, {
* Prepare to do some drawing on the canvas. * Prepare to do some drawing on the canvas.
*/ */
js_canvas_start_draw: function() { js_canvas_start_draw: function() {
ctx = offscreen_canvas.getContext('2d', { alpha: false });
update_xmin = update_xmax = update_ymin = update_ymax = undefined; update_xmin = update_xmax = update_ymin = update_ymax = undefined;
}, },
@ -267,7 +266,6 @@ mergeInto(LibraryManager.library, {
update_xmax - update_xmin, update_xmax - update_xmin,
update_ymax - update_ymin); update_ymax - update_ymin);
} }
ctx = null;
}, },
/* /*

View File

@ -22,7 +22,7 @@
var onscreen_canvas, offscreen_canvas; var onscreen_canvas, offscreen_canvas;
// A persistent drawing context for the offscreen canvas, to save // A persistent drawing context for the offscreen canvas, to save
// constructing one per individual graphics operation. // requesting it for each individual graphics operation.
var ctx; var ctx;
// Bounding rectangle for the copy to the onscreen canvas that will be // Bounding rectangle for the copy to the onscreen canvas that will be
@ -245,6 +245,7 @@ function initPuzzle() {
// Construct the off-screen canvas used for double buffering. // Construct the off-screen canvas used for double buffering.
onscreen_canvas = document.getElementById("puzzlecanvas"); onscreen_canvas = document.getElementById("puzzlecanvas");
offscreen_canvas = document.createElement("canvas"); offscreen_canvas = document.createElement("canvas");
ctx = offscreen_canvas.getContext('2d', { alpha: false });
// Stop right-clicks on the puzzle from popping up a context menu. // Stop right-clicks on the puzzle from popping up a context menu.
// We need those right-clicks! // We need those right-clicks!