skia2/demos.skia.org/demos/path_performance/worker.js
Kevin Lubick 7026961111 [canvaskit] Remove references to particles.skia.org from demos
We should encourage users to use a real CDN, not a random build of ours.

This also fixes a few broken demos.

Change-Id: I3b607665744b5f481bca0b9b3072aaa25b3d2c57
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/371221
Reviewed-by: Kevin Lubick <kjlubick@google.com>
2021-02-18 19:00:44 +00:00

55 lines
2.0 KiB
JavaScript

importScripts('https://unpkg.com/canvaskit-wasm@0.23.0/bin/canvaskit.js');
importScripts('shared.js');
const CanvasKitPromise =
CanvasKitInit({locateFile: (file) => 'https://unpkg.com/canvaskit-wasm@0.23.0/bin/'+file});
const path2dAnimator = new Animator();
const canvasKitAnimator = new Animator();
addEventListener('message', async ({ data: {svgData, offscreenCanvas, type, switchMethod} }) => {
// The worker expect to receive 2 messages after initialization: One with an offscreenCanvas
// for Path2D rendering, and one with an offscreenCanvas for CanvasKit rendering.
if (svgData && offscreenCanvas && type) {
if (type === 'Path2D') {
path2dAnimator.renderer =
new Path2dRenderer(svgData, offscreenCanvas);
}
if (type === 'CanvasKit') {
const CanvasKit = await CanvasKitPromise;
canvasKitAnimator.renderer =
new CanvasKitRenderer(svgData, offscreenCanvas, CanvasKit);
}
}
// The worker receives a "switchMethod" message whenever the user clicks a rendering
// method button on the web page.
if (switchMethod) {
// Pause other renderers and start correct renderer
canvasKitAnimator.stop();
path2dAnimator.stop();
if (switchMethod === 'Path2D') {
path2dAnimator.start();
} else if (switchMethod === 'CanvasKit') {
canvasKitAnimator.start();
}
}
});
// Report framerates of Path2D and CanvasKit rendering back to main.js
setInterval(() => {
if (path2dAnimator.framesCount > 0) {
postMessage({
renderMethod: 'Path2D',
framesCount: path2dAnimator.framesCount,
totalFramesMs: path2dAnimator.totalFramesMs
});
}
if (canvasKitAnimator.framesCount > 0) {
postMessage({
renderMethod: 'CanvasKit',
framesCount: canvasKitAnimator.framesCount,
totalFramesMs: canvasKitAnimator.totalFramesMs
});
}
}, 1000);