-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-rect.js
34 lines (30 loc) · 997 Bytes
/
find-rect.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
28
29
30
31
32
33
34
/**
* Main thread interface to find-rect-worker.
*
* Copyright (c) 2017-2025 Alex Grant (@localnerve), LocalNerve LLC
* Copyrights licensed under the BSD License. See the accompanying LICENSE file for terms.
*/
/**
* Transfer the imageData to the worker and invoke the worker algo.
*
* @param {WebWorker} worker - The rect finding web worker.
* @param {CanvasRenderingContext2D} ctx - The canvas context to search.
* @param {Number} width - The image width.
* @param {Number} height - The image height.
* @param {Object} options - algorithm options.
* @returns {Promise} Resolves to found rect.
*/
export default function findRect (worker, ctx, width, height, options) {
const imageData = ctx.getImageData(0, 0, width, height);
const result = new Promise((resolve, reject) => {
worker.onmessage = (e) => {
resolve(e.data);
};
worker.onerror = reject;
});
worker.postMessage({
imageData,
options
}, [imageData.data.buffer]);
return result;
}