[ SDK ] /docs/sdk/card
card
Draw an SVG card from someone's GitHub profile.
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:
- 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 bytesText from GitHub (login, name, bio, location, repo and language names) and URL parameters are cut to 1024 bytes before the script sees them.
Values#
| Value | Description |
|---|---|
let user: User | The GitHub profile the card is drawn for. |
Functions#
The data#
| Signature | Description |
|---|---|
fn param(name: string, fallback: string) -> string | a URL query parameter, e.g. ?theme=dark |
The canvas#
| Signature | Description |
|---|---|
fn size(width: i64, height: i64) -> void | 100-1200 by 50-1200, default 495 by 195 |
fn background(color: string, radius: i64) -> void | Fills 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) -> void | Draws 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) -> void | Defines 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) -> void | CSS motion inside the SVG |
The pen#
| Signature | Description |
|---|---|
fn fill(color: string) -> void | Sets the fill colour (a colour or "@name") for the shapes, text, icons and charts drawn after it. |
fn stroke(color: string, width: i64) -> void | width 0 for no outline |
fn font(size: i64, weight: Weight, mono: bool) -> void | Sets the text size (6-96), the weight and whether to use the monospace face. |
fn align(anchor: Align) -> void | Sets how text sits around its x position. |
fn opacity(percent: i64) -> void | 0-100 |
Shapes#
| Signature | Description |
|---|---|
fn rect(x: i64, y: i64, w: i64, h: i64, radius: i64) -> void | A 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) -> void | A circle with its centre at x, y. |
fn line(x1: i64, y1: i64, x2: i64, y2: i64) -> void | A 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) -> void | Draws 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) -> void | Draws 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) -> void | Draws 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#
| Signature | Description |
|---|---|
fn languages(x: i64, y: i64, w: i64, h: i64) -> void | stacked bar of user.languages |
fn heatmap(x: i64, y: i64, cell: i64, gap: i64, weeks: i64) -> void | contribution grid in the fill colour |
fn sparkline(x: i64, y: i64, w: i64, h: i64, values: []i64) -> void | in the stroke colour |
Helpers, written in Eitri#
| Signature | Description |
|---|---|
fn compact(n: i64) -> string | 1234 -> "1.2k", 12345 -> "12k", 3400000 -> "3.4M", 999 -> "999". Rounds down.written in Eitri |
fn percent(share: f64) -> string | 0.734 -> "73%". Clamped to 0-100 and rounded to the nearest whole percent.written in Eitri |
fn clip(s: string, max: i64) -> string | At most `max` characters of `s`, ending in "..." when it was cut.written in Eitri |
fn peak(values: []i64) -> i64 | The 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#
| Field | Type | Notes |
|---|---|---|
name | string | |
color | string | GitHub's colour for the language |
share | f64 | 0-1 of the user's code |
struct Repo#
| Field | Type | Notes |
|---|---|---|
name | string | |
description | string | |
language | string | "" when unknown |
stars | i64 | |
forks | i64 |
struct User#
| Field | Type | Notes |
|---|---|---|
login | string | |
name | string | falls back to the login |
bio | string | |
location | string | |
followers | i64 | |
following | i64 | |
repos | i64 | public, owned, not forks |
stars | i64 | across those repos |
forks | i64 | |
commits | i64 | this calendar year |
pull_requests | i64 | |
issues | i64 | |
contributed_to | i64 | |
joined | i64 | year the account was created |
contributions | i64 | last 365 days |
days | []i64 | daily contributions, oldest first, up to 371 |
languages | []Language | top 8 by size |
top | []Repo | top 6 by stars |
Examples#
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")
Sample profile
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")
Sample profile
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)
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)
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")
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)}")
Sample profile
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)
Sample profile
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")
}
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: 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
}