xxxxxxxxxx
55
// 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
// with more loops to avoid costly computation
// 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 * bpp
loadPixels()
let logo = 105684975
let oy = ((128 + 64) * width) * bpp
let oi = (128 + 64 + 32) * bpp + oy
while (logo > 0) {
// draw a glyph
for (let gy = 0; gy < glyphWidth; gy += blockHeight) {
for (let gx = 0; gx < glyphHeight; gx += blockWidth) {
// bitfield lookup
let b = logo & 0x1
logo >>= 1
for (let by = -blockHeight / 2; by < blockHeight / 2; by += 1) {
for (let bx = -blockWidth / 2; bx < blockWidth / 2; bx += 1) {
let index = oi + (gx + bx + (gy + by) * width) * bpp
let c = b << 8
pixels[index + 0] = c
pixels[index + 1] = c
pixels[index + 2] = c
}
}
}
}
oi += glyphWidth * bpp + spacing
}
updatePixels()
}