Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 1 | A WASM version of Skia's PathOps toolkit. |
| 2 | |
| 3 | To use the library, run `npm install experimental-pathkit-wasm` and then simply include it: |
| 4 | |
| 5 | <script src="/node_modules/experimental-pathkit-wasm/bin/pathkit.js"></script> |
| 6 | PathKitInit({ |
| 7 | locateFile: (file) => '/node_modules/experimental-pathkit-wasm/bin/'+file, |
| 8 | }).then((PathKit) => { |
| 9 | // Code goes here using PathKit |
| 10 | }); |
| 11 | |
| 12 | PathKit comes in two parts, a JS loader and the actual WASM code. The JS loader creates |
| 13 | a global `PathKitInit` that can be called to load the WASM code. The `locateFile` function |
| 14 | is used to tell the JS loader where to find the .wasm file. By default, it will |
| 15 | look for /pathkit.wasm, so if this is not the case, use `locateFile` to configure |
| 16 | this properly. |
| 17 | The `PathKit` object returned through the .then() callback is fully loaded and ready to use. |
| 18 | |
| 19 | See example.html for a fuller example of how to use the library. |
| 20 | |
| 21 | Using PathKit and WebPack |
| 22 | ------------------------- |
| 23 | |
| 24 | WebPack's support for WASM is still somewhat experimental, but PathKit can be |
| 25 | used with a few configuration changes. |
| 26 | |
| 27 | In the JS code, use require(): |
| 28 | |
| 29 | const PathKitInit = require('experimental-pathkit-wasm/bin/pathkit.js') |
| 30 | PathKitInit().then((PathKit) => { |
| 31 | // Code goes here using PathKit |
| 32 | }) |
| 33 | |
| 34 | Since WebPack does not expose the entire `/node_modules/` directory, but instead |
| 35 | packages only the needed pieces, we have to copy pathkit.wasm into the build directory. |
Kevin Lubick | b3d0e3e | 2018-08-03 12:24:06 -0400 | [diff] [blame] | 36 | One such solution is to use [CopyWebpackPlugin](https://github.com/webpack-contrib/copy-webpack-plugin). |
Kevin Lubick | e1b36fe | 2018-08-02 11:30:33 -0400 | [diff] [blame] | 37 | For example, add the following plugin: |
| 38 | |
| 39 | config.plugins.push( |
| 40 | new CopyWebpackPlugin([ |
| 41 | { from: 'node_modules/experimental-pathkit-wasm/bin/pathkit.wasm' } |
| 42 | ]) |
| 43 | ); |
| 44 | |
| 45 | If webpack gives an error similar to: |
| 46 | |
| 47 | ERROR in ./node_modules/experimental-pathkit-wasm/bin/pathkit.js |
| 48 | Module not found: Error: Can't resolve 'fs' in '...' |
| 49 | |
| 50 | Then, add the following configuration change to the node section of the config: |
| 51 | |
| 52 | config.node = { |
| 53 | fs: 'empty' |
Kevin Lubick | 30cc00c | 2018-08-03 10:26:00 -0400 | [diff] [blame] | 54 | }; |