-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.ts
135 lines (107 loc) · 3 KB
/
script.ts
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Canvas
*/
var canvas = <HTMLCanvasElement>document.getElementById("ca");
var bgc = "#ebebe1";
document.body.style.backgroundColor = bgc;
var defaultImage = "./img3.jpg";
var pr = Math.min(window.devicePixelRatio, 2);
/**
* Worker
*/
var worker = new Worker("./worker.js");
/**
* User generated
*/
var controls = <HTMLInputElement>document.getElementById("controls");
var userImageInput = <HTMLInputElement>document.getElementById("userimage");
var form = <HTMLFormElement>document.getElementById("userimageform");
var resolutionInput = <HTMLInputElement>document.getElementById("resolution");
var currentImage: ImageBitmap | null = null;
var getCanvasSide = (pr) => (
window.innerWidth < window.innerHeight
? window.innerWidth * pr
: window.innerHeight * pr
);
var resizeCanvas = (canvas, width, height, pr) => {
canvas.style.width = width / pr + "px";
canvas.style.height = height / pr + "px";
};
var prepareDataForWorker = (image) => {
var canvasWidth = getCanvasSide(pr);
var canvasHeight = canvasWidth;
var resolution = Number(resolutionInput.value);
resizeCanvas(canvas, canvasWidth, canvasHeight, pr);
worker.postMessage({
message: "processFirstFrame",
image,
canvasWidth,
canvasHeight,
resolution
});
};
var onImageLoaded = (image) => {
createImageBitmap(image)
.then((bitmap) => {
currentImage = bitmap;
prepareDataForWorker(bitmap);
});
};
var loadImage = (urlOrData) => {
var image = new Image();
image.src = urlOrData;
image.addEventListener("load", onImageLoaded.bind(null, image), { once: true });
};
var loadImageData = (file) => {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (readerEvent) => {
loadImage(readerEvent.target.result);
};
};
var getImageFromInput = () => {
var data = new FormData(form);
var file = data.get("userimage");
if (!file || file.name === "") {
return null;
}
return file;
};
form.addEventListener("submit", () => {
var file = getImageFromInput();
file && loadImageData(file);
controls.classList.remove("dragover");
});
resolutionInput!.addEventListener("change", () => {
prepareDataForWorker(currentImage);
});
window.addEventListener("resize", () => {
prepareDataForWorker(currentImage);
});
controls.addEventListener("dragover", () => {
controls.classList.add("dragover");
});
controls.addEventListener("dragleave", () => {
controls.classList.remove("dragover");
});
userImageInput.addEventListener("dragover", () => {
userImageInput.classList.add("dragover");
});
userImageInput.addEventListener("dragleave", () => {
userImageInput.classList.remove("dragover");
});
userImageInput.addEventListener("drop", () => {
userImageInput.classList.remove("dragover");
});
var setup = () => {
var osc = canvas.transferControlToOffscreen();
worker.postMessage({
message: "initCanvas",
canvas: osc,
}, [osc]);
var userFile = getImageFromInput();
userFile
? loadImageData(userFile)
: loadImage(defaultImage);
};
setup();