Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
patsissons committed Feb 18, 2024
1 parent f16f32f commit 458b185
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 41 deletions.
4 changes: 1 addition & 3 deletions src/lib/components/Seo/seo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
$: ({ ids, image, seoTitle, seoDescription } = hydrate(metadata))
function hydrate(metadata?: POAPEventMetadata | POAPEventMetadata[]) {
const image = tokenId
? `https://${baseUrl}/og/${tokenId}`
: `https://${baseUrl}/images/twitter-card.png`
const image = tokenId ? `${baseUrl}/og/${tokenId}` : `${baseUrl}/images/twitter-card.png`
if (!metadata) {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/Token/token.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
$: if (!ens) {
fetchENS(token.owner.id)
.then((res) => {
.then((res: ENSRecords) => {
ens = res
})
.catch(console.error)
Expand Down
58 changes: 39 additions & 19 deletions src/routes/og/[token_id]/+server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { renderIcon } from '@download/blockies'
import inter400 from '@fontsource/inter/files/inter-latin-400-normal.woff?arraybuffer'
import inter600 from '@fontsource/inter/files/inter-latin-600-normal.woff?arraybuffer'
import robotoMono400 from '@fontsource/roboto-mono/files/roboto-mono-latin-400-normal.woff?arraybuffer'
import { error } from '@sveltejs/kit'
import { createCanvas } from '@napi-rs/canvas'
import { loadTokenData } from '../../token/[id]/data'
import backgroundImage from './og-background.png?arraybuffer'
import OgToken from './og-token.svelte'
import { svelteToPngResponse } from './svelte-to-png'
import type { RequestHandler } from './$types'
Expand All @@ -12,27 +15,44 @@ export const GET: RequestHandler = async ({ params: { token_id }, fetch }) => {
const props = await loadTokenData(token_id, fetch)
if (!props.token) throw error(404, 'Token not found')

return await svelteToPngResponse(OgToken, props, {
width: 1200,
height: 630,
fonts: [
const avatar =
props.ens.avatar ||
renderIcon(
{
name: 'sans-serif',
data: inter400,
weight: 400,
seed: props.token.owner.id,
size: 20,
scale: 10,
},
{
name: 'sans-serif',
data: inter600,
weight: 600,
},
{
name: 'monospace',
data: robotoMono400,
weight: 400,
},
],
})
createCanvas(200, 200),
).toDataURL()

const background = `data:image/png;base64,${Buffer.from(backgroundImage).toString('base64')}`

return await svelteToPngResponse(
OgToken,
{ ...props, avatar, background },
{
width: 1200,
height: 630,
fonts: [
{
name: 'sans-serif',
data: inter400,
weight: 400,
},
{
name: 'sans-serif',
data: inter600,
weight: 600,
},
{
name: 'monospace',
data: robotoMono400,
weight: 400,
},
],
},
)
} catch (err) {
console.error('error building opengraph image', err)
throw error(500, err instanceof Error ? err.message : 'Opengraph image error')
Expand Down
18 changes: 18 additions & 0 deletions src/routes/og/[token_id]/debug/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { renderIcon } from '@download/blockies'
import { createCanvas } from '@napi-rs/canvas'
import { error } from '@sveltejs/kit'
import { loadTokenData } from '../../../token/[id]/data'
import backgroundImage from '../og-background.png?arraybuffer'
import type { PageServerLoad } from './$types'

export const load: PageServerLoad = async ({ fetch, params }) => {
Expand All @@ -11,9 +14,24 @@ export const load: PageServerLoad = async ({ fetch, params }) => {

const { token, metadata, ens } = data

const avatar =
ens.avatar ||
renderIcon(
{
seed: token.owner.id,
size: 20,
scale: 10,
},
createCanvas(200, 200),
).toDataURL()

const background = `data:image/png;base64,${Buffer.from(backgroundImage).toString('base64')}`

return {
token,
metadata,
ens,
avatar,
background,
}
}
31 changes: 13 additions & 18 deletions src/routes/og/[token_id]/og-token.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<script>
import { renderIcon } from '@download/blockies'
import { createCanvas } from '@napi-rs/canvas'
/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call */
import { formatDistanceToNow } from 'date-fns'
import background from './og-background.png?arraybuffer'
/**
* @type {import('$lib/types/poap').POAPToken}
Expand All @@ -16,23 +14,19 @@
* @type {import('$lib/types/ens').ENSRecords}
*/
export let ens
const avatarSrc =
ens.avatar ||
renderIcon(
{
seed: token.owner.id,
size: 20,
scale: 10,
},
createCanvas(200, 200),
).toDataURL()
const backgrondSrc = `data:image/png;base64,${Buffer.from(background).toString('base64')}`
/**
* @type {string}
*/
export let avatar
/**
* @type {string}
*/
export let background
</script>

<div
style:display="flex"
style:position="relative"
style:align-items="center"
style:justify-content="center"
style:width="100%"
Expand All @@ -41,7 +35,7 @@
style:font-weight="400"
>
<div style:display="flex" style:position="absolute" style:width="100%" style:height="100%">
<img src={backgrondSrc} alt="background" />
<img src={background} alt="background" />
</div>
<div
style:display="flex"
Expand All @@ -52,6 +46,7 @@
style:border-color="rgba(0, 0, 0, 0.3)"
style:border-radius="12px"
style:overflow="hidden"
style:z-index="1"
>
<div
style:display="flex"
Expand All @@ -71,7 +66,7 @@
style:border-radius="4px"
style:overflow="hidden"
style:object-fit="scale-down"
src={avatarSrc}
src={avatar}
alt="{ens.name || `${token.owner.id.slice(0, 6)}…${token.owner.id.slice(-4)}`} avatar"
/>
</div>
Expand Down

0 comments on commit 458b185

Please sign in to comment.