如何在VID内部实现动态绘图?现有代码无报错但未绘图
Let’s get your VID drawing working and add the conditional logic you need. I’ve spent plenty of time debugging draw block issues in VID, so let’s break this down clearly.
First: Why Your Original Code Isn’t Drawing
The most common reason draw blocks don’t show up is that they’re not attached to a specific face (like a box or image component) in your layout. VID doesn’t render top-level draw blocks—you need to tie your drawing to a visible surface. Also, if you’re changing conditions later, you probably forgot to trigger a refresh of the face.
Basic Working Draw Block Example
Let’s start with a minimal working example to confirm drawing works:
view layout [ ; A box acts as our drawing canvas (300x300 pixels) box 300x300 "Drawing Canvas" [ draw [ pen red ; Set pen color circle 150 150 50 ; Draw a circle at center, radius 50 ] ] ]
This will render a red circle in the center of a box—no more invisible drawings!
Adding Conditional Logic
Now let’s add conditions to control what gets drawn. We’ll use state variables and REBOL’s native conditional expressions (like either, if, or case) inside the draw block. We’ll also add buttons to toggle conditions and refresh the canvas.
; Define our state variables (these control the drawing logic) draw-circle?: true current-color: red view layout [ ; Our drawing canvas box 300x300 "Conditional Canvas" [ draw [ pen current-color ; Use `either` to switch between circle and square either draw-circle? [ circle 150 150 50 ] [ square 100 100 100 ; Square at (100,100), size 100 ] ; Add extra conditional drawing with `if` if current-color = red [ pen white text 120 120 "Red Mode!" ] ] ] ; Buttons to modify state and trigger refresh button "Toggle Shape" [ draw-circle?: not draw-circle? ; Critical: Refresh the box face to update the drawing show find face 'box ] button "Random Color" [ current-color: random [blue green orange purple] show find face 'box ] ]
Key Notes for Conditional Drawing
- Attach draw to a face: Always put your
drawblock inside a component likeboxorimage—this gives the drawing a surface to render on. - Use state variables: Store conditions (like
draw-circle?orcurrent-color) in variables that you can modify based on your needs (user input, external data, timers, etc.). - Refresh with
show: When you change state variables, callshow find face 'box(or whatever your canvas face is named) to tell VID to redraw the component. Without this, the drawing won’t update. - Embed conditionals directly: REBOL lets you use its full expression syntax inside
drawblocks, so you can useif,either,case, or even function calls to generate dynamic drawing commands.
Advanced: Drawing Based on Events
You can also tie drawing to events (like mouse movement) for interactive conditional drawing:
mouse-position: 150x150 view layout [ box 300x300 "Mouse-Follow Canvas" [ draw [ pen blue circle mouse-position/x mouse-position/y 30 pen yellow ; Draw crosshairs at mouse position line 0 mouse-position/y 300 mouse-position/y line mouse-position/x 0 mouse-position/x 300 ] ] on-action [ ; Update mouse position when the mouse moves if event-type? 'move [ mouse-position: event-pos show find face 'box ] ] ]
This will draw a circle and crosshairs that follow your mouse cursor inside the box.
内容的提问来源于stack exchange,提问作者Alexander Ryan Baggett

