Skip to content

Commit c745196

Browse files
authored
Merge pull request #151 from martinRenou/fill_stroke_polygon
Add fill_polygon and stroke_polygon methods
2 parents 02acf7b + 9b126b0 commit c745196

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

ipycanvas/canvas.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
'bezierCurveTo': 26, 'fillText': 27, 'strokeText': 28, 'setLineDash': 29, 'drawImage': 30,
3030
'putImageData': 31, 'clip': 32, 'save': 33, 'restore': 34, 'translate': 35,
3131
'rotate': 36, 'scale': 37, 'transform': 38, 'setTransform': 39, 'resetTransform': 40,
32-
'set': 41, 'clear': 42, 'sleep': 43,
32+
'set': 41, 'clear': 42, 'sleep': 43, 'fillPolygon': 44, 'strokePolygon': 45,
3333
}
3434

3535

@@ -551,6 +551,25 @@ def stroke_circles(self, x, y, radius):
551551

552552
self._send_canvas_command(COMMANDS['strokeCircles'], args, buffers)
553553

554+
# Polygon methods
555+
def fill_polygon(self, points):
556+
"""Fill a polygon from a list of points ``[(x1, y1), (x2, y2), ..., (xn, yn)]``."""
557+
args = []
558+
buffers = []
559+
560+
populate_args(points, args, buffers)
561+
562+
self._send_canvas_command(COMMANDS['fillPolygon'], args, buffers)
563+
564+
def stroke_polygon(self, points):
565+
"""Draw polygon outline from a list of points ``[(x1, y1), (x2, y2), ..., (xn, yn)]``."""
566+
args = []
567+
buffers = []
568+
569+
populate_args(points, args, buffers)
570+
571+
self._send_canvas_command(COMMANDS['strokePolygon'], args, buffers)
572+
554573
# Lines methods
555574
def stroke_line(self, x1, y1, x2, y2):
556575
"""Draw a line from ``(x1, y1)`` to ``(x2, y2)``."""

src/widget.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const COMMANDS = [
6969
'bezierCurveTo', 'fillText', 'strokeText', 'setLineDash', 'drawImage',
7070
'putImageData', 'clip', 'save', 'restore', 'translate',
7171
'rotate', 'scale', 'transform', 'setTransform', 'resetTransform',
72-
'set', 'clear', 'sleep',
72+
'set', 'clear', 'sleep', 'fillPolygon', 'strokePolygon',
7373
];
7474

7575

@@ -361,6 +361,12 @@ class CanvasModel extends DOMWidgetModel {
361361
case 'strokeLine':
362362
this.strokeLine(args, buffers);
363363
break;
364+
case 'fillPolygon':
365+
this.fillPolygon(args, buffers);
366+
break;
367+
case 'strokePolygon':
368+
this.strokePolygon(args, buffers);
369+
break;
364370
case 'fillPath':
365371
await this.fillPath(args, buffers);
366372
break;
@@ -494,6 +500,34 @@ class CanvasModel extends DOMWidgetModel {
494500
this.ctx.closePath();
495501
}
496502

503+
protected fillPolygon(args: any[], buffers: any) {
504+
this.ctx.beginPath();
505+
const points = getArg(args[0], buffers);
506+
507+
// Move to the first point, then create lines between points
508+
this.ctx.moveTo(points.getItem(0), points.getItem(1));
509+
for (let idx = 2; idx < points.length; idx += 2) {
510+
this.ctx.lineTo(points.getItem(idx), points.getItem(idx + 1));
511+
}
512+
513+
this.ctx.closePath();
514+
this.ctx.fill();
515+
}
516+
517+
protected strokePolygon(args: any[], buffers: any) {
518+
this.ctx.beginPath();
519+
const points = getArg(args[0], buffers);
520+
521+
// Move to the first point, then create lines between points
522+
this.ctx.moveTo(points.getItem(0), points.getItem(1));
523+
for (let idx = 2; idx < points.length; idx += 2) {
524+
this.ctx.lineTo(points.getItem(idx), points.getItem(idx + 1));
525+
}
526+
527+
this.ctx.closePath();
528+
this.ctx.stroke();
529+
}
530+
497531
protected async fillPath(args: any[], buffers: any) {
498532
const [serializedPath] = args;
499533

@@ -668,6 +702,28 @@ class RoughCanvasModel extends CanvasModel {
668702
this.roughCanvas.arc(x, y, ellipseSize, ellipseSize, start, end, false, this.getRoughStrokeStyle());
669703
}
670704

705+
protected fillPolygon(args: any[], buffers: any) {
706+
const points = getArg(args[0], buffers);
707+
708+
const polygon: [number, number][] = [];
709+
for (let idx = 0; idx < points.length; idx += 2) {
710+
polygon.push([points.getItem(idx), points.getItem(idx + 1)]);
711+
}
712+
713+
this.roughCanvas.polygon(polygon, this.getRoughFillStyle());
714+
}
715+
716+
protected strokePolygon(args: any[], buffers: any) {
717+
const points = getArg(args[0], buffers);
718+
719+
const polygon: [number, number][] = [];
720+
for (let idx = 0; idx < points.length; idx += 2) {
721+
polygon.push([points.getItem(idx), points.getItem(idx + 1)]);
722+
}
723+
724+
this.roughCanvas.polygon(polygon, this.getRoughStrokeStyle());
725+
}
726+
671727
protected async setAttr(attr: number, value: any) {
672728
if (RoughCanvasModel.ROUGH_ATTRS[attr]) {
673729
(this as any)[RoughCanvasModel.ROUGH_ATTRS[attr]] = value;

0 commit comments

Comments
 (0)