-
Notifications
You must be signed in to change notification settings - Fork 5
/
image.js
28 lines (24 loc) · 899 Bytes
/
image.js
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
// taken from https://stackoverflow.com/questions/37854355/wait-for-image-loading-to-complete-in-javascript
const loadImage = async function(imageUrl) {
let img;
const imageLoadPromise = new Promise(resolve => {
img = new Image();
img.onload = resolve;
img.crossOrigin = "anonymous"; // without this opencv imread throws "this operation is unsecure"
img.src = imageUrl;
});
await imageLoadPromise;
return img;
}
// Borrowed from docs.opencv.org sources
const loadImageToCanvas = async function(url, domCanvas) {
let ctx = domCanvas.getContext('2d');
let img = await loadImage(url);
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
}
const clearCanvas = async (domCanvas) => {
const context = domCanvas.getContext('2d');
context.clearRect(0, 0, domCanvas.width, domCanvas.height);
}