-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.ts
139 lines (108 loc) · 4.12 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
136
137
138
139
let speed: number = 40
const maxFactor: number = 40
const sides: string[] = ['front', 'back', 'right', 'left', 'top', 'bottom']
const factor = document.getElementById('input-1') as HTMLInputElement
const button = document.getElementById('button') as HTMLButtonElement
const calContainer = document.querySelector('.cal-container') as HTMLDivElement
const cube = document.querySelector('.cube') as HTMLDivElement
const buttonRax = document.getElementById('rax') as HTMLButtonElement
const buttonRay = document.getElementById('ray') as HTMLButtonElement
const speedUp = document.getElementById('speedUp') as HTMLButtonElement
const speedDown = document.getElementById('speedDown') as HTMLButtonElement
button.addEventListener('click', (): void => {
if (factor.valueAsNumber > maxFactor) {
factor.valueAsNumber = 0
}
const square: number = factor.valueAsNumber * factor.valueAsNumber
renderCube(square, Math.sqrt(square), factor.valueAsNumber)
})
//build, render, inject function
const renderCube = (xYZ: number, columnsRows: number, factorX: number): void => {
calContainer.innerHTML = ' '
cube.innerHTML = ' '
factor.value = ''
const img = document.createElement('img') as HTMLImageElement
const calculation = document.createElement('h4') as HTMLHeadingElement
factorX ? (calculation.innerText = ` ${factorX} x ${factorX} x ${factorX} = ${xYZ * factorX}`) : error(calculation) //input control
img.src = 'img/cube-svgrepo-com%20(3).svg'
img.id = 'logo'
calContainer.appendChild(calculation)
calContainer.appendChild(img)
for (let sideNumber: number = 0; sideNumber < sides.length; sideNumber++) {
const cubeSides = document.createElement('div') as HTMLDivElement
cubeSides.className = `side ${sides[sideNumber]}`
cubeSides.style.gridTemplateColumns = `repeat(${columnsRows}, 1fr)`
cubeSides.style.gridTemplateRows = `repeat(${columnsRows}, 1fr)`
cubeSides.id = `sides${sideNumber}`
cube.appendChild(cubeSides)
for (let x: number = 0; x < xYZ; x++) {
const tile = document.createElement('div') as HTMLDivElement
tile.className = 'tile'
tile.id = `tile${x}-${sideNumber}`
tile.style.cssText = `--index: ${x}`
cubeSides.appendChild(tile)
}
}
}
// cube rotation color and speed manipulation
const colorPicker = document.querySelector('.color-picker') as HTMLDivElement
const defaultColors = ['#ff00fc', '#00fde1', '#0c34f8', '#ffffff', '#11ffc8']
for (let i: number = 1; i <= 5; i++) {
let cssColor: string = `--color-${i}`
let color: HTMLInputElement = document.createElement('input') as HTMLInputElement
color.type = 'color'
color.id = `color${i}`
color.className = 'color-pickers'
// Set the color picker value to the default color
color.value = defaultColors[i - 1]
colorPicker.appendChild(color)
color.addEventListener('input', (): void => {
document.documentElement.style.setProperty(cssColor, color.value)
})
}
speedUp.addEventListener('click', (): void => {
speed += 40
xText = xAxisTrue ? `${speed}turn` : '0'
yText = yAxisTrue ? '0' : `${speed}turn`
updateKeyframes()
})
speedDown.addEventListener('click', (): void => {
speed -= 40
xText = xAxisTrue ? `${speed}turn` : '0'
yText = yAxisTrue ? '0' : `${speed}turn`
updateKeyframes()
})
buttonRax.addEventListener('click', (): void => {
xAxisTrue = !xAxisTrue
xText = xAxisTrue ? `${speed}turn` : '0'
updateKeyframes()
})
buttonRay.addEventListener('click', (): void => {
yAxisTrue = !yAxisTrue
yText = yAxisTrue ? '0' : `${speed}turn`
updateKeyframes()
})
let xAxisTrue: boolean = false
let yAxisTrue: boolean = false
let xText: string = '-20deg'
let yText: string = `${speed}turn`
const updateKeyframes = (): void => {
document.styleSheets[0].deleteRule(0)
document.styleSheets[0].insertRule(
`
@keyframes cube-rotation {
from { transform: rotateX(-20deg) rotateY(-45deg); }
to { transform: rotateX(${xText}) rotateY(${yText}); }
}
`,
0
)
}
//error handler
const error = (errorText: HTMLHeadingElement): void => {
errorText.innerText = `ERROR only numbers 1-${maxFactor}`
errorText.style.color = 'rgb(255,5,5)'
}
updateKeyframes()
const square: number = 3 * 3
renderCube(square, Math.sqrt(square), 3)