Skip to content

Commit

Permalink
Premier succès dans l'affichage de sprites perso
Browse files Browse the repository at this point in the history
  • Loading branch information
laem committed Jan 20, 2025
1 parent 045b7b4 commit 436fb3c
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 52 deletions.
16 changes: 16 additions & 0 deletions app/api/icons/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { categories } from '@/components/categories'
import colors from '@/app/categoryColors.yaml'

export async function GET(request: Request) {
const json = categories.map((c) => ({
...c,
color: colors[c.category],
}))
return Response.json(json, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
},
})
}
6 changes: 3 additions & 3 deletions app/categories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#emoji: 🥖
query: '[shop=bakery]'
icon: bakery
category: Manger et boire
category: Alimentation
- name: restaurant
dictionary:
- resto
Expand All @@ -20,7 +20,7 @@
- '[amenity=restaurant]'
- '[vending=pizza]'
icon: restaurant
category: Manger et boire
category: Alimentation
- name: cafe
title: Café, bar et pub
dictionary:
Expand All @@ -36,7 +36,7 @@
- '["amenity"="cafe"]'
- '["amenity"="pub"]'
icon: cafe
category: Manger et boire
category: Bars et boisson
- name: épicerie
dictionary:
- supermarché
Expand Down
10 changes: 9 additions & 1 deletion app/styles/france.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ export default function franceStyle(transportMode, noVariableTiles = false) {
// Voir nos villes juste avec les arbres
//layers: layers.filter(({ id }) => id === 'Background' || id === 'Trees'),
glyphs: getFetchUrlBase() + '/fonts/glyphs/{fontstack}/{range}.pbf',
sprite: getFetchUrlBase() + '/sprite/sprite',
sprite: [
/*
{
id: 'default',
url: getFetchUrlBase() + '/sprite/sprite',
},
*/
{ id: 'default', url: getFetchUrlBase() + '/sprite/cartes' },
],
bearing: 0,
pitch: 0,
center: [0, 0],
Expand Down
Binary file modified bun.lockb
Binary file not shown.
100 changes: 53 additions & 47 deletions lib/buildSprites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,63 @@ import spritezero from '@jutaz/spritezero'
import fs from 'fs'
import path from 'path'

const pixelRatios = [1, 2]
const pixelRatios = [2, 4]
const sdf = true

pixelRatios.forEach(async function (pxRatio) {
var svgs = await Promise.all(
['restaurant', 'cafe']
// .sync(path.resolve(path.join(__dirname, 'input/*.svg')))
.map(async function (id) {
const url = `${getFetchUrlBase()}/svgo/?svgFilename=${id}&background=chartreuse&format=svg`
console.log(url)
const request = await fetch(url)

const svg = await request.text()
/*
export default async function buildSprites() {
const catRequest = await fetch(getFetchUrlBase() + '/api/icons')
const categories = await catRequest.json()

pixelRatios.forEach(async function (pxRatio) {
var svgs = await Promise.all(
['restaurant', 'cafe']
// .sync(path.resolve(path.join(__dirname, 'input/*.svg')))
.map(async function (id) {
const url = `${getFetchUrlBase()}/svgo/?svgFilename=${id}&background=${
categories.find((c) => c.name === id).color
}&format=svg`
console.log(url)
const request = await fetch(url)

const svg = await request.text()
/*
fs.readFileSync(
path.resolve(path.join(__dirname, '../public/icons/' + id + '.svg'))
)*/

return {
svg,
id,
}
})
)

var pngPath = path.resolve(
path.join(__dirname, '../public/sprite/sprite-cartes@' + pxRatio + '.png')
)
var jsonPath = path.resolve(
path.join(__dirname, '../public/sprite/sprite-cartes@' + pxRatio + '.json')
)

// Pass `true` in the layout parameter to generate a data layout
// suitable for exporting to a JSON sprite manifest file.
spritezero.generateLayout(
{ imgs: svgs, pixelRatio: pxRatio, sdf, format: true },
function (err, dataLayout) {
if (err) return
fs.writeFileSync(jsonPath, JSON.stringify(dataLayout))
}
)

// Pass `false` in the layout parameter to generate an image layout
// suitable for exporting to a PNG sprite image file.
spritezero.generateLayout(
{ imgs: svgs, pixelRatio: pxRatio, sdf, format: false },
function (err, imageLayout) {
spritezero.generateImage(imageLayout, function (err, image) {
return {
svg,
id,
}
})
)

const spritePath = '../public/sprite/cartes' + (pxRatio == 4 ? '@2' : '')
var pngPath = path.resolve(path.join(__dirname, spritePath + '.png'))
var jsonPath = path.resolve(path.join(__dirname, spritePath + '.json'))

// Pass `true` in the layout parameter to generate a data layout
// suitable for exporting to a JSON sprite manifest file.
spritezero.generateLayout(
{ imgs: svgs, pixelRatio: pxRatio, sdf, format: true },
function (err, dataLayout) {
if (err) return
fs.writeFileSync(pngPath, image)
})
}
)
})
fs.writeFileSync(jsonPath, JSON.stringify(dataLayout))
}
)

// Pass `false` in the layout parameter to generate an image layout
// suitable for exporting to a PNG sprite image file.
spritezero.generateLayout(
{ imgs: svgs, pixelRatio: pxRatio, sdf, format: false },
function (err, imageLayout) {
spritezero.generateImage(imageLayout, function (err, image) {
if (err) return
fs.writeFileSync(pngPath, image)
})
}
)
})
}

buildSprites()
File renamed without changes.
Binary file added public/sprite/cartes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/sprite/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"cafe":{"width":60,"height":60,"x":0,"y":0,"pixelRatio":4},"restaurant":{"width":60,"height":60,"x":60,"y":0,"pixelRatio":4}}
Binary file added public/sprite/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion public/sprite/[email protected]

This file was deleted.

Binary file removed public/sprite/[email protected]
Binary file not shown.
Binary file removed public/sprite/[email protected]
Binary file not shown.

0 comments on commit 436fb3c

Please sign in to comment.