Skip to content

Commit 636de85

Browse files
docs: explain loading and reading images functions for browser
1 parent 50442dd commit 636de85

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

blog/releases/1.0.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ Developers can now catch bugs at compile time rather than discovering them in pr
3131

3232
#### Loading and saving images
3333

34-
`load()` and `save()` have been replaced with dedicated functions `read()` and `write()`.
34+
`load()` and `save()` have been restructured and replaced with different functions.
35+
36+
#### Node.js only
37+
38+
`read()` and `write()` are for reading and writing images on the file system with Node.js.
3539

3640
```ts
3741
//Before
@@ -55,7 +59,33 @@ const img = readSync('cat.jpg');
5559
writeSync('newCat.jpg', img);
5660
```
5761

58-
Those changes separate I/O operations from image manipulation for a clearer API design.
62+
#### Node.js and browser
63+
64+
You can read an image via URL, by using `fetchURL`. This function works both for Node.js and browser.
65+
66+
```ts
67+
import { fetchURL } from 'image-js';
68+
let image = await fetchURL('https://example.com/image.jpg');
69+
image = image.grey();
70+
```
71+
72+
#### Browser only
73+
74+
If you have an HTML element that you want to read an image from, use `readImg`.
75+
76+
```ts
77+
const htmlImage = document.querySelector('img');
78+
const img = readImg(htmlImage);
79+
```
80+
81+
For writing an image in the browser to canvas, `writeCanvas` is available.
82+
83+
```ts
84+
const canvas = document.getElementById('myCanvas');
85+
writeCanvas(img, canvas);
86+
```
87+
88+
These changes separate I/O operations from image manipulation for a clearer API design.
5989

6090
#### Creating images
6191

@@ -254,6 +284,8 @@ Several methods have been renamed for consistency:
254284

255285
`img.getMedian()` ➡️ `img.median()`
256286

287+
`getBase64()` ➡️ `encodeDataURL()`
288+
257289
### Compatibility requirements
258290

259291
- Node.js: 18+ (previously 14+)

docs/getting-started.mdx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,45 @@ let image = await fetchURL('https://example.com/image.jpg');
9696
image = image.grey();
9797
```
9898

99+
You can also read and write an image from and to HTML image source. Here is a simple example:
100+
101+
```html
102+
<html>
103+
<head> </head>
104+
<body>
105+
<img src="https://example.com/image.jpg" />
106+
<canvas id="myCanvas"></canvas>
107+
<script type="module">
108+
import {
109+
readImg,
110+
writeCanvas,
111+
} from 'https://cdn.jsdelivr.net/npm/image-js@latest/+esm';
112+
113+
async function run() {
114+
const htmlImg = document.querySelector('img');
115+
// Wait for image to load
116+
const { promise, resolve } = Promise.withResolvers();
117+
118+
if (htmlImg.complete) {
119+
resolve();
120+
} else {
121+
htmlImg.onload = resolve;
122+
}
123+
124+
await promise;
125+
126+
let img = await readImg(htmlImg);
127+
img = img.grey();
128+
const canvas = document.getElementById('myCanvas');
129+
writeCanvas(img, canvas);
130+
}
131+
132+
await run();
133+
</script>
134+
</body>
135+
</html>
136+
```
137+
99138
:::info
100139
To see more methods, visit the ["Features"](./features/features.mdx) category.
101140
:::

0 commit comments

Comments
 (0)