RedcrabX – DrawBox Guide

Pixel-canvas drawing on the worksheet  |  Version 1.0
Available in RedcrabX from version 10.2.0

Back to Quick Start Guide

Table of Contents


1. Overview

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.

PropertyDefaultDescription
Default size320 × 240 pxWidth and height of a freshly inserted DrawBox
Minimum size200 × 160 pxThe box cannot be made smaller than this
BackgroundWhiteCanvas background is always white
Coordinate originTop-left (0, 0)X increases rightward, Y increases downward
Default box nameD1Editable in the status toolbar name field

2. Inserting a DrawBox

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.

Tip: Place the DrawBox on the worksheet before the MathBox cells that draw on it, so the target is visible when expressions are evaluated.

3. Box Name and Target Selection

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.

StyleExampleWhen 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
Note: Target name lookup is case-insensitive. d1 and D1 refer to the same DrawBox.

4. Draw Functions

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.

4.1   drawtarget

Sets the active DrawBox target. Subsequent drawrect and drawcircle calls without an explicit target name will draw on this box.

ParameterTypeDescription
nameidentifierName of the DrawBox as shown in its toolbar (e.g. D1)

Syntax

drawtarget(D1)

Examples

// 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)
Note: The current target is workspace-global and persists until drawtarget is called again with a different name.

4.2   drawrect

Draws a rectangle (unfilled, black border) on the active or specified DrawBox.

ParameterUnitDescription
xpxLeft edge of the rectangle (horizontal pixel coordinate)
ypxTop edge of the rectangle (vertical pixel coordinate)
hpxHeight of the rectangle in pixels
wpxWidth of the rectangle in pixels

Syntax

// Implicit target (drawtarget must be called first)
drawrect(x, y, h, w)

// Explicit target as first argument
drawrect(D1, x, y, h, w)

Examples

// 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)
Tip: Negative h or w values flip the rectangle. The drawing is always normalized so that the top-left corner has the smaller coordinate.

4.3   drawcircle

Draws a circle (unfilled, black border) by center point and radius on the active or specified DrawBox.

ParameterUnitDescription
xpxHorizontal pixel coordinate of the circle center
ypxVertical pixel coordinate of the circle center
rpxRadius in pixels (negative values are treated as positive)

Syntax

// Implicit target (drawtarget must be called first)
drawcircle(x, y, r)

// Explicit target as first argument
drawcircle(D1, x, y, r)

Examples

// 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)
Note: A radius of exactly 0 is silently ignored (nothing is drawn).

4.4   drawline

Draws a line (straight, 1 px black stroke) between two points on the active or specified DrawBox.

ParameterUnitDescription
x1pxHorizontal pixel coordinate of the first endpoint
y1pxVertical pixel coordinate of the first endpoint
x2pxHorizontal pixel coordinate of the second endpoint
y2pxVertical pixel coordinate of the second endpoint

Syntax

// Implicit target (drawtarget must be called first)
drawline(x1, y1, x2, y2)

// Explicit target as first argument
drawline(D1, x1, y1, x2, y2)

Examples

// 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)

4.5   drawarrow

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.

ParameterUnitDescription
x1pxHorizontal coordinate of the arrow start
y1pxVertical coordinate of the arrow start
x2pxHorizontal coordinate of the arrow tip
y2pxVertical coordinate of the arrow tip
Arrowhead: 10 px long, 8 px wide, filled solid black. The shaft is a 1 px black line. If start and end point coincide (length < 0.5 px) only the shaft is drawn.

Syntax

// Implicit target
drawarrow(x1, y1, x2, y2)

// Explicit target
drawarrow(D1, x1, y1, x2, y2)

Examples

// 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)

4.6   drawdot

Draws a filled circle (solid black disk) at the given center point. Useful for marking joints, origins, and notable points in technical sketches.

ParameterUnitDescription
xpxHorizontal pixel coordinate of the dot center
ypxVertical pixel coordinate of the dot center
rpxRadius in pixels (negative values are treated as positive)
Note: A radius of exactly 0 is silently ignored. Unlike drawcircle, the dot is drawn filled (solid black).

Syntax

// Implicit target
drawdot(x, y, r)

// Explicit target
drawdot(D1, x, y, r)

Examples

// 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)

4.7   drawellipse

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.

ParameterUnitDescription
xpxHorizontal pixel coordinate of the ellipse center
ypxVertical pixel coordinate of the ellipse center
rxpxHorizontal half-axis (half-width). Negative values are treated as positive.
rypxVertical half-axis (half-height). Negative values are treated as positive.

Syntax

// Implicit target
drawellipse(x, y, rx, ry)

// Explicit target
drawellipse(D1, x, y, rx, ry)

Examples

// 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)

4.8   drawarc

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).

ParameterUnitDescription
xpxHorizontal pixel coordinate of the arc center
ypxVertical pixel coordinate of the arc center
rpxRadius in pixels
a1deg / radStart angle. 0° points right (east); angles increase clockwise.
a2deg / radEnd angle (clockwise from a1).
Coordinate convention: The canvas Y-axis points downward, so 0° = right, 90° = down, 180° = left, 270° = up. A span of exactly 360° draws a full circle. If the radius is less than 0.5 px, nothing is drawn.

Syntax

// Implicit target
drawarc(x, y, r, a1, a2)

// Explicit target
drawarc(D1, x, y, r, a1, a2)

Examples

// 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)

4.9   drawtext

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.

ParameterTypeDescription
xpxHorizontal pixel coordinate of the top-left corner of the text
ypxVertical pixel coordinate of the top-left corner of the text
"text"stringA string literal in double quotes. Escape \" for an embedded quote.
Note: The text argument must be a quoted string literal, not a variable or expression. Use "cm", "120 mm", etc.

Syntax

// Implicit target
drawtext(x, y, "label")

// Explicit target
drawtext(D1, x, y, "label")

Examples

// 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")

4.10   drawclear

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.

ParameterTypeDescription
No parameters — clears the current target
drawclear()           // clear current target
drawclear(D1)         // clear box D1 explicitly

Examples

drawtarget(D1)
drawgrid(20)
drawcircle(100, 80, 40)
// later, wipe everything and redraw:
drawclear()
drawrect(10, 10, 60, 80)

4.11   drawcross

Draws a crosshair marker (two perpendicular lines) centered at (x, y). Useful for marking origins, center points, or joints.

ParameterTypeDescription
xnumberX center [px]
ynumberY center [px]
sizenumberTotal arm length [px]
drawcross(x, y, size)
drawcross(D1, x, y, size)

Examples

drawtarget(D1)
drawcross(100, 80, 20)    // 20 px cross at (100,80)
drawcross(160, 40, 10)    // small marker at corner

4.12   drawdim

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.

ParameterTypeDescription
x1, y1numberStart point of the measured edge [px]
x2, y2numberEnd point of the measured edge [px]
offsetnumberPerpendicular offset [px]. Positive = CCW-perpendicular direction (below for horizontal lines).
drawdim(x1, y1, x2, y2, offset)
drawdim(D1, x1, y1, x2, y2, offset)

Examples

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")

4.13   drawpoly

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.

ParameterTypeDescription
xn, ynnumber pairsVertex coordinates [px], at least 3 pairs
drawpoly(x1,y1, x2,y2, x3,y3, ...)
drawpoly(D1, x1,y1, x2,y2, x3,y3, ...)

Examples

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))

4.14   drawgrid

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.

ParameterTypeDescription
spacingnumberGrid interval in both directions [px] (> 0)
sx, synumbersIndependent horizontal / vertical spacing [px]
drawgrid(spacing)         // square grid
drawgrid(sx, sy)          // rectangular grid
drawgrid(D1, spacing)     // explicit target

Examples

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.

Basic shapes on one target

// MathBox — select target once, then draw
drawtarget(D1)
drawrect(10, 10, 60, 100)
drawcircle(60, 40, 30)
drawline(10, 10, 100, 40)

Multi-target workflow

// 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)

Computed geometry

// Scale a shape from a variable
s := 80
drawrect(10, 10, s, s)
drawcircle(10 + s/2, 10 + s/2, s/2)

Annotated technical sketch (dimensioning)

// 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)

6. Refresh and Canvas Clearing

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.

Tip: Because the canvas is reset on every refresh, draw commands in MathBox cells are re-evaluated from scratch. There is no need to manually clear shapes between edits.

7. Error Messages

Error messageCauseFix
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).

8. Tips


RedcrabX DrawBox Guide  —  further topics: Quick Start Guide  |  ProgramBox Guide  |  MathBox Guide  |  Geometry Guide  |  Chart Guide  |  ImageBox Guide