-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
image.ts
48 lines (44 loc) · 1.23 KB
/
image.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Xpra Typescript Client
* @link https://github.com/andersevenrud/xpra-html5-client
* @author Anders Evenrud <andersevenrud@gmail.com>
* @license Mozilla Public License Version 2.0
*
* ---
*
* Based on original Xpra source
* @copyright Copyright (C) 2016-2022 Antoine Martin <antoine@devloop.org.uk>
* @license Licensed under MPL 2.0, see: http://www.mozilla.org/MPL/2.0/
* @link https://github.com/Xpra-org/xpra-html5
*/
import { uint8fromStringOrUint8, arrayBufferToBase64 } from './data'
/**
* A promise wrapper for loading images in DOM
*/
export const loadImage = (src: string) =>
new Promise<HTMLImageElement | null>((resolve) => {
const image = new Image()
image.onload = () => {
if (image.width && image.height) {
resolve(image)
} else {
resolve(null)
}
}
image.onerror = () => {
resolve(null)
}
image.src = src
})
/**
* Creates an "image source" (aka URL) from image data
*/
export function imageSourceFromData(
encoding: string,
data: Uint8Array | string
) {
const imageData = uint8fromStringOrUint8(data)
const src = arrayBufferToBase64(imageData)
const enc = encoding === 'svg' ? 'svg+xml' : encoding
return `data:image/${enc};base64,${src}`
}