skia2/experimental/canvaskit/canvaskit
Kevin Lubick 269fe89950 [canvaskit] Add particles integration
Bug: skia:
Change-Id: I5bf08e8007b59b5e21005f5004d698e53b99ffe6
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/197849
Commit-Queue: Kevin Lubick <kjlubick@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
2019-03-06 14:52:56 +00:00
..
.gitignore Add Correctness tests for CanvasKit 2018-10-16 14:32:28 +00:00
CODE_OF_CONDUCT.md [canvaskit] Add files needed for npm release 2018-12-17 17:21:34 +00:00
CONTRIBUTING.md [canvaskit] Add files needed for npm release 2018-12-17 17:21:34 +00:00
example.html [canvaskit] Add particles integration 2019-03-06 14:52:56 +00:00
LICENSE [canvaskit] Add files needed for npm release 2018-12-17 17:21:34 +00:00
node.example.js [canvaskit] JS API in place for shaped text 2019-02-22 15:39:55 +00:00
NotoSerif-Regular.ttf [canvaskit] JS API in place for shaped text 2019-02-22 15:39:55 +00:00
package.json [canvaskit] Roll 0.4.1 2019-03-01 16:24:13 +00:00
README.md [canvaskit] Add .ready() which is a real promise 2019-01-04 19:48:19 +00:00
Roboto-Regular.ttf [canvaskit] Allow users to load their own fonts 2018-12-12 15:40:08 +00:00
Roboto-Regular.woff [canvaskit] Allow users to load their own fonts 2018-12-12 15:40:08 +00:00
test.png [canvaskit] Add drawImage support 2018-12-03 18:47:29 +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,
}).ready().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.3.0/bin/canvaskit.js"></script>
CanvasKitInit({
     locateFile: (file) => 'https://unpkg.com/canvaskit-wasm@0.3.0/bin/'+file,
}).ready().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,
}).ready().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().ready().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 API.

More detailed docs will be coming soon.

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 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.

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.