Pixel-canvas drawing on the worksheet | Version 1.0
Available in RedcrabX from version 10.2.0
The DrawBox provides a blank pixel canvas in the worksheet. MathBox cells can draw geometric shapes on it using side-effect draw commands. Draw commands do not produce a numeric result — they act purely as drawing instructions.
| Property | Default | Description |
|---|---|---|
| Default size | 320 × 240 px | Width and height of a freshly inserted DrawBox |
| Minimum size | 200 × 160 px | The box cannot be made smaller than this |
| Background | White | Canvas background is always white |
| Coordinate origin | Top-left (0, 0) | X increases rightward, Y increases downward |
| Default box name | D1 | Editable in the status toolbar name field |
Use the Insert menu on the main ribbon to add a DrawBox to the worksheet. The new box appears on the canvas and can be moved and resized like any other workspace box.
Every DrawBox has a name shown in the status toolbar at the bottom of the box.
The default name is D1. You can change it by clicking the name field and typing a new name.
Draw commands reference a DrawBox by its name. You can either set a persistent current target
with drawtarget and then omit the name from each draw call, or pass the target name
explicitly as the first argument of any draw function.
| Style | Example | When to use |
|---|---|---|
| Implicit target | drawtarget(D1)drawrect(10, 20, 80, 120) |
Multiple draw calls on the same box |
| Explicit target | drawrect(D1, 10, 20, 80, 120) |
Single call or drawing on different boxes |
d1 and D1
refer to the same DrawBox.
All draw functions are side-effect commands. They do not display a numeric result in the MathBox;
they act purely as drawing instructions. The available functions are:
drawtarget, drawrect, drawcircle, drawline,
drawarrow, drawdot, drawellipse, drawarc,
drawtext, drawclear, drawcross, drawdim,
drawpoly, and drawgrid.
Sets the active DrawBox target. Subsequent drawrect and
drawcircle calls without an explicit target name will draw on this box.
| Parameter | Type | Description |
|---|---|---|
name | identifier | Name of the DrawBox as shown in its toolbar (e.g. D1) |
drawtarget(D1)
// Select D1 as the current drawing target drawtarget(D1) // All subsequent draw calls without an explicit name go to D1 drawrect(10, 20, 80, 120) drawcircle(120, 120, 40)
drawtarget is called again with a different name.
Draws a rectangle (unfilled, black border) on the active or specified DrawBox.
| Parameter | Unit | Description |
|---|---|---|
x | px | Left edge of the rectangle (horizontal pixel coordinate) |
y | px | Top edge of the rectangle (vertical pixel coordinate) |
h | px | Height of the rectangle in pixels |
w | px | Width of the rectangle in pixels |
// Implicit target (drawtarget must be called first) drawrect(x, y, h, w) // Explicit target as first argument drawrect(D1, x, y, h, w)
// Draw a 120 × 80 px rectangle at (10, 20) on the current target drawtarget(D1) drawrect(10, 20, 80, 120) // Draw directly on D2 without changing the global target drawrect(D2, 50, 50, 60, 100) // Negative dimensions are accepted: the rectangle is flipped automatically drawrect(200, 150, -40, -60) // same as drawrect(140, 110, 40, 60)
h or w values flip the rectangle.
The drawing is always normalized so that the top-left corner has the smaller coordinate.
Draws a circle (unfilled, black border) by center point and radius on the active or specified DrawBox.
| Parameter | Unit | Description |
|---|---|---|
x | px | Horizontal pixel coordinate of the circle center |
y | px | Vertical pixel coordinate of the circle center |
r | px | Radius in pixels (negative values are treated as positive) |
// Implicit target (drawtarget must be called first) drawcircle(x, y, r) // Explicit target as first argument drawcircle(D1, x, y, r)
// Draw a circle centered at (120, 120) with radius 40 px on the current target drawtarget(D1) drawcircle(120, 120, 40) // Draw directly on D2 without changing the global target drawcircle(D2, 80, 80, 30) // Use a variable for the radius r := 50 drawcircle(160, 100, r)
0 is silently ignored (nothing is drawn).
Draws a line (straight, 1 px black stroke) between two points on the active or specified DrawBox.
| Parameter | Unit | Description |
|---|---|---|
x1 | px | Horizontal pixel coordinate of the first endpoint |
y1 | px | Vertical pixel coordinate of the first endpoint |
x2 | px | Horizontal pixel coordinate of the second endpoint |
y2 | px | Vertical pixel coordinate of the second endpoint |
// Implicit target (drawtarget must be called first) drawline(x1, y1, x2, y2) // Explicit target as first argument drawline(D1, x1, y1, x2, y2)
// Draw a line from (10, 10) to (200, 100) on the current target drawtarget(D1) drawline(10, 10, 200, 100) // Draw multiple lines to form a triangle scaffold drawline(10, 10, 100, 10) drawline(100, 10, 55, 90) drawline(55, 90, 10, 10) // Draw directly on D2 without changing the global target drawline(D2, 0, 0, 150, 150) // Use variables for dynamic line endpoints x := 80 y := 60 drawline(x, y, x + 60, y + 80)
Draws a dimensioning arrow: a straight line from (x1, y1) to
(x2, y2) with a small filled arrowhead at the end point.
Typically used to annotate measurements on technical drawings.
| Parameter | Unit | Description |
|---|---|---|
x1 | px | Horizontal coordinate of the arrow start |
y1 | px | Vertical coordinate of the arrow start |
x2 | px | Horizontal coordinate of the arrow tip |
y2 | px | Vertical coordinate of the arrow tip |
// Implicit target drawarrow(x1, y1, x2, y2) // Explicit target drawarrow(D1, x1, y1, x2, y2)
// Horizontal dimension arrow on D1 drawtarget(D1) drawarrow(40, 135, 160, 135) // Vertical dimension arrow pointing upward drawarrow(180, 160, 180, 40) // Use on a second box without changing the global target drawarrow(D2, 10, 50, 190, 50)
Draws a filled circle (solid black disk) at the given center point. Useful for marking joints, origins, and notable points in technical sketches.
| Parameter | Unit | Description |
|---|---|---|
x | px | Horizontal pixel coordinate of the dot center |
y | px | Vertical pixel coordinate of the dot center |
r | px | Radius in pixels (negative values are treated as positive) |
0 is silently ignored. Unlike
drawcircle, the dot is drawn filled (solid black).
// Implicit target drawdot(x, y, r) // Explicit target drawdot(D1, x, y, r)
// Mark the corner of a rectangle drawtarget(D1) drawrect(40, 40, 80, 120) drawdot(40, 40, 4) // top-left corner drawdot(160, 40, 4) // top-right corner // Mark a midpoint computed from variables x := 100 y := 60 drawdot(x, y, 3)
Draws an unfilled ellipse (black stroke) by center point and the two
half-axes. A circle is the special case where rx = ry; use
drawcircle for that.
| Parameter | Unit | Description |
|---|---|---|
x | px | Horizontal pixel coordinate of the ellipse center |
y | px | Vertical pixel coordinate of the ellipse center |
rx | px | Horizontal half-axis (half-width). Negative values are treated as positive. |
ry | px | Vertical half-axis (half-height). Negative values are treated as positive. |
// Implicit target drawellipse(x, y, rx, ry) // Explicit target drawellipse(D1, x, y, rx, ry)
// Flat ellipse 60 × 30 px centered at (120, 100) drawtarget(D1) drawellipse(120, 100, 60, 30) // Tall ellipse for an isometric projection drawellipse(160, 80, 20, 50) // Ellipse on D2 without changing the global target drawellipse(D2, 80, 60, 40, 25)
Draws a circular arc by center, radius, and two angles.
The arc is drawn clockwise from a1 to a2.
Angles follow the toolbar setting (degrees / radians).
| Parameter | Unit | Description |
|---|---|---|
x | px | Horizontal pixel coordinate of the arc center |
y | px | Vertical pixel coordinate of the arc center |
r | px | Radius in pixels |
a1 | deg / rad | Start angle. 0° points right (east); angles increase clockwise. |
a2 | deg / rad | End angle (clockwise from a1). |
// Implicit target drawarc(x, y, r, a1, a2) // Explicit target drawarc(D1, x, y, r, a1, a2)
// Quarter-circle arc (0° to 90°, i.e. right → down) at corner (40, 40) drawtarget(D1) drawarc(40, 40, 20, 0, 90) // Semicircle (top half of a circle, 180° to 360°) drawarc(120, 100, 50, 180, 360) // Full circle via drawarc (same as drawcircle) drawarc(100, 100, 40, 0, 360) // Arc on D2 without changing the global target drawarc(D2, 80, 80, 30, 45, 315)
Places a text label on the canvas at pixel position (x, y).
The text is rendered in 11 pt black using the default UI font.
Typically used for dimension labels and annotations.
| Parameter | Type | Description |
|---|---|---|
x | px | Horizontal pixel coordinate of the top-left corner of the text |
y | px | Vertical pixel coordinate of the top-left corner of the text |
"text" | string | A string literal in double quotes. Escape \" for an embedded quote. |
"cm", "120 mm", etc.
// Implicit target drawtext(x, y, "label") // Explicit target drawtext(D1, x, y, "label")
// Dimension label below a rectangle drawtarget(D1) drawrect(40, 40, 80, 120) drawarrow(40, 135, 160, 135) drawtext(90, 148, "120 mm") // Axis labels drawtext(5, 5, "Y") drawtext(285, 115, "X") // Annotate a point drawdot(100, 80, 4) drawtext(106, 74, "P")
Removes all shapes from the DrawBox canvas. Useful when you want to repaint the canvas from a button or script without a full worksheet refresh.
| Parameter | Type | Description |
|---|---|---|
| No parameters — clears the current target | ||
drawclear() // clear current target drawclear(D1) // clear box D1 explicitly
drawtarget(D1) drawgrid(20) drawcircle(100, 80, 40) // later, wipe everything and redraw: drawclear() drawrect(10, 10, 60, 80)
Draws a crosshair marker (two perpendicular lines) centered at (x, y).
Useful for marking origins, center points, or joints.
| Parameter | Type | Description |
|---|---|---|
x | number | X center [px] |
y | number | Y center [px] |
size | number | Total arm length [px] |
drawcross(x, y, size) drawcross(D1, x, y, size)
drawtarget(D1) drawcross(100, 80, 20) // 20 px cross at (100,80) drawcross(160, 40, 10) // small marker at corner
Draws a complete dimension line: a double-headed arrow parallel to the measured edge, offset
perpendicularly, with extension lines from both endpoints. Replaces the manual
drawarrow + drawarrow + drawtext pattern
for annotation.
| Parameter | Type | Description |
|---|---|---|
x1, y1 | number | Start point of the measured edge [px] |
x2, y2 | number | End point of the measured edge [px] |
offset | number | Perpendicular offset [px]. Positive = CCW-perpendicular direction (below for horizontal lines). |
drawdim(x1, y1, x2, y2, offset) drawdim(D1, x1, y1, x2, y2, offset)
drawtarget(D1) drawrect(40, 40, 80, 120) // Horizontal dimension below drawdim(40, 120, 160, 120, 20) drawtext(88, 148, "120 mm") // Vertical dimension to the right drawdim(160, 40, 160, 120, 20) drawtext(184, 76, "80 mm")
Draws a closed polygon from a flat list of coordinate pairs
(x1,y1, x2,y2, x3,y3, …). At least three pairs are required.
The last point is automatically connected back to the first.
| Parameter | Type | Description |
|---|---|---|
xn, yn | number pairs | Vertex coordinates [px], at least 3 pairs |
drawpoly(x1,y1, x2,y2, x3,y3, ...) drawpoly(D1, x1,y1, x2,y2, x3,y3, ...)
drawtarget(D1)
// Triangle
drawpoly(50, 10, 100, 90, 10, 90)
// Hexagon (regular, radius 50 around center 120,100)
r := 50 ; cx := 120 ; cy := 100
drawpoly(cx+r,cy, cx+r/2,cy-r*sin(60), cx-r/2,cy-r*sin(60),
cx-r,cy, cx-r/2,cy+r*sin(60), cx+r/2,cy+r*sin(60))
Draws a light gray background grid across the entire canvas. Grid lines are placed at regular intervals and rendered behind all other shapes. Call before other draw functions.
| Parameter | Type | Description |
|---|---|---|
spacing | number | Grid interval in both directions [px] (> 0) |
sx, sy | numbers | Independent horizontal / vertical spacing [px] |
drawgrid(spacing) // square grid drawgrid(sx, sy) // rectangular grid drawgrid(D1, spacing) // explicit target
drawtarget(D1) drawgrid(20) // 20 px square grid drawcircle(100, 80, 40) // Grid with different x/y spacing drawgrid(40, 30)
The following examples show how the draw functions work together, from a simple shape to a fully annotated technical sketch.
// MathBox — select target once, then draw drawtarget(D1) drawrect(10, 10, 60, 100) drawcircle(60, 40, 30) drawline(10, 10, 100, 40)
// MathBox 1 — draw on D1 drawtarget(D1) drawrect(5, 5, 80, 120) // MathBox 2 — draw on a second box without changing the global target drawcircle(D2, 100, 100, 50) drawrect(D2, 20, 20, 40, 60)
// Scale a shape from a variable s := 80 drawrect(10, 10, s, s) drawcircle(10 + s/2, 10 + s/2, s/2)
// Draw a rectangle and annotate its width with a dimension arrow and label drawtarget(D1) // Shape drawrect(40, 40, 80, 120) // Corner dots drawdot(40, 40, 3) drawdot(160, 40, 3) // Angle arc at top-left corner drawarc(40, 40, 18, 0, 90) // Horizontal dimension line (offset below the rectangle) drawarrow(40, 140, 160, 140) // arrow right drawarrow(160, 140, 40, 140) // arrow left (double arrow) drawtext(88, 148, "120 mm") // Vertical dimension line (offset to the right) drawarrow(175, 40, 175, 120) // arrow down drawarrow(175, 120, 175, 40) // arrow up drawtext(180, 76, "80 mm") // Ellipse decoration drawellipse(100, 80, 15, 8)
Every time the worksheet is recalculated (e.g. after editing a MathBox or pressing Refresh), all DrawBox canvases are automatically cleared before the draw commands are re-executed. This ensures the canvas always reflects the current state of the expressions without accumulating stale shapes from previous runs.
| Error message | Cause | Fix |
|---|---|---|
drawtarget: target name must not be empty. |
An empty string was passed to drawtarget. |
Provide a valid DrawBox name, e.g. drawtarget(D1). |
drawrect: no active target. Use drawtarget(D1) first or call drawrect(D1,x,y,h,w). |
drawrect was called without arguments and no target is set. |
Call drawtarget(D1) before drawrect, or use the explicit form. |
drawrect: DrawBox 'D1' was not found. |
No DrawBox with the given name exists on the worksheet. | Check the name in the DrawBox toolbar and correct the function call. |
drawrect: parameters must be finite numbers. |
One of the numeric arguments is NaN or infinite. |
Ensure all coordinate and size expressions produce valid finite values. |
drawcircle: no active target. Use drawtarget(D1) first or call drawcircle(D1,x,y,r). |
drawcircle was called without arguments and no target is set. |
Call drawtarget(D1) before drawcircle, or use the explicit form. |
drawcircle: DrawBox 'D1' was not found. |
No DrawBox with the given name exists on the worksheet. | Check the name in the DrawBox toolbar and correct the function call. |
drawcircle: parameters must be finite numbers. |
One of the numeric arguments is NaN or infinite. |
Ensure all coordinate and radius expressions produce valid finite values. |
drawline: no active target. Use drawtarget(D1) first or call drawline(D1,x1,y1,x2,y2). |
drawline was called without arguments and no target is set. |
Call drawtarget(D1) before drawline, or use the explicit form. |
drawline: DrawBox 'D1' was not found. |
No DrawBox with the given name exists on the worksheet. | Check the name in the DrawBox toolbar and correct the function call. |
drawline: parameters must be finite numbers. |
One of the numeric arguments is NaN or infinite. |
Ensure all coordinate and endpoint expressions produce valid finite values. |
drawarrow: no active target. Use drawtarget(D1) first or call drawarrow(D1,x1,y1,x2,y2). |
drawarrow was called without a target set. |
Call drawtarget(D1) first, or pass the box name explicitly. |
drawarrow: DrawBox 'D1' was not found. |
No DrawBox with the given name exists on the worksheet. | Check the name in the DrawBox toolbar and correct the function call. |
drawdot: no active target. Use drawtarget(D1) first or call drawdot(D1,x,y,r). |
drawdot was called without a target set. |
Call drawtarget(D1) first, or pass the box name explicitly. |
drawellipse: no active target. Use drawtarget(D1) first or call drawellipse(D1,x,y,rx,ry). |
drawellipse was called without a target set. |
Call drawtarget(D1) first, or pass the box name explicitly. |
drawarc: no active target. Use drawtarget(D1) first or call drawarc(D1,x,y,r,a1,a2). |
drawarc was called without a target set. |
Call drawtarget(D1) first, or pass the box name explicitly. |
drawarc: parameters must be finite numbers. |
One of the numeric arguments is NaN or infinite. |
Ensure center, radius, and angle expressions produce valid finite values. |
drawtext: no active target. Use drawtarget(D1) first or call drawtext(D1,x,y,"text"). |
drawtext was called without a target set. |
Call drawtarget(D1) first, or pass the box name explicitly. |
drawtext: expected a string literal (e.g. "Hello"). |
The text argument is missing or not a quoted string. | Pass the text as a string literal: drawtext(10, 20, "label"). |
drawtext: unterminated string literal. |
The closing quote of the text argument is missing. | Ensure the string is closed: "120 mm". |
drawclear: no active target. Use drawtarget(D1) first or call drawclear(D1). |
drawclear was called without a target set. |
Call drawtarget(D1) first, or pass the box name: drawclear(D1). |
drawcross: no active target. Use drawtarget(D1) first or call drawcross(D1,x,y,size). |
drawcross was called without a target set. |
Call drawtarget(D1) first, or pass the box name explicitly. |
drawdim: no active target. Use drawtarget(D1) first or call drawdim(D1,x1,y1,x2,y2,offset). |
drawdim was called without a target set. |
Call drawtarget(D1) first, or pass the box name explicitly. |
drawdim: parameters must be finite numbers. |
One of the numeric arguments is NaN or infinite. |
Ensure all coordinate and offset expressions produce valid finite values. |
drawpoly: no active target. Use drawtarget(D1) first or call drawpoly(D1,x1,y1,...). |
drawpoly was called without a target set. |
Call drawtarget(D1) first, or pass the box name explicitly. |
drawpoly: requires at least 3 coordinate pairs (6 numbers, alternating x y). |
Fewer than 6 numbers, an odd count, or no arguments were supplied. | Provide at least 3 vertex pairs, e.g. drawpoly(0,0,100,0,50,80). |
drawgrid: no active target. Use drawtarget(D1) first or call drawgrid(D1,spacing). |
drawgrid was called without a target set. |
Call drawtarget(D1) first, or pass the box name explicitly. |
drawgrid: spacing values must be positive finite numbers. |
A zero, negative, or non-finite spacing was supplied. | Use a positive value, e.g. drawgrid(20). |
D1, D2) and address them with the
explicit-target form or by switching targets with drawtarget.
drawdot draws a solid filled disk.
All other shapes (drawrect, drawcircle, drawellipse,
drawarc) are unfilled with a 1 px black border. Colored or custom-fill
shapes are not yet supported.
drawarc uses the toolbar angle unit (degrees or radians).
Angles are measured clockwise from east (right), matching screen convention (Y-axis points down).
A span of 360° draws a complete circle.
(x, y) position in
drawtext is the top-left corner of the text block.
Add an offset to center a label over an arrow or shape.
(0, 0) is the top-left corner.
Y increases downward, which is the standard screen convention.
drawdim draws the full double-headed dimension line
in one call. Use a positive offset to place it below a horizontal edge
or to the right of a vertical one. Combine with drawtext to add the measurement label.
drawgrid first so it is rendered behind all
other shapes. The grid extends to the full canvas size and uses a light Gainsboro color.
drawpoly accepts any number of vertex pairs
and automatically closes the polygon. Use loop expressions or precomputed arrays for
regular polygons and stars.
RedcrabX DrawBox Guide — further topics: Quick Start Guide | ProgramBox Guide | MathBox Guide | Geometry Guide | Chart Guide | ImageBox Guide