@@ -34,57 +34,38 @@ Try the live demo at: [https://wasm-ip-demo.vercel.app](https://wasm-ip-demo.ver
3434npm i wasm-image-processor
3535```
3636
37- ## Building
38-
39- ### Prerequisites
40-
41- - [ Rust] ( https://rustup.rs/ )
42- - [ wasm-pack] ( https://rustwasm.github.io/wasm-pack/ )
43-
44- ### Steps
45- 1 . Clone the repository:
46- ``` bash
47- git clone https://github.com/yourusername/wasm-image-processor.git
48- cd wasm-image-processor
49- ```
50-
51- 2 . Build the WASM package:
52- ``` bash
53- wasm-pack build --target web
54- ```
37+ ### Usage
5538
56- 3 . Copy files to demo directory:
57- ``` bash
58- # Copy JS and WASM files to demo folder
59- ./prep-demo.sh
60- ```
39+ Include the WASM module in your web page:
6140
62- 4 . Serve the demo locally:
63- ``` bash
64- # Using Node.js
65- npx serve .
41+ ``` javascript
42+ import { resize_square } from " wasm-image-processor" ;
6643
67- # Or any other static file server
68- ```
44+ // Example: resize an image uploaded via <input type="file">
45+ const fileInput = document . querySelector < HTMLInputElement > ( " #fileInput " ) ! ;
6946
70- 5 . Open ` http://localhost:3000/demo ` in your browser
47+ fileInput .addEventListener (" change" , () => {
48+ const file = fileInput .files ? .[0 ]
49+ if (! file) return
7150
72- ### Usage
51+ const reader = new FileReader ()
52+ reader .onload = () => {
53+ const arrayBuffer = reader .result as ArrayBuffer
54+ const uint8Array = new Uint8Array (arrayBuffer)
7355
74- Include the WASM module in your web page:
56+ // Resize to 512x512
57+ const resizedBytes = resize_square (uint8Array, 512 )
7558
76- ``` html
77- <script type =" module" >
78- import init , { resize_square } from " pwa_image_generator" ;
59+ // Create a new File from the resized bytes
60+ const resizedImage = new File ([resizedBytes], " resized.png" , {
61+ type: " image/png" ,
62+ })
7963
80- init ().then (() => {
81- // WASM module is ready
82- const imageData = new Uint8Array (/* your image data */ );
83- const resizedBytes = resize_square (Array .from (imageData), 500 );
64+ console .log (" Resized image:" , resizedImage)
65+ }
8466
85- // Use resizedBytes as needed
86- });
87- </script >
67+ reader .readAsArrayBuffer (file)
68+ })
8869` ` `
8970
9071### API Reference
0 commit comments