Skip to content

Commit

Permalink
feat: server-side svg rasterizing
Browse files Browse the repository at this point in the history
  • Loading branch information
gnlow committed Dec 21, 2023
1 parent 4da986e commit c37c165
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
23 changes: 23 additions & 0 deletions api/image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as resvg from "https://esm.sh/v135/@resvg/[email protected]"

await resvg.initWasm(fetch("https://esm.sh/v135/@resvg/[email protected]/index_bg.wasm"))

export const svgToPng =
(svg: string) => {
const resvgJS = new resvg.Resvg(svg, {})
const pngData = resvgJS.render()
const pngBuffer = pngData.asPng()
return pngBuffer
}

export const image =
async (path: string) => {
if (path.endsWith(".svg.png")) {
path = path.slice(0, -4)
return await fetch(path).then(async res => {
return svgToPng(await res.text())
})
} else {
return await fetch(path).then(res => res.body)
}
}
3 changes: 2 additions & 1 deletion api/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./js.ts"
export * from "./project.ts"
export * from "./project.ts"
export * from "./image.ts"
2 changes: 2 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,20 @@ app.get("/image/lib/entry-js/images/*", async c => {
const path = new URL(c.req.url).pathname
.replace(/^\/image\//, "https://playentry.org/")
return c.body(
await fetch(path).then(x => x.body)
await api.image(path)
)
})
app.get("/image/:id", async c => {
const id = c.req.param("id")
const [a,b,d,e] = id
return c.body(
await fetch(
await api.image(
`https://playentry.org/uploads/${
a + b
}/${
d + e
}/image/${id}`
).then(x => x.body)
)
)
})

Expand Down
9 changes: 8 additions & 1 deletion src/Entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,20 @@ export class Entry {
this.project.objects.map(({sprite}) =>
sprite.pictures.map(
async ({id, fileurl, filename, imageType, name}) => {
const url = `/image/${
let url = `/image/${
filename
? (filename + `.${imageType}`)
: fileurl.substring(1)
}`

// for server-side svg rasterize
if (url.endsWith(".svg")) {
url += ".png"
}

await Assets.load(url)
const texture = Texture.from(url)
console.log(texture)
texture.label = name
return [
id,
Expand Down

0 comments on commit c37c165

Please sign in to comment.