mirror of
https://github.com/chartjs/Chart.js.git
synced 2026-03-04 07:24:02 +01:00
Fix context state restoration on destroy
In many cases, the canvas render size is changed by the lib, causing the state stack to be discarded, meaning that we can't use save() and restore() to release the context with its initial state (i.e. before creating the chart). Since we don't need (want) to manually save / restore the context initial state, simply make sure to reset it to the default state to give a fresh context back to the user. That also means we don't need to revert the scale when the pixel device ratio is not 1.
This commit is contained in:
committed by
Evert Timberg
parent
040b0e160d
commit
efced4780c
@@ -109,6 +109,12 @@ module.exports = function(Chart) {
|
||||
canvas.style[key] = value;
|
||||
});
|
||||
|
||||
// The canvas render size might have been changed (and thus the state stack discarded),
|
||||
// we can't use save() and restore() to restore the initial state. So make sure that at
|
||||
// least the canvas context is reset to the default state by setting the canvas width.
|
||||
// https://www.w3.org/TR/2011/WD-html5-20110525/the-canvas-element.html
|
||||
canvas.width = canvas.width;
|
||||
|
||||
delete canvas._chartjs;
|
||||
}
|
||||
|
||||
@@ -673,11 +679,6 @@ module.exports = function(Chart) {
|
||||
me.chart.ctx = null;
|
||||
}
|
||||
|
||||
// if we scaled the canvas in response to a devicePixelRatio !== 1, we need to undo that transform here
|
||||
if (me.chart.originalDevicePixelRatio !== undefined) {
|
||||
me.chart.ctx.scale(1 / me.chart.originalDevicePixelRatio, 1 / me.chart.originalDevicePixelRatio);
|
||||
}
|
||||
|
||||
Chart.plugins.notify('destroy', [me]);
|
||||
|
||||
delete Chart.instances[me.id];
|
||||
|
||||
@@ -842,11 +842,6 @@ module.exports = function(Chart) {
|
||||
canvas.height = height * pixelRatio;
|
||||
canvas.width = width * pixelRatio;
|
||||
ctx.scale(pixelRatio, pixelRatio);
|
||||
|
||||
// Store the device pixel ratio so that we can go backwards in `destroy`.
|
||||
// The devicePixelRatio changes with zoom, so there are no guarantees that it is the same
|
||||
// when destroy is called
|
||||
chart.originalDevicePixelRatio = chart.originalDevicePixelRatio || pixelRatio;
|
||||
}
|
||||
};
|
||||
// -- Canvas methods
|
||||
|
||||
Reference in New Issue
Block a user