-
Notifications
You must be signed in to change notification settings - Fork 41
/
canvas-utils.js
64 lines (50 loc) · 1.29 KB
/
canvas-utils.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const splitRGB = (data) => {
const rgba = {
red: [],
green: [],
blue: [],
alpha: []
}
for (let i = 0; i < data.length - 1; i += 4) {
rgba.red.push(data[i])
rgba.green.push(data[i + 1])
rgba.blue.push(data[i + 2])
rgba.alpha.push(data[i + 3])
}
return rgba
}
const mountRGB = ({red, green, blue, alpha}) => {
const arr = new Uint8ClampedArray(red.length * 4)
const length = arr.length
let iterator = 0
for (let i = 0; i < length - 1; i += 4) {
arr[i] = red[iterator]
arr[i + 1] = green[iterator]
arr[i + 2] = blue[iterator]
arr[i + 3] = alpha[iterator]
iterator++
}
return arr
}
const plotFiltered = (signal, plotContext) => {
const { width, height } = plotContext.canvas
const imgData = plotContext.getImageData(0, 0, width, height)
const dataArr = new Uint8ClampedArray(imgData.data.length)
for (let i = 0; i < signal.length; i += 4) {
dataArr[i] = signal[i]
dataArr[i + 1] = signal[i + 1]
dataArr[i + 2] = signal[i + 2]
dataArr[i + 3] = 255
}
imgData.data.set(dataArr)
plotContext.putImageData(imgData, 0, 0)
}
const removeAlpha = (data) => {
const arr = []
for (let i = 0; i < data.length - 1; i += 4) {
arr.push(data[i])
arr.push(data[i + 1])
arr.push(data[i + 2])
}
return arr
}