-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglitchRGB.js
67 lines (63 loc) · 2.45 KB
/
glitchRGB.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
65
66
67
"use strict";
/*
* 2023/05/05- (c) [email protected]
*/
document.addEventListener("DOMContentLoaded", function(event) {
main();
});
function main() {
// console.debug("main");
const srcCanvas = document.getElementById("srcCanvas");
const dstCanvas = document.getElementById("dstCanvas");
const srcImage = new Image();
const params = {};
srcImage.onload = function() {
drawSrcImageAndGlitchRGB(srcImage, srcCanvas, dstCanvas, params);
}
srcImage.src = "./img/RGBCube.png"
dropFunction(document, function(dataURL) {
srcImage.src = dataURL;
}, "DataURL");
bindFunction({"maxWidthHeightRange":"maxWidthHeightText",
"dxR":"dxRText", "dyR":"dyRText",
"dxG":"dxGText", "dyG":"dyGText",
"dxB":"dxBText", "dyB":"dyBText",
"dxA":"dxAText", "dyA":"dyAText"},
function() {
drawSrcImageAndGlitchRGB(srcImage, srcCanvas, dstCanvas,
params);
}, params);
}
function drawSrcImageAndGlitchRGB(srcImage, srcCanvas, dstCancas, params) {
const maxWidthHeight = params.maxWidthHeightRange;
drawSrcImage(srcImage, srcCanvas, maxWidthHeight);
drawGlitchRGB(srcCanvas, dstCanvas, params);
}
function drawGlitchRGB(srcCanvas, dstCanvas, params) {
// console.debug("drawGlitchRGB");
const srcCtx = srcCanvas.getContext("2d");
const dstCtx = dstCanvas.getContext("2d");
const width = srcCanvas.width, height = srcCanvas.height;
dstCanvas.width = width;
dstCanvas.height = height;
//
const srcImageData = srcCtx.getImageData(0, 0, width, height);
const dstImageData = dstCtx.createImageData(width, height);
const {dxR, dxG, dxB, dxA} = params;
const {dyR, dyG, dyB, dyA} = params;
for (let y = 0 ; y < height; y++) {
for (let x = 0 ; x < width; x++) {
const rgbaR = getRGBA(srcImageData, x-dxR, y-dyR, OUTFILL_EDGE);
const rgbaG = getRGBA(srcImageData, x-dxG, y-dyG, OUTFILL_EDGE);
const rgbaB = getRGBA(srcImageData, x-dxB, y-dyB, OUTFILL_EDGE);
const rgbaA = getRGBA(srcImageData, x-dxA, y-dyA, OUTFILL_EDGE);
const data = dstImageData.data;
const offset = 4 * (x + y * width);
data[offset + 0] = rgbaR[0];
data[offset + 1] = rgbaG[1];
data[offset + 2] = rgbaB[2];
data[offset + 3] = rgbaA[3];
}
}
dstCtx.putImageData(dstImageData, 0, 0);
}