gateway-services / src /canvas /welcomeCard.js
Archnemix's picture
Upload 21 files
e903adb verified
Raw History Blame Contribute Delete
3.35 kB
import { Canvas, loadImage } from 'skia-canvas'
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import { downloadBinary } from '../storage/hfClient.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const DEFAULT_BACKGROUNDS = {
default1: path.join(__dirname, '../assets/backgrounds/default1.png'),
default2: path.join(__dirname, '../assets/backgrounds/default2.png'),
default3: path.join(__dirname, '../assets/backgrounds/default3.png'),
}
const WIDTH = 800
const HEIGHT = 200
// Pre-load all default backgrounds once at module load time
const defaultBackgroundCache = new Map()
await Promise.all(
Object.entries(DEFAULT_BACKGROUNDS).map(async ([key, filePath]) => {
try {
const image = await loadImage(filePath)
defaultBackgroundCache.set(key, image)
console.log(`[card] Pre-loaded background: ${key}`)
} catch (err) {
console.error(`[card] Failed to pre-load background "${key}":`, err.message)
}
})
)
export async function generateWelcomeCard(data) {
const canvas = new Canvas(WIDTH, HEIGHT)
const ctx = canvas.getContext('2d')
try {
// STEP 1 β€” Load background
let bgImage
try {
if (data.isCustom) {
const buffer = await downloadBinary(`guilds/${data.guildId}/assets/welcome_bg.png`)
if (buffer) {
bgImage = await loadImage(buffer)
} else {
bgImage = defaultBackgroundCache.get('default1')
}
} else if (defaultBackgroundCache.has(data.background)) {
bgImage = defaultBackgroundCache.get(data.background)
} else {
bgImage = defaultBackgroundCache.get('default1')
}
} catch (err) {
console.error('[card] Background load failed, using default:', err.message)
bgImage = defaultBackgroundCache.get('default1')
}
ctx.drawImage(bgImage, 0, 0, WIDTH, HEIGHT)
// STEP 2 β€” Dark overlay
ctx.fillStyle = 'rgba(0, 0, 0, 0.45)'
ctx.fillRect(0, 0, WIDTH, HEIGHT)
// STEP 3 β€” Accent bar
ctx.fillStyle = data.cardAccentColor
ctx.fillRect(0, 0, 6, HEIGHT)
// STEP 4 β€” Avatar
const avatarImage = await loadImage(data.avatarURL)
const centerX = 110
const centerY = 100
const radius = 70
ctx.save()
ctx.beginPath()
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2)
ctx.clip()
ctx.drawImage(avatarImage, centerX - radius, centerY - radius, radius * 2, radius * 2)
ctx.restore()
ctx.beginPath()
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2)
ctx.strokeStyle = data.cardAccentColor
ctx.lineWidth = 4
ctx.stroke()
// STEP 5 β€” Text
// Welcome label
ctx.font = 'bold 15px DejaVu Sans'
ctx.fillStyle = data.cardAccentColor
ctx.fillText('WELCOME TO ' + data.guildName.toUpperCase(), 200, 70)
// Username
ctx.font = 'bold 32px DejaVu Sans'
ctx.fillStyle = data.cardTextColor
ctx.fillText(data.username, 200, 115)
// Member count
ctx.font = '16px DejaVu Sans'
ctx.fillStyle = 'rgba(255,255,255,0.7)'
ctx.fillText('Member #' + data.memberCount, 200, 148)
// STEP 6 β€” Return buffer
const buffer = await canvas.toBuffer('png')
return buffer
} catch (err) {
console.error('[card] generateWelcomeCard failed:', err.message)
throw err
}
}