Teek::UI::CanvasItem Class
A handle onto one or more canvas items, addressed by Tk’s own
tagOrId - either the numeric id a create call returns (a single
item) or an arbitrary tag string (every item currently carrying it,
zero or more). Every method here is exactly that uniform - Tk’s own
canvas command already treats a tag and an id identically for
move/coords/itemconfigure/delete/stacking/scale, so a shared tag is
addressable as a group with no separate “group handle” type: get one
via a shape-creation method (see Handle#line and friends, a
single-item handle) or Handle#tagged (an existing tag, whatever it
currently matches).
Inherits: Object
Instance Methods ↑
[](opt)
Read back a single item option - item[:fill].
Parameters
optSymbol, String— e.g.:fill
Returns String
[]=(opt, value)
Set a single item option - item[:fill] = 'red'. Shorthand for
configure(opt => value) when there’s only one to change.
Parameters
optSymbol, String— e.g.:fillvalueObject
Returns Object — value
bounds
Returns Array<Float>, nil — [x1, y1, x2, y2] bounding box, or
nil if nothing currently matches #tag_or_id
bring_to_front(above nil)
Bring to the front of the stacking order (drawn last, on top of
everything), or - given above - just in front of that one
item/tag instead of all the way to the front.
Parameters
aboveCanvasItem, String, nil
Returns self
configure(**opts)
Mutate several item options at once.
Parameters
optsHash— item options, e.g.fill: 'red'
Returns self
draggable(&block)
Makes this item movable by mouse drag, with zero coordinate math
of your own - press and drag it around the canvas, it follows the
pointer. Binds <Button-1> (to capture the starting position) and
<B1-Motion> (to shift #move by the delta each tick) on this
item/tag, replacing any #on_click/#on_drag binding already set
on it, the same way any two binds on the same item/event replace
each other in Tk.
Returns self
@yield x, y optional - the item's new pointer-relative position
(same Integer canvasx/canvasy coordinates #on_drag delivers)
after each move, e.g. to react to where it's been dragged to
exists?
Returns Boolean — whether any item currently matches #tag_or_id -
always true for a single-item handle from a creation method
(the item exists until #deleted), meaningful for a Handle#tagged
group that may currently match zero items
initialize(app, canvas_path, tag_or_id)
Returns CanvasItem — a new instance of CanvasItem
@api private
on_click(&block)
Fires on a left click, only when the click lands on this specific
item/tag - other items on the same canvas are untouched. Wired
immediately, via the canvas’s own bind subcommand (Tk has no
per-item widget path to bind a plain bind against) - unlike
Handle, a CanvasItem only ever exists post-realize, so there’s no
queue-before-realize phase to worry about here.
Returns self
@yield called with no arguments
on_drag(&block)
Fires while dragging this item (left button held down and moving).
Delivers Integer x/y already converted through the canvas’s own
canvasx/canvasy, same as Handle#on_drag does when bound to a
canvas - callers never have to remember to do that themselves.
Returns self
@yield x, y Integer coordinates
on_right_click(menu nil, &block)
Fires on a right click, however the platform spells it - see
MouseEvents::RIGHT_CLICK_EVENTS. Either handle it yourself with a
block, or hand it a :menu/:context_menu handle to pop up at the
click’s screen position - not both.
Parameters
menuHandle, nil— a `:menu` or `:context_menu` handle to tk_popup
Returns self
@raise ArgumentError if given neither or both, or menu isn't a menu handle
@yield called with no arguments (only when menu isn't given)
points
Returns Array<Float> — the current coordinate list
points=(new_coords)
Replace the coordinate list outright (as opposed to #move’s
relative shift).
Parameters
new_coordsArray<Numeric>— flat or nested (e.g.[[x1, y1], [x2, y2]]) - flattened either way
Returns void
scale(ox, oy, sx, sy)
Scale coordinates relative to a fixed point.
Parameters
oxNumeric— x origin scaling is relative tooyNumeric— y origin scaling is relative tosxNumeric— x scale factorsyNumeric— y scale factor
Returns self
send_to_back(below nil)
Send to the back of the stacking order (drawn first, under
everything), or - given below - just behind that one item/tag
instead of all the way to the back.
Parameters
belowCanvasItem, String, nil
Returns self
virtual_path
Returns String — the canvas's own path, marked past the point a
real Tk path stops applying - an item/tag has no independent Tk
path of its own, only the canvas does. ! is illegal in a Tk
path segment, so handing this to a raw Tk command fails loudly
(an "invalid command name" Tcl error) instead of silently
misbehaving - the same marked-address shape
MenuEntryAddressing#virtual_path uses for a menu entry, the
other kind of thing with no Tk path of its own.
Attributes ↑
tag_or_id [R]
Returns String — the tagOrId this handle addresses