Skip to content

Canvas

A Canvas is a leaf element whose content box its painter draws, cell by cell, for pictures, plots, game boards and anything else that isn't text. A Text of chunks can draw the same cells, but layout measures and wraps a Text every frame, which a picture never needs. A canvas skips all of that: its painter says how big it is, and draws whatever box it ends up with.

This page covers drawing pixels, how a canvas is sized, loading an image, mouse input, and writing a painter of your own.

Drawing Pixels

counterweight.painters splits a picture from how it's drawn:

  • Pixels is the picture: a width, a height, and RGBA bytes, with nothing about cells or terminals. Pixels.blank, Pixels.from_points and with_points build one from colors, and the constructor takes raw RGBA bytes from any decoder.
  • HalfBlocks is a painter that draws a Pixels in cells, two pixels to a cell, one above the other, as ▀ with the top pixel as its foreground and the bottom one as its background. A cell is about twice as tall as it is wide, so the pixels come out about square.
SKY_TOP = Color.from_hex("#1e1b4b")
SKY_BOTTOM = Color.from_hex("#f97316")
SUN = Color.from_hex("#fde047")
SEA = Color.from_hex("#0e7490")


def blend(a: Color, b: Color, t: float) -> Color:
    return Color(*(round(x + (y - x) * t) for x, y in zip(a, b, strict=True)))


def sunset(width: int, height: int) -> Pixels:
    """A sky that brightens toward the horizon, a sun setting on it, and the sea below."""
    horizon = height * 3 // 4
    radius = width / 4

    def color(x: int, y: int) -> Color:
        if y >= horizon:
            return SEA
        if (x + 0.5 - width / 2) ** 2 + (y + 0.5 - horizon) ** 2 < radius**2:
            return SUN
        return blend(SKY_TOP, SKY_BOTTOM, y / horizon)

    rgba = b"".join(bytes((*color(x, y), 255)) for y in range(height) for x in range(width))
    return Pixels(width, height, rgba)


SUNSET = sunset(16, 16)


@component
def picture() -> Canvas:
    return Canvas(painter=HalfBlocks(SUNSET), style=border)

A sunset drawn with half blocks

┌──────────────────┐
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│
└──────────────────┘

A canvas compares its painter with == to the one it had last frame, and doesn't draw again while it's equal and its box is the same size. Two HalfBlocks are equal when their pixels' bytes, fit and position are, so a component that holds a Pixels in state can build its HalfBlocks in every render, and the canvas is drawn again only when the pixels or its box change. Changing a picture means making a new Pixels, as with_points does.

Sizing

A canvas is sized as HTML's replaced elements (<img>, <canvas>) are. Its painter's natural size is its intrinsic size, which it takes on each axis that its style and its parent's layout leave undetermined, as an <img> takes its picture's. A HalfBlocks is as wide as its picture and half as tall, rounded up.

def labeled(label: str, canvas: Canvas) -> Div:
    return Div(style=col | align_children_start, children=(Text(content=label), canvas))


@component
def sizing() -> Div:
    return Div(
        style=row | gap(2) | align_children_start,
        children=(
            labeled("natural size", Canvas(painter=HalfBlocks(SUNSET), style=border)),
            labeled("width(10)", Canvas(painter=HalfBlocks(SUNSET), style=border | width(10))),
            labeled("height(4)", Canvas(painter=HalfBlocks(SUNSET), style=border | height(4))),
            Div(
                style=col | width(12),
                children=(
                    Text(content="min_width(0)"),
                    Div(
                        style=row,
                        children=(Canvas(painter=HalfBlocks(SUNSET), style=border | min_width(0)),),
                    ),
                ),
            ),
        ),
    )

Canvases at their natural size, with a width, with a height, and shrunk

natural size        width(10)   height(4)  min_width(0)           
┌────────────────┐  ┌────────┐  ┌────┐     ┌──────────┐           
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  │▀▀▀▀▀▀▀▀│  │▀▀▀▀│     │▀▀▀▀▀▀▀▀▀▀│           
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  │▀▀▀▀▀▀▀▀│  │▀▀▀▀│     │▀▀▀▀▀▀▀▀▀▀│           
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  │▀▀▀▀▀▀▀▀│  └────┘     │▀▀▀▀▀▀▀▀▀▀│           
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  │▀▀▀▀▀▀▀▀│             │▀▀▀▀▀▀▀▀▀▀│           
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  └────────┘             │▀▀▀▀▀▀▀▀▀▀│           
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│                         └──────────┘           
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│                                                
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│                                                
└────────────────┘                                                
  • A size from the style, or from a parent that stretches or flexes it, wins on its axis, and the painter draws at the size the canvas's content box ends up.
  • A painter that keeps its aspect ratio, as HalfBlocks does, gives a canvas sized on only one axis its other axis by that ratio, so width(10) or height(4) alone keeps the picture's proportions. The ratio is between content boxes, so a border or padding doesn't distort it.
  • In a flex row, a canvas shrinks no narrower than its intrinsic width, as an <img> doesn't, so a canvas wider than its row overflows it and draws over what's beside it. min_width(0) lets it shrink, as min-width: 0 does for an <img>.
  • A max_width on a canvas in a column that doesn't stretch its children, such as one with align_children_start, narrows it without shortening it. Set a width instead, or let the column stretch it.

Fitting a Picture to Its Box

When the box isn't the picture's natural size, HalfBlocks's fit scales the picture as CSS's object-fit does, and position places it in the space left over as object-position does. fit is on the painter rather than the canvas's style, because only a painter with pixels has a picture to scale.

FITS: tuple[Fit, ...] = ("fill", "contain", "cover", "none", "scale-down")


@component
def fits() -> Div:
    return Div(
        style=row | gap(1),
        children=tuple(
            labeled(f'fit="{fit}"', Canvas(painter=HalfBlocks(SUNSET, fit=fit), style=border | size(22, 6)))
            for fit in FITS
        ),
    )

The same picture with each fit

fit="fill"             fit="contain"          fit="cover"            fit="none"             fit="scale-down"          
┌────────────────────┐ ┌────────────────────┐ ┌────────────────────┐ ┌────────────────────┐ ┌────────────────────┐    
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│ │      ▀▀▀▀▀▀▀▀      │ │▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│ │  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀  │ │      ▀▀▀▀▀▀▀▀      │    
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│ │      ▀▀▀▀▀▀▀▀      │ │▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│ │  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀  │ │      ▀▀▀▀▀▀▀▀      │    
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│ │      ▀▀▀▀▀▀▀▀      │ │▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│ │  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀  │ │      ▀▀▀▀▀▀▀▀      │    
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│ │      ▀▀▀▀▀▀▀▀      │ │▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│ │  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀  │ │      ▀▀▀▀▀▀▀▀      │    
└────────────────────┘ └────────────────────┘ └────────────────────┘ └────────────────────┘ └────────────────────┘    
  • "fill", the default, stretches the picture to the box on each axis.
  • "contain" scales it to fit inside the box, keeping its proportions.
  • "cover" scales it to cover the box, keeping its proportions, and cuts off what overflows.
  • "none" leaves it at its natural size, cut off where it overflows.
  • "scale-down" is whichever of "none" and "contain" is smaller.

position is the fractions of the space left over on each axis to put to the left of and above the picture, (0.5, 0.5) by default, which centers it. It places the picture in whole pixels, so a picture can start halfway down a cell.

Each cell shows the pixels nearest the middle of its two halves, which is fast but rough when shrinking a photo a long way. For a smoother picture, scale it to the size you'll show it at first, such as with Pillow's Image.thumbnail.

Transparency

A pixel less than half opaque is transparent, and so is any part of the box the picture doesn't cover. A transparent half of a cell shows the canvas's background: its text_bg if its style sets one, and its content_color otherwise, so a canvas with only a content_color shows it everywhere the picture doesn't.

Loading an Image

Pixels.from_image takes a Pillow Image, or anything else with Pillow's size, convert and tobytes. Converting costs as much as the image has pixels, so do it once: before the app starts, in an effect that sets state, or in a cached function, as here, rather than in every render.

@cache
def mandelbrot() -> Pixels:
    """Pillow's Mandelbrot set, colored, converted the first time it's drawn rather than in every render."""
    gray = Image.effect_mandelbrot((40, 32), (-2.2, -1.2, 0.8, 1.2), 100)
    return Pixels.from_image(ImageOps.colorize(gray, black="#1e1b4b", white="#fde047", mid="#f97316"))


@component
def image() -> Div:
    pixels = mandelbrot()
    return Div(
        style=row | gap(2) | align_children_start,
        children=(
            Canvas(painter=HalfBlocks(pixels), style=border | content_color("slate", 700)),
            Canvas(painter=HalfBlocks(pixels, fit="contain"), style=border | size(24, 6) | content_color("slate", 700)),
        ),
    )

Pillow's Mandelbrot set, at its natural size and contained in a smaller box

┌────────────────────────────────────────┐  ┌──────────────────────┐  
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  │      ▀▀▀▀▀▀▀▀▀▀      │  
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  │      ▀▀▀▀▀▀▀▀▀▀      │  
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  │      ▀▀▀▀▀▀▀▀▀▀      │  
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  │      ▀▀▀▀▀▀▀▀▀▀      │  
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  └──────────────────────┘  
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│                            
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│                            
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│                            
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│                            
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│                            
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│                            
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│                            
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│                            
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│                            
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│                            
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│                            
└────────────────────────────────────────┘                            

Gradients

HalfBlockGradient fills its canvas with a gradient, two pixels to a cell, drawing a new picture at whatever size the canvas is, so it shows a gradient at twice the vertical resolution of the same gradient as an element's content_color. Pixels.from_gradient makes a picture of one, for a picture that draws more on top of it with with_points. Its pixels are square, so the gradient's angles and circles are in pixels rather than in cells.

ARC = conic_gradient(rose_500, amber_300, teal_500, violet_600, rose_500)


@component
def on_a_canvas() -> Div:
    return Div(
        style=row | gap(2),
        children=(
            labeled("content_gradient", Div(style=SWATCH | content_gradient(ARC))),
            labeled("HalfBlockGradient", Canvas(painter=HalfBlockGradient(ARC), style=SWATCH)),
        ),
    )

A conic gradient as a content gradient and on a canvas

content_gradient  HalfBlockGradient 
                  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀  
                  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀  
                  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀  
                  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀  
                  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀  

A cell a painter leaves without a background shows the canvas's content_color, and a content_color gradient maps over the canvas's border box there too, so the cell matches the rest of the box.

Drawing a Screenshot

A canvas can draw what the app drew, as a browser's <canvas> draws another canvas or an image with drawImage. A Screenshot's handler receives the frame on screen, whose paint holds its cells, and as_painter makes a painter of a region of it, here the panel's box, which use_rects reports:

@component
def snapshot() -> Div:
    count, set_count = use_state(0)
    panel = use_state(ElementHandle)[0]
    where = use_rects(panel).border
    no_picture: PaintView | None = None
    taken, set_taken = use_state(no_picture)

    def keep(frame: Frame) -> None:
        # Built once, from the frame as it was, rather than in every render.
        set_taken(frame.paint.as_painter(where))

    def on_input(event: InputEvent, _: Receiver) -> InputResponse:
        match event:
            case KeyPressed(key="+"):
                set_count(lambda count: count + 1)
                return ()
            case KeyPressed(key="s"):
                return (Screenshot(handler=keep),)
            case _:
                return None

    return Div(
        style=row | gap(2),
        on_input=on_input,
        children=(
            Div(
                handle=panel,
                style=border | border_lightrounded | border_color("teal", 400) | pad_x(1),
                children=(Text(content=f"Count: {count}"),),
            ),
            Text(content="Press s", style=text_color("slate", 400)) if taken is None else Canvas(painter=taken),
        ),
    )

The panel as it is, and the picture taken of it two presses earlier

╭──────────╮  ╭──────────╮          
│ Count: 4 │  │ Count: 2 │          
╰──────────╯  ╰──────────╯          

The painter, a PaintView, holds the cells as they were when it was made, so the picture stays as it was while the app changes. Its natural size is the region's, and since cells can't scale, it cuts off whatever doesn't fit the canvas's box. A canvas compares its painter with the last one it drew, so make the painter once, as the handler does here, rather than in every render.

To change a picture cell by cell, as a transition between two screens does, take the cells with cells_in, which gives a region's cells row by row with the two halves of a wide character in cells of their own, so a cell's position in its row is its column. Rearrange them, mix in another picture's, and turn the result back into a painter's rows with rows_of, which draws a wide character's halves that end up apart as spaces, as a terminal does.

This wipe takes a picture of each card when s is pressed, then, on each frame of a use_progress, draws the second card's cells left of a cut that sweeps across and the first card's to its right:

type Picture = tuple[tuple[Cell, ...], ...]

NO_PICTURE: Picture = ()


def wipe(before: Picture, after: Picture, progress: float) -> Picture:
    """`after` from the left edge up to `progress` of the way across, and `before` from there on."""
    cut = round(ease_in_out(progress) * max((len(row) for row in before), default=0))
    return tuple((*new[:cut], *old[cut:]) for old, new in zip(before, after, strict=True))


def card(handle: ElementHandle, color: Color, lines: tuple[str, ...]) -> Div:
    return Div(
        handle=handle,
        style=col | border | border_lightrounded | border_color(color) | pad_x(1) | size(14, 5),
        children=tuple(Text(content=line, style=text_color(color)) for line in lines),
    )


@component
def transition() -> Div:
    first, second = use_state(ElementHandle)[0], use_state(ElementHandle)[0]
    before, after = use_rects(first).border, use_rects(second).border
    pictures, set_pictures = use_state((NO_PICTURE, NO_PICTURE))
    plays, set_plays = use_state(0)
    progress = use_progress(timedelta(seconds=1), deps=(plays,))

    def keep(frame: Frame) -> None:
        # The cells of each card, as they are in this frame, to mix on every frame of the wipe.
        set_pictures((frame.paint.cells_in(before), frame.paint.cells_in(after)))
        set_plays(lambda plays: plays + 1)

    def on_input(event: InputEvent, _: Receiver) -> InputResponse:
        match event:
            case KeyPressed(key="s"):
                return (Screenshot(handler=keep),)
            case KeyPressed(key=Key.Space):
                set_plays(lambda plays: plays + 1)
                return ()
            case _:
                return None

    old, new = pictures
    return Div(
        style=row | gap(2),
        on_input=on_input,
        children=(
            card(first, Color.from_hex("#2dd4bf"), ("Before", "日本語", "─" * 10)),
            card(second, Color.from_hex("#fb7185"), ("After", "ひらがな", "═" * 10)),
            Text(content="Press s", style=text_color("slate", 400))
            if old == NO_PICTURE
            else Canvas(painter=PaintView(rows=rows_of(wipe(old, new, progress)), width=before.width)),
        ),
    )

Halfway through the wipe, the third box shows the left half of After and the right half of Before

╭────────────╮  ╭────────────╮  ╭────────────╮
│ Before     │  │ After      │  │ Aftere     │
│ 日本語     │  │ ひらがな   │  │ ひらが     │
│ ────────── │  │ ══════════ │  │ ═════───── │
╰────────────╯  ╰────────────╯  ╰────────────╯

The pictures are values, taken once, so each frame only rearranges cells it already has, and the painter it builds is equal to the last one whenever the cut hasn't moved. Space plays the wipe again:

Mouse Input

A canvas takes an on_input handler as any element does, and mouse events arrive in cells, as positions on the screen. HalfBlocks.pixels_at turns a cell of a box into the pixels of the picture that cell shows, scaled and placed by fit and position, so a paint program sets the pixels under the mouse:

BLANK = Pixels.blank(24, 16, Color.from_name("black"))
INK = Color.from_hex("#fbbf24")


@component
def paint() -> Div:
    pixels, set_pixels = use_state(BLANK)
    painter = HalfBlocks(pixels)

    def on_input(event: InputEvent, receiver: Receiver) -> InputResponse:
        match event:
            case MouseDown(absolute=a, button=1) | MouseMoved(absolute=a, button=1):
                content = receiver.rects.content
                under = painter.pixels_at(receiver.relative(a), content.width, content.height)
                set_pixels(lambda p: p.with_points(dict.fromkeys(under, INK)))
                return ()
            case _:
                return None

    return Div(style=col | align_children_start, children=(Canvas(painter=painter, style=border, on_input=on_input),))

A line drawn by dragging across the canvas

┌────────────────────────┐  
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  
│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│  
└────────────────────────┘  

Writing a Painter

A painter is any object with the three members of Painter: a natural_size in cells, whether it keeps_aspect_ratio, and draw(width, height), which returns rows of StyledGraphemes for a box of that size, top to bottom, each drawn rightward from the box's left edge. Cells past the box's edges are cut off, and any part of the box the rows don't reach shows the canvas's background. Whatever a cell's style leaves unset comes from the canvas, as it does for a Text's chunks, so a painter never needs the canvas's style.

A painter that takes whatever box it gets has a natural size of zero, and is sized with its style. This ruler draws a tick in every column of its box and a label in every tenth, however wide the box is:

@dataclass(frozen=True, slots=True)
class Ruler:
    """Draws a ruler as wide as whatever box it's given: a tick on every column, and a label on every tenth."""

    natural_size: tuple[int, int] = (0, 2)
    keeps_aspect_ratio: bool = False

    def draw(self, width: int, height: int) -> Rows:
        ticks = tuple(StyledGrapheme(grapheme="┴" if x % 10 == 0 else "╵" if x % 5 == 0 else "·") for x in range(width))
        labels = "".join(f"{x:<10}" for x in range(0, width, 10))[:width]
        label_style = CellStyle(foreground=Color.from_hex("#fbbf24"))
        return ticks, tuple(StyledGrapheme(grapheme=c, style=label_style) for c in labels)


@component
def ruler() -> Div:
    return Div(style=col, children=(Canvas(painter=Ruler(), style=border),))

A ruler as wide as its box

┌──────────────────────────────────────────────────────────────┐
│┴····╵····┴····╵····┴····╵····┴····╵····┴····╵····┴····╵····┴·│
│0         10        20        30        40        50        60│
└──────────────────────────────────────────────────────────────┘

Since a canvas draws again only when its painter isn't equal to the last one, draw must depend only on the painter and the size it's given. A frozen dataclass is the easiest way to get that: two built from the same values are equal, so a component can build its painter in every render.

API

counterweight.elements.Canvas dataclass

Canvas(
    *,
    painter: Painter,
    style: Style = _DEFAULT_STYLE,
    on_input: InputHandler | None = None,
    handle: ElementHandle | None = None,
)

A leaf element whose content box its painter draws, cell by cell, instead of laying out text.

It's sized as HTML's replaced elements (<img>, <canvas>) are: its painter's natural_size is its intrinsic size, which it takes on each axis that its style and its parent's layout leave undetermined, and draw is called with the size its content box ends up. A painter that keeps its aspect ratio gives a canvas sized on only one axis the other by that ratio. In a flex row, a canvas shrinks no narrower than its intrinsic width, as an <img> doesn't, unless its style sets min_width(0).

A background that its painter leaves unset is the canvas's text_style background if its style sets one, and its content_color otherwise, so a cell the painter leaves transparent matches the rest of the box.

painter instance-attribute

painter: Painter

What draws the canvas's content box.

style class-attribute instance-attribute

style: Style = _DEFAULT_STYLE

How the canvas is laid out and drawn, and the style its painter's cells take wherever they leave theirs unset.

on_input class-attribute instance-attribute

on_input: InputHandler | None = None

The handler for the input events that reach this element, as their target: key presses and pastes while it is focused, and mouse events over the parts of it that are painted.

handle class-attribute instance-attribute

handle: ElementHandle | None = None

The element handle that names this element for controls such as ScrollTo, and for use_scroll.

children property

children: tuple[Component | AnyElement, ...]

counterweight.elements.Painter

What draws a Canvas's content box.

A painter is compared with == to the one its canvas had in the last frame, and draw isn't called again while it's equal and the box is the same size, so what draw returns must depend only on the painter and the size it's given. A frozen dataclass built from the same values in each render is equal to the last one, so a component can build its painter in every render.

natural_size property

natural_size: tuple[int, int]

The canvas's intrinsic size in cells, as (width, height), which it takes on each axis that its style and its parent's layout leave undetermined.

keeps_aspect_ratio property

keeps_aspect_ratio: bool

Whether a canvas sized on one axis takes its size on the other from the ratio of natural_size, as an <img> with only a width keeps its picture's proportions. A natural size of zero on either axis has no ratio to keep.

draw

draw(width: int, height: int) -> Rows

The rows of cells to show in a width by height content box, top to bottom, each drawn rightward from the box's left edge.

Each cell's style is resolved against the canvas's, so whatever a cell leaves unset comes from the canvas: its foreground from the text_style, and its background as the Canvas describes. Cells past the box's edges are cut off, and any part of the box the rows don't reach shows the canvas's background. It isn't called for a box with no cells.

counterweight.elements.Rows

Rows = tuple[tuple[StyledGrapheme, ...], ...]

Rows of grapheme clusters, top to bottom, each drawn left to right, as a painter's draw returns them.

counterweight.elements.StyledGrapheme dataclass

StyledGrapheme(
    *, grapheme: str, style: CellStyle = _DEFAULT_CELL_STYLE
)

One grapheme cluster of text, the style it's drawn with, and how many terminal cells it takes.

grapheme instance-attribute

grapheme: str

style class-attribute instance-attribute

style: CellStyle = _DEFAULT_CELL_STYLE

width class-attribute instance-attribute

width: int = field(init=False, compare=False, repr=False)

__post_init__

__post_init__() -> None

counterweight.painters.Pixels dataclass

Pixels(width: int, height: int, rgba: bytes)

A picture: a width by height grid of pixels, as RGBA bytes, row by row from the top left, four bytes (red, green, blue and alpha) to a pixel. It raises ValueError if the bytes aren't four for each pixel.

Two Pixels are equal when their bytes are, which a canvas compares its painter by from frame to frame, so changing a picture means making a new one, as with_points does. Build or decode one outside of rendering, such as when the app starts or in an effect that sets state, since it costs as much as the picture has pixels.

width instance-attribute

width: int

height instance-attribute

height: int

rgba class-attribute instance-attribute

rgba: bytes = field(repr=False)

__post_init__

__post_init__() -> None

blank classmethod

blank(width: int, height: int, color: Color) -> Pixels

A width by height picture of pixels all color.

from_points classmethod

from_points(
    width: int,
    height: int,
    points: Mapping[Position, Color],
    default: Color,
) -> Pixels

A width by height picture of default pixels, but for the points given their own colors.

from_gradient classmethod

from_gradient(
    width: int, height: int, gradient: Gradient
) -> Pixels

A width by height picture of gradient, mapped over the whole picture, with its pixels taken as square, as HalfBlocks draws them, so its angles and circles are in pixels, as are any lengths it gives in cells. As in an element's box, the gradient spans the centers of the corner pixels, which take its end colors. Its box names one of an element's boxes, so it has no effect here.

from_image classmethod

from_image(image: SupportsRGBA) -> Pixels

The pixels of image, such as a Pillow Image, converted to RGBA.

with_points

with_points(points: Mapping[Position, Color]) -> Pixels

This picture with the pixel at each of points set to its color, which raises ValueError for a point outside the picture.

counterweight.painters.HalfBlocks dataclass

HalfBlocks(
    pixels: Pixels,
    fit: Fit = "fill",
    position: tuple[float, float] = (0.5, 0.5),
)

A Canvas painter that draws pixels two to a cell, one above the other, as a half block (▀) whose foreground is the top pixel and whose background is the bottom one. A cell is about twice as tall as it is wide, so the pixels are about square, and its natural size is the picture's width by half its height, rounded up.

In a box of another size, fit scales the picture as CSS's object-fit does, and position places it as object-position does, as the fractions of the space left over on each axis to put to its left and above it, so (0.5, 0.5), the default, centers it, and (0, 0) puts it in the top left corner. A picture larger than its box is cut off.

Each cell shows the pixels nearest the middle of its two halves. A pixel less than half opaque is transparent, and leaves its half of the cell to show the canvas's background, as does a half the picture doesn't cover.

pixels instance-attribute

pixels: Pixels

fit class-attribute instance-attribute

fit: Fit = 'fill'

position class-attribute instance-attribute

position: tuple[float, float] = (0.5, 0.5)

natural_size property

natural_size: tuple[int, int]

keeps_aspect_ratio property

keeps_aspect_ratio: bool

draw

draw(width: int, height: int) -> Rows

The cells of a width by height box that show the picture, scaled by fit and placed by position.

pixels_at

pixels_at(
    cell: Position, width: int, height: int
) -> tuple[Position, ...]

The pixels of the picture shown in cell of a width by height box, from its top half to its bottom half, such as for a paint program to set the pixels under the mouse. There's one pixel if both halves show the same one, and none if the picture doesn't cover the cell.

sources

sources(
    width: int, height: int
) -> tuple[tuple[int | None, ...], tuple[int | None, ...]]

For each pixel column and pixel row of a width by height box, two pixel rows to a cell, the column or row of the picture it shows, or None where the picture doesn't cover it.

counterweight.painters.Fit

Fit = Literal[
    "fill", "contain", "cover", "none", "scale-down"
]

How a picture is scaled to fit its box, as CSS's object-fit scales an <img>.

counterweight.painters.HalfBlockGradient dataclass

HalfBlockGradient(gradient: Gradient)

A Canvas painter that fills its canvas with gradient, two pixels to a cell, as HalfBlocks draws a picture, which shows a gradient at twice the vertical resolution of an element's color slots.

It draws a new picture at its box's size each time its canvas is drawn, so the gradient is smooth at any size, rather than a fixed picture scaled to it, and the canvas draws it again only when the gradient or the box's size changes. It has no natural size, so size its canvas with its style.

gradient instance-attribute

gradient: Gradient

natural_size property

natural_size: tuple[int, int]

keeps_aspect_ratio property

keeps_aspect_ratio: bool

draw

draw(width: int, height: int) -> Rows

The cells of a width by height box, showing the gradient over width by 2 * height pixels.

counterweight.painters.SupportsRGBA

An image that converts to RGBA bytes, as a Pillow Image does.

size property

size: tuple[int, int]

The image's width and height, in pixels.

convert

convert(mode: str) -> SupportsRGBA

The image in another mode, such as "RGBA".

tobytes

tobytes() -> bytes

The image's pixels, row by row.

counterweight.painters.PaintView dataclass

PaintView(rows: Rows, width: int)

A Canvas painter that draws a picture of cells taken from a paint, which Paint.as_painter makes. Its natural size is the picture's, as an <img>'s is, and it doesn't scale, since cells can't, so rows and columns past the canvas's box are cut off, and the rest of a bigger box is left empty.

rows instance-attribute

rows: Rows

width instance-attribute

width: int

How many cells wide the picture is.

natural_size property

natural_size: tuple[int, int]

keeps_aspect_ratio property

keeps_aspect_ratio: bool

draw

draw(width: int, height: int) -> Rows

The picture's top height rows.

counterweight.paint.Paint dataclass

Paint(width: int, height: int, cells: list[Cell])

A screen's worth of cells, stored row by row.

Index it with a Position or an (x, y) pair. Indexing outside the screen raises IndexError (negative coordinates don't wrap around), while get returns None there instead.

Every two-cell-wide cluster is followed by its continuation cell on the same row, and every continuation cell follows one. Writing over one half of a pair erases the other half to a space, as a terminal does, so the cells written must not split a pair themselves (except where write_row clips them).

width instance-attribute

width: int

height instance-attribute

height: int

cells instance-attribute

cells: list[Cell]

bounds property

bounds: Region

The region the paint covers, which clips nothing when given as a clip.

blank classmethod

blank(width: int, height: int) -> Paint

A width by height paint of BLANK cells.

index

index(x: int, y: int) -> int

The offset of the cell at (x, y) in cells.

__getitem__

__getitem__(position: Position | tuple[int, int]) -> Cell

__setitem__

__setitem__(
    position: Position | tuple[int, int], cell: Cell
) -> None

get

get(position: Position) -> Cell | None

The cell at position, or None if it's off the screen.

fill

fill(region: Region, cell: Cell, clip: Region) -> None

Set every cell of region that is inside clip and on the screen to cell.

write_row

write_row(
    x: int, y: int, row: Sequence[Cell], clip: Region
) -> None

Set the cells from (x, y) rightward to row, dropping any that fall outside clip or off the screen, and erasing the half left inside of a two-cell-wide cluster that straddles their edge.

rows

rows() -> Iterator[list[Cell]]

Each row of cells, top to bottom.

items

items() -> Iterator[tuple[Position, Cell]]

Each cell with its position, row by row.

copy

copy() -> Paint

A paint with the same cells, which changing this one doesn't affect.

cells_in

cells_in(
    region: Region | None = None,
) -> tuple[tuple[Cell, ...], ...]

The cells inside region, or the whole paint's if it's None, row by row, as a value that changing the paint doesn't change. Any part of region off the paint, as after the screen shrinks, is BLANK. A two-cell-wide cluster that straddles the region's edge keeps only its half inside, which rows_of draws as a space.

as_painter

as_painter(region: Region | None = None) -> PaintView

A Canvas painter that draws the cells inside region, or the whole paint if it's None, as they are now: a picture of part of a screenshot's frame, say. Build it once, rather than in every render, since a canvas compares it with the last one it drew.

counterweight.paint.Cell dataclass

Cell(grapheme: str, style: ResolvedCellStyle[AnyColor])

What one terminal cell shows: a grapheme cluster and the resolved style it's drawn with.

A cluster two cells wide is followed by a continuation cell, whose grapheme is empty, because the terminal draws the cluster across both.

grapheme instance-attribute

grapheme: str

style instance-attribute

is_continuation property

is_continuation: bool

Whether this is the right half of a two-cell-wide cluster, which the cell to its left draws.

blank cached classmethod

blank(color: AnyColor) -> Cell

A space on a color background.

erased

erased() -> Cell

A space in this cell's style, which is what a terminal leaves in the other half of a two-cell-wide cluster when one half is drawn over.

counterweight.paint.BLANK module-attribute

BLANK = Cell.blank(color=Color.from_name('black'))

A space on black, what a cell nothing paints shows.

counterweight.paint.rows_of

rows_of(cells: Iterable[Sequence[Cell]]) -> Rows

cells, row by row, as a Canvas painter's rows, such as cells taken from a paint with cells_in, or put together from several. A two-cell-wide cluster followed by its continuation cell is drawn as one, and either half of one whose other half isn't beside it is a space in its style, as a terminal leaves it.

counterweight.paint.styled_grapheme cached

styled_grapheme(cell: Cell) -> StyledGrapheme

cell as a Canvas painter draws it: its grapheme, in its style with every field set, so the canvas's own styles change nothing about it. rows_of draws whole rows of cells this way, and handles the two halves of a wide grapheme, which this leaves to the caller.