-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
80 lines (61 loc) · 2.31 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2022 Ivan Teplov
const canvas = document.querySelector("#canvas")
const context = canvas.getContext("2d")
// Scales an X coordinate from [-1; 1] to [-widthOfCanvas / 2; widthOfCanvas / 2]
const scaleX = number => number * canvas.width / 2
// Scales a Y coordinate from [-1; 1] to [-heightOfCanvas / 2; heightOfCanvas / 2]
const scaleY = number => number * canvas.height / 2
// Object with all parameters of the superellipse
const parameters = {
get power() {
return +document.querySelector("#power").value
},
get xDivisor() {
return +document.querySelector("#xDivisor").value
},
get yDivisor() {
return +document.querySelector("#yDivisor").value
}
}
const y = (x) => parameters.yDivisor * Math.pow(
1 - Math.pow(Math.abs(x / parameters.xDivisor), parameters.power), 1 / parameters.power
)
const draw = () => {
// Clear the canvas
context.clearRect(0, 0, canvas.width, canvas.height)
// Save canvas context's default state
context.save()
// Move start of coordinates to the center of the canvas
context.translate(canvas.width / 2, canvas.height / 2)
// Start the path of superellipse
context.beginPath()
// Move to the left point of the superellipse
context.moveTo(scaleX(-1 / xDivisor), 0)
// Draw the top part of the superellipse
for (let x = -1; x <= 1; x += 0.01) {
context.lineTo(scaleX(x), scaleY(y(x)))
}
// Draw the bottom part of the superellipse
for (let x = 1; x >= -1; x -= 0.01) {
context.lineTo(scaleX(x), scaleY(-y(x)))
}
// Close the superellise's path
context.closePath()
// Fill the path with a color that's set in CSS
context.fillStyle = "cornflowerblue"
context.fill()
// Restore canvas context's default state
context.restore()
}
draw()
/* When the window is resized, canvas usually gets cleared.
* That's why we need to draw the superellipse again */
window.addEventListener("resize", () => draw())
// When any of the available parameters get changed, redraw the superellipse
for (const input of document.querySelectorAll("input")) {
input.addEventListener("input", () => {
// Update the input's 'value' attribute in order for `attr(value)` to work in CSS
input.setAttribute("value", input.value)
draw()
})
}