xxxxxxxxxx
67
// 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 l1 = 1
let l2 = 105684975
let logo = l2
/*
for (let g = 0; g < 3; g += 1) {
let glyph1 = (l1 >> (g * 9)) & 511
let glyph2 = (l2 >> (g * 9)) & 511
let glyph = lerp(glyph1, glyph2, round(abs(sin(frameCount / 300 * PI * 2)) * 4) / 4)
logo = logo | (glyph << (g * 9))
}
*/
let oy = (128 * width) * bpp
let oi = (128 + 64) * 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 = 0; by < blockHeight; by += 1) {
for (let bx = 0; bx < blockWidth; 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()
}