--- title: "Stickers, people, templates and diagrams" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Stickers, people, templates and diagrams} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>", dev = "ragg_png", dpi = 110, fig.width = 7, fig.height = 3, out.width = "100%") library(scratchy) library(ggplot2) ``` ## Stickers Twelve community libraries from come with the package. Stickers are native Excalidraw elements: hand-drawn, editable, and recolourable. ```{r} table(exc_stickers()$library) ``` ```{r, fig.height = 4, fig.alt = "A sheet of hand-drawn stick figures in five columns, each with its name"} exc_sticker_sheet("stick-figures", columns = 5) |> plot() ``` ```{r, fig.alt = "The title Sticker party above a stick-figure girl, an R logo, a speech bubble and an orange star"} exc_scene(exc_text("Sticker party", 0, 0, font_size = 36)) |> exc_sticker("stick-figures:Girl", 0, 60, width = 90) |> exc_sticker("r-icons:1", 120, 70, width = 90) |> exc_sticker("bubbles:1", 230, 50, width = 160) |> exc_sticker("stars:3", 420, 70, width = 70, stroke = "#f08c00", fill = "#ffec99", fill_style = "solid") |> plot() ``` Any other library works too: `exc_library("https://...excalidrawlib")` and `exc_library_item()`. ## Hand-drawn people `exc_peep()` draws people in Pablo Stanley's Open Peeps style. Each seed is a different person; see `exc_peep_options()` for faces, heads and accessories. ```{r, fig.alt = "Three hand-drawn Open Peeps people side by side, one smiling, one in a light blue top, one with a cheeky face"} exc_scene() |> exc_peep(0, 0, seed = "Ada", face = "smile") |> exc_peep(170, 0, seed = "Grace", clothing = "#a5d8ff") |> exc_peep(340, 0, seed = "Hadley", face = "cheeky") |> plot() ``` ## Templates Design a card or poster in Excalidraw, with `{{column}}` in its text, and shapes labelled `{{chart:name}}` or `{{image:name}}` where charts and images should go. Then fill it from data: ```{r, fig.height = 3.5, fig.alt = "A hand-drawn KPI card reading Sketches drawn, 1,204, +18% this month, with a small rising line chart"} card <- exc_template_fill( exc_template_example(), data = list(metric = "Sketches drawn", value = "1,204", change = "+18% this month"), charts = list(trend = ggplot(data.frame(x = 1:12, y = c(3, 4, 4, 6, 5, 7, 8, 8, 9, 11, 10, 12)), aes(x, y)) + geom_line(linewidth = 1, colour = "#1971c2") + labs(x = NULL, y = NULL) + theme_excalidraw(base_size = 11)) ) plot(card) ``` `exc_template_render()` does this for every row of a data frame and saves one file per row. ## Diagrams `exc_mermaid()` converts Mermaid text into editable Excalidraw shapes with Excalidraw's own converter (it needs Chrome, through the chromote package). `exc_d2()` renders D2 diagrams in D2's sketch mode, if the `d2` program is installed. ```{r, eval = FALSE} exc_mermaid("flowchart LR\n A[Data] --> B[Sketch it] --> C[Share]") |> plot() exc_d2("data -> sketch -> share") |> plot() ``` ## Drawings that draw themselves `exc_animate()` saves a scene as an SVG that sketches itself, element by element in z-order: strokes are traced, fills fade in after their outline and text is written letter by letter. The SVG plays in any browser, also as an `` in a web page, slide or README. ```{r} chart <- as_excalidraw(ggplot(mpg, aes(class)) + geom_bar()) svg <- tempfile(fileext = ".svg") exc_animate(chart, svg, duration = 5, loop = TRUE) ``` ```{r, echo = FALSE} htmltools::img(src = knitr::image_uri(svg), alt = "A bar chart drawing itself", style = "width: 100%") ``` Save to `.gif` to record the animation in headless Chrome instead (it needs the chromote package, and gifski or magick to put the frames together): ```{r, eval = FALSE} exc_animate(chart, "chart.gif", loop = TRUE) ``` ## More fonts ```{r} exc_fonts() ``` `exc_font_fetch("xkcd")` downloads the xkcd Script font (non-commercial licence) for `font = "xkcd Script"`. ## Sketch Studio `run_sketch_studio()` puts all of this behind a point-and-click app, around the live Excalidraw editor.