Canvas reset()
The CanvasRenderingContext2D.reset() method of the Canvas 2D API resets the rendering context to its default state, allowing it to be reused for drawing something else without having to explicitly reset all the properties.
Resetting clears the backing buffer, drawing state stack, any defined paths, and styles. This includes the current transformation matrix, compositing properties, clipping region, dash list, line styles, text styles, shadows, image smoothing, filters, and so on.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 99 | 99 | 113 | 17.2 | 99 | 17.2 | |
| The CanvasRenderingContext2D.reset() method of the Canvas 2D API resets the rendering context to its default state, allowing it to be reused for drawing something else without having to explicitly reset all the properties. | 99 | 99 | 113 | 17.2 | 99 | 17.2 |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Syntax
JAVASCRIPT
// Get the 2d context
const canvas = document.getElementById("my-house");
const ctx = canvas.getContext("2d");
function drawRect() {
// Set line width
ctx.lineWidth = 10;
// Stroke rect outline
ctx.strokeRect(50, 50, 150, 100);
// Create filled text
ctx.font = "50px serif";
ctx.fillText("Rect!", 70, 110);
}
function drawCircle() {
// Set line width
ctx.lineWidth = 5;
// Stroke out circle
ctx.beginPath();
ctx.arc(300, 100, 50, 0, 2 * Math.PI);
ctx.stroke();
// Create filled text
ctx.font = "25px sans-serif";
ctx.fillText("Circle!", 265, 100);
} Use cases
-
Using Canvas reset()
The CanvasRenderingContext2D.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.