Skip to content

Docs menu // card

[ SDK ] /docs/sdk/card

card

Draw an SVG card from someone's GitHub profile.

import
import card from "card"

Your script gets the user's data and draws with canvas-style calls; the page turns them into an SVG. Coordinates and sizes are pixels from the top left. Colours are "#rgb", "#rrggbb", "#rrggbbaa", "none", or "@name" for a gradient made with card.gradient. The pen is stateful: set fill, stroke, font and alignment, then draw. Everything is clamped to the card.

Limits. A render is cut short with a runtime error, never silently truncated, when it passes any of these:

note
- a string argument over 4096 bytes, an array argument over 400 elements
- more than 2000 drawing calls, or 256 KiB of drawing output
- about 200,000 steps (loop turns and calls), 6 MB of memory (compiling a card alone takes up to 3)
- more than 20,000 units of host calls: a call costs 1, reading card.user
  costs 20 because it copies the whole profile. Read it once:
  let u = card.user
- native work counts as steps too: an array insert or remove moves one
  step per 16 elements, a string search or count one step per 32 bytes

Text from GitHub (login, name, bio, location, repo and language names) and URL parameters are cut to 1024 bytes before the script sees them.

Values#

ValueDescription
let user: UserThe GitHub profile the card is drawn for.

Functions#

The data#

SignatureDescription
fn param(name: string, fallback: string) -> stringa URL query parameter, e.g. ?theme=dark

The canvas#

SignatureDescription
fn size(width: i64, height: i64) -> void100-1200 by 50-1200, default 495 by 195
fn background(color: string, radius: i64) -> voidFills the card with a colour or a gradient ("@name"). The radius rounds the corners, up to half the card.
fn border(color: string, width: i64) -> voidDraws a frame just inside the card edge. The colour must be a plain colour, not a gradient; the width is 0-20.
fn gradient(name: string, from: string, to: string, angle: i64) -> voidDefines a linear gradient called name, to use as "@name" in fill and background. The angle is in degrees: 0 points up, 90 points right.
fn animate(kind: Motion) -> voidCSS motion inside the SVG

The pen#

SignatureDescription
fn fill(color: string) -> voidSets the fill colour (a colour or "@name") for the shapes, text, icons and charts drawn after it.
fn stroke(color: string, width: i64) -> voidwidth 0 for no outline
fn font(size: i64, weight: Weight, mono: bool) -> voidSets the text size (6-96), the weight and whether to use the monospace face.
fn align(anchor: Align) -> voidSets how text sits around its x position.
fn opacity(percent: i64) -> void0-100

Shapes#

SignatureDescription
fn rect(x: i64, y: i64, w: i64, h: i64, radius: i64) -> voidA rectangle with its top left corner at x, y. It is filled with the fill colour and outlined by the stroke, if one is set.
fn circle(x: i64, y: i64, r: i64) -> voidA circle with its centre at x, y.
fn line(x1: i64, y1: i64, x2: i64, y2: i64) -> voidA line between two points. It is drawn with the stroke; if no stroke is set it is a 1 px line in the fill colour.
fn text(x: i64, y: i64, content: string) -> voidDraws text with its baseline at y. Line breaks become spaces and the text is cut to 300 characters.
fn icon(kind: Icon, x: i64, y: i64, size: i64) -> voidDraws one of the built-in outline icons with its top left corner at x, y, scaled to size (4-256), in the fill colour.
fn avatar(x: i64, y: i64, size: i64, round: bool) -> voidDraws the user's avatar with its top left corner at x, y (size 8-512, round for a circle). A grey placeholder is drawn if there is no avatar. At most two per card.

Charts from the user's data#

SignatureDescription
fn languages(x: i64, y: i64, w: i64, h: i64) -> voidstacked bar of user.languages
fn heatmap(x: i64, y: i64, cell: i64, gap: i64, weeks: i64) -> voidcontribution grid in the fill colour
fn sparkline(x: i64, y: i64, w: i64, h: i64, values: []i64) -> voidin the stroke colour

Helpers, written in Eitri#

SignatureDescription
fn compact(n: i64) -> string1234 -> "1.2k", 12345 -> "12k", 3400000 -> "3.4M", 999 -> "999". Rounds down.written in Eitri
fn percent(share: f64) -> string0.734 -> "73%". Clamped to 0-100 and rounded to the nearest whole percent.written in Eitri
fn clip(s: string, max: i64) -> stringAt most `max` characters of `s`, ending in "..." when it was cut.written in Eitri
fn peak(values: []i64) -> i64The largest value, or 0 for none.written in Eitri

Types#

enum Weight#

.Regular.Medium.Bold

enum Align#

.Start.Middle.End

enum Icon#

.Star.Fork.Repo.People.Commit.PullRequest.Issue.Location.Flame.Code.Calendar

enum Motion#

.None.Fade.Rise.Pulse.Scan

struct Language#

FieldTypeNotes
namestring
colorstringGitHub's colour for the language
sharef640-1 of the user's code

struct Repo#

FieldTypeNotes
namestring
descriptionstring
languagestring"" when unknown
starsi64
forksi64

struct User#

FieldTypeNotes
loginstring
namestringfalls back to the login
biostring
locationstring
followersi64
followingi64
reposi64public, owned, not forks
starsi64across those repos
forksi64
commitsi64this calendar year
pull_requestsi64
issuesi64
contributed_toi64
joinedi64year the account was created
contributionsi64last 365 days
days[]i64daily contributions, oldest first, up to 371
languages[]Languagetop 8 by size
top[]Repotop 6 by stars

Examples#

The smallest card
import card from "card"

let u = card.user
card.size(420, 110)
card.background("#0d1117", 10)
card.border("#30363d", 1)

card.fill("#e6edf3")
card.font(18, .Bold, false)
card.text(24, 46, u.name)

card.fill("#8b949e")
card.font(12, .Regular, false)
card.text(24, 70, "@${u.login}, ${u.followers} followers")
The card drawn by "The smallest card"

Sample profile

Pen state and shapes
import card from "card"

card.size(420, 150)
card.background("#0d1117", 10)

card.fill("#58a6ff")
card.rect(20, 20, 120, 40, 8)

card.fill("#3fb950")
card.opacity(60)
card.circle(190, 40, 22)
card.opacity(100)

card.stroke("#f0883e", 2)
card.line(240, 20, 400, 60)
card.stroke("none", 0)

card.fill("#e6edf3")
card.icon(.Star, 20, 80, 16)
card.font(13, .Medium, false)
card.align(.Start)
card.text(44, 93, "start-aligned")
card.align(.End)
card.text(400, 93, "end-aligned")
card.align(.Middle)
card.font(12, .Regular, true)
card.text(210, 128, "centred, mono")
The card drawn by "Pen state and shapes"

Sample profile

Gradients
import card from "card"

card.size(420, 120)
card.gradient("sky", "#1b0338", "#07061f", 90)
card.gradient("neon", "#ff2bd6", "#00e5ff", 0)
card.background("@sky", 14)
card.border("@neon", 2)

card.fill("@neon")
card.rect(24, 80, 372, 6, 3)

card.fill("#ffffff")
card.font(20, .Bold, false)
card.text(24, 52, card.user.name)
The card drawn by "Gradients"

Sample profile

import card from "card"

let u = card.user
card.size(420, 230)
card.background("#0d1117", 10)

card.fill("#e6edf3")
card.font(12, .Medium, false)
card.text(20, 28, "Languages")
card.languages(20, 38, 380, 8)

card.fill("#39d353")
card.heatmap(20, 66, 5, 2, 53)

card.stroke("#58a6ff", 2)
card.sparkline(20, 150, 380, 50, u.days)
The card drawn by "Charts"

Sample profile

import card from "card"

card.size(420, 100)
card.background("#0d1117", 10)
card.animate(.Rise)

card.fill("#e6edf3")
card.font(18, .Bold, false)
card.text(24, 42, card.user.name)
card.fill("#8b949e")
card.font(12, .Regular, false)
card.text(24, 68, "each element rises in turn")
The card drawn by "Motion"

Sample profile

import card from "card"

let u = card.user
card.size(420, 130)
card.background("#0d1117", 10)
card.fill("#e6edf3")
card.font(13, .Regular, true)
card.text(20, 32, "stars     ${card.compact(u.stars)}")
card.text(20, 56, "bio       ${card.clip(u.bio, 24)}")
card.text(20, 80, "top lang  ${card.percent(u.languages[0].share)}")
card.text(20, 104, "best day  ${card.peak(u.days)}")
The card drawn by "Helpers"

Sample profile

Parameters
import card from "card"

// ?theme=light and ?accent=#d29922 change this card.
let light = card.param("theme", "dark") == "light"
let accent = card.param("accent", "#58a6ff")
let bg = if light { "#ffffff" } else { "#0d1117" }
let ink = if light { "#1f2328" } else { "#e6edf3" }

card.size(420, 90)
card.background(bg, 10)
card.fill(accent)
card.rect(0, 0, 6, 90, 0)
card.fill(ink)
card.font(18, .Bold, false)
card.text(24, 50, card.user.name)
The card drawn by "Parameters"

Sample profile

Looping over data
import card from "card"

let u = card.user
let rows = u.top.len()
card.size(420, 50 + 28 * rows)
card.background("#0d1117", 10)
card.fill("#e6edf3")
card.font(14, .Bold, false)
card.text(20, 30, "Top repositories")

card.font(12, .Regular, false)
for i in 0..rows {
    let r = u.top[i]
    let y = 58 + 28 * i
    card.fill("#e6edf3")
    card.align(.Start)
    card.text(20, y, r.name)
    card.fill("#8b949e")
    card.align(.End)
    card.text(400, y, "${card.compact(r.stars)} stars")
}
The card drawn by "Looping over data"

Sample profile

Limits#

  • A card is 100-1200 px wide and 50-1200 px high (default 495 by 195). Positions may sit up to 50 px outside it; sizes and numbers are clamped.
  • A render records at most 2,000 draw calls; the next one faults. The SVG is limited to 3,000 elements and about 600 KB, and the text of one text call to 300 characters.
  • user.days holds up to 371 days, user.languages up to 8 entries and user.top up to 6.
  • Colours are "#rgb", "#rrggbb", "#rrggbbaa", "none", or "@name" for a gradient defined earlier. Anything else is ignored and the pen keeps its previous colour.

Declaration file#

This page is generated from the file below, served as /eitri/sdk/card.eith. Types and signatures are exactly what the compiler checks scripts against. Back to all modules.

card.eith
// card: draw an SVG card from someone's GitHub profile.
//
// Your script gets the user's data and draws with canvas-style calls; the
// page turns them into an SVG. Coordinates and sizes are pixels from the top
// left. Colours are "#rgb", "#rrggbb", "#rrggbbaa", "none", or "@name" for a
// gradient made with card.gradient. The pen is stateful: set fill, stroke,
// font and alignment, then draw. Everything is clamped to the card.
//
// Limits. A render is cut short with a runtime error, never silently
// truncated, when it passes any of these:
//   - a string argument over 4096 bytes, an array argument over 400 elements
//   - more than 2000 drawing calls, or 256 KiB of drawing output
//   - about 200,000 steps (loop turns and calls), 6 MB of memory (compiling a card alone takes up to 3)
//   - more than 20,000 units of host calls: a call costs 1, reading card.user
//     costs 20 because it copies the whole profile. Read it once:
//     let u = card.user
//   - native work counts as steps too: an array insert or remove moves one
//     step per 16 elements, a string search or count one step per 32 bytes
// Text from GitHub (login, name, bio, location, repo and language names) and
// URL parameters are cut to 1024 bytes before the script sees them.

pub struct Language {
    pub name: string
    pub color: string       // GitHub's colour for the language
    pub share: f64          // 0-1 of the user's code
}

pub struct Repo {
    pub name: string
    pub description: string
    pub language: string    // "" when unknown
    pub stars: i64
    pub forks: i64
}

pub struct User {
    pub login: string
    pub name: string        // falls back to the login
    pub bio: string
    pub location: string
    pub followers: i64
    pub following: i64
    pub repos: i64          // public, owned, not forks
    pub stars: i64          // across those repos
    pub forks: i64
    pub commits: i64        // this calendar year
    pub pull_requests: i64
    pub issues: i64
    pub contributed_to: i64
    pub joined: i64         // year the account was created
    pub contributions: i64  // last 365 days
    pub days: []i64         // daily contributions, oldest first, up to 371
    pub languages: []Language   // top 8 by size
    pub top: []Repo             // top 6 by stars
}

pub enum Weight {
    Regular
    Medium
    Bold
}

pub enum Align {
    Start
    Middle
    End
}

pub enum Icon {
    Star
    Fork
    Repo
    People
    Commit
    PullRequest
    Issue
    Location
    Flame
    Code
    Calendar
}

pub enum Motion {
    None
    Fade
    Rise
    Pulse
    Scan
}

// The data.
pub let user: User
pub fn param(name: string, fallback: string) -> string  // a URL query parameter, e.g. ?theme=dark

// The canvas.
pub fn size(width: i64, height: i64) -> void            // 100-1200 by 50-1200, default 495 by 195
pub fn background(color: string, radius: i64) -> void
pub fn border(color: string, width: i64) -> void
pub fn gradient(name: string, from: string, to: string, angle: i64) -> void
pub fn animate(kind: Motion) -> void                    // CSS motion inside the SVG

// The pen.
pub fn fill(color: string) -> void
pub fn stroke(color: string, width: i64) -> void        // width 0 for no outline
pub fn font(size: i64, weight: Weight, mono: bool) -> void
pub fn align(anchor: Align) -> void
pub fn opacity(percent: i64) -> void                    // 0-100

// Shapes.
pub fn rect(x: i64, y: i64, w: i64, h: i64, radius: i64) -> void
pub fn circle(x: i64, y: i64, r: i64) -> void
pub fn line(x1: i64, y1: i64, x2: i64, y2: i64) -> void
pub fn text(x: i64, y: i64, content: string) -> void
pub fn icon(kind: Icon, x: i64, y: i64, size: i64) -> void
pub fn avatar(x: i64, y: i64, size: i64, round: bool) -> void

// Charts from the user's data.
pub fn languages(x: i64, y: i64, w: i64, h: i64) -> void        // stacked bar of user.languages
pub fn heatmap(x: i64, y: i64, cell: i64, gap: i64, weeks: i64) -> void  // contribution grid in the fill colour
pub fn sparkline(x: i64, y: i64, w: i64, h: i64, values: []i64) -> void  // in the stroke colour

// Helpers, written in Eitri.

// 1234 -> "1.2k", 12345 -> "12k", 3400000 -> "3.4M", 999 -> "999". Rounds down.
pub fn compact(n: i64) -> string {
    if n < 1000 {
        return "${n}"
    }
    if n < 1000000 {
        return scaled(n / 100, "k")
    }
    return scaled(n / 100000, "M")
}

// tenths of a unit: 12 -> "1.2", 10 -> "1", 123 -> "12".
fn scaled(tenths: i64, unit: string) -> string {
    if tenths >= 100 || tenths % 10 == 0 {
        return "${tenths / 10}${unit}"
    }
    return "${tenths / 10}.${tenths % 10}${unit}"
}

// 0.734 -> "73%". Clamped to 0-100 and rounded to the nearest whole percent.
pub fn percent(share: f64) -> string {
    var p = i64(share * 100.0 + 0.5)
    if p < 0 {
        p = 0
    }
    if p > 100 {
        p = 100
    }
    return "${p}%"
}

// At most `max` characters of `s`, ending in "..." when it was cut.
pub fn clip(s: string, max: i64) -> string {
    var out = ""
    var n = 0
    for c in s.chars() {
        if n == max {
            return out + "..."
        }
        out = out + "${c}"
        n += 1
    }
    return out
}

// The largest value, or 0 for none.
pub fn peak(values: []i64) -> i64 {
    var best = 0
    for v in values {
        if v > best {
            best = v
        }
    }
    return best
}