-
Notifications
You must be signed in to change notification settings - Fork 2
/
flipflop.js
67 lines (62 loc) · 2.19 KB
/
flipflop.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
"use strict";
/*
* 2021/10/16- (c) [email protected]
*/
document.addEventListener("DOMContentLoaded", function(event) {
main();
});
function main() {
// console.debug("main");
const srcCanvas = document.getElementById("srcCanvas");
const dstCanvas = document.getElementById("dstCanvas");
let srcImage = new Image(srcCanvas.width, srcCanvas.height);
const params = {};
dropFunction(document, function(dataURL) {
srcImage = new Image();
srcImage.onload = function() {
drawSrcImageAndFlipflop(srcImage, srcCanvas, dstCanvas, params);
}
srcImage.src = dataURL;
}, "DataURL");
bindFunction({"maxWidthHeightRange":"maxWidthHeightText",
"verticalCheckbox":null, "horizontalCheckbox":null},
function() {
drawSrcImageAndFlipflop(srcImage, srcCanvas, dstCanvas,
params);
}, params);
}
function drawSrcImageAndFlipflop(srcImage, srcCanvas, dstCancas, params) {
const maxWidthHeight = params.maxWidthHeightRange;
drawSrcImage(srcImage, srcCanvas, maxWidthHeight);
drawFlipflop(srcCanvas, dstCanvas, params);
}
function drawFlipflop(srcCanvas, dstCanvas, params) {
// console.debug("drawFlipflop");
const vertical = params.verticalCheckbox;
const horizontal = params.horizontalCheckbox;
const srcCtx = srcCanvas.getContext("2d");
const dstCtx = dstCanvas.getContext("2d");
const width = srcCanvas.width, height = srcCanvas.height;
dstCanvas.width = width;
dstCanvas.height = height;
//
const imageData = srcCtx.getImageData(0, 0, width, height);
const data = new Uint32Array(imageData.data.buffer);
const n = data.length;
//
if (vertical && horizontal) {
data.reverse();
} else if (vertical) {
const lineData = new (data.constructor)(width);
for (let i = 0, j = n - width; i < j; i += width, j -= width) {
lineData.set(data.subarray(i, i + width));
data.set(data.subarray(j, j + width), i);
data.set(lineData, j);
}
} else if (horizontal) {
for (let i = 0; i < n; i += width) {
data.subarray(i, i + width).reverse();
}
}
dstCtx.putImageData(imageData, 0, 0);
}