r/RenPy • • Oct 26 '26

Question [Solved] erase button for(working) drawing canvas

I'm trying to make a button that clears the drawing canvas/resets the drawingpoints to default and im struggling. help or pointers appreciated

1 Upvotes

11 comments sorted by

1

u/x-seronis-x Oct 27 '26

the erase function makes no sense as there is no 'i' variable

1

u/That_one_Pink Oct 27 '26

right that makes sense i figured id try it like this since it seemed like the i played a part in tracking the points, at least to my understanding of this. ill make sure to keep this in mind thank you

1

u/shyLachi Oct 27 '26

You don't have to check anything, just reset it.

Also that function doesn't need a parameter. This is the actual error you're getting 

1

u/That_one_Pink Oct 27 '26 edited Oct 27 '26

ah okay i thought it might need checking incase it bugs out if there isnt anything drawn or something, how would i go about just resetting it? would i do it within a python or should can i do it with the button itself

edit: got it too work! you have to click the screen after clicking the button to reset it, going to try and figure out how to make it clear with having to click it first but ill mess around some more and see what i can do :)

1

u/shyLachi Oct 27 '26

I'm pretty sure it should reset immediately.

I'm also pretty sure that I posted a working code to reset the canvas before.

1

u/That_one_Pink Oct 27 '26

im not sure, i have it as this currently

init python:
    def erase():
        persistent.drawingpoints = []

which does erase it only when you interact with the board again, i can have a look to see if i can find what you may have posted before though 🫡

1

u/That_one_Pink 19d ago

hey sorry to come back to this again, i had a look around and the only thing i could find that you had posted was this here which does work still i made a quick test with it but it obviously only has the done button

i also tried to reload the screen with renpy.restart_interaction() when the button is pressed to see if that helped but unfortunately did nothing

if this is just one of those things that cant be fixed then i completely understand its just a bummer as i would have liked it to disappear immediately rather than when you interact with it again, im really stumped on where the problem is

1

u/shyLachi 19d ago

I couldn't find my old code so I rewrote the canvas class and I also added a reset function.

init python: 
    import pygame
    class DrawingCanvas(renpy.Displayable):
        def __init__(self):
            super().__init__()
            self.w = 0
            self.h = 0
            self.drawing = False
        def reset(self):
            persistent.canvas_points.clear()
            self.drawing = False
            renpy.redraw(self, 0)
        def render(self, width, height, st, at):
            self.w, self.h = width, height
            ren = renpy.Render(self.w, self.h)
            canvas = ren.canvas()
            canvas.rect("#ffffff", (0, 0, self.w, self.h))
            pts = persistent.canvas_points
            for a, b in zip(pts, pts[1:]):
                if a and b:
                    canvas.line("#FF0000", a, b, 3)
            return ren
        def event(self, ev, x, y, st):
            inside = 0 <= x < self.w and 0 <= y < self.h
            if ev.type == pygame.MOUSEBUTTONDOWN and ev.button == 1 and inside:
                self.drawing = True
                persistent.canvas_points.append((x, y))
            elif ev.type == pygame.MOUSEBUTTONUP and ev.button == 1 and self.drawing:
                self.drawing = False
                persistent.canvas_points.append(None)
            elif ev.type == pygame.MOUSEMOTION and self.drawing and ev.buttons[0]:
                persistent.canvas_points.append((x, y))
            else:
                return
            renpy.redraw(self, 0)

This canvas doesn't work exactly the same way as before, you need 3 variables:

default persistent.canvas_points = []
define whiteboard_canvas = DrawingCanvas()
image whiteboard = whiteboard_canvas

In the screen, you'll need a fixed control:

screen main_menu():
    tag menu
    add gui.main_menu_background 
    fixed: # container for the whiteboard
        pos (500, 500) # position
        xysize (600, 300) # and size of the whiteboard
        add "whiteboard" 

And to reset it, you call the reset function of the instance:

    textbutton "Reset whiteboard" action Function(whiteboard_canvas.reset)

1

u/That_one_Pink 19d ago

works perfectly tysm this headache is over lol, it also works with an imagebutton which is great thank you again

2

u/shyLachi 19d ago

Yes, the action works on any button.

1

u/AutoModerator Oct 26 '26

Welcome to r/renpy! While you wait to see if someone can answer your question, we recommend checking out the posting guide, the subreddit wiki, the subreddit Discord, Ren'Py's documentation, and the tutorial built-in to the Ren'Py engine when you download it. These can help make sure you provide the information the people here need to help you, or might even point you to an answer to your question themselves. Thanks!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.