Skip to content

Commit 6918047

Browse files
committed
Implement clear method
1 parent ad41cfd commit 6918047

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

examples/introduction.ipynb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@
7676
"c"
7777
]
7878
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"c.clear()"
86+
]
87+
},
7988
{
8089
"cell_type": "code",
8190
"execution_count": null,

ipycanvas/canvas.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Canvas(DOMWidget):
3333

3434
def __init__(self, *args, **kwargs):
3535
self.caching = kwargs.get('caching', False)
36-
self.commands_cache = []
36+
self._commands_cache = []
3737

3838
super(Canvas, self).__init__(*args, **kwargs)
3939
self.layout.width = str(self.size[0]) + 'px'
@@ -87,14 +87,20 @@ def quadratic_curve_to(self, cp1x, cp1y, x, y):
8787
def bezier_curve_to(self, cp1x, cp1y, cp2x, cp2y, x, y):
8888
self._send_canvas_command('bezierCurveTo', cp1x, cp1y, cp2x, cp2y, x, y)
8989

90+
def clear(self):
91+
"""Clear the entire canvas."""
92+
self._commands_cache = []
93+
self.send({'name': 'clear'})
94+
9095
def flush(self):
96+
"""Flush all the cached commands."""
9197
if not self.caching:
9298
return
9399

94-
self.send(self.commands_cache)
100+
self.send(self._commands_cache)
95101

96102
self.caching = False
97-
self.commands_cache = []
103+
self._commands_cache = []
98104

99105
@observe('fill_style', 'stroke_style', 'global_alpha')
100106
def _on_set_attr(self, change):
@@ -110,6 +116,6 @@ def _send_canvas_command(self, name, *args):
110116

111117
def _send_command(self, command):
112118
if self.caching:
113-
self.commands_cache.append(command)
119+
self._commands_cache.append(command)
114120
else:
115121
self.send(command)

src/widget.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ class CanvasModel extends DOMWidgetModel {
3333

3434
this.commandsCache = [];
3535

36-
this.on('msg:custom', (command) => { this.commandsCache.push(command); });
36+
this.on('msg:custom', (command) => {
37+
if (!(command instanceof Array) && command.name == 'clear') {
38+
this.commandsCache = [];
39+
return;
40+
}
41+
42+
this.commandsCache.push(command);
43+
});
3744
}
3845

3946
static model_name = 'CanvasModel';
@@ -87,11 +94,17 @@ class CanvasView extends DOMWidgetView {
8794
return;
8895
}
8996

97+
if (command.name == 'clear') {
98+
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
99+
return;
100+
}
101+
90102
if (command.name == 'set') {
91103
this.ctx[command.attr] = command.value;
92-
} else {
93-
this.ctx[command.name](...command.args);
104+
return;
94105
}
106+
107+
this.ctx[command.name](...command.args);
95108
}
96109

97110
resize_canvas() {

0 commit comments

Comments
 (0)