mt-polygon-simplification/lib/simplify-wasm/index.js
2019-07-16 17:31:20 +02:00

41 lines
1.3 KiB
JavaScript

/*
This file is meant to be processed in the context of an asset bundler.
Specifically the import of 'simplify.wasm' is meant to be resolved by the
corresponding path to that file. Configure a file loader to resolve .wasm
files (e.g. Webpack: file-loader) accordingly.
Otherwise you have to use the module factory 'simplify.js' manually and
reproduce the steps from below to fit your build context. See example.html.
Other than wrapping the memory handling for convenience this method also makes
sure the module is only loaded once.
*/
import wasmModuleFactory from './simplify.js'
import wasmUrl from './simplify.wasm'
import { initEmscriptenModule } from '../wasm-util/initEmscripten.js'
import {
storeCoords,
loadResultAndFreeMemory
} from '../wasm-util/coordinates.js'
export async function simplifyWasm(coords, tolerance, highestQuality) {
const module = await getModule()
const buffer = storeCoords(module, coords)
const resultInfo = module._simplify(
buffer,
coords.length * 2,
tolerance,
highestQuality
)
module._free(buffer)
return loadResultAndFreeMemory(module, resultInfo)
}
let emscriptenModule
export async function getModule() {
if (!emscriptenModule)
emscriptenModule = initEmscriptenModule(wasmModuleFactory, wasmUrl)
return await emscriptenModule
}