Draw rectangle on GLMakie fig

Hi, I want to interactively draw rectangle on the GLMakie figure using mouse’s left button (by pressing and dragging the mouse). However, GLMakie only zooms in when I press and drag the mouse. How can I draw rectangle, thanks!

using Revise
using GLMakie
using ColorSchemes
using GeometryBasics: Point2f0, HyperRectangle, Vec2f0
using Observables: on

image = rand(100, 100)

function interactive_rectangle(img::Matrix{Float64})
    fig = Figure(resolution = (800, 800))
    ax = Axis(fig[1, 1])
    image!(ax, img)

    start_point = Observable(Vec2f0(0, 0))
    end_point = Observable(Vec2f0(0, 0))
    rect = Observable(HyperRectangle(Vec2f0(0, 0), Vec2f0(0, 0)))

    drawing = Observable(false)
    on(events(fig).mousebutton) do event
        if event.button == Mouse.left
            pos = event.position
            if event.action == Mouse.press
                start_point[] = Vec2f0(pos...)
                end_point[] = Vec2f0(pos...)
                rect[] = HyperRectangle(start_point[], Vec2f0(0, 0))
                drawing[] = true
            elseif event.action == Mouse.release
                drawing[] = false
            end
        end
    end

    on(events(fig).mouseposition) do pos
        if drawing[]
            end_point[] = Vec2f0(pos...)
            width = end_point[][1] - start_point[][1]
            height = end_point[][2] - start_point[][2]
            rect[] = HyperRectangle(start_point[], Vec2f0(width, height))
        end
    end

    # Observable for the line coordinates
    line_coords = Observable((Float32[], Float32[]))

    on(rect) do r
        xs = convert(Vector{Float32}, [r.origin[1], r.origin[1] + r.widths[1], r.origin[1] + r.widths[1], r.origin[1], r.origin[1]])
        ys = convert(Vector{Float32}, [r.origin[2], r.origin[2], r.origin[2] + r.widths[2], r.origin[2] + r.widths[2], r.origin[2]])
        line_coords[] = (xs, ys)
    end

    # Draw the rectangle lines
    lines_plot = lift(line_coords) do (xs, ys)
        lines!(ax, xs, ys, color=:red)
    end

    display(fig)
end
# Call the function with the image
interactive_rectangle(image)

This issue has been resolved by me.

You also might be interested in the MakieDraw package. GitHub - MakieOrg/MakieDraw.jl which supports these kinds of operations.