2018-10-17 11:57:18 +00:00
|
|
|
const CanvasKitInit = require('./bin/canvaskit.js');
|
2018-12-03 17:31:04 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
2018-10-17 11:57:18 +00:00
|
|
|
CanvasKitInit({
|
|
|
|
locateFile: (file) => __dirname + '/bin/'+file,
|
2020-05-21 13:59:44 +00:00
|
|
|
}).then((CanvasKit) => {
|
2018-10-19 18:34:34 +00:00
|
|
|
let canvas = CanvasKit.MakeCanvas(300, 300);
|
|
|
|
|
2018-12-03 17:31:04 +00:00
|
|
|
let img = fs.readFileSync(path.join(__dirname, 'test.png'));
|
|
|
|
img = canvas.decodeImage(img);
|
|
|
|
|
2018-12-18 12:43:02 +00:00
|
|
|
let fontData = fs.readFileSync(path.join(__dirname, './Roboto-Regular.woff'));
|
|
|
|
canvas.loadFont(fontData, {
|
|
|
|
'family': 'Roboto',
|
|
|
|
'style': 'normal',
|
|
|
|
'weight': '400',
|
|
|
|
});
|
|
|
|
|
2018-10-19 18:34:34 +00:00
|
|
|
let ctx = canvas.getContext('2d');
|
2018-12-18 12:43:02 +00:00
|
|
|
ctx.font = '30px Roboto';
|
2018-10-19 18:34:34 +00:00
|
|
|
ctx.rotate(.1);
|
|
|
|
ctx.fillText('Awesome ', 50, 100);
|
2021-02-16 20:38:42 +00:00
|
|
|
ctx.strokeText('Groovy!', 250, 100);
|
2018-10-19 18:34:34 +00:00
|
|
|
|
|
|
|
// Draw line under Awesome
|
|
|
|
ctx.strokeStyle = 'rgba(125,0,0,0.5)';
|
|
|
|
ctx.beginPath();
|
2018-12-18 12:43:02 +00:00
|
|
|
ctx.lineWidth = 6;
|
2018-10-19 18:34:34 +00:00
|
|
|
ctx.lineTo(50, 102);
|
2021-02-16 20:38:42 +00:00
|
|
|
ctx.lineTo(250, 102);
|
2018-10-19 18:34:34 +00:00
|
|
|
ctx.stroke();
|
|
|
|
|
2018-12-03 17:31:04 +00:00
|
|
|
// squished vertically
|
|
|
|
ctx.globalAlpha = 0.7
|
|
|
|
ctx.imageSmoothingQuality = 'medium';
|
|
|
|
ctx.drawImage(img, 150, 150, 150, 100);
|
|
|
|
ctx.rotate(-.2);
|
|
|
|
ctx.imageSmoothingEnabled = false;
|
|
|
|
ctx.drawImage(img, 100, 150, 400, 350, 10, 200, 150, 100);
|
|
|
|
|
2018-10-19 18:34:34 +00:00
|
|
|
console.log('<img src="' + canvas.toDataURL() + '" />');
|
2019-01-14 13:36:08 +00:00
|
|
|
|
|
|
|
fancyAPI(CanvasKit);
|
2018-10-19 18:34:34 +00:00
|
|
|
});
|
|
|
|
|
2018-12-12 15:35:13 +00:00
|
|
|
function fancyAPI(CanvasKit) {
|
2018-10-17 11:57:18 +00:00
|
|
|
let surface = CanvasKit.MakeSurface(300, 300);
|
|
|
|
const canvas = surface.getCanvas();
|
|
|
|
|
2020-10-07 20:09:22 +00:00
|
|
|
const paint = new CanvasKit.Paint();
|
2018-10-17 11:57:18 +00:00
|
|
|
|
2020-10-07 20:09:22 +00:00
|
|
|
const fontMgr = CanvasKit.FontMgr.RefDefault();
|
2018-12-12 15:35:13 +00:00
|
|
|
let robotoData = fs.readFileSync(path.join(__dirname, './Roboto-Regular.woff'));
|
|
|
|
const roboto = fontMgr.MakeTypefaceFromData(robotoData);
|
|
|
|
|
2020-10-07 20:09:22 +00:00
|
|
|
const textPaint = new CanvasKit.Paint();
|
2018-11-20 17:51:16 +00:00
|
|
|
textPaint.setColor(CanvasKit.Color(40, 0, 0));
|
2018-10-17 11:57:18 +00:00
|
|
|
textPaint.setAntiAlias(true);
|
2019-01-14 13:36:08 +00:00
|
|
|
|
2020-10-07 20:09:22 +00:00
|
|
|
const textFont = new CanvasKit.Font(roboto, 30);
|
2018-10-17 11:57:18 +00:00
|
|
|
|
2018-12-12 15:35:13 +00:00
|
|
|
const skpath = starPath(CanvasKit);
|
2020-10-07 20:09:22 +00:00
|
|
|
const dpe = CanvasKit.PathEffect.MakeDash([15, 5, 5, 10], 1);
|
2018-10-17 11:57:18 +00:00
|
|
|
|
|
|
|
paint.setPathEffect(dpe);
|
2018-12-12 15:35:13 +00:00
|
|
|
paint.setStyle(CanvasKit.PaintStyle.Stroke);
|
2018-10-17 11:57:18 +00:00
|
|
|
paint.setStrokeWidth(5.0);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(CanvasKit.Color(66, 129, 164, 1.0));
|
|
|
|
|
|
|
|
canvas.clear(CanvasKit.Color(255, 255, 255, 1.0));
|
|
|
|
|
2018-12-12 15:35:13 +00:00
|
|
|
canvas.drawPath(skpath, paint);
|
2019-02-22 15:04:06 +00:00
|
|
|
canvas.drawText('Try Clicking!', 10, 280, textPaint, textFont);
|
2018-10-17 11:57:18 +00:00
|
|
|
|
|
|
|
surface.flush();
|
|
|
|
|
2021-02-16 20:38:42 +00:00
|
|
|
const img = surface.makeImageSnapshot();
|
2018-10-17 11:57:18 +00:00
|
|
|
if (!img) {
|
|
|
|
console.error('no snapshot');
|
|
|
|
return;
|
|
|
|
}
|
2021-02-16 20:38:42 +00:00
|
|
|
const pngBytes = img.encodeToBytes();
|
|
|
|
if (!pngBytes) {
|
2018-10-17 11:57:18 +00:00
|
|
|
console.error('encoding failure');
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// See https://stackoverflow.com/a/12713326
|
|
|
|
let b64encoded = Buffer.from(pngBytes).toString('base64');
|
|
|
|
console.log(`<img src="data:image/png;base64,${b64encoded}" />`);
|
|
|
|
|
2018-12-18 12:43:02 +00:00
|
|
|
// These delete calls free up memeory in the C++ WASM memory block.
|
2018-10-17 11:57:18 +00:00
|
|
|
dpe.delete();
|
2018-12-12 15:35:13 +00:00
|
|
|
skpath.delete();
|
2018-10-17 11:57:18 +00:00
|
|
|
textPaint.delete();
|
|
|
|
paint.delete();
|
2018-12-12 15:35:13 +00:00
|
|
|
roboto.delete();
|
2019-01-14 13:36:08 +00:00
|
|
|
textFont.delete();
|
2018-10-17 11:57:18 +00:00
|
|
|
|
|
|
|
surface.dispose();
|
2018-10-19 18:34:34 +00:00
|
|
|
}
|
2018-10-17 11:57:18 +00:00
|
|
|
|
|
|
|
function starPath(CanvasKit, X=128, Y=128, R=116) {
|
2020-10-07 20:09:22 +00:00
|
|
|
let p = new CanvasKit.Path();
|
2018-10-17 11:57:18 +00:00
|
|
|
p.moveTo(X + R, Y);
|
|
|
|
for (let i = 1; i < 8; i++) {
|
|
|
|
let a = 2.6927937 * i;
|
|
|
|
p.lineTo(X + R * Math.cos(a), Y + R * Math.sin(a));
|
|
|
|
}
|
|
|
|
return p;
|
2020-05-21 13:59:44 +00:00
|
|
|
}
|