Skip to content

Commit

Permalink
Added support to resize canvas
Browse files Browse the repository at this point in the history
Fixes #132

The width and height variables are updated everytime the canvas is resized.
  • Loading branch information
arihantparsoya committed Nov 24, 2019
1 parent 531fa35 commit 1b233fd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
5 changes: 4 additions & 1 deletion p5/sketch/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
38 changes: 38 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 1b233fd

Please sign in to comment.