pnh-rrss/kit/referencia/12octubre_mapa.mjs

161 lines
9.8 KiB
JavaScript

import fs from 'fs';
import { geoMercator, geoPath } from 'd3-geo';
import { feature } from 'topojson-client';
import { chromium } from 'playwright-core';
const D = process.cwd();
const topo = JSON.parse(fs.readFileSync(D + '/node_modules/world-atlas/countries-50m.json'));
const countries = feature(topo, topo.objects.countries).features;
const b64 = f => fs.readFileSync(D + '/' + f).toString('base64');
const font = f => fs.readFileSync(D + '/node_modules/@fontsource/' + f).toString('base64');
const HISP = new Set([724, 620, 484, 320, 340, 222, 558, 188, 591, 192, 214, 630, 170, 862, 218, 604, 68, 152, 32, 858, 600, 76, 226].map(String).map(s => s.padStart(3, '0')));
const PHI = '608';
// main map: Americas
const W = 1080;
const proj = geoMercator().fitExtent([[80, 470], [640, 975]], {
type: 'MultiPoint', coordinates: [[-117, 32.5], [-60, 23], [-74, -55.5], [-34.8, -10]]
});
const path = geoPath(proj);
const iproj = geoMercator().fitExtent([[760, 455], [990, 645]], { type: 'MultiPoint', coordinates: [[-9.5, 36], [4.3, 43.8]] });
const ipath = geoPath(iproj);
const IBE = new Set(['724', '620']);
let canar = '';
const cproj = geoMercator().scale(iproj.scale()).center([-15.6, 28.3]).translate([742, 668]);
let hisp = '', iber = '';
for (const c of countries) {
if (c.id === PHI) continue;
if (IBE.has(c.id)) {
if (c.id === '724') {
const polys = c.geometry.coordinates;
const main = polys.filter(p => p[0].every(([x]) => x > -12));
const can = polys.filter(p => p[0].some(([x]) => x <= -12) && p[0].every(([, y]) => y > 26.5));
iber += ipath({ type: 'MultiPolygon', coordinates: main });
canar = geoPath(cproj)({ type: 'MultiPolygon', coordinates: can });
} else iber += ipath(c);
continue;
}
if (!HISP.has(c.id)) continue;
const d = path(c); if (!d) continue;
const [[x0, y0], [x1, y1]] = path.bounds(c);
if (x1 < 0 || x0 > W || y1 < 0 || y0 > W) continue;
hisp += d;
}
hisp += iber + canar;
const phi = countries.find(c => c.id === PHI);
const pproj = geoMercator().fitExtent([[905, 700], [1000, 880]], phi);
const phiPath = geoPath(pproj)(phi);
const P = iproj([-6.9, 37.2]), G = cproj([-17.2, 28.1]), S = proj([-74.5, 24.1]);
const route = `M${P[0]},${P[1]} Q${(P[0] + G[0]) / 2 + 10},${(P[1] + G[1]) / 2 + 5} ${G[0]},${G[1]} Q${(G[0] + S[0]) / 2},${Math.min(G[1], S[1]) - 70} ${S[0]},${S[1]}`;
const mid = [(G[0] + S[0]) / 2 + 10, (G[1] + S[1]) / 2 - 50];
// sun centre in mid-Atlantic
const sun = [645, 650];
let rays = '';
for (let i = 0; i < 36; i++) {
const a = i * Math.PI * 2 / 36, r0 = 70, r1 = i % 2 ? 190 : 270;
rays += `<line x1="${sun[0] + r0 * Math.cos(a)}" y1="${sun[1] + r0 * Math.sin(a)}" x2="${sun[0] + r1 * Math.cos(a)}" y2="${sun[1] + r1 * Math.sin(a)}"/>`;
}
// ragged Cross of Burgundy (background, faint)
function raggedBar(x0, y0, x1, y1, w) {
const dx = x1 - x0, dy = y1 - y0, L = Math.hypot(dx, dy), ux = dx / L, uy = dy / L, nx = -uy, ny = ux;
const pts = [], n = 9;
for (let s = 0; s <= 1; s++) {
const side = s ? -1 : 1, seq = [];
for (let k = 0; k <= n; k++) {
const t = k / n * L, knot = (k > 0 && k < n && k % 2 === 1) ? 26 : 0;
seq.push([x0 + ux * t + nx * side * (w / 2 + knot), y0 + uy * t + ny * side * (w / 2 + knot)]);
if (knot) seq.push([x0 + ux * (t + 18) + nx * side * w / 2, y0 + uy * (t + 18) + ny * side * w / 2]);
}
pts.push(...(s ? seq.reverse() : seq));
}
return 'M' + pts.map(p => p.map(v => v.toFixed(1)).join(',')).join('L') + 'Z';
}
const cross = raggedBar(-60, -60, 1140, 1140, 110) + raggedBar(1140, -60, -60, 1140, 110);
const caravel = `<g transform="translate(${mid[0]},${mid[1]}) scale(0.95)">
<path d="M-34,6 L34,6 L26,18 L-26,18 Z" fill="#E8C36A"/>
<path d="M-36,6 L-28,-2 L-22,6Z" fill="#E8C36A"/>
<line x1="-12" y1="6" x2="-12" y2="-34" stroke="#E8C36A" stroke-width="2.4"/>
<line x1="10" y1="6" x2="10" y2="-40" stroke="#E8C36A" stroke-width="2.4"/>
<line x1="26" y1="6" x2="26" y2="-22" stroke="#E8C36A" stroke-width="2"/>
<path d="M-24,-30 Q-12,-26 0,-30 L0,-4 Q-12,0 -24,-4 Z" fill="#FBF3E4"/>
<path d="M-2,-37 Q10,-33 22,-37 L22,-6 Q10,-2 -2,-6 Z" fill="#FBF3E4"/>
<path d="M26,-22 L38,-4 L26,-4 Z" fill="#FBF3E4"/>
<path d="M8,-26 h4 v-4 h4 v4 h4 v4 h-4 v4 h-4 v-4 h-4z" transform="translate(-4,6)" fill="#A10F2B"/>
<path d="M8,-26 h4 v-4 h4 v4 h4 v4 h-4 v4 h-4 v-4 h-4z" transform="translate(-26,10)" fill="#A10F2B"/>
</g>`;
const html = `<!doctype html><html><head><meta charset="utf-8"><style>
@font-face{font-family:Cinzel;font-weight:700;src:url(data:font/woff2;base64,${font('cinzel/files/cinzel-latin-700-normal.woff2')})}
@font-face{font-family:Cinzel;font-weight:900;src:url(data:font/woff2;base64,${font('cinzel/files/cinzel-latin-900-normal.woff2')})}
@font-face{font-family:Garamond;font-style:italic;font-weight:500;src:url(data:font/woff2;base64,${font('eb-garamond/files/eb-garamond-latin-500-italic.woff2')})}
@font-face{font-family:Garamond;font-weight:600;src:url(data:font/woff2;base64,${font('eb-garamond/files/eb-garamond-latin-600-normal.woff2')})}
html,body{margin:0;background:#2a0508}
</style></head><body>
<svg width="1080" height="1080" viewBox="0 0 1080 1080" xmlns="http://www.w3.org/2000/svg">
<defs>
<radialGradient id="bg" cx="50%" cy="55%" r="75%"><stop offset="0" stop-color="#6a0a14"/><stop offset=".55" stop-color="#4B0000"/><stop offset="1" stop-color="#220306"/></radialGradient>
<linearGradient id="gold" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#FFE9A6"/><stop offset=".45" stop-color="#F2C64B"/><stop offset="1" stop-color="#C9962C"/></linearGradient>
<linearGradient id="goldMap" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="#F6D27A"/><stop offset="1" stop-color="#D9A63A"/></linearGradient>
<radialGradient id="sunG"><stop offset="0" stop-color="#FFE7A0" stop-opacity=".95"/><stop offset=".55" stop-color="#F2C64B" stop-opacity=".55"/><stop offset="1" stop-color="#F2C64B" stop-opacity="0"/></radialGradient>
<filter id="sh" x="-20%" y="-20%" width="140%" height="140%"><feDropShadow dx="0" dy="4" stdDeviation="6" flood-color="#000" flood-opacity=".55"/></filter>
<filter id="glow"><feGaussianBlur stdDeviation="18"/></filter>
<clipPath id="inner"><rect x="44" y="44" width="992" height="992"/></clipPath>
</defs>
<rect width="1080" height="1080" fill="url(#bg)"/>
<g clip-path="url(#inner)">
<path d="${cross}" fill="#A10F2B" opacity=".22"/>
<circle cx="${sun[0]}" cy="${sun[1]}" r="120" fill="url(#sunG)" opacity=".35"/>
<g stroke="#F2C64B" stroke-width="2.2" opacity=".35">${rays}</g>
<path d="${hisp}" fill="url(#goldMap)" stroke="#8B0000" stroke-width=".7" filter="url(#sh)"/>
<path d="${phiPath}" fill="url(#goldMap)" stroke="#8B0000" stroke-width=".6" filter="url(#sh)"/>
<path d="${route}" fill="none" stroke="#FBF3E4" stroke-width="2.6" stroke-dasharray="2 9" stroke-linecap="round" opacity=".9"/>
<circle cx="${P[0]}" cy="${P[1]}" r="5" fill="#FBF3E4"/><circle cx="${S[0]}" cy="${S[1]}" r="5" fill="#FBF3E4"/>
<text x="${S[0] - 70}" y="${S[1] - 12}" font-family="Cinzel" font-weight="700" font-size="17" fill="#FBF3E4" letter-spacing="2">1492</text>
${caravel}
</g>
<!-- Pilar -->
<ellipse cx="160" cy="790" rx="110" ry="150" fill="#F2C64B" opacity=".22" filter="url(#glow)"/>
<image href="data:image/png;base64,${b64('pilar.png')}" x="94" y="640" width="132" height="334" filter="url(#sh)"/>
<text x="160" y="1000" text-anchor="middle" font-family="Garamond" font-style="italic" font-size="17" fill="#F2C64B">N.ª S.ª del Pilar</text>
<!-- emblem -->
<image href="data:image/png;base64,${b64('logo_full_hd.png')}" x="575" y="752" width="330" height="219" filter="url(#sh)"/>
<!-- frame -->
<rect x="26" y="26" width="1028" height="1028" fill="none" stroke="#D9A63A" stroke-width="4"/>
<rect x="38" y="38" width="1004" height="1004" fill="none" stroke="#D9A63A" stroke-width="1.5"/>
<!-- header (2024 banner) -->
<g font-family="Cinzel" font-weight="700" font-size="27" fill="url(#gold)" letter-spacing="3">
<text x="460" y="104" text-anchor="end">PARTIDO NACIONAL</text>
<text x="620" y="104" text-anchor="start">HISPANISTA</text>
</g>
<image href="data:image/png;base64,${b64('horse.png')}" x="483" y="62" width="114" height="72"/>
<line x1="90" y1="148" x2="990" y2="148" stroke="#D9A63A" stroke-width="1.5"/>
<line x1="90" y1="154" x2="990" y2="154" stroke="#D9A63A" stroke-width="1"/>
<!-- titles -->
<text x="540" y="222" text-anchor="middle" font-family="Cinzel" font-weight="700" font-size="50" fill="url(#gold)" letter-spacing="6">— 12 DE OCTUBRE —</text>
<text x="540" y="345" text-anchor="middle" font-family="Cinzel" font-weight="900" font-size="122" fill="#FBF3E4" letter-spacing="2" filter="url(#sh)">HISPANIDAD</text>
<g font-family="Garamond" font-style="italic" font-size="40" fill="#F2C64B">
<text x="540" y="408" text-anchor="middle">Unidad, Fe y Destino</text>
</g>
<g stroke="#D9A63A" stroke-width="1.5"><line x1="215" y1="396" x2="355" y2="396"/><line x1="725" y1="396" x2="865" y2="396"/></g>
<g fill="#D9A63A"><path d="M360,396 l7,-6 l7,6 l-7,6z"/><path d="M706,396 l7,-6 l7,6 l-7,6z"/></g>
<!-- footer -->
<text x="540" y="1012" text-anchor="middle" font-family="Cinzel" font-weight="700" font-size="25" fill="url(#gold)" letter-spacing="4">¡ARRIBA LA HISPANIDAD! · 2026</text>
</svg></body></html>`;
fs.writeFileSync(D + '/poster.html', html);
const browser = await chromium.launch({ executablePath: '/opt/pw-browsers/chromium' + (fs.existsSync('/opt/pw-browsers/chromium') && fs.statSync('/opt/pw-browsers/chromium').isDirectory() ? '/chrome' : '') });
const pg = await browser.newPage({ viewport: { width: 1080, height: 1080 }, deviceScaleFactor: 2 });
await pg.setContent(html); await pg.evaluate(() => document.fonts.ready); await pg.waitForTimeout(300);
await pg.screenshot({ path: D + '/out.png', clip: { x: 0, y: 0, width: 1080, height: 1080 } });
await browser.close();
console.log('ok', P, G, S, sun);