c48c3e5d57
Known hacks for now: - only default typeface - only ascii support Fonts have several limitations for now. SkParagraph (our temporary backend) wants a fontmgr up front, rather than just relying on the typefaces in its styles. This makes it hard to inter-op with our JS api. Not a long-term problem, as this backend is short-term. The hacky work-around is just to pass NULL for the typeface for now. Related but different, I haven't yet figured out how to "return" a JS Typeface object. Something about bare-pointers and sk_sp. I'll figure it out eventually, but for now I just set the output slot to null as well. (there's a TODO in the .cpp) Ascii support JS strings are UTF16 (afaik). However, our current backend thinks in terms of UTF8 offsets. Plus, (at the moment) when I try to access the JS string for C++, it coerces to utf8 (std::string). We can fix both of these eventually, but for now I'll just test with ascii input characters, and the API can still work. Change-Id: I62ca71b0b45d017ac8e3881c06720776dc2d75a1 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/405400 Commit-Queue: Mike Reed <reed@google.com> Reviewed-by: Julia Lavrova <jlavrova@google.com> |
||
---|---|---|
.. | ||
types | ||
.gitignore | ||
CODE_OF_CONDUCT.md | ||
CONTRIBUTING.md | ||
example.html | ||
extra.html | ||
LICENSE | ||
node.example.js | ||
package-lock.json | ||
package.json | ||
README.md | ||
shaping.html | ||
textapi_utils.js |
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 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.
Types and Documentation
There are Typescript types and associated API docs in types/.