Files
ESBSC-CONVOC/public/assets/convocation-image.js
T
2026-11-11 09:24:28 +01:00

32 lines
4.4 KiB
JavaScript

'use strict';
(async () => {
const data=JSON.parse(document.getElementById('share-data').textContent),canvas=document.getElementById('convocation-canvas'),ctx=canvas.getContext('2d'),groups=data.groups,n=groups.length;
const brand=data.branding||{short_name:'ESBSC',primary:'#143852',accent:'#246b80',logo:''};
let logo=null;if(brand.logo){try{logo=await new Promise((resolve,reject)=>{const i=new Image();i.onload=()=>resolve(i);i.onerror=reject;i.src=brand.logo;});}catch(e){document.getElementById('image-status').textContent='Logo indisponible : image créée sans logo.';}}
const rgb=brand.primary.match(/[a-f0-9]{2}/gi).map(x=>parseInt(x,16)/255).map(x=>x<=.04045?x/12.92:((x+.055)/1.055)**2.4),light=rgb[0]*.2126+rgb[1]*.7152+rgb[2]*.0722>.179;
const width=n===1?900:1800,margin=40,col=(width-2*margin)/n;
function lines(text,bold=false,size=26,available=col-36){
ctx.font=`${bold?'bold ':''}${size}px sans-serif`;
const result=[];let line='';
for(const char of text){if(ctx.measureText(line+char).width>available&&line){const cut=line.lastIndexOf(' ');if(cut>line.length/2){result.push(line.slice(0,cut));line=line.slice(cut+1);}else{result.push(line);line='';}}line+=char;}
result.push(line);return result;
}
const rows=[{kind:'team',values:groups.map(g=>g.team)},{kind:'opponent',values:groups.map(g=>g.opponent)},{kind:'date',values:groups.map(g=>g.when)},{kind:'section',values:groups.map(()=>'JOUEURS CONVOQUÉS')}];
for(let i=0;i<Math.max(...groups.map(g=>g.players.length));i++)rows.push({kind:'player',values:groups.map(g=>g.players[i]||'')});
if(data.include_staff){rows.push({kind:'section',values:groups.map(()=>'STAFF ACCOMPAGNATEUR')});for(let i=0;i<Math.max(1,...groups.map(g=>g.staff.length));i++)rows.push({kind:'staff',values:groups.map(g=>g.staff[i]||(i===0?'—':''))});}
rows.forEach(row=>{row.bold=['team','opponent','section'].includes(row.kind);row.size=row.kind==='team'?32:row.kind==='section'?20:row.kind==='date'?22:26;row.lines=row.values.map(v=>lines(v,row.bold,row.size,col-(row.kind==='opponent'?84:36)));row.height=Math.max(...row.lines.map(l=>l.length))*(row.size+10)+24;});
canvas.width=width;canvas.height=160+rows.reduce((h,r)=>h+r.height,0)+(data.legend?100:40);ctx.fillStyle='#ffffff';ctx.fillRect(0,0,canvas.width,canvas.height);
ctx.fillStyle=brand.primary;ctx.fillRect(margin,20,8,100);let tx=margin+26;if(logo){const scale=Math.min(105/logo.width,105/logo.height);ctx.drawImage(logo,tx,20+(105-logo.height*scale)/2,logo.width*scale,logo.height*scale);tx+=130;}
const title=brand.short_name+' · CONVOCATIONS';let titleSize=42;do{ctx.font=`bold ${titleSize}px sans-serif`;if(ctx.measureText(title).width<=width-margin-tx)break;titleSize--;}while(titleSize>18);ctx.fillStyle='#162b38';ctx.fillText(title,tx,65);ctx.font='24px sans-serif';ctx.fillText('Semaine du '+data.week.split('-').reverse().join('/'),tx,110);
function symbol(x,y,side){
ctx.save();ctx.translate(x,y);ctx.scale(2,2);ctx.fillStyle=brand.primary;ctx.beginPath();
const pts=side==='home'?[[0,5],[7,-2],[14,5],[12,5],[12,13],[8,13],[8,8],[5,8],[5,13],[2,13],[2,5]]:[[0,8],[6,7],[6,0],[8,-2],[9,7],[15,10],[15,12],[9,11],[9,15],[11,16],[11,18],[7,16],[3,18],[3,16],[6,15],[6,11],[0,10]];
if(side==='unknown'){ctx.font='bold 16px sans-serif';ctx.fillText('?',2,12);}else{pts.forEach(([a,b],i)=>i?ctx.lineTo(a,b):ctx.moveTo(a,b));ctx.closePath();ctx.fill();}ctx.restore();
}
let y=145;rows.forEach((r,index)=>{
r.lines.forEach((ls,i)=>{const x=margin+i*col;ctx.fillStyle=r.kind==='team'?brand.primary:r.kind==='section'?'#d4e6ef':index%2?'#eef4f7':'#ffffff';ctx.fillRect(x,y,col-3,r.height);ctx.fillStyle=r.kind==='team'?(light?'#111111':'#ffffff'):'#162b38';ctx.font=`${r.bold?'bold ':''}${r.size}px sans-serif`;ls.forEach((line,j)=>ctx.fillText(line,x+(r.kind==='opponent'?62:18),y+12+r.size+j*(r.size+10)));if(r.kind==='opponent')symbol(x+16,y+18,groups[i].side);});y+=r.height;
});
if(data.legend){symbol(margin,y+30,'home');symbol(margin+225,y+25,'away');ctx.fillStyle=brand.primary;ctx.font='23px sans-serif';ctx.fillText('Domicile',margin+45,y+53);ctx.fillText('Extérieur',margin+270,y+53);}
canvas.toBlob(blob=>{const status=document.getElementById('image-status');if(!blob){status.textContent='Image indisponible : utilisez le PDF ou Excel.';return;}const link=document.getElementById('download-convocations');link.href=URL.createObjectURL(blob);link.download=`convocations-${data.week}.png`;link.hidden=false;status.textContent='Image prête à télécharger.';},'image/png');
})();