From 1b233fdd59f19e97341e5cfb75bccb821ff79eab Mon Sep 17 00:00:00 2001 From: Arihant Parsoya Date: Sun, 24 Nov 2019 11:13:04 +0530 Subject: [PATCH] Added support to resize canvas Fixes #132 The width and height variables are updated everytime the canvas is resized. --- p5/sketch/base.py | 5 ++++- test.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 test.py diff --git a/p5/sketch/base.py b/p5/sketch/base.py index e7f59e19..9284a1bb 100644 --- a/p5/sketch/base.py +++ b/p5/sketch/base.py @@ -62,7 +62,7 @@ def __init__(self, setup_method, draw_method, title=builtins.title, size=(builtins.width, builtins.height), keys='interactive', - resizable=False, + resizable=True, ) self.setup_method = setup_method @@ -145,6 +145,9 @@ def on_draw(self, event): pass def on_resize(self, event): + builtins.width = int(self.size[0]) + builtins.height = int(self.size[1]) + p5.renderer.reset_view() with p5.renderer.draw_loop(): p5.renderer.clear() diff --git a/test.py b/test.py new file mode 100644 index 00000000..13cdedf6 --- /dev/null +++ b/test.py @@ -0,0 +1,38 @@ +from p5 import * + +x = 0 +y = 0 +angle1 = 0.0 +angle2 = 0.0 +segLength = 100 + +def setup(): + size(640, 360) + stroke(255, 160) + stroke_weight(30) + + global x, y + x = width * 0.3 + y = height * 0.5 + +def draw(): + x = width * 0.3 + y = height * 0.5 + + background(0) + + angle1 = (mouse_x/width - 0.5) * -PI + angle2 = (mouse_y/height - 0.5) * PI + + with push_matrix(): + segment(x, y, angle1) + segment(segLength, 0, angle2) + pop_matrix() + +def segment(x, y, a): + translate(x, y) + rotate(a) + line((0, 0), (segLength, 0)) + +if __name__ == '__main__': + run() \ No newline at end of file