Media and drawing
Media
Media and drawing
media.md

Media

Pictures (img), pictures that are drawn on (paint, canvas layers=), codes (scan), the camera, video and sound (vid, aud, reel: a film of tracks, keyframes and grades), speech and microphones (recorded, too), an audio or video led from the page, and sounds with no element (sound). Drawing with your own code is in drawing.md. Back to the map.

Examplea whole program
state shot: bytes? = none
state info = ""
action choose
let file = fs.pick("image/png,image/jpeg")?
if file == none return
let im = img.decode(file.data)?
info = `${im.width} by ${im.height}`
shot = img.jpeg(img.fit(im, 1600)?, 80)?
column gap=8 padding=16
button "Choose a picture" press=choose disabled=pending(choose)
if shot
image shot.url() max-width=100%
text info color=dim
text (failure(choose) ?? "") color=danger

Mental model#

img#

Examplea whole program
state said = ""
state frames: bytes[] = []
state out: bytes? = none
action open
let f = fs.pick("image/*,.tif,.tiff,.dng")?
if f == none return
let info = img.info(f.data)?
said = `${info.format}, ${info.width} by ${info.height}, ${info.depth} bits, ${info.space == "" ? "no space named" : info.space}`
if info.exif.model != "" said += `, taken with a ${info.exif.model}`
frames = []
if info.frames > 1
for fr in img.frames(f.data)?
frames.push(img.png(fr)?)
return
let p = img.decode(f.data, {deep: true})?
let wide = img.convert(img.fit(p, 2000)?, "display-p3")?
out = img.tiff(wide)?
frames = [img.png(img.depth(wide, 8)?)?]
column gap=8 padding=16
row gap=8
button "Open a picture" press=open disabled=pending(open)
if out
link "Get it as a TIFF" href=out.url("image/tiff") download="picture.tif"
text said
row gap=8 flex-wrap=wrap
for f in frames
image f.url("image/png") max-width=240
text (failure(open) ?? "") color=danger

Formats#

A photo that fits#

What a file says#

Deep pictures#

Colour#

paint#

Examplea whole program
state stack: {sheet: sheet, opacity: number?, blend: string?}[] = []
state color = "#1d4ed8"
state picked = ""
action start
let paper = paint.new(600, 400, "#ffffff")
let ink = paint.new(600, 400)
paint.text(paper, "Draw here", 20, 20, {color: "#999999", size: 24})
stack = [{sheet: paper}, {sheet: ink, opacity: 0.9, blend: "multiply"}]
action stroke(p: {x: number, y: number})
let top = stack.at(-1)
if top == none return
paint.line(top.sheet, p.x, p.y, p.x + 1, p.y + 1, {color, size: 8, soft: 0.4})
action soften
let top = stack.at(-1)
if top == none return
paint.put(top.sheet, img.blur(paint.image(top.sheet), 6)?)
action look(p: {x: number, y: number})
picked = paint.color(stack, p.x, p.y)
task
start()
column gap=8 padding=16
canvas layers=stack width=600 height=400 pointerdown=stroke pointermove=stroke dblclick={soften()} max-width=100%
row gap=8
button "Blur the ink" press=soften
text picked

Brushes#

Examplea whole program
state ink = paint.new(600, 300)
state mask = paint.new(600, 300)
state tip = paint.new(32, 32)
state stack: {sheet: sheet?, mask: sheet?, blend: string?, filter: string?, clip: bool?}[] = []
state erasing = false
state last: {x: number, y: number}? = none
action start
let paper = paint.new(600, 300, "#ffffff")
paint.shape(tip, "ellipse", 4, 10, 28, 22, {color: "#000000", fill: true})
paint.shape(mask, "rect", 20, 20, 580, 280, {color: "#000000", fill: true, radius: 24})
paint.path(paper, [[[40, 260], [300, 40], [560, 260]], [[220, 220], [300, 140], [380, 220]]], {color: "#eeeeee", fill: true, rule: "evenodd"})
let m = paint.measure("Draw here", {size: 28, weight: 700})
paint.text(paper, "Draw here", 300, 20, {size: 28, weight: 700, align: "center", color: "#999999", spacing: 1})
paint.shape(paper, "line", 300 - m.width / 2, 24 + m.height, 300 + m.width / 2, 24 + m.height, {color: "#cccccc", size: 2})
stack = [{sheet: paper}, {sheet: ink, mask, blend: "multiply"}, {filter: "contrast(1.1)"}]
action down(p: {x: number, y: number, width: number})
last = {x: p.x * 600 / p.width, y: p.y * 600 / p.width}
action stroke(p: {x: number, y: number, width: number, pressure: number})
let from = last
if from == none return
let to = {x: p.x * 600 / p.width, y: p.y * 600 / p.width}
paint.line(ink, from.x, from.y, to.x, to.y, {tip, color: "#1d4ed8", size: 12 + 20 * p.pressure, spacing: 0.2, follow: true, scatter: 0.1, erase: erasing})
last = to
action spray(p: {x: number, y: number, width: number})
paint.stamp(ink, tip, p.x * 600 / p.width, p.y * 600 / p.width, {size: 48, turn: 45, color: "#dc2626", opacity: 0.5})
action copyOut
let both = paint.new(600, 300)
paint.draw(both, stack)
paint.draw(both, ink, 400, 200, {width: 150, height: 75, turn: -8, opacity: 0.8, blend: "multiply"})
fs.save(img.png(paint.image(both))?, "drawing.png")?
paint.drop(both)
task
start()
column gap=8 padding=16
canvas layers=stack width=600 height=300 pointerdown=down pointermove=stroke pointerup={last = none} dblclick=spray max-width=100%
row gap=8
button (erasing ? "Draw" : "Erase") press={erasing = !erasing}
button "Save" press=copyOut

Layers#

canvas layers=#

scan#

camera#

Examplea whole program
state live = false
state seen = ""
action found(c: {format: string, text: string})
seen = `${c.format}: ${c.text}`
live = false
action snap
let p = cam.shot()?
let codes = scan.read(p)?
seen = `${p.width} by ${p.height}, ${codes.length} codes`
column gap=8 padding=16
camera on=live facing="back" scan=found max-width=100%
row gap=8
button (live ? "Stop" : "Start") press={live = !live}
button "Snap" press=snap
text seen
text (cam.error ?? "") color=danger

vid, aud and reel#

Examplea whole program
state clips: {src: video, from: number, to: number, dissolve: number?}[] = []
state titles: {at: number, len: number, text: string?, size: number?, color: string?}[] = []
state time = 0
state on = false
action open
let f = fs.pick("video/mp4,video/quicktime")?
if f == none return
let v = vid.open(f.data)?
let half = v.duration / 2
clips = [{src: v, from: 0, to: half}, {src: v, from: half, to: v.duration, dissolve: 0.5}]
titles = [{at: 0, len: 2, text: "A film", size: 0.1, color: "#ffffff"}]
action save
let film = vid.export(clips, {layers: titles, width: 1280, height: 720})?
fs.save(film, "film.mp4")?
column gap=8 padding=16
reel clips=clips layers=titles at=time playing=on max-width=100%
row gap=8
button "Open" press=open
button (on ? "Pause" : "Play") press={on = !on}
button "Export" press=save disabled=pending(save)
if pending(save)
progress value=vid.progress max=1
text `${time.toFixed(1)} s` color=dim

vid#

A film: clips, tracks, keys#

Examplea whole program
shape Clip
src: video
from: number
to: number
track: number?
at: number?
scale: number?
x: number?
y: number?
opacity: number?
dissolve: number?
wipe: string?
reverse: bool?
crop: {left: number?, right: number?, top: number?, bottom: number?, round: number?}?
grade: {gain: number[]?, lift: number[]?, gamma: number[]?}?
keys: {at: number, x: number?, opacity: number?, ease: string?}[]?
end: {scale: number?}?
state clips: Clip[] = []
state titles: {at: number, len: number, text: string?, size: number?, italic: bool?, stroke: string?, strokeWidth: number?, y: number?}[] = []
state music: {src: sound, at: number, volume: number?, pan: number?, keys: {at: number, volume: number?}[]?}[] = []
state time = 0
state on = false
state heard = {peak: [0, 0], rms: [0, 0]}
action open
let f = fs.pick("video/mp4,video/quicktime")?
if f == none return
let v = vid.open(f.data)?
let half = v.duration / 2
clips = [{src: v, from: 0, to: half, end: {scale: 1.1}}, {src: v, from: half, to: v.duration, dissolve: 0.5, wipe: "iris", grade: {gain: [1.1, 1, 0.9]}}, {src: v, from: 0, to: 2, track: 1, at: 1, scale: 0.3, x: 0.3, y: -0.3, crop: {round: 0.5}, keys: [{at: 0, opacity: 0}, {at: 0.5, opacity: 1}, {at: 1.5, x: 0.3, ease: "hold"}, {at: 2, x: -0.3, ease: "out"}]}]
titles = [{at: 0, len: 2, text: "A film", size: 0.1, italic: true, stroke: "#000000", strokeWidth: 0.004, y: 0.35}]
action score
let f = fs.pick("audio/*")?
if f == none return
let s = aud.open(f.data)?
music = [{src: s, at: 0, volume: 0.8, pan: -0.2, keys: [{at: s.duration - 2, volume: 0.8}, {at: s.duration, volume: 0}]}]
action save
let film = vid.export(clips, {layers: titles, sounds: music, width: 1280, height: 720})?
fs.save(film, "film.mp4")?
column gap=8 padding=16
reel clips=clips layers=titles sounds=music at=time playing=on levels=heard frame=({width: 1280, height: 720}) max-width=100%
row gap=8 align-items=center
button "Open" press=open
button "Music" press=score
button (on ? "Pause" : "Play") press={on = !on}
button "Export" press=save disabled=pending(save)
progress value=heard.peak[0] max=1
progress value=heard.peak[1] max=1
text `${time.toFixed(1)} s` color=dim

Moving: end and keys#

Tracks#

Layers over the clips#

Grades#

aud#

Levels#

reel#

Speech and microphones#

Recording a microphone#

Examplea whole program
state rec: recording? = none
state clip: bytes? = none
state bars: number[] = []
state seconds = 0
action start
rec = mic.record({levels: true})?
action hold
if rec
if rec.state == "paused"
rec.resume()
else
rec.pause()
action finish
if rec
let got = rec.stop()?
clip = got.bytes
bars = got.peaks
seconds = got.length
rec = none
action drop
rec?.cancel()
rec = none
column gap=8 padding=16
if rec
row gap=8 align-items=center
text `${Math.round(rec.time)} s`
progress value=rec.level max=1
button (rec.state == "paused" ? "Go on" : "Pause") press=hold
button "Done" press=finish
button "Drop" press=drop
else
button "Record" press=start
if clip
text `${Math.round(seconds)} s, ${bars.length} bars`
text (failure(start) ?? "") color=danger
Examplea whole program
state ear: meter? = none
action test
ear = mic.meter()?
action done
ear?.stop()
ear = none
column gap=8 padding=16
if ear
progress value=ear.level max=1
button "That works" press=done
else
button "Test the microphone" press=test

audio and video#

Examplea whole program
state on = false
state at = 0
state length = 0
state speed = 1
column gap=8 padding=16
audio src="/file/Clip/7/sound/note.webm" preload="metadata" playing=on at=at rate=speed duration=length ended={at = 0}
row gap=8 align-items=center
button (on ? "Pause" : "Play") press={on = !on}
input type="range" min=0 max=length step=0.01 value=at
text `${Math.round(at)} / ${Math.round(length)} s`
button "2x" press={speed = speed == 1 ? 2 : 1}

Sound#

Examplea whole program
state note = ""
action ping
let r = sound.play("ping", {volume: 0.5})
note = r.played ? "" : r.why
action ring
sound.play("ring", {loop: true, id: "call"})
action answer
sound.stop("call")
column gap=8 padding=16
if !sound.ready
text "Press anywhere to turn sound on" color=dim
row gap=8
button "Ping" press=ping
button "Ring" press=ring
button "Answer" press=answer
text note
text (sound.error ?? "") color=danger

Pitfalls#

Where a new call goes#

In the source#

esc
1072 sections of 111 pages, ranked as you type.A heading's words count for more than what stands under it.
↑↓move
↵open
ranked by BM25, in the page