Skip to content

Commit 47efe9e

Browse files
committed
Add more canvas APIs
1 parent 746bc03 commit 47efe9e

File tree

4 files changed

+86
-61
lines changed

4 files changed

+86
-61
lines changed

examples/canvas.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ def create():
1010
colors = ["black", "red", "white", "blue", "green", "yellow", "purple", "teal", "orange"]
1111

1212
def mousemove(event):
13-
canvas.fillStyle = random.choice(colors)
14-
canvas.fillRect(event.offsetX, event.offsetY, 50, 50)
13+
canvas.fill_style = random.choice(colors)
14+
canvas.fill_rect(event.offsetX, event.offsetY, 50, 50)
15+
16+
canvas.fill_style = random.choice(colors)
17+
canvas.fill_circle(event.offsetX + 25, event.offsetY + 25, 25)
18+
19+
canvas.stroke_style = "black"
20+
canvas.rect(event.offsetX + 8, event.offsetY + 8, 34, 34)
21+
canvas.circle(event.offsetX + 25, event.offsetY + 25, 12)
1522

1623
return (
1724
ltk.VBox(
18-
ltk.Heading2("This is a Canvas. Move the mouse draw squares."),
25+
ltk.Heading2("This is a Canvas. Move the mouse to draw."),
1926
canvas
2027
.attr("id", "pink-canvas") \
2128
.on("mousemove", ltk.proxy(mousemove)) \

ltk/ltk.js

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -87,57 +87,34 @@
8787
$.ajax({ url, type: "DELETE", success}).fail(error)
8888
}
8989

90-
window.canvasRects = (context, coordinatesJson) => {
91-
const coordinates = JSON.parse(coordinatesJson)
92-
context.beginPath()
93-
for (var n = 0; n < coordinates.length; n += 5) {
94-
x = coordinates[n]
95-
y = coordinates[n + 1]
96-
w = coordinates[n + 2]
97-
h = coordinates[n + 3]
90+
window.canvas = {
91+
line: (context, x1, y1, x2, y2) => {
92+
context.beginPath()
93+
context.moveTo(x1, y1)
94+
context.lineTo(x2, y2)
95+
context.stroke()
96+
},
97+
rect: (context, x, y, w, h) => {
98+
context.beginPath()
9899
context.rect(x, y, w, h)
99-
}
100-
}
101-
102-
window.canvasFillRects = (context, coordinatesJson) => {
103-
const coordinates = JSON.parse(coordinatesJson)
104-
context.beginPath()
105-
for (var n = 0; n < coordinates.length; n += 5) {
106-
x = coordinates[n]
107-
y = coordinates[n + 1]
108-
w = coordinates[n + 2]
109-
h = coordinates[n + 3]
110-
context.fillStyle = coordinates[n + 4]
111-
context.fillRect(x, y, w, h)
112-
}
113-
}
114-
115-
window.canvasDrawTexts = (context, coordinatesJson) => {
116-
const coordinates = JSON.parse(coordinatesJson)
117-
context.beginPath()
118-
context.strokeStyle = "red"
119-
for (var n = 0; n < coordinates.length; n += 5) {
120-
x = coordinates[n]
121-
y = coordinates[n + 1]
122-
text = coordinates[n + 2]
123-
color = coordinates[n + 3]
124-
w = coordinates[n + 4]
125-
context.fillStyle = color
126-
context.fillText(text, x, y, w)
127-
}
128-
}
129-
130-
window.canvasDrawLines = (context, lineWidth, strokeStyle, coordinatesJson) => {
131-
const coordinates = JSON.parse(coordinatesJson)
132-
context.lineWidth = lineWidth
133-
context.strokeStyle = strokeStyle
134-
for (var n = 0; n < coordinates.length; n += 4) {
100+
context.stroke()
101+
},
102+
text: (context, x, y, text) => {
135103
context.beginPath()
136-
context.moveTo(coordinates[n], coordinates[n + 1])
137-
context.lineTo(coordinates[n + 2], coordinates[n + 3])
104+
context.strokeText(x, y, text)
138105
context.stroke()
106+
},
107+
circle: (context, x, y, radius) => {
108+
context.beginPath()
109+
context.arc(x, y, radius, 0, 2 * Math.PI)
110+
context.stroke()
111+
},
112+
fillCircle: (context, x, y, radius) => {
113+
context.beginPath()
114+
context.arc(x, y, radius, 0, 2 * Math.PI)
115+
context.fill()
139116
}
140-
}
117+
};
141118

142119
$.fn.isInViewport = function() {
143120
const offset = $(this).offset();

ltk/widgets.py

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,9 @@ class Canvas(Widget):
11351135

11361136
def __init__(self, style=DEFAULT_CSS) -> None:
11371137
self._context = None
1138+
self._font = None
1139+
self._fill_style = None
1140+
self._stroke_style = None
11381141
Widget.__init__(self, style)
11391142

11401143
def __getattr__(self, name):
@@ -1160,19 +1163,57 @@ def context(self):
11601163
self._context = self.element[0].getContext("2d")
11611164
return self._context
11621165

1163-
def rects(self, rects):
1164-
return window.canvasRects(self.context, json.dumps(list(rects)))
1166+
@property
1167+
def stroke_style(self):
1168+
return self._stroke_style
1169+
1170+
@stroke_style.setter
1171+
def stroke_style(self, value):
1172+
if self._stroke_style != value:
1173+
self._stroke_style = value
1174+
self.context.strokeStyle = value
1175+
1176+
@property
1177+
def fill_style(self):
1178+
return self._fill_style
1179+
1180+
@fill_style.setter
1181+
def fill_style(self, value):
1182+
if self._fill_style != value:
1183+
self._fill_style = value
1184+
self.context.fillStyle = value
1185+
1186+
@property
1187+
def font(self):
1188+
return self._font
1189+
1190+
@font.setter
1191+
def font(self, value):
1192+
if self._font != value:
1193+
self._font = value
1194+
self.context.font = value
1195+
1196+
def line(self, x1, y1, x2, y2):
1197+
window.canvas.line(self.context, x1, y1, x2, y2)
1198+
1199+
def text(self, x, y, text):
1200+
window.canvas.text(self.context, x, y, text)
1201+
1202+
def fill_text(self, x, y, text):
1203+
self.context.fillText(x, y, text)
1204+
1205+
def rect(self, x, y, w, h):
1206+
window.canvas.rect(self.context, x, y, w, h)
1207+
1208+
def fill_rect(self, x, y, w, h):
1209+
self.context.fillRect(x, y, w, h)
11651210

1166-
def fill_rects(self, rects):
1167-
return window.canvasFillRects(self.context, json.dumps(list(rects)))
1211+
def circle(self, x, y, radius):
1212+
window.canvas.circle(self.context, x, y, radius)
11681213

1169-
def lines(self, lines, width, color):
1170-
return window.canvasDrawLines(self.context, width, color, json.dumps(list(lines)))
1214+
def fill_circle(self, x, y, radius):
1215+
window.canvas.fillCircle(self.context, x, y, radius)
11711216

1172-
def texts(self, texts, font):
1173-
self.set_font(font)
1174-
return window.canvasDrawTexts(self.context, json.dumps(list(texts)))
1175-
11761217

11771218
def _close_all_menus(event=None):
11781219
if event and jQuery(event.target).hasClass("ltk-menulabel"):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
66

77
[project]
88
name = "pyscript-ltk"
9-
version = "0.1.40"
9+
version = "0.1.41"
1010
description = "A little toolkit for writing UIs in PyScript"
1111
readme = "README.md"
1212
authors = [{ name = "Chris Laffra", email = "[email protected]" }]

0 commit comments

Comments
 (0)