Skip to content

Commit 724d733

Browse files
committed
Resize
1 parent a623320 commit 724d733

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

examples/introduction.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"metadata": {},
5151
"outputs": [],
5252
"source": [
53-
"c = ipycanvas.Canvas(stroke_style='red')\n",
53+
"c = ipycanvas.Canvas(stroke_style='red', size=(700, 200))\n",
5454
"c.layout.height = '200px'\n",
5555
"\n",
5656
"c"
@@ -81,7 +81,7 @@
8181
"metadata": {},
8282
"outputs": [],
8383
"source": [
84-
"c = ipycanvas.Canvas()\n",
84+
"c = ipycanvas.Canvas(size=(700, 200))\n",
8585
"c.layout.height = '200px'\n",
8686
"\n",
8787
"c"
@@ -111,7 +111,7 @@
111111
"metadata": {},
112112
"outputs": [],
113113
"source": [
114-
"c = ipycanvas.Canvas()\n",
114+
"c = ipycanvas.Canvas(size=(700, 200))\n",
115115
"c.layout.height = '200px'\n",
116116
"\n",
117117
"c"

ipycanvas/canvas.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
# Copyright (c) Martin Renou.
55
# Distributed under the terms of the Modified BSD License.
66

7-
from ipywidgets import DOMWidget, Color
7+
from ipywidgets import Color, DOMWidget
88

9-
from traitlets import Unicode
9+
from traitlets import Tuple, Unicode
1010

1111
from ._frontend import module_name, module_version
1212

@@ -19,6 +19,8 @@ class Canvas(DOMWidget):
1919
_view_module = Unicode(module_name).tag(sync=True)
2020
_view_module_version = Unicode(module_version).tag(sync=True)
2121

22+
size = Tuple((700, 500), help='Size of the Canvas, this is not equal to the size of the view').tag(sync=True)
23+
2224
fill_style = Color('black').tag(sync=True)
2325
stroke_style = Color('black').tag(sync=True)
2426

src/widget.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class CanvasModel extends DOMWidgetModel {
2222
_view_name: CanvasModel.view_name,
2323
_view_module: CanvasModel.view_module,
2424
_view_module_version: CanvasModel.view_module_version,
25+
size: [],
2526
fill_style: 'black',
2627
stroke_style: 'black'
2728
};
@@ -55,8 +56,6 @@ class CanvasView extends DOMWidgetView {
5556
this.resize_canvas();
5657

5758
this.model_events();
58-
59-
window.addEventListener('resize', () => { this.resize_canvas(); });
6059
}
6160

6261
model_events() {
@@ -66,24 +65,15 @@ class CanvasView extends DOMWidgetView {
6665

6766
this.ctx[event.msg](...event.args);
6867
});
69-
}
7068

71-
processPhosphorMessage(msg: any) {
72-
super.processPhosphorMessage(msg);
73-
74-
switch (msg.type) {
75-
case 'resize':
76-
this.resize_canvas();
77-
break;
78-
}
69+
this.model.on('change;size', () => { this.resize_canvas(); });
7970
}
8071

8172
resize_canvas() {
82-
// TODO replay all drawings after resize?
73+
const size = this.model.get('size');
8374

84-
const rect = this.el.getBoundingClientRect();
85-
this.canvas.setAttribute('width', rect.width);
86-
this.canvas.setAttribute('height', rect.height);
75+
this.canvas.setAttribute('width', size[0]);
76+
this.canvas.setAttribute('height', size[1]);
8777
}
8878

8979
canvas: any;

0 commit comments

Comments
 (0)