35 lines
2.2 KiB
JavaScript
35 lines
2.2 KiB
JavaScript
import CanvasKitInit from 'canvaskit-wasm/full';
|
|
import fs from 'node:fs';
|
|
const CK = await CanvasKitInit();
|
|
console.log('MakeSurface', typeof CK.MakeSurface, 'MakeWebGLSurface', typeof CK.MakeWebGLSurface, 'Font', typeof CK.Font, 'TypefaceFontProvider', typeof CK.TypefaceFontProvider, 'MakeManagedAnimation', typeof CK.MakeManagedAnimation);
|
|
console.log('top keys sample:', Object.keys(CK).filter(k=>/^Make|Font|Typeface|Paragraph/.test(k)).join(','));
|
|
const surf = CK.MakeSurface(400, 200);
|
|
console.log('surface', !!surf);
|
|
const c = surf.getCanvas();
|
|
c.clear(CK.Color(255,255,255,255));
|
|
const p = new CK.Paint();
|
|
p.setColor(CK.Color(20,20,20,255));
|
|
const ttf = fs.readFileSync('../assets/Cousine-Regular.ttf');
|
|
const ab = ttf.buffer.slice(ttf.byteOffset, ttf.byteOffset+ttf.byteLength);
|
|
const tf = CK.Typeface.MakeFreeTypeFaceFromData(ab);
|
|
console.log('typeface', !!tf, tf && tf.getGlyphIDs('AB')[0] !== undefined);
|
|
const font = new CK.Font(tf, 32);
|
|
c.drawText('RecipesCam ABC 0123', 20, 80, p, font);
|
|
const emoji = fs.readFileSync('../assets/NotoEmoji-GPS.ttf');
|
|
const eab = emoji.buffer.slice(emoji.byteOffset, emoji.byteOffset+emoji.byteLength);
|
|
const etf = CK.Typeface.MakeFreeTypeFaceFromData(eab);
|
|
console.log('emoji typeface', !!etf);
|
|
if (etf) { const ef = new CK.Font(etf, 32); c.drawText('\u{1F4CD} \u{1F4F7}', 20, 140, p, ef); }
|
|
const img = surf.makeImageSnapshot();
|
|
const bytes = img.encodeToBytes(CK.ImageFormat.JPEG, 95);
|
|
console.log('jpeg bytes', bytes && bytes.length);
|
|
fs.writeFileSync('/tmp/ck-text.jpg', Buffer.from(bytes));
|
|
// runtime effect with child shader
|
|
const eff = CK.RuntimeEffect.Make(`
|
|
uniform shader img; uniform float2 texel; uniform float amount;
|
|
half4 main(float2 p){ half4 c = img.eval(p); half4 n = img.eval(p+float2(0.0,-texel.y)); half4 s = img.eval(p+float2(0.0,texel.y)); half4 w = img.eval(p+float2(-texel.x,0.0)); half4 e = img.eval(p+float2(texel.x,0.0)); return c + amount*(4.0*c-n-s-w-e); }`);
|
|
console.log('runtime effect', !!eff, eff && eff.getUniformCount?.());
|
|
const imgSh = img.makeShaderOptions(CK.TileMode.Clamp, CK.TileMode.Clamp, CK.FilterMode.Linear, CK.MipmapMode.None);
|
|
const sh = eff && eff.makeShaderWithChildren([1/400, 1/200, 1.0], [imgSh]);
|
|
console.log('shader', !!sh);
|