skia2/modules/canvaskit/npm_build
Kevin Lubick 88e0e75380 [canvaskit] Fix uniform handling for makeShader and MallocObj
shaders.skia.org was passing in a Malloc'd TypedArray for uniforms
(to avoid unnecessary copying of that buffer on every frame).

However, CanvasKit would always take ownership of that buffer and
free it when the shader was freed. CanvasKit should only take
ownership (and clean up) the uniform buffer if it was not
copied into pre-malloc'd memory.

This also adds a handy make target that builds CanvasKit
and copies it into the proper place in the infra repository
to run the shaders app locally.

See also: https://skia-review.googlesource.com/c/buildbot/+/543436

Change-Id: I6c6b7f94f8c8571f9ce0613ccdd7fceb0f315a12
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/543416
Reviewed-by: Jorge Betancourt <jmbetancourt@google.com>
2022-05-24 16:33:17 +00:00
..
types [canvaskit] Fix uniform handling for makeShader and MallocObj 2022-05-24 16:33:17 +00:00
.gitignore [canvaskit] Add full build to npm release. 2021-02-23 14:40:43 +00:00
CODE_OF_CONDUCT.md [canvaskit] Add full build to npm release. 2021-02-23 14:40:43 +00:00
CONTRIBUTING.md [canvaskit] Add full build to npm release. 2021-02-23 14:40:43 +00:00
example.html [canvaskit] Add rough measureText to Canvas2D emulation layer 2021-12-09 13:07:24 +00:00
extra.html Remove isOpaque parameter from CanvasKit SkRuntimeEffect APIs 2022-02-10 17:15:13 +00:00
LICENSE [canvaskit] Add full build to npm release. 2021-02-23 14:40:43 +00:00
multicanvas.html [infra] Add POC Bazel rules for CanvasKit 2021-11-12 21:38:46 +00:00
node.example.js [canvaskit] Remove deprecated MakeTypefaceFromData and RefDefault 2021-10-21 16:45:42 +00:00
package-lock.json [canvaskit] Deploy 0.34.0 2022-05-05 19:35:46 +00:00
package.json [canvaskit] Deploy 0.34.0 2022-05-05 19:35:46 +00:00
README.md [canvaskit] Add rough measureText to Canvas2D emulation layer 2021-12-09 13:07:24 +00:00
shaping.html [infra] Add POC Bazel rules for CanvasKit 2021-11-12 21:38:46 +00:00
textapi_utils.js Expose glyphIntercepts to CK 2021-05-15 15:30:50 +00:00

A WASM version of Skia's Canvas API.

See https://skia.org/user/modules/canvaskit for more background information.

Getting Started

Browser

To use the library, run npm install canvaskit-wasm and then simply include it:

<script src="/node_modules/canvaskit-wasm/bin/canvaskit.js"></script>
CanvasKitInit({
    locateFile: (file) => '/node_modules/canvaskit-wasm/bin/'+file,
}).then((CanvasKit) => {
    // Code goes here using CanvasKit
});

As with all npm packages, there's a freely available CDN via unpkg.com:

<script src="https://unpkg.com/canvaskit-wasm@0.18.1/bin/canvaskit.js"></script>
CanvasKitInit({
     locateFile: (file) => 'https://unpkg.com/canvaskit-wasm@0.18.1/bin/'+file,
}).then(...)

Node

To use CanvasKit in Node, it's similar to the browser:

const CanvasKitInit = require('/node_modules/canvaskit-wasm/bin/canvaskit.js');
CanvasKitInit({
    locateFile: (file) => __dirname + '/bin/'+file,
}).then((CanvasKit) => {
    // Code goes here using CanvasKit
});

With node, you also need to supply the --expose-wasm flag.

WebPack

WebPack's support for WASM is still somewhat experimental, but CanvasKit can be used with a few configuration changes.

In the JS code, use require():

const CanvasKitInit = require('canvaskit-wasm/bin/canvaskit.js')
CanvasKitInit().then((CanvasKit) => {
    // Code goes here using CanvasKit
});

Since WebPack does not expose the entire /node_modules/ directory, but instead packages only the needed pieces, we have to copy canvaskit.wasm into the build directory. One such solution is to use CopyWebpackPlugin. For example, add the following plugin:

config.plugins.push(
    new CopyWebpackPlugin([
        { from: 'node_modules/canvaskit-wasm/bin/canvaskit.wasm' }
    ])
);

If webpack gives an error similar to:

ERROR in ./node_modules/canvaskit-wasm/bin/canvaskit.js
Module not found: Error: Can't resolve 'fs' in '...'

Then, add the following configuration change to the node section of the config:

config.node = {
    fs: 'empty'
};

Using the CanvasKit API

See example.html and node.example.js for demos of how to use the core API.

See extra.html for some optional add-ins like an animation player (Skottie) and a particles system.

See types/index.d.ts for a typescript definition file that contains all the APIs and some documentation about them.

Drop-in Canvas2D replacement

For environments where an HTML canvas is not available (e.g. Node, headless servers), CanvasKit has an optional API (included by default) that mostly mirrors the HTML canvas.

let skcanvas = CanvasKit.MakeCanvas(600, 600);

let ctx = skcanvas.getContext('2d');
let rgradient = ctx.createRadialGradient(200, 300, 10, 100, 100, 300);

// Add three color stops
rgradient.addColorStop(0, 'red');
rgradient.addColorStop(0.7, 'white');
rgradient.addColorStop(1, 'blue');

ctx.fillStyle = rgradient;
ctx.globalAlpha = 0.7;
ctx.fillRect(0, 0, 600, 600);

let imgData = skcanvas.toDataURL();
// imgData is now a base64 encoded image.

See more examples in example.html and node.example.js.

Known issues with Canvas2D Emulation layer

  • measureText returns width only and does no shaping. It is only sort of valid with ASCII letters.
  • textAlign is not supported.
  • textBaseAlign is not supported.
  • fillText does not support the width parameter.

Filing bugs

Please file bugs at skbug.com. It may be convenient to use our online fiddle to demonstrate any issues encountered.

See CONTRIBUTING.md for more information on sending pull requests.

Types and Documentation

There are Typescript types and associated API docs in types/.