Skip to content

Commit 2f38a32

Browse files
committed
add reset() method to context
- it erases the canvas, resets the transform, and clears the current path
1 parent d7ff052 commit 2f38a32

File tree

7 files changed

+41
-2
lines changed

7 files changed

+41
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### New Features
66
- The new [Window][window] class can display a **Canvas** on screen, respond to mouse and keyboard input, and fluidly [animate][window_anim] by calling user-defined [event handlers][window_events].
77
- Bitmap rendering now occurs on the GPU by default and can be configured using the **Canvas**'s [`.gpu`][canvas_gpu] property. If the platform supports hardware-accelerated rendering (using Metal on macOS and Vulkan on Linux & Windows), the property will be `true` by default and can be set to `false` to use the software renderer.
8+
- Added support for Chrome’s [`reset()`][chrome_reset] context method which erases the canvas, resets the transformation state, and clears the current path
89

910
### Bugfixes
1011
- The `FontLibrary.reset()` method didn't actually remove previously installed fonts that had already been drawn with (and thus cached). It now clears those caches, which also means previously used fonts can now be replaced by calling `.use()` again with the same family name.
@@ -21,6 +22,7 @@
2122
[window_events]: https://github.com/samizdatco/skia-canvas#on--off--once
2223
[canvas_gpu]: https://github.com/samizdatco/skia-canvas#gpu
2324
[filter]: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/filter
25+
[chrome_reset]: https://developer.chrome.com/blog/canvas2d/#context-reset
2426

2527
## 📦 ⟩ [v0.9.30] ⟩ Jun 7, 2022
2628

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ Most of your interaction with the canvas will actually be directed toward its
342342
| [isPointInStroke()][isPointInStroke()] | [fillText()][fillText()] ⧸[][drawText] | [createLinearGradient()][createLinearGradient()] | [**lineDashOffset**][lineDashOffset] | [setTransform()][setTransform()] | [bezierCurveTo()][bezierCurveTo()] | [**textAlign**][textAlign] | [getImageData()][getImageData()] | [**shadowBlur**][shadowBlur] |
343343
| [save()][save()] | [strokeText()][strokeText()] ⧸[][drawText] | [createRadialGradient()][createRadialGradient()] | [**lineJoin**][lineJoin] | [resetTransform()][resetTransform()] | [conicCurveTo() ⚡][conicCurveTo] | [**textBaseline**][textBaseline] | [putImageData()][putImageData()] | [**shadowColor**][shadowColor] |
344344
| [restore()][restore()] | [fill()][fill()] | [createPattern()][createPattern()] | [**lineWidth**][lineWidth] | [transform()][transform()] | [quadraticCurveTo()][quadraticCurveTo()] | [**textTracking** ⚡](#texttracking) | [drawCanvas() ⚡](#drawcanvascanvas-x-y-) | [**shadowOffsetX**][shadowOffsetX] |
345-
| [clip()][clip()] | [stroke()][stroke()] | [createTexture() ⚡][createTexture()] | [**miterLimit**][miterLimit] | [translate()][translate()] | [closePath()][closePath()] | [**textWrap** ⚡](#textwrap) | [drawImage()][drawImage()] | [**shadowOffsetY**][shadowOffsetY] |
346-
| | | | [getLineDash()][getLineDash()] | [rotate()][rotate()] | [arc()][arc()] | [measureText()][measureText()] ⧸[](#measuretextstr-width) | | |
345+
| [reset()][reset()] | [stroke()][stroke()] | [createTexture() ⚡][createTexture()] | [**miterLimit**][miterLimit] | [translate()][translate()] | [closePath()][closePath()] | [**textWrap** ⚡](#textwrap) | [drawImage()][drawImage()] | [**shadowOffsetY**][shadowOffsetY] |
346+
| [clip()][clip()] | | | [getLineDash()][getLineDash()] | [rotate()][rotate()] | [arc()][arc()] | [measureText()][measureText()] ⧸[](#measuretextstr-width) | | |
347347
| | | | [setLineDash()][setLineDash()] | [scale()][scale()] | [ellipse()][ellipse()] | [outlineText() ⚡][outlineText()] | | |
348348
| | | | | | [rect()][rect()] | | |
349349

@@ -1361,6 +1361,7 @@ Many thanks to the [`node-canvas`](https://github.com/Automattic/node-canvas) de
13611361
[strokeText()]: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/strokeText
13621362
[transform()]: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
13631363
[translate()]: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/translate
1364+
[reset()]: https://developer.chrome.com/blog/canvas2d/#context-reset
13641365
13651366
[nonzero]: https://en.wikipedia.org/wiki/Nonzero-rule
13661367
[evenodd]: https://en.wikipedia.org/wiki/Even–odd_rule

lib/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ export interface CanvasRenderingContext2D extends CanvasCompositing, CanvasDrawI
139139
strokeText(text: string, x: number, y:number, maxWidth?: number): void
140140
measureText(text: string, maxWidth?: number): TextMetrics
141141
outlineText(text: string): Path2D
142+
143+
reset(): void
142144
}
143145

144146
//

lib/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,9 @@ class CanvasRenderingContext2D extends RustClass{
567567

568568
get canvas(){ return this.#canvas.deref() }
569569

570+
// -- global state & content reset ------------------------------------------
571+
reset(){ this.ƒ('reset') }
572+
570573
// -- grid state ------------------------------------------------------------
571574
save(){ this.ƒ('save') }
572575
restore(){ this.ƒ('restore') }

src/context/api.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ pub fn set_size(mut cx: FunctionContext) -> JsResult<JsUndefined> {
6464
Ok(cx.undefined())
6565
}
6666

67+
pub fn reset(mut cx: FunctionContext) -> JsResult<JsUndefined> {
68+
let this = cx.argument::<BoxedContext2D>(0)?;
69+
let mut this = this.borrow_mut();
70+
let size = this.bounds.size();
71+
72+
this.reset_size(size);
73+
Ok(cx.undefined())
74+
}
75+
6776
//
6877
// Grid State
6978
//

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
119119
cx.export_function("CanvasRenderingContext2D_resetSize", ctx::resetSize)?;
120120
cx.export_function("CanvasRenderingContext2D_get_size", ctx::get_size)?;
121121
cx.export_function("CanvasRenderingContext2D_set_size", ctx::set_size)?;
122+
cx.export_function("CanvasRenderingContext2D_reset", ctx::reset)?;
122123

123124
// grid state
124125
cx.export_function("CanvasRenderingContext2D_save", ctx::save)?;

test/context2d.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,28 @@ describe("Context2D", ()=>{
752752
expect( () => ctx.drawCanvas(image, 0, 0) ).not.toThrow()
753753
})
754754

755+
test('reset()', async () => {
756+
ctx.fillStyle = 'green'
757+
ctx.scale(2, 2)
758+
ctx.translate(0, -HEIGHT/4)
759+
760+
ctx.fillRect(WIDTH/4, HEIGHT/4, WIDTH/8, HEIGHT/8)
761+
expect(pixel(WIDTH * .5 + 1, 0)).toEqual(GREEN)
762+
expect(pixel(WIDTH * .75 - 1, 0)).toEqual(GREEN)
755763

764+
ctx.beginPath()
765+
ctx.rect(WIDTH/4, HEIGHT/2, 100, 100)
766+
ctx.reset()
767+
ctx.fill()
768+
expect(pixel(WIDTH/2 + 1, HEIGHT/2 + 1)).toEqual(CLEAR)
769+
expect(pixel(WIDTH * .5 + 1, 0)).toEqual(CLEAR)
770+
expect(pixel(WIDTH * .75 - 1, 0)).toEqual(CLEAR)
771+
772+
ctx.globalAlpha = 0.4
773+
ctx.reset()
774+
ctx.fillRect(WIDTH/2, HEIGHT/2, 3, 3)
775+
expect(pixel(WIDTH/2 + 1, HEIGHT/2 + 1)).toEqual(BLACK)
776+
})
756777
})
757778

758779

0 commit comments

Comments
 (0)