Skip to content

Allow the User to Draw Digits #308

Answered by Paul2708
Paul2708 asked this question in Q&A
Dec 29, 2022 · 2 comments · 2 replies
Discussion options

You must be logged in to vote

Instead of filling arcs, I use the method line_to(...) with a modified line width and cap.
This enables "real-time" drawing without lags or gaps.

from ipycanvas import Canvas
from ipywidgets import Output


canvas = Canvas(width=400, height=400)

canvas.stroke_rect(0, 0, 400, 400)

canvas.line_width = 28
canvas.line_cap = "round"

x_prev = 0
y_prev = 0

out = Output()

@out.capture()
def handle_mouse_move(x, y):    
    global x_prev
    global y_prev
    
    canvas.begin_path();
    
    canvas.move_to(x_prev, y_prev);
    canvas.line_to(x, y)

    canvas.stroke();
    
    x_prev = x;
    y_prev = y;


canvas.on_mouse_move(handle_mouse_move)

canvas

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@Paul2708
Comment options

Comment options

You must be logged in to vote
1 reply
@martinRenou
Comment options

Answer selected by Paul2708
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants