-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
193 lines (164 loc) · 6.23 KB
/
app.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
function getRandomColor() : string {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function getRandomArbitrary(min : number, max : number) :number {
return Math.random() * (max - min) + min;
}
function setBackgroundColor(canvas: HTMLCanvasElement, backgroundColor: string) : void {
let ctx = getContext(canvas);
ctx.fillStyle = backgroundColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function getCanvas() : HTMLCanvasElement {
let canvas = document.getElementById("canvas") as HTMLCanvasElement;
if(!canvas) {
throw "cannot find the canvas in the page";
}
return canvas;
}
function getContext(canvas: HTMLCanvasElement) : CanvasRenderingContext2D {
let context = canvas.getContext("2d");
if(!context) {
throw "cannot find the context in the page";
}
return context;
}
function createCross(
context: CanvasRenderingContext2D,
widthCross: number,
x: number,
y: number,
angle: number,
crossesColor: string
) : void {
context.save();
context.strokeStyle = crossesColor;
context.rotate(angle);
context.beginPath();
context.moveTo(x,y + widthCross/2);
context.lineTo(x + widthCross, y + widthCross/2);
context.moveTo(x + widthCross/2 ,y);
context.lineTo(x + widthCross/2, y + widthCross);
context.stroke();
context.restore();
}
function draw(
widthCross: number,
lineWidth: number,
areaSize: number,
percentageAppears: number,
angle: number,
backgroundColor: string,
crossesColor: string
) {
let canvas = getCanvas();
let context = getContext(canvas);
context.canvas.width = window.innerWidth;
context.canvas.height = window.innerHeight;
context.lineWidth = lineWidth;
context.clearRect(0, 0, canvas.width, canvas.height);
setBackgroundColor(canvas, backgroundColor);
for(let _x = 0; _x < canvas.width; _x += areaSize) {
for(let _y = 0; _y < canvas.height; _y += areaSize) {
if(Math.random() >= (percentageAppears / 100)) {
const x = getRandomArbitrary(_x, _x + areaSize - widthCross);
const y = getRandomArbitrary(_y, _y + areaSize - widthCross);
const angleInDeg = getRandomArbitrary(-angle, angle);
const angleInRand = angleInDeg * Math.PI / 180;
createCross(context, widthCross, x, y, angleInRand, crossesColor);
}
}
}
}
window.addEventListener("load", function(event) {
const settingsDom = document.getElementById("settings");
const closeButtonDom = document.getElementById("close-button");
const openButtonDom = document.getElementById("open-button");
const saveAsPngDom = document.getElementById("save-as-png");
const widthCrossDom = document.getElementById("width-cross");
const areaSizeDom = document.getElementById("area-size");
const lineWidthDom = document.getElementById("line-width");
const percentageAppearsDom = document.getElementById("percentage-appears");
const angleDom = document.getElementById("angle");
const backgroundColorDom = document.getElementById("background");
const crossesColorDom = document.getElementById("crosses-color");
let widthCross = 20;
let areaSize = 50;
let lineWidth = 5;
let percentageAppears = 50; //%
let angle = 0;
let backgroundColor = "#ff0000";
let crossesColor = "#000000";
draw(widthCross, lineWidth, areaSize, percentageAppears, angle, backgroundColor, crossesColor);
if(widthCrossDom) {
widthCrossDom.addEventListener("change", (event : any) => {
widthCross = parseInt(event.target.value, 10);
(widthCrossDom as Element).nextElementSibling!.innerHTML = event.target.value;
draw(widthCross, lineWidth, areaSize, percentageAppears, angle, backgroundColor, crossesColor);
});
}
if(areaSizeDom) {
areaSizeDom.addEventListener("change", (event : any) => {
areaSize = parseInt(event.target.value, 10);
(areaSizeDom as Element).nextElementSibling!.innerHTML = event.target.value;
draw(widthCross, lineWidth, areaSize, percentageAppears, angle, backgroundColor, crossesColor);
});
}
if(lineWidthDom) {
lineWidthDom.addEventListener("change", (event : any) => {
lineWidth = parseInt(event.target.value, 10);
(lineWidthDom as Element).nextElementSibling!.innerHTML = event.target.value;
draw(widthCross, lineWidth, areaSize, percentageAppears, angle, backgroundColor, crossesColor);
});
}
if(percentageAppearsDom) {
percentageAppearsDom.addEventListener("change", (event : any) => {
percentageAppears = 100 - parseInt(event.target.value, 10);
(percentageAppearsDom as Element).nextElementSibling!.innerHTML = event.target.value;
draw(widthCross, lineWidth, areaSize, percentageAppears, angle, backgroundColor, crossesColor);
});
}
if(angleDom) {
angleDom.addEventListener("change", (event : any) => {
angle = parseInt(event.target.value, 10);
(angleDom as Element).nextElementSibling!.innerHTML = event.target.value;
draw(widthCross, lineWidth, areaSize, percentageAppears, angle, backgroundColor, crossesColor);
});
}
if(backgroundColorDom) {
backgroundColorDom.addEventListener("change", (event : any) => {
backgroundColor = event.target.value;
draw(widthCross, lineWidth, areaSize, percentageAppears, angle, backgroundColor, crossesColor);
});
}
if(crossesColorDom) {
crossesColorDom.addEventListener("change", (event : any) => {
crossesColor = event.target.value;
draw(widthCross, lineWidth, areaSize, percentageAppears, angle, backgroundColor, crossesColor);
});
}
// I assume everything is in the dom
if(settingsDom && openButtonDom && closeButtonDom) {
settingsDom.style.display = "none";
openButtonDom.addEventListener("click", () => {
settingsDom.style.display = "flex";
openButtonDom.style.display = "none";
closeButtonDom.style.display = "flex";
});
closeButtonDom.addEventListener("click", () => {
settingsDom.style.display = "none";
openButtonDom.style.display = "flex";
closeButtonDom.style.display = "none";
});
}
if(saveAsPngDom) {
saveAsPngDom.addEventListener("click", (event: any) => {
event.target.href = getCanvas().toDataURL("image/png");
});
}
});