How to build a custom R graphics device

Did I need to write a graphics device to output plots to an HTML canvas? No I did not. Did anyone ask for this? Oh hells no. Ah, but did I have fun? Hahahahahahahaha. No
R
C
Javascript
LLMs
Author
Published

October 18, 2026

For the longest time, graphics devices in R have been a mystery to me. Yes, I know a lot about how to create graphics in R: I can create some rather lovely data visualisations with ggplot2, I can write grid code directly, and I can use base R graphics when I need to. But this code operates at a level of abstraction one step above the creation of the image itself: in all cases, what these tools do is send instructions to an R graphics device. This device is the thing responsible for taking the R code and transforming it into a graphic of some kind. Depending on what kind of device you’re using, this output could be an on-screen image, a png file, a jpeg, an svg file, or any of a number of other possibilities.

Since my very earliest days of using R I have always wondered what in the everliving fuck actually happens at this step. Graphics devices are rather opaque and mysterious entities, in large part because you can’t write them in R: all the work is done in C, with a thin R wrapper laid over the top. There is of course documentation for this but, well, it’s not easy for a novice to dive into. And so it inevitably came to pass that my frustration over my own incompetence spilled over, and I decided to use the master’s tools to build my own goddamn house: I shamelessly used Claude to teach me how these things are constructed, so that I could do it myself if I bloody well wanted to.1

This is that story.

(R code)
library(cnvs)
library(ggplot2)



The cnvs package

The initial brief I gave to Claude was explicitly pedagogical. I initialised the djnavarro/cnvs repository as an empty R package and then gave it the following task:

I’d like to create a personal package that I can use to learn how R graphics devices work. The goal here is to implement an R graphics device that sends output to an HTML canvas object, similar to how the “canvas” package does or how the webr::canvas() function solves the same task, by having the R graphics device emit the commands required to draw the output using the JavaScript 2D context API. As noted the primary goal is pedagogical, so it is important that the code be well-commented and interpretable to someone who does not already understand the structure of an R graphics device

I chose this as my learning example because I know a fair bit of HTML, CSS, and javascript already, and because I’ve hand-coded my own <canvas> objects in HTML/JS I knew that the end result would be a document that would be comprehensible to me. I know just enough C that I knew I’d be able to read the C code itself (albeit perhaps needing a little refresher), and I know how to write an R package. The “learning gap” that I wanted Claude to help me fill relates specifically to the R graphics device itself.2

When given those instructions, Claude did its usual thing… it burned a bucketload of tokens thinking about the problem, wrote out a plan, and then coded a first-pass version of the package. As is usually the case with LLMs it worked very quickly, so much so that I couldn’t follow it in real time. I had to wait until it completed its first attempt. I read through the code, played around with the package, and asked it a lot of questions that would probably have been suuuuuper-annoying if I’d asked a human. Some of my questions revealed bugs in the code that the LLM had written, others unearthed some poor design choices that made the code harder to read than it needed to be, and so on. It felt slightly gratifying to me that even in the early stages of my learning I was able to ask the kinds of questions that improved the code base, but I’m under no illusions about it: I didn’t write very much of the code myself, I asked an agent to do it and then tried to learn from the experience. In any case, the outcome of this process was the cnvs package itself, which supplies a simple and heavily-documented graphics device whose code I could dive into.

To give you a sense of what the resulting device does, here’s what the vanilla usage of the cnvs::cnvs() function looks like. Just like any other graphics device, we open the device, execute the plotting commands, and then close the device:

(R code)
cnvs(file = "mpg.html")
ggplot(mpg, aes(displ, hwy)) + geom_point()
dev.off()

Instead of printing the plot to an on-screen device or writing it to a PNG file via the png() device, the cnvs() device writes a self-contained HTML file, mpg.html in which the plot is written into an HTML canvas, complete with all the javascript code required so that the browser itself can take care of the drawing. If you follow the link to the mpg.html document and inspect the source, you can see the HTML/JS code for yourself. Or, alternatively, I could do that from this blog post and save you the effort:

(R code)
cat(readLines("mpg.html")[8:18], sep = "\n")
<canvas id="cnvs-page-1" width="480" height="480" style="border: none; margin-bottom: 1em;"></canvas>
<script>
(function() {
  var canvas = document.getElementById("cnvs-page-1");
  var ctx = canvas.getContext("2d");
  ctx.fillStyle = "rgba(255, 255, 255, 1.000)";
  ctx.fillRect(0, 0, 480, 480);
  ctx.save();
  ctx.beginPath();
  ctx.rect(0.00, 0.00, 480.00, 480.00);
  ctx.clip();

That’s terribly nice, and indeed the entire goddamn purpose of the tool, but as a convenience for myself the package also has a cnvs_embed() function that lets me embed the resulting canvas object directly into my quarto output:

(R code)
plt <- ggplot(mpg, aes(displ, hwy)) + geom_point()
cnv <- cnvs_embed(print(plt), width = 600, height = 600, bg = "#00000000")
cnv

Under the hood, calling cnvs_embed() enacts the same process: the ggplot object is printed to a cnvs() device, which writes it to a temporary file that contains the canvas element (omitting the boilerplate code needed to turn it into a standalone HTML file), reads it back into memory and then returns it to R as cnv, a “knitr-asis” object. So when my quarto blog post is rendered, the knitr engine passes the HTML/JS code in the cnv object directly into my document.3 The plot shown above isn’t rendered to PNG, it is a pure HTML/JS canvas object. The ggplot output shown above isn’t an image file, it’s an HTML canvas.

Neat.



Okay but… how???

So now we have this package, and it does the thing I wanted it to, but… how does it work? It’s a cute piece of functionality, but the truth is that the R ecosystem already has validated tools that do this. The real purpose of spending a few dollars to get Claude to write this thing for me is so that I have a simple and concrete example of an R graphics device, built from scratch with none of those tedious “how does it perform in production” considerations that might lead to complicated code and make it hard for me to understand. What precisely does my fancy new cnvs() graphics device do?? Wrapping my head around this is the actual purpose of the exercise.

Let’s get started. The first step in unpacking this is to look at the R code in the cnvs package, because that reveals the fundamental property that makes R graphics devices mysterious. Here’s the entire source for the cnvs() function:

cnvs.R (R code)
cnvs <- function(file = "cnvs.html", 
                 width = 480, 
                 height = 480,
                 pointsize = 12, 
                 bg = "white",
                 standalone = TRUE,
                 id_prefix = "cnvs-page") {
  stopifnot(
    is.character(file), length(file) == 1, !is.na(file),
    is.numeric(width), length(width) == 1, width > 0,
    is.numeric(height), length(height) == 1, height > 0,
    is.numeric(pointsize), length(pointsize) == 1, pointsize > 0,
    is.logical(standalone), length(standalone) == 1, !is.na(standalone),
    is.character(id_prefix), length(id_prefix) == 1, !is.na(id_prefix),
    nzchar(id_prefix)
  )

  # The real work happens in C, in src/device.c: this call builds the
  # device description struct and registers it with R's graphics engine.
  # Everything after this point -- every line(), rect(), text() call that
  # happens before dev.off() -- is routed by R straight to our C callbacks,
  # never back through this R function.
  invisible(.Call(
    "cnvs_device_",
    file,
    as.integer(width),
    as.integer(height),
    as.double(pointsize),
    bg,
    standalone,
    id_prefix
  ))
}

Lol. Lmao. Etc. This tells us nothing at all except that the graphics device doesn’t exist anywhere within the R code. The cnvs() function is nothing more than a wrapper around something else, and that something else only exists in C code. The moment the user calls cnvs(), everything gets delegated to the C function cnvs_device_. The R code itself doesn’t do anything besides that. In other words: an R graphics device is a tiny library written in C. So if we want to understand the graphics device, we need to start a slightly unnerving dive into the R internals and understand what the C code for a graphics device is expected to do…

Code
(R code)
# Some artwork to generate separator images, why not? The larger 
# separators in this blog post are created using this tiny
# generative art system. It's designed to create pieces that 
# look a little bit like hand-drawn sketches of multilayer 
# printed circuitry boards, which seems vaguely on-topic for a 
# blog post about graphics devices. They're not terribly 
# realistic though :)

# Palette generator
sample_palette <- function(seed = NULL) {

  palettes <- list(
    c("#de9151", "#f34213", "#2e2e3a", "#bc5d2e", "#bbb8b2"),
    c("#a63446", "#fbfef9", "#0c6291", "#000004", "#7e1946"),
    c("#ffffff", "#ffcad4", "#b0d0d3", "#c08497", "#f7af9d"),
    c("#aa8f66", "#ed9b40", "#ffeedb", "#61c9a8", "#ba3b46"),
    c("#241023", "#6b0504", "#a3320b", "#d5e68d", "#47a025"),
    c("#64113f", "#de4d86", "#f29ca3", "#f7cacd", "#84e6f8"),
    c("#660000", "#990033", "#5f021f", "#8c001a", "#ff9000"),
    c("#c9cba3", "#ffe1a8", "#e26d5c", "#723d46", "#472d30"),
    c("#0e7c7b", "#17bebb", "#d4f4dd", "#d62246", "#4b1d3f"),
    c("#0a0908", "#49111c", "#f2f4f3", "#a9927d", "#5e503f"),
    c("#020202", "#0d324d", "#7f5a83", "#a188a6", "#9da2ab"),
    c("#c2c1c2", "#42213d", "#683257", "#bd4089", "#f51aa4"),
    c("#820263", "#d90368", "#eadeda", "#2e294e", "#ffd400"),
    c("#f4e409", "#eeba0b", "#c36f09", "#a63c06", "#710000"),
    c("#d9d0de", "#bc8da0", "#a04668", "#ab4967", "#0c1713"),
    c("#012622", "#003b36", "#ece5f0", "#e98a15", "#59114d"),
    c("#3c1518", "#69140e", "#a44200", "#d58936", "#fffb46"),
    c("#6e0d25", "#ffffb3", "#dcab6b", "#774e24", "#6a381f"),
    c("#bcabae", "#0f0f0f", "#2d2e2e", "#716969", "#fbfbfb"),
    c("#2b4162", "#385f71", "#f5f0f6", "#d7b377", "#8f754f")
  )

  if (!is.null(seed)) set.seed(seed)
  ind  <- sample(seq_along(palettes), 1) 
  base <- sample(palettes[[ind]])
  return(base)
}

# Data generator
sample_circuit_data <- function(seed = NULL, n, width, height){

  if(!is.null(seed)) set.seed(seed)
  dat <- tibble::tibble(
    x0 = runif(n, min = 0, max = width),
    y0 = runif(n, min = 0, max = height),
    x1 = runif(n, min = 0, max = width),
    y1 = runif(n, min = 0, max = height),
    shade = runif(n), 
    size = runif(n)
  )
  return(dat)
}

# Plot constructor
plot_circuit <- function(dat, pal, width, height) {

  dat_black <- dat |> dplyr::slice_sample(prop = .4)
  x_limit <- c(-20, width + 20)
  y_limit <- c(-40, height + 40)

  plt <- ggplot2::ggplot(
    data = dat,
    mapping = ggplot2::aes(
      xmin      = x0,
      ymin      = y0,
      xmax      = x1,
      ymax      = y1,
      fill      = shade,
      colour    = shade
    )) + 
    ggplot2::coord_equal(xlim = x_limit, ylim = y_limit) +
    ggplot2::scale_colour_gradientn(colours = pal) + 
    ggplot2::scale_fill_gradientn(colours = pal) + 
    ggsketch::geom_sketch_rect(
      data = dat |> dplyr::slice_sample(prop = .1),
      linewidth = .5, 
      show.legend = FALSE,
      alpha = .5,
    ) +
    ggsketch::geom_sketch_rect(
      linewidth = .5, 
      show.legend = FALSE,
      fill = NA
    ) +
    ggsketch::geom_sketch_point(
      mapping = ggplot2::aes(
        x = x0, 
        y = y0, 
        color = shade
      ),
      data = dat |> dplyr::slice_sample(prop = .8),
      show.legend = FALSE,
      size = 4
    ) +
    ggsketch::geom_sketch_point(
      mapping = ggplot2::aes(x = x1, y = y1),
      data = dat_black,
      show.legend = FALSE,
      color = "black",
      size = 4
    ) +
    ggsketch::geom_sketch_segment(
      mapping = ggplot2::aes(
        x = x1, 
        y = y1,
        xend = (1-size)*x1 + size*x0/2,
        yend = y1,
      ),
      data = dat_black |> dplyr::slice_sample(prop = .5),
      show.legend = FALSE,
      color = "black",
      linewidth = 1
    ) +
    ggsketch::geom_sketch_segment(
      mapping = ggplot2::aes(
        x = x1, 
        y = y1,
        xend = x1,
        yend = (1-size)*y1 + size*y0/2,
      ),
      data = dat_black |> dplyr::slice_sample(prop = .5),
      show.legend = FALSE,
      color = "black",
      linewidth = 1
    ) +
    ggplot2::theme_void()
  return(plt)
}

# Top-level art generator
circuit <- function(seed, n = 50, width = 746, height = 400) {
  dat <- sample_circuit_data(seed, n, width, height)
  pal <- sample_palette(seed)
  plt <- plot_circuit(dat, pal, width, height)
  return(plt)
}

# Call the art generator, and embed the image
# as an HTML canvas within the document
cnvs_embed(
  expr = print(circuit(seed = 234)),
  width = 746,
  height = 400, 
  bg = "#00000000"
)



RTFM, if you can find TFM

One of the standout features of the R ecosystem is that documentation is both prioritised and standardised, and has been from the beginning. You cannot get a package onto CRAN without supplying a manual, which is constructed from the various .Rd files that come with your package. Nowadays we rarely write the .Rd files directly: we write a roxygen2 skeleton that describes the documentation we want, and more often than not also supply a pkgdown site that puts that documentation online in a predictable standard format. It’s so standard that there is even a pkgdown site for my no-no-dear-god-you-should-not-be-using-this cnvs package.

Unfortunately, the ubiquity of user-facing R documentation makes it come as something of a shock to discover that the documentation for internal R features is a little less easy to access. The R internals manual is excellent for providing an overview, but often you need to look into the source code for R itself: the comments inside the source are often the place where the API is documented. For graphics devices, the three places I’ve found it most useful to do a bit of reading are these:

Compared to the documentation for a typical R package, these are all a step up in difficulty for several reasons, not least of which is that the task is now to understand a C interface rather than an R interface. And let’s be honest here… C programming sucks. It’s psychologically painful, visually unpleasant, and has exactly one redeeming feature: C is fast.4 Indeed, this is arguably one of the places where R really does need to delegate the work to a compiled language like C. The data structure that underpins a plot like the one I created above is very minimal, and the performance cost for doing that with interpreted code like R is not all that much of a concern. But translating it to a sequence of low level drawing instructions is… yeah, that is quite a bit more work. You don’t want that step to be handled by interpreted code. So it inevitably comes to pass that we are working in C for this exercise.

Fine.

Whatever.

See if I care.



What’s she building in there?

Because all the important work in defining an R graphics device is done in C, the place you should look when trying to understand even a simple graphics package like cnvs is the src folder. That’s where all the C code lives. Thankfully, because I yelled at Claude a lot and told it how unbearably stupid I am when it comes to this particular topic, it did something terribly kind and structured the files nicely and commented them excessively. There are six files in this folder, and they each serve a distinct purpose. For example, there are two files that do a lot of the “background” work that is utterly critical to make things work, but don’t immediately help us conceptually:

  • cnvs.h. This is a header file that contains shared declarations used everywhere. It’s important in the sense that a lot of the code you see in the other files relies on these declarations and you need to read it to understand what those other files are up to, but it actually doesn’t do much of the work.

  • init.c. This one is bookkeeping. Any time an R package comes bundled with compiled code, it needs to tell R which bits of the C code should be considered callable from R. For that to work we need a function called R_init_cnvs that exposes that information, because R expects to find a function called R_init_<pkgname> that supplies this information. Fair enough, but in our case there’s only one: R needs to know that cnvs_device_ exists as an R-callable C function. If it doesn’t know that the user-facing cnvs() function won’t work. Yep, important, but also not central to understanding the graphics device itself.

Noting that those two files are in the background, and that – especially for cnvs.h – we might need to pivot back to them in order to understand what the other files are doing, we can turn our attention to the four files that do the substantive work of constructing a graphics device:

  • device.c. This file handles the interface between the cnvs graphics device and the R graphics engine. It is responsible for managing the lifecycle of a graphics device: whenever the R graphics engine asks it to do something like create a device, close a device, resize a device,5 create a new page, etc, it’s the code in this file that takes responsibility. It doesn’t do any of the drawing though.

  • draw.c. This file is where graphical primitives are handled. Any time that R needs to draw a line, a rectangle, a circle, a polygon, or even text, the code in this file handles that work. But it only does shapes. When you look at the code in this file, you’ll see that stylistic properties like colour and font get quietly farmed out to functions that live in style.c.

  • style.c. This is the file used for handling non-shape aspects of the graphics rendering: it is the file that handles colour conversions from R to javascript, but also handles the styling of lines, and the specification of fonts. All terribly handy things to have in a graphics device.6

  • write.c. This file is responsible for writing the output to an HTML file. It doesn’t know anything about the R graphics engine, and does not care. It exists to write the HTML document itself: every “page” in the R graphics sense of the term corresponds to a <canvas> and <script> pair in the HTML output. It’s the last step in the process.

Oddly, I find this scaffolding helpful in and of itself. I mean, I genuinely don’t understand the graphics device yet, but somehow seeing the structure of how you organise one of these things into a sensible set of files is informative. It tells me something about the specific tasks that go into building one of these things, and it also tells me where to look when I want to wrap my head around one of these parts. It also tells me that, if I want to start in a “safe” place and begin with the parts of the codebase that are likely to be least terrifying to me, I should begin with draw.c and style.c. I already know a lot of R and honestly more javascript than I really have any right to, so this part will probably make sense to me.

I am hopeful, then, that decrypting the strange inscriptions that Claude has provided for me in these files will not be as horrible a process as I fear it will be. Fingers crossed, I guess? But first, a little digression…

Code
(R code)
cnvs_embed(
  expr = print(circuit(seed = 102)),
  width = 746,
  height = 400, 
  bg = "#00000000"
)



The internal C interface

From a pedagogical standpoint, the place to go next is not to the C code in cnvs. Rather, the thing you want to do at this point is to learn a little bit about the internal interface for graphics devices supplied within the R source code. It’s not actually the thing I did, because it’s quite dense when you look at the real thing and required me to dredge my antiquated recollection of C back from the depths of my undergraduate memory. And to be honest, when I started trying to learn this stuff I didn’t even know what questions to ask, so it was only quite late in the process of detangling the cnvs source code that I started really trying to grasp it. Aaaanyway… I’ll spare you the pain I went through and show you the parts that are worth knowing about in advance. As the author of the blog post that seems like the polite thing for me to do ☺️

From the perspective of a would-be developer of an R graphics device, there are two key data structures defined in the R source code that you really need to know about:

  • The pGEcontext type supplies a pointer to a struct containing graphical parameters. Your graphics device never needs to define one of these itself, but it needs to be able to work with them when the R graphics engine tries to pass one of them to your device!
  • The pDevDesc type supplies a pointer to a struct that defines a graphical device. This is the more complicated and important one: all the heavy lifting is done via the device description, and (shockingly!) it turns out to be rather helpful if you understand what this thing actually is.

So okay, these two structures are important enough that they’re worth devoting some space to, even before we dive into the details of the specific device that cnvs defines. Let’s start with the easy one.


Graphical context

I’ll start by looking at pGEcontext first, defined in GraphicsEngine.h. It’s the simpler of the two, and provides a good pedagogical opportunity to talk about a few bits of C syntax that I once knew reasonably well but had forgotten because it’s been such a long time since I’ve needed to use this knowledge. The code below isn’t the exact source code, but it’s close enough for our purposes:

Adapted from "GraphicsEngine.h" (C code)
typedef struct {
    /* Colour */
    int col;                /* Pen colour (lines, text, borders, ...) */
    int fill;               /* Fill colour (polygons, circles, rects, ...) */
    double gamma;           /* Gamma correction */

    /* Lines */
    double lwd;             /* Line width (roughly number of pixels) */
    int lty;                /* Line type (solid, dashed, dotted, ...) */
    R_GE_lineend lend;      /* Line end */
    R_GE_linejoin ljoin;    /* Line join */
    double lmitre;          /* Line mitre */

    /* Text */
    double cex;             /* Character expansion (font size = fontsize*cex) */
    double ps;              /* Font size in points */
    double lineheight;      /* Line height (multiply by font size) */
    int fontface;           /* Font face (plain, italic, bold, ...) */
    char fontfamily[201];   /* Font family */

    /* Definitions */
    SEXP patternFill;       /* Reference to a pattern fill */
} R_GE_gcontext;

typedef R_GE_gcontext *pGEcontext;  /* Callbacks receive a pointer */

The first thing to note is the two-stage type definition: an R_GE_gcontext object is a struct (roughly7 analogous to a list in R) containing the various slots listed in the code extract above. In contrast, a pGEcontext object is a pointer to a struct of that type.

  • R_GE_gcontext is the struct itself: If your code contained an obj struct of this type, that object would have the slots obj.col, obj.fill, obj.gamma, and so on. In C syntax . is used as the accessor to a struct: it’s more like Python and Javascript in that respect, as they both use dots for that purpose. In R the closer analog is to $, the accessor that we use to extract an element of a list.

  • pGEcontext is the pointer: If our code contains a ptr pointer of this type it does not contain any slots, but we can access the slots of the struct to which it points by first dereferencing the pointer, so the code to extract the colour slot would be (*ptr).col. However, this code pattern is so common that C supplies the -> operator as shorthand and ptr->col is the same as (*ptr).col. It’s the latter form using -> that appears throughout the cnvs code. I swear to dog that I really did once know what -> means, but it still took me longer than I care to admit for it to come back to my mind.

In any case, once the “omg it’s been so long since I’ve written actual C code” tears dried up, this all made sense to me. In truth a graphical context pointer isn’t a super-complicated thing. All we need to do is remember that there are going to be places in our code when the R graphics engine passes one of these things to us, and we might want to use this information when generating output from our device. Cool. Let’s move on.


Device description

The other data structure you really need to understand is the device description. That’s the more important and more complicated one. Like pGEcontext the pDevDesc is a pointer type, but the struct to which it refers is quite a bit more elaborate. It is defined in the GraphicsDevice.h file, and I’m not going to show the whole thing here. Instead, here’s a heavily truncated version:

Adapted from "GraphicsDevice.h" (C code)
struct _DevDesc {
    /* Device physical characteristics */
    double left;            /* left raster coordinate */
    double right;           /* right raster coordinate */
    double bottom;          /* bottom raster coordinate */
    double top;             /* top raster coordinate */

    /* Device capabilities */
    Rboolean canClip;       /* Device-level clipping */

    /* Device initial settings -- the device must set these up when
     * created; the graphics system can modify them from there */
    int startcol;           /* sets par("fg"), par("col") and gpar("col") */
    int startfill;          /* sets par("bg") and gpar("fill") */

    /* Device specific information */
    void *deviceSpecific;   /* pointer to device specific parameters --
                             * this is where a device like cnvs stashes
                             * its own private struct (cnvs_state here) */

    /* Device display list */
    Rboolean displayListOn; /* toggle for initial display list status */

    /* Device procedures -- one function pointer per drawing/lifecycle
     * action the graphics engine might ask the device to perform. Two
     * examples: */
    void (*line)(double x1, double y1, double x2, double y2,
                 const pGEcontext gc, pDevDesc dd);
    void (*close)(pDevDesc dd);

    /* ... and many more fields and function pointers besides -- the real
     * struct also covers character-metric bookkeeping, starting graphical
     * parameters, event-handling capability flags, and a callback for
     * essentially every other drawable shape and lifecycle event (circle,
     * rect, polygon, text, newPage, activate, clip, locator, ...). */
};

typedef struct _DevDesc DevDesc;
typedef DevDesc* pDevDesc;

Once again we have the two-stage construction: a DevDesc object is a struct, and a pDevDesc object is a pointer to a struct of that type. For our graphics device code, it’s the pointers that get passed around, not the structs. You can see this in play in the function signature that we’ll need to implement if we want our graphics device to draw a line. The relevant snippet is this bit:

void (*line)(double x1, double y1, double x2, double y2,
             const pGEcontext gc, pDevDesc dd);

The arguments that the graphic engine will pass to our line-drawing function (when we actually get around to writing it!) will consist of four doubles and two pointers:

  • x1, y1, x2, and y2 are the numeric values that define the start and end points of the line
  • gc is a pointer to the graphical style parameters for the line (i.e., it’s a pGEcontext object)
  • dd is a pointer to the device description for our canvas device (i.e., it’s a pDevDesc object)

In short, the type definition for pDevDesc is the place to look if you want to find the moving parts of a graphics device. It tells you what slots it has to have, and what callback functions it needs to implement if you want the graphics device to function. The full version is rather long and annoying, and if I am perfectly honest I am still digesting the longer form of the code. But at least I now understand what I’m looking at, and that gives me the context that I need to make sense of the source code to cnvs.


Device specific data

One more thing to mention before I move on. Notice that a device description pointer like dd contains a slot allocated for dd->deviceSpecific. This is the place that the graphics engine sets aside so that the graphics device can define its own storage, in whatever form it needs. I’ll talk about this more explicitly later, but for now it’s sufficient to note that the cnvs package defines its very own cnvs_state type to place in the deviceSpecific slot, and uses it to store information like “what’s the name of the file I have to write this to anyway?????” Because this is a data structure specific to cnvs, the type definition exists within the cnvs.h header file, and here it is:

cnvs.h (C code)
typedef struct {
    FILE *fp;              /* the open .html file we are writing to */
    int width;             /* device width, in CSS pixels */
    int height;            /* device height, in CSS pixels */
    int page_count;        /* how many <canvas> pages we've started so far */
    Rboolean clip_active;  /* TRUE if we've done ctx.save()+ctx.clip() since
                            * the last matching ctx.restore() */
    Rboolean standalone;   /* TRUE (the default): write a complete, openable
                            * HTML document (<html>/<head>/<body> and all).
                            * FALSE: write only the <canvas>/<script> blocks
                            * for each page, with no surrounding document --
                            * meant to be read back and embedded as a
                            * fragment inside someone else's HTML (e.g. a
                            * knitted Quarto/R Markdown document), not
                            * opened directly in a browser. */
    char *id_prefix;       /* base string for each page's <canvas> id, e.g.
                            * "cnvs-page" produces ids "cnvs-page-1",
                            * "cnvs-page-2", ... Caller-supplied (rather than
                            * a fixed string) so that several cnvs devices
                            * writing fragments (standalone = FALSE) into
                            * the same host document -- e.g. several chunks
                            * of one Quarto/R Markdown file -- can be given
                            * distinct prefixes and never collide on
                            * `id="..."` in the final HTML. Owned by this
                            * struct: allocated in cnvs_device_(), freed in
                            * cnvs_Close(). */
} cnvs_state;

Because there will always be one of these objects wired up to the device description pointers that get passed around all over the place, the various drawing functions will have access to this information. Handy to have that lying around, really 🙃

In fact, it’s so handy – and it gets used so often – that when Claude wrote this type definition for cnvs_state it also added some convenience code into cnvs.h that simplifies our access to the device specific storage: we can use CNVS(dd) as a shorthand to pull out a pointer to the dd->deviceSpecific state information:

cnvs.h (C code)
#define CNVS(dd) ((cnvs_state *) (dd)->deviceSpecific)

Yep, makes sense to me (finally, after a great many tears were shed). Let’s move on, shall we?



Drawing shapes: draw.c

Okay. With all that as the background context for graphics devices, it’s time to dive into the C source within the cnvs package to see how it works. Because I like to do my learning in a sensible way, I decided to tackle the easy parts before I approach the terrifying things. Since I already know quite a bit about what the output should look like (i.e., some HTML/JS code for a canvas object), the least brain-breaking way for me to figure out what the fuck Claude just did was to inspect the draw.c file. That’s the place where my fancy new graphics device receives instructions from the R graphics engine about a shape it wants to draw, and then translates those into a set of instructions that javascript can apply when it writes to an HTML canvas. So that’s where I started.8

Code
(R code)
# Time to mix up the artwork a little bit. A minor variation
# of the "circuits" system that just draws some arcs. Not 
# very clever, admittedly, but it's not meant to do anything
# except provide a visual spacer between one subsection and
# the next

# Data generator
sample_arcs_data <- function(seed = NULL, n, width, height){

  if(!is.null(seed)) set.seed(seed)
  dat <- tibble::tibble(
    id = 1:n,
    x0 = runif(n, min = 50, max = width-50),
    y0 = runif(n, min = 50, max = height-50),
    r  = runif(n, min = 0, max = 50),
    a0 = runif(n, min = 0, max = pi * 2),
    a1 = runif(n, min = 0, max = pi * 2),
    shade = runif(n)
  ) |> 
    dplyr::reframe(
      .by = id,
      arc0 = min(a0, a1),
      arc1 = max(a0, a1), 
      x = x0 + cos(seq(arc0, arc1, .01)) * r,
      y = y0 + sin(seq(arc0, arc1, .01)) * r,
      shade = shade
    )
  return(dat)
}

# Plot constructor
plot_paths <- function(dat, pal, width, height) {

  x_limit <- c(-20, width + 20)
  y_limit <- c(-40, height + 40)

  plt <- ggplot2::ggplot(
    data = dat,
    mapping = ggplot2::aes(x, y, colour = shade),
  ) + 
    ggplot2::coord_equal(xlim = x_limit, ylim = y_limit) +
    ggplot2::scale_colour_gradientn(colours = pal) + 
    ggplot2::scale_fill_gradientn(colours = pal) + 
    ggsketch::geom_sketch_path(
      mapping = aes(group = id),
      linewidth = .5, 
      show.legend = FALSE
    ) +
    ggsketch::geom_sketch_point(
      data = dat |> dplyr::slice_sample(prop = .0025),
      show.legend = FALSE,
      size = 4
    ) +
    ggplot2::theme_void()
  return(plt)
}

# Top-level art generator
arcs <- function(seed, n = 20, width = 746, height = 150) {
  dat <- sample_arcs_data(seed, n, width, height)
  pal <- sample_palette(seed)
  plt <- plot_paths(dat, pal, width, height)
  return(plt)
}

cnvs_embed(
  expr = print(arcs(seed = 1101)),
  width = 746, height = 150, bg = "#00000000"
)


Drawing a line

Inside the draw.c script there is a function called cnvs_Line() that specifies what actions the graphics device should take when it is asked to draw a line. It takes the exact arguments that I described in the previous section (x1, y1, x2, y2 for the line end points, gc for the style, and dd for the device), and it translates those arguments into the javascript code needed to draw the corresponding line into the HTML canvas:

draw.c (C code)
void cnvs_Line(double x1, double y1, double x2, double y2,
               const pGEcontext gc, pDevDesc dd)
{
    /* pointer to the device-specific state */
    cnvs_state *state = CNVS(dd);

    /* nothing to draw if there's no stroke colour, or the line type
     * itself is invisible */
    if (gc->col == NA_INTEGER || gc->lty == LTY_BLANK) return;

    /* convert colour to CSS, and write the line's stroke style
     * (width, caps, joins, dashing) straight to the output file */
    char css[32];
    cnvs_colour_to_css(gc->col, css, sizeof(css));
    cnvs_set_line_style(state->fp, gc);

    /* write the JS that actually strokes the line itself */
    fprintf(state->fp, "  ctx.strokeStyle = \"%s\";\n", css);
    fprintf(state->fp, "  ctx.beginPath();\n");
    fprintf(state->fp, "  ctx.moveTo(%.2f, %.2f);\n", x1, y1);
    fprintf(state->fp, "  ctx.lineTo(%.2f, %.2f);\n", x2, y2);
    fprintf(state->fp, "  ctx.stroke();\n");
}

If you know a little bit about HTML canvas objects and the javascript framework used to draw content into them, the last five lines of this function make a lot of sense. From the perspective of the javascript engine that eventually has to render this code, ctx is the “context” object. That is, when we create an HTML <canvas> object, it is generally accompanied by some javascript wrapped in a <script> tag, and that javascript usually begins with a line of code that looks like this:

(javascript code)
var ctx = canvas.getContext("2d");

The ctx object is used by javascript to undertake all the drawing activities. So if we want to draw a line that starts at a location defined by x1 = 1 and y1 = 1 and terminates at a location defined by x2 = 2 and y2 = 2, then the javascript code we want looks like this:

(javascript code)
ctx.beginPath()
ctx.moveTo(1, 1)
ctx.lineTo(2, 2)
ctx.stroke()

So if we ignore the styling code for now – we’ll come back to that shortly – and just focus on the line-drawing behaviour, we can see that this is what the cnvs_Line() function exists for. The purpose of this callback is to write this text into the HTML file, so that the canvas will contain the correct description of the line that the R graphics engine has asked us to draw.

Code
(R code)
cnvs_embed(
  expr = print(arcs(seed = 1102)),
  width = 746, height = 150, bg = "#00000000"
)


Drawing a rectangle

Okay, we now know how to draw a line. What about rectangles? Within our graphics device, this task gets assigned to the cnvs_Rect() function, and the R graphics engine calls it with the following arguments:

  • x0, y0, x1 and y1 describe the opposite corners of the rectangle
  • gc supplies the style information associated with the rectangle
  • dd supplies the device description

The code is not terribly complicated:

draw.c (C code)
void cnvs_Rect(double x0, double y0, double x1, double y1,
               const pGEcontext gc, pDevDesc dd)
{
    /* pointer to the device-specific state */
    cnvs_state *state = CNVS(dd);

    /* (x0,y0) and (x1,y1) are opposite corners, but not guaranteed to
     * arrive as (top-left, bottom-right) -- normalise to canvas's
     * expected (x, y, width, height) form. */
    double left = (x0 < x1) ? x0 : x1;
    double top = (y0 < y1) ? y0 : y1;
    double w = fabs(x1 - x0);
    double h = fabs(y1 - y0);

    /* fill and stroke are independent -- a rectangle can have either, both,
     * or neither -- so each is checked and drawn separately, rather than
     * sharing one early-return guard the way cnvs_Line() does */
    if (gc->fill != NA_INTEGER && !R_TRANSPARENT(gc->fill)) {
        char css[32];
        cnvs_colour_to_css(gc->fill, css, sizeof(css));
        fprintf(state->fp, "  ctx.fillStyle = \"%s\";\n", css);
        fprintf(state->fp, "  ctx.fillRect(%.2f, %.2f, %.2f, %.2f);\n", left, top, w, h);
    }
    if (gc->col != NA_INTEGER && gc->lty != LTY_BLANK) {
        char css[32];
        cnvs_colour_to_css(gc->col, css, sizeof(css));
        cnvs_set_line_style(state->fp, gc);
        fprintf(state->fp, "  ctx.strokeStyle = \"%s\";\n", css);
        fprintf(state->fp, "  ctx.strokeRect(%.2f, %.2f, %.2f, %.2f);\n", left, top, w, h);
    }
}

The key thing to notice here is the thing noted in the comments. On the javascript side, drawing a filled rectangle is two separate actions: the ctx.fillRect() command provides the fill, and the ctx.strokeRect() command provides the stroke. Neither one is required, either on the R side (rectangles don’t have to have a fill or a line specification) or the javascript side, so it’s quite possible that this code does nothing whatsoever if there’s no fill or no stroke required.

Code
(R code)
cnvs_embed(
  expr = print(circuit(seed = 1003, n = 5, height = 150)),
  width = 746, height = 150, bg = "#00000000"
)


Drawing a circle

Let’s see a third example. The draw.c file also contains a function called cnvs_Circle(), which gets called whenever the graphics device is asked to draw a circle. We can see from looking at the code that it’s designed with a similar interface to the cnvs_Line() function from the last section. There are three numeric values x, y and r that define the circle itself, the gc pointer for style information, and the dd pointer for the device itself:

draw.c (C code)
void cnvs_Circle(double x, double y, double r,
                  const pGEcontext gc, pDevDesc dd)
{
    /* pointer to the device-specific state */
    cnvs_state *state = CNVS(dd);

    /* trace the circular path unconditionally -- ctx.arc() has to run
     * before either ctx.fill() or ctx.stroke() can act on it, so unlike
     * cnvs_Rect() there's no single colour/line-type check that would let
     * us skip this step entirely */
    fprintf(state->fp, "  ctx.beginPath();\n");
    fprintf(state->fp, "  ctx.arc(%.2f, %.2f, %.2f, 0, 2 * Math.PI);\n", x, y, r);

    /* fill, then stroke the same path -- same independence as
     * cnvs_Rect(): a circle can have either, both, or neither */
    if (gc->fill != NA_INTEGER && !R_TRANSPARENT(gc->fill)) {
        char css[32];
        cnvs_colour_to_css(gc->fill, css, sizeof(css));
        fprintf(state->fp, "  ctx.fillStyle = \"%s\";\n", css);
        fprintf(state->fp, "  ctx.fill();\n");
    }
    if (gc->col != NA_INTEGER && gc->lty != LTY_BLANK) {
        char css[32];
        cnvs_colour_to_css(gc->col, css, sizeof(css));
        cnvs_set_line_style(state->fp, gc);
        fprintf(state->fp, "  ctx.strokeStyle = \"%s\";\n", css);
        fprintf(state->fp, "  ctx.stroke();\n");
    }
}

As before, the core of this code is easy enough to follow if you know a little bit about the javascript framework used to draw onto an HTML canvas, and are willing to ignore the bits that are clearly related to something else. For instance, if we want to draw a unit circle centred on the origin, then our input arguments will be x = 0, y = 0, and r = 1, and the javascript we need to draw an unfilled circle would look something like this:

(javascript code)
ctx.beginPath()
ctx.arc(0, 0, 1, 0, 2 * Math.PI)
ctx.stroke()

As with the line and rectangle examples, I’m being a little sneaky by ignoring the ctx.strokeStyle() part of the output, because I haven’t started looking at the style.c file yet, but you get the idea. The cnvs_Circle() function has one job, and that job is to write the javascript specification for a circle. I mean… what else were we expecting, really?

Code
(R code)
cnvs_embed(
  expr = print(arcs(seed = 1105)),
  width = 746, height = 150, bg = "#00000000"
)


The draw.c overview

Now that we’ve seen three of the specific functions bundled in the draw.c file, we can now do the high-level overview without it being completely mysterious. It supplies the following functions, all of which have essentially the same structure as the two we’ve seen:

  • cnvs_Line(): draw a line
  • cnvs_Rect(): draw a rectangle
  • cnvs_Circle(): draw a circle
  • cnvs_Polyline(): draw multiple lines
  • cnvs_Polygon(): draw a polygon

In all cases the function takes some specific parameters defining the graphical object, along with the two all-important pointers gc and dd.

On top of these, there are three functions that are all related to drawing text into the output, which is invariably the most annoying part of rendering any graphic:

  • cnvs_Text()
  • cnvs_StrWidth()
  • cnvs_MetricInfo()

Text is inherently a pain in the arse, so I’m going to skip those in this post. For the time being it’s enough to note that the function signature for these is pretty similar to the others: there are arguments specific to “the thing to be drawn”, and arguments for the gc and dd objects. However, although text is kind of an awkward thing to work with, if you read the source code for these it’s not too difficult to grasp what they’re doing.

Finally, there’s a special function used to handle clipping (i.e., restricting the output to a specific region). There’s nothing novel or surprising in this one. It’s just like the other primitives, taking inputs from R and emitting the required javascript (i.e., ctx.clip()):

  • cnvs_Clip(): clip the output to a specified region

Big picture, then… we understand the structure of what draw.c is doing. Any time R requests that a shape be drawn (text counts as a shape), it gets routed to one of these functions, and the function in question writes the required javascript into the output file. Cool.

Code
(R code)
cnvs_embed(
  expr = print(circuit(seed = 654)),
  width = 746,
  height = 400, 
  bg = "#00000000"
)



Applying styles: style.c

Next, let’s look at the style.c file. It contains three functions, all of which are used to specify style information in a CSS format that the HTML/javascript code for the canvas object can understand:

  • cnvs_colour_to_css()
  • cnvs_set_line_style()
  • cnvs_set_font()

None of these are “front line” functions in the same sense that the draw.c functions are. As we’ll see later, all of those functions are callbacks that get wired directly into the R graphics device. In contrast, these three are all helper functions that get called by the various drawing functions inside draw.c. As I mentioned before, writing text into a graphical object sucks, so I’m going to ignore cnvs_set_font() for this post. But the other two we can look at now.


Specifying colour

Here’s the source code for cnvs_colour_to_css(). It’s not terribly complicated in itself, but requires a tiny bit of context. R packs a colour’s red, green, blue and alpha channels into a single integer, allocating one byte to each channel. That’s why the col argument is an integer, and why the code uses the R_RED(), R_GREEN(), R_BLUE() and R_ALPHA() functions to extract the values for each channel. The graphics device doesn’t have to define these functions, they’re supplied in R_ext/GraphicsDevice.h:

style.c (C code)
void cnvs_colour_to_css(int col, char *buf, size_t buf_size)
{
    int r = R_RED(col);
    int g = R_GREEN(col);
    int b = R_BLUE(col);
    double a = R_ALPHA(col) / 255.0;
    snprintf(buf, buf_size, "rgba(%d, %d, %d, %.3f)", r, g, b, a);
}

The first part of this function extracts r, g, b, and a in the format that the CSS rgba() function expects (i.e., red, blue and green are all integers between 0 and 255, whereas alpha is supplied as a value between 0 and 1). So the end result might be a string like "rgba(123, 45, 67, 0.890)" that is written into the buffer buf. If you scroll back up to the source code for the drawing functions I discussed in the last section, you can see that this generally turns out to be the css string that gets passed into the ctx.strokeStyle() and ctx.fillStyle() commands that are inserted in the javascript.


Specifying line style

The design of the cnvs_set_line_style() function is a little different. Unlike the cnvs_colour_to_css() function, which writes a CSS colour specification into a C string and then lets the drawing function itself take care of inserting it into the HTML canvas, the cnvs_set_line_style() function writes line style information directly into the canvas. Because of this, it takes two arguments:

  • A file pointer *fp, so that the function knows where to write
  • A graphical context pointer gc that contains the style information

There are several slots within the struct to which gc points that are relevant to the line style that we need to write into our HTML canvas. The table below shows the correspondence between the style parameters as they are represented in a graphical context struct on the input side, and the javascript that needs to be invoked on the output side:


Graphical parameter Graphical context slot Javascript property/method
Line width gc->lwd ctx.lineWidth
Line end gc->lend ctx.lineCap
Line join gc->ljoin ctx.lineJoin
Line mitre gc->lmitre ctx.miterLimit
Line type gc->lty ctx.setLineDash()


On the input side, the slots carry the same meaning that they do in R: whenever calling par() to get and set graphical parameters, you’ll notice that it contains options for lwd, lend, ljoin, lmitre, and lty. On the output side, javascript has the capacity to set any of these style parameters for the canvas, with only a small amount of work required for the translation. The source code shows the details of how the translation is done:

style.c (C code)
void cnvs_set_line_style(FILE *fp, const pGEcontext gc)
{
    fprintf(fp, "  ctx.lineWidth = %.2f;\n", gc->lwd);

    switch (gc->lend) {
    case GE_ROUND_CAP:  fprintf(fp, "  ctx.lineCap = \"round\";\n");  break;
    case GE_SQUARE_CAP: fprintf(fp, "  ctx.lineCap = \"square\";\n"); break;
    case GE_BUTT_CAP:
    default:            fprintf(fp, "  ctx.lineCap = \"butt\";\n");   break;
    }

    switch (gc->ljoin) {
    case GE_ROUND_JOIN: fprintf(fp, "  ctx.lineJoin = \"round\";\n"); break;
    case GE_BEVEL_JOIN: fprintf(fp, "  ctx.lineJoin = \"bevel\";\n"); break;
    case GE_MITRE_JOIN:
    default:
        fprintf(fp, "  ctx.lineJoin = \"miter\";\n");
        fprintf(fp, "  ctx.miterLimit = %.2f;\n", gc->lmitre);
        break;
    }

    /* R encodes a dash pattern as up to 8 hex digits packed into one int,
     * read from the least-significant nibble outward, each nibble giving
     * the length of one dash/gap segment in multiples of the line width
     * (see LTY_DASHED, LTY_DOTTED etc. in R_ext/GraphicsEngine.h, and try
     * `strtoi("42", base = 16L)` style arithmetic by hand on LTY_DASHED to
     * see where the "4" and "4" come from). lty == LTY_SOLID (0) means
     * "no dashing"; LTY_BLANK (-1) means invisible, which callers should
     * check for *before* drawing anything at all. */
    if (gc->lty == LTY_SOLID) {
        fprintf(fp, "  ctx.setLineDash([]);\n");
    } else {
        fprintf(fp, "  ctx.setLineDash([");
        unsigned int lty = (unsigned int) gc->lty;
        for (int i = 0; i < 8; i++) {
            unsigned int segment = lty & 0xF;
            if (segment == 0) break;
            fprintf(fp, "%s%.2f", i == 0 ? "" : ", ", segment * gc->lwd);
            lty >>= 4;
        }
        fprintf(fp, "]);\n");
    }
}

Okay yes there’s a little fancy footwork going on when translating the line type, and okay yes you do need to know that GE_ROUND_CAP, LTY_SOLID, and friends are all defined elsewhere by R itself. If I were writing another graphics device from scratch (without using an LLM coding agent to help me) it would take me a modest amount of work to find all those things and make sure I was using them correctly, but there’s nothing conceptually difficult going on here.9

Code
(R code)
cnvs_embed(
  expr = print(circuit(seed = 108)),
  width = 746,
  height = 400, 
  bg = "#00000000"
)



The device itself: device.c

Okay. At this point I feel like I understand the basics of how my fancy new cnvs device handles the “translating R into javascript” part of the process. But right now, none of this functionality connects into R. It’s just a collection of C functions that take input in a format that just so happens to be exactly what the R graphics engine supplies, and then translates that into some javascript that presumably will end up in an HTML file eventually. I mean, I’m guessing that this what the write.c file will handle for us later on. But right now what I don’t understand is how any of this gets wired into the R graphics engine. How does R know to call any of these functions? And where is this magical cnvs_device_() C function to whom the R code for cnvs() palms off all responsibility?

The answer to the last question is “it’s in the device.c file”, and the code for it – after Claude and I had quite a few back and forths about making the cursed thing comprehensible to me – is shown later in this section. But first, a small digression…

Code
(R code)
# The third and last of the tiny art systems tucked away in
# these code chunks. This one draws some wavy lines across
# the canvas, again using ggsketch to give them a hand-drawn
# appearance

# Data generator
sample_waves_data <- function(seed = NULL, n, width, height){

  if(!is.null(seed)) set.seed(seed)
  dat <- tibble::tibble(
    id = 1:n,
    x0 = runif(n, min = 0, max = width),
    x1 = runif(n, min = 0, max = width),
    ym = runif(n, min = 50, max = height-50),
    ht = runif(1, min = 0, max = 50),
    fq = runif(1, min = .01, max = .1),
    shade = runif(n)
  ) |> 
    dplyr::reframe(
      .by = id,
      wave0 = min(x0, x1),
      wave1 = max(x0, x1), 
      x = seq(wave0, wave1),
      y = ym + ht * sin(fq * seq(wave0, wave1)),
      shade = shade
    )
  return(dat)
}

# Top-level art generator
waves <- function(seed, n = 20, width = 746, height = 150) {
  dat <- sample_waves_data(seed, n, width, height)
  pal <- sample_palette(seed)
  plt <- plot_paths(dat, pal, width, height)
  return(plt)
}

cnvs_embed(
  expr = print(waves(seed = 2005)),
  width = 746, height = 150, bg = "#00000000"
)