Files
MangaBom/scripts/calculate-image-spec.cjs
2026-11-02 04:08:02 +03:00

9 lines
1.3 KiB
JavaScript

const fs=require('node:fs'),path=require('node:path');const root=path.resolve(__dirname,'..');
const measurements=JSON.parse(fs.readFileSync(path.join(root,'docs/image-spec/slot-measurements.json'),'utf8'));
const W=2400,H=1600;
function crop(slot,z=1.045){const fit=Math.max(slot.width/W,slot.height/H);const [px,py]=slot.objectPosition.split(' ').map(v=>parseFloat(v)/100);const dx=(slot.width-W*fit)*px,dy=(slot.height-H*fit)*py;return {left:(slot.width/2*(1-1/z)-dx)/fit/W*100,right:(slot.width/2*(1+1/z)-dx)/fit/W*100,top:(slot.height/2*(1-1/z)-dy)/fit/H*100,bottom:(slot.height/2*(1+1/z)-dy)/fit/H*100};}
const slots=measurements.flatMap(m=>m.slots.filter(s=>s.type!=='home').map(s=>({case:m.case,...s,crop:crop(s)})));
const intersection={left:Math.max(...slots.map(s=>s.crop.left)),right:Math.min(...slots.map(s=>s.crop.right)),top:Math.max(...slots.map(s=>s.crop.top)),bottom:Math.min(...slots.map(s=>s.crop.bottom))};
const output={master:{width:W,height:H},intersection,crops:slots};
fs.mkdirSync(path.join(root,'docs/image-spec'),{recursive:true});fs.writeFileSync(path.join(root,'docs/image-spec/crop-measurements.json'),JSON.stringify(output,null,2));
console.log(JSON.stringify({intersection,crops:slots.map(s=>({case:s.case,type:s.type,width:s.width,height:s.height,crop:s.crop}))},null,2));