xxxxxxxxxx
60
// 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
// reduced loops (bits are ordered differently, it draw per line)
// 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 bpp = 4
let blockWidth = 64
let blockHeight = 64
let glyphWidth = blockWidth * 3
let glyphHeight = blockHeight * 3
let spacing = 2
loadPixels()
let logo = 104667903
let oy = ((128 + 32) * width) * bpp
let os = (128 + 64) * bpp + oy
let oi = os
let blocksWidth = 9
let blocksHeight = 3
for (let y = 0; y < blocksHeight; y += 1) {
for (let x = 0; x < blocksWidth; x += 1) {
// bitfield lookup
let b = logo & 0x1
logo >>= 1
for (let by = 0; by < blockHeight; by += 1) {
for (let bx = 0; bx < blockWidth; bx += 1) {
let index = oi + (bx + (by) * width) * bpp
let c = b << 8
pixels[index + 0] = c
pixels[index + 1] = c
pixels[index + 2] = c
}
}
oi += blockWidth * bpp
if ((x+1) % 3 == 0) {
oi += spacing * bpp
}
}
os += blockHeight * width * bpp
oi = os
}
updatePixels()
}