SDK Installation
The functionality of the libpag SDK relies on two key components: libpag.js and libpag.wasm files. To simplify, libpag.js serves as the interface module while libpag.wasm acts as the core module. When integrating libpag.js, it is necessary to instantiate the loading of libpag.wasm by invoking the PAGInit()
nterface. The libpag.wasm file in the same directory as the currently executing script will be loaded by default. In cases where libpag.wasm is not located in the same directory, the locateFile
hook on PAGInit()
can be utilized to specify the path for libpag.wasm.
Wasm files can be GZIP. Public CDN enables GZIP of wasm files by default. If it's your own static resource service, you need to configure it manually.
Use Via CDN
Directly use <script>
o import js files on the CDN.
<script src="https://cdn.jsdelivr.net/npm/libpag@latest/lib/libpag.min.js"></script>
You can browse the contents of the NPM package in the public CDN cdn.jsdelivr.net/npm/libpag/,if you want to specify a certain version, you can use @<version> to specify it.
Alternatively, you can use other public CDNs that synchronize NPM, such as unpkg , or download the files and load them yourself.
Use the Globally Imported Version
libpag
will be registered as a global variable. You can call libpag.PAGInit
o instantiate to obtain the PAG
instance:
<script src="https://cdn.jsdelivr.net/npm/libpag@latest/lib/libpag.min.js"></script>
<script>
libpag.PAGInit().then((pag) => {
// Instantiation succeeded
})
</script>
Versions Imported Using ES Modules
Most modern browsers now natively support ES modules, so you can utilize it as follows:
<script type="module">
import { PAGInit } from "https://cdn.jsdelivr.net/npm/libpag@latest/lib/libpag.esm.js";
PAGInit({
locateFile: (file) => "https://cdn.jsdelivr.net/npm/libpag@latest/lib/" + file
}).then((pag) => {
// Instantiation succeeded
})
</script>
In the PAGInit method, you'll see that the hook using locateFile indicates the location of libpag.wasm.
Use Via NPM
Prerequisites
- You have successfully installed Node.jsand are well-versed with using the command line.
- You are familiar with packaging tools, such as Webpack/Rollup.
In this section, we will introduce how to introduce libpag into the engineering project, in which Webpack/Rollup needs to be configured to copy libpag.wasm.
Run npm on the command line to install libpag (without the >
symbol):
> npm install libpag
Introduce in the code:
import { PAGInit } from 'libpag';
PAGInit().then(pag => {
// Instantiation succeeded
})
Please keep in mind that packaging tools like Webpack and Rollup do not include .wasm files in the packaging process by default. If your project is built using scaffolding tools such as Vue or React, you will need to package the libpag/lib/libpag.wasm file located under node_modules into the final product. Additionally, you should use the locateFile
hook to retrieve the path of the libpag.wasm file. This ensures that the libpag.wasm file can be successfully loaded during network requests.
For simple access examples and configuration examples of Vue/React/PixiJS, you can click here to view.