Skip to content

Draft: "User-space" CTX

rahix requested to merge rahix/user-space-ctx into master

Add python API for CTX vector graphics. This is a work-in-progress!!!

There is still lot's missing, see the todo list below. That said, I've opened this PR so people can start playing with CTX on card10. You can download a card10.bin for this MR from the "Exposed Artifacts" under the CI pipeline below. Documentation is null at the moment, but you can probably get quite far by reading the HTML5 Canvas Docs and consulting

import ctx_graphics
for e in dir(ctx_graphics.Ctx): print(e)

on card10 for method and constant names.

As a kick-start, here is some example code:

import ctx_graphics, display, color

ctx = ctx_graphics.Ctx()

# Global Settings
ctx.line_width(5)
ctx.line_cap(ctx.CAP_ROUND)
ctx.line_join(ctx.JOIN_ROUND)
ctx.font_size(30)
ctx.font("ctx-mono")
ctx.text_align(ctx.TEXT_ALIGN_CENTER)
ctx.text_baseline(ctx.TEXT_BASELINE_MIDDLE)

with display.open() as d:
    d.clear()
    ctx.save()

    # Some Lines with nice joints
    ctx.stroke_color(color.CHAOSBLUE)
    ctx.move_to(10, 10).line_to(10, 70).line_to(150, 70).line_to(150, 10).stroke()

    # Draw a rectangle which perfectly fits the text
    width = ctx.text_width("Hello CTX!") + 4
    ctx.color(color.WHITE).rectangle(80 - width / 2, 20, width, 40).fill()

    # Draw some text!
    ctx.color(color.CAMPGREEN).fill_text("Hello CTX!", 80, 40)

    ctx.restore()
    ctx.update(d)
    d.update()

TODO

  • Finish covering relevant API surface
  • Implement alpha for color functions
  • Take colors in many formats
  • Cleanup the CTX when the python object gets garbage collected
  • Improve API ergonomics
Edited by rahix

Merge request reports