xxxxxxxxxx
53
// tiny text rendering demonstrating 3 glyphs using a bitfield (a 32 bit value)
// basically use a 3x3 font (upscaled), each glyph is encoded as 9 bit
// no mul nor div
// can be cool if the bitfield is modulated in realtime
// mostly related: https://github.com/Michaelangel007/nanofont3x4
function setup() {
pixelDensity(1)
createCanvas(1920/2, 1080/2)
background(0)
}
function draw() {
background(0)
// bit shift should be changed accordingly if these change
let blockWidth = 64
let glyphWidth = blockWidth * 4
let glyphHeight = blockWidth * 3
loadPixels()
let logo = 105684975
let sx = (128 + 128 * width) * 4
let ox = sx
while (logo > 0) {
// draw a glyph
for (let i = 0; i < glyphWidth * glyphHeight; i += 1) {
let x = i & (glyphWidth - 1)
let y = i >> 8
// bitfield lookup
let bx = x >> 6
let by = y >> 6
let bl = bx + by + (by << 1) // with mul: by * 3
let b = (logo >> bl) & 0x1
// without condition
//let col = ((b << 8) - 1 + ((bx+1) & 3)) & b
if (bx < 3) {
let index = ox + (x + y * width) * 4
let c = b << 8
pixels[index + 0] = c
pixels[index + 1] = c
pixels[index + 2] = c
}
}
logo >>= 9
ox += glyphWidth * 4
}
updatePixels()
}