Files
site-portfolio/demook.mjs
T

27 lines
1.7 KiB
JavaScript

import puppeteer from 'puppeteer';
import sharp from 'sharp';
const b = await puppeteer.launch({ args:['--no-sandbox','--disable-setuid-sandbox','--use-gl=angle','--enable-unsafe-swiftshader','--ignore-gpu-blocklist'] });
const p = await b.newPage();
await p.setViewport({width:1100,height:1300});
const errs=[]; p.on('console',m=>{if(m.type()==='error')errs.push(m.text())}); p.on('pageerror',e=>errs.push('PE '+e.message));
await p.goto('http://10.200.201.3:4175/_lab/neu-card.html',{waitUntil:'load',timeout:60000});
await new Promise(r=>setTimeout(r,1200));
await p.evaluate(()=>document.querySelector('#sec-shader').scrollIntoView());
await new Promise(r=>setTimeout(r,700));
async function probe(key){
if(key){ await p.click(`[data-shader-switch="${key}"]`); await new Promise(r=>setTimeout(r,900)); }
const buf = await p.evaluate(()=>{ const cv=document.querySelector('[data-shader-demo] canvas'); return cv?cv.toDataURL('image/png'):null; });
let avg=[0,0,0], distinct=0;
if(buf){ const {data,info}=await sharp(Buffer.from(buf.split(',')[1],'base64')).raw().toBuffer({resolveWithObject:true});
let n=info.width*info.height,rs=0,gs=0,bs=0; const cols=new Set();
for(let i=0;i<data.length;i+=4){ rs+=data[i];gs+=data[i+1];bs+=data[i+2]; if(i%1500===0) cols.add(`${data[i]>>5},${data[i+1]>>5},${data[i+2]>>5}`); }
avg=[Math.round(rs/n),Math.round(gs/n),Math.round(bs/n)]; distinct=cols.size; }
return { key:key||'mesh', avg, distinct, hasCanvas:!!buf };
}
console.log(JSON.stringify(await probe(),null,0));
console.log(JSON.stringify(await probe('grain'),null,0));
console.log(JSON.stringify(await probe('voronoi'),null,0));
console.log(JSON.stringify(await probe('staticMesh'),null,0));
console.log('errs:', errs);
await b.close();