Skip to content

Commit

Permalink
feat: add .pixel() API
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed Jun 24, 2022
1 parent 6240299 commit f1fbf97
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ async function main() {
const pngBuffer = pngData.asPng()

console.info('Original SVG Size:', `${resvg.width} x ${resvg.height}`)
console.info('Output PNG Size :', `${pngData.width} x ${pngData.height}`)
// console.info('Output PNG Size :', `${pngData.width} x ${pngData.height}`)
// console.info('pixmap :', `${pngData.pixel}`)

console.info(typeof pngData.pixel)
console.info('✨ Done in', performance.now() - t, 'ms')

await promises.writeFile(join(__dirname, './text-out.png'), pngBuffer)
Expand Down
2 changes: 2 additions & 0 deletions js-binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export class Resvg {
export class RenderedImage {
/** Write the image data to Buffer */
asPng(): Buffer
/** Get Pixmap */
get pixel(): Buffer
/** Get the PNG width */
get width(): number
/** Get the PNG height */
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ impl RenderedImage {
Ok(buffer.as_slice().into())
}

/// Get pixel data
#[cfg(target_arch = "wasm32")]
pub fn pixel(&self) -> js_sys::Uint8Array {
self.pix.data().into()
}

/// Get pixel data
#[cfg(not(target_arch = "wasm32"))]
#[napi(getter)]
pub fn pixel(&self) -> Result<Buffer, NapiError> {
let buffer = self.pix.data();
Ok(buffer.into())
}

#[cfg(not(target_arch = "wasm32"))]
#[napi(getter)]
/// Get the PNG width
Expand Down

0 comments on commit f1fbf97

Please sign in to comment.