Media and drawing
Drawing
Examplea whole program
state values = [4, 8, 15, 16, 23, 42]state hot = -1fn chart(g: pen, size: {width: number, height: number, time: number}) draw.clear(g, "card") let w = size.width / values.length let top = Math.max(...values) let i = 0 for v in values let h = v / top * (size.height - 24) draw.rect(g, i * w + 4, size.height - h, w - 8, h, {fill: i == hot ? "accent" : "dim", radius: 4}) draw.text(g, `${v}`, i * w + w / 2, size.height - h - 16, {color: "fg", size: 12, align: "center"}) i += 1action over(p: {x: number, width: number}) hot = Math.floor(p.x / (p.width / values.length))surface draw=chart pointermove=over pointerleave={hot = -1} width=100% height=240Mental model#
surface (2D)#
Examplea whole program
state running = truestate at = {x: 120, y: 80}fn scene(g: pen, size: {width: number, height: number, time: number}) draw.clear(g, "bg") draw.line(g, 0, size.height - 0.5, size.width, size.height - 0.5, {color: "line", dash: [4, 4]}) draw.path(g, [[10, 60], [60, 20], [80, 20, 100, 100, 140, 60]], {stroke: "ok", width: 2}) draw.push(g) draw.move(g, at.x, at.y) draw.turn(g, size.time * 90) draw.rect(g, -20, -20, 40, 40, {fill: "accent", radius: 6}) draw.pop(g) let label = `${Math.round(at.x)}, ${Math.round(at.y)}` let m = draw.measure(g, label, {size: 12}) draw.text(g, label, size.width - m.width - 8, 8, {color: "dim", size: 12})action moved(p: {x: number, y: number}) at = {x: p.x, y: p.y}column gap=8 padding=16 surface draw=scene animate=running pointerdown=moved pointermove=moved height=200 button (running ? "Stop" : "Spin") press={running = !running}Gradients and patterns#
Layers#
A drawing as a picture#
Examplea whole program
state shot: bytes? = nonefn ring(x: number, y: number, r: number) = [[[x - r, y - r], [x + r, y - r], [x + r, y + r], [x - r, y + r]], [[x - r / 2, y - r / 2], [x + r / 2, y - r / 2], [x + r / 2, y + r / 2], [x - r / 2, y + r / 2]]]fn card(g: pen, size: {width: number, height: number, time: number}) draw.rect(g, 0, 0, size.width, size.height, {fill: {linear: [0, 0, size.width, size.height], stops: ["#0a84ff", "#5e5ce6"]}}) draw.circle(g, 70, 70, 50, {fill: {radial: [70, 70, 50], stops: [[0, "#ffffff"], [1, "#ffffff00"]]}}) draw.path(g, ring(200, 70, 40), {fill: "#ffffff", rule: "evenodd", shadow: {y: 4, blur: 12, color: "#0006"}}) draw.layer(g, {opacity: 0.5}) draw.rect(g, 270, 30, 60, 60, {fill: "#ff3b30"}) draw.rect(g, 300, 50, 60, 60, {fill: "#ffcc00"}) draw.pop(g) draw.push(g) draw.clip(g, ring(430, 70, 40), {rule: "evenodd"}) draw.rect(g, 390, 30, 80, 80, {fill: "#34c759", blend: "screen"}) draw.pop(g) let label = "Mote" let m = draw.measure(label, {size: 32, weight: 700, spacing: 2}) draw.text(g, label, size.width - m.width - 16, size.height - 16 - m.descent, {size: 32, weight: 700, spacing: 2, baseline: "alphabetic", color: "#ffffff", stroke: "#0008", width: 1})action save shot = img.png(draw.picture(500, 140, card, 2))? fs.save(shot, "card.png")?column gap=8 padding=16 surface draw=card width=500 height=140 max-width=100% button "Save as a PNG" press=save disabled=pending(save)Typing on a surface#
Examplea whole program
state words = ""state draft = ""state inside = falsederive spot = {x: 8 + draw.measure(words + draft, {size: 18}).width, y: 12, height: 22}fn paper(g: pen, size: {width: number, height: number, time: number}) draw.clear(g, "card") draw.text(g, words + draft, 8, 12, {size: 18, color: "fg"}) if inside draw.rect(g, spot.x, spot.y, 1, spot.height, {fill: "accent"})action wrote(said: string) words += saidaction keyed(k: string) if k == "Backspace" words = words.slice(0, -1) if k == "Enter" words = ""surface draw=paper typed=wrote composing={draft = it} caret=spot key=keyed focus={inside = true} blur={inside = false} width=100% height=48surface gl#
Examplea whole program
state quad = [-1, -1, 1, -1, -1, 1, 1, 1]state tint = [1, 0.4, 0.1]fn frame(g: pen, size: {width: number, height: number, time: number, pixels: number}) gl.clear(g, "#000000ff") gl.draw(g, {vs: "in vec2 p; out vec2 uv; void main(){ uv = p * 0.5 + 0.5; gl_Position = vec4(p, 0.0, 1.0); }", fs: "in vec2 uv; uniform vec3 tint; uniform float time; out vec4 o; void main(){ o = vec4(tint * (0.5 + 0.5 * sin(time + uv.x * 6.0)), 1.0); }", attributes: {p: {data: quad, size: 2}}, uniforms: {tint, time: size.time}, count: 4, mode: "strip"})surface gl draw=frame animate=true width=100% height=300A pen that is not on the page#
Examplea whole program
state tri = [-1, -1, 1, -1, 0, 1]state said = ""action render let g = gl.pen(64, 64) gl.clear(g, "#000000ff") gl.draw(g, {vs: "in vec2 p; void main(){ gl_Position = vec4(p, 0.0, 1.0); }", fs: "out vec4 o; void main(){ o = vec4(1.0, 0.5, 0.0, 1.0); }", attributes: {p: {data: tri, size: 2}}, count: 3}) let p = gl.image(g) let mid = gl.pixel(g, 32, 40) said = `${p.width} by ${p.height}, the middle is ${mid.join(" ")}`button "Render" press=rendertext saidA picture through a shader#
Examplea whole program
state shot: bytes? = nonestate strength = 1fn sepia(): string = "uniform sampler2D src; uniform float strength; in vec2 at; out vec4 o; void main(){ vec4 c = texture(src, at); float g = dot(c.rgb, vec3(0.299, 0.587, 0.114)); o = vec4(mix(c.rgb, g * vec3(1.07, 0.74, 0.43), strength), c.a); }"action choose let file = fs.pick("image/png,image/jpeg")? if file == none return let p = img.decode(file.data)? shot = img.jpeg(gl.filter(p, sepia(), {strength}), 85)?column gap=8 padding=16 button "Choose a picture" press=choose disabled=pending(choose) if shot image shot.url("image/jpeg") max-width=100% text (failure(choose) ?? "") color=dangerThe 3D canvas: canvas boxes=#
Examplea whole program
shape Box id: string name: string x: number y: number z: number w: number d: number h: number color: string?state boxes: Box[] = [{id: "a", name: "Base", x: 0, y: 0, z: 0, w: 600, d: 560, h: 720, color: "#c8a26b"}, {id: "b", name: "Tall", x: 600, y: 0, z: 0, w: 400, d: 560, h: 2100}]state view = "iso"state picked = ""action pick(id: string) picked = idcolumn gap=8 padding=16 height=480 row gap=8 for v in ["iso", "front", "left", "plan"] button v press={view = v} canvas boxes=boxes view=view change=pick flex=1 text (picked == "" ? "Press a box" : `Picked ${picked}`)The lit picture: render=#
The 3D canvas: canvas scene=#
Examplea whole program
state turn = 25state picked = ""derive oak = three.material({color: "#c8a878", roughness: 0.6})derive steel = three.material({color: "#c9ccd1", roughness: 0.2, metalness: 1})derive table = three.group([three.mesh(three.box(120, 70, 4), oak, {position: [0, 0, 72], id: "top", name: "Top"}), three.mesh(three.cylinder(3, 70), steel, {position: [-52, -28, 35]}), three.mesh(three.cylinder(3, 70), steel, {position: [52, 28, 35]})], {rotation: [0, 0, turn]})derive world = three.scene([table], {lights: [three.sun(300, 50, 1), three.sky(1)]})derive eye = three.perspective({position: [220, -300, 200], target: [0, 0, 40], fov: 40})action pick(id: string) picked = idcolumn gap=8 padding=16 height=480 canvas scene=world camera=eye change=pick flex=1 row gap=8 button "Turn" press={turn += 15} text (picked == "" ? "Press a thing" : `Picked ${picked}`)Pitfalls#
Where a new call goes#
In the source#
Edit this page:
docs/drawing.md