fix colors on wheel & rotating

This commit is contained in:
p-wojt 2022-01-02 21:15:55 +01:00
parent 0f955e3ce2
commit a8ce06c3b8
6 changed files with 114 additions and 308 deletions

View File

@ -18,12 +18,16 @@ const counterEl = document.getElementsByClassName(
const removeAllItemsButtonEl = document.getElementsByClassName(
"remove-all-items-button"
)[0]! as HTMLButtonElement;
const lightDarkModeEl = document.getElementsByClassName('light-dark-mode')[0]! as HTMLDivElement;
const canvasEl = document.querySelector('canvas')! as HTMLCanvasElement;
const ctx = canvasEl.getContext('2d')!;
const bodyEl = document.querySelector('body')! as HTMLBodyElement;
const modeEl = lightDarkModeEl.querySelector('img')! as HTMLImageElement;
const canvasWrapper = document.getElementsByClassName('roulette-wheel')[0]! as HTMLDivElement;
const lightDarkModeEl = document.getElementsByClassName(
"light-dark-mode"
)[0]! as HTMLDivElement;
const canvasEl = document.querySelector("canvas")! as HTMLCanvasElement;
const ctx = canvasEl.getContext("2d")!;
const bodyEl = document.querySelector("body")! as HTMLBodyElement;
const modeEl = lightDarkModeEl.querySelector("img")! as HTMLImageElement;
const canvasWrapper = document.getElementsByClassName(
"roulette-wheel"
)[0]! as HTMLDivElement;
const MAXIMUM_SIZE = 16;
@ -35,13 +39,13 @@ const items: MenuItem[] = [];
configure();
function configure() {
LightDarkMode.currentMode = 'dark';
LightDarkMode.currentMode = "dark";
IdPool.initializeIdsPool(MAXIMUM_SIZE);
addRemoveItem();
addItemByEnter();
removeAllItems();
lightDarkMode();
drawRouletteWheel();
drawRouletteWheel(0);
}
function addRemoveItem() {
@ -60,7 +64,7 @@ function addRemoveItem() {
itemsList.remove(item);
IdPool.addId(item.id);
updateCounter();
drawRouletteWheel();
drawRouletteWheel(0);
});
registerChangeColorByClick(item, menuItem);
@ -69,28 +73,34 @@ function addRemoveItem() {
items.push(menuItem);
itemListEl.appendChild(menuItem.el);
updateCounter();
drawRouletteWheel();
guessItemIndex = Math.floor(Math.random() * items.length);
maxAngle = 360 * rotations + segmentAngle * guessItemIndex;
segmentAngle = 360 / items.length;
drawRouletteWheel(0);
}
});
}
function addItemByEnter(){
bodyEl.addEventListener('keyup', (event: KeyboardEvent) => {
if(event.key !== 'Enter'){
function addItemByEnter() {
bodyEl.addEventListener("keyup", (event: KeyboardEvent) => {
if (event.key !== "Enter") {
return;
}
addButtonEl.click();
})
});
}
function registerChangeColorByClick(item: Item, menuItem: MenuItem) {
menuItem.el.addEventListener('click', () => {
const actualColorIndex = avaliableRGBs.indexOf(menuItem.el.style.backgroundColor);
if(actualColorIndex !== -1){
const color = avaliableRGBs[(actualColorIndex + 1) % avaliableRGBs.length];
menuItem.el.addEventListener("click", () => {
const actualColorIndex = avaliableRGBs.indexOf(
menuItem.el.style.backgroundColor
);
if (actualColorIndex !== -1) {
const color =
avaliableRGBs[(actualColorIndex + 1) % avaliableRGBs.length];
menuItem.el.style.backgroundColor = color;
item.color = color;
drawRouletteWheel();
drawRouletteWheel(0);
}
});
}
@ -103,7 +113,7 @@ function removeAllItems() {
itemsList.clear();
IdPool.initializeIdsPool(MAXIMUM_SIZE);
updateCounter();
drawRouletteWheel();
drawRouletteWheel(0);
}
});
}
@ -122,256 +132,113 @@ function animateCounter() {
);
}
function lightDarkMode(){
lightDarkModeEl.addEventListener('click', () => {
modeEl.animate(
[
{ transform: 'rotate(360deg)' }
],
{
duration: 500
}
);
function lightDarkMode() {
lightDarkModeEl.addEventListener("click", () => {
modeEl.animate([{ transform: "rotate(360deg)" }], {
duration: 500,
});
if(LightDarkMode.currentMode == 'dark'){
modeEl.src = 'static/sun.png';
bodyEl.style.backgroundColor = 'snow';
bodyEl.style.color = 'black';
}else{
modeEl.src = 'static/moon.png';
bodyEl.style.backgroundColor = '#121212';
bodyEl.style.color = 'white';
if (LightDarkMode.currentMode == "dark") {
modeEl.src = "static/sun.png";
bodyEl.style.backgroundColor = "snow";
bodyEl.style.color = "black";
} else {
modeEl.src = "static/moon.png";
bodyEl.style.backgroundColor = "#121212";
bodyEl.style.color = "white";
}
LightDarkMode.changeMode();
})
});
}
function drawRouletteWheel(){
ctx.clearRect(0, 0, canvasEl.width, canvasEl.height);
function drawRouletteWheel(angle: number) {
canvasEl.width = canvasWrapper.offsetWidth;
canvasEl.height = canvasWrapper.offsetHeight;
const x = canvasEl.width / 2;
const y = canvasEl.height / 2;
const radius = canvasEl.height / 2;
let startAngle = 0;
ctx.clearRect(0, 0, canvasEl.width, canvasEl.height);
let startAngle = angle;
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2, true);
ctx.stroke();
const segmentWidth = 360 / items.length;
let endAngle = segmentWidth;
console.log(items.length);
for(let i = 0; i < items.length; i++){
let endAngle = segmentWidth + startAngle;
for (let i = 0; i < items.length; i++) {
ctx.beginPath();
if(i === 1){
ctx.lineTo(x, y);
}
ctx.arc(x, y, radius, (startAngle * Math.PI / 180), (endAngle * Math.PI / 180), false);
ctx.lineTo(x, y);
ctx.lineTo(x, y)
ctx.arc(
x,
y,
radius,
(startAngle * Math.PI) / 180,
(endAngle * Math.PI) / 180,
false
);
ctx.lineTo(x, y)
ctx.fillStyle = itemsList.items[i].color;
ctx.fill();
ctx.stroke();
if (items.length !== 1) ctx.stroke();
startAngle += segmentWidth;
endAngle += segmentWidth;
}
}
// // import { MenuItem } from "./components/menu-item";
// import { CounterItem } from "./components/counter-item";
// import { MenuItem } from "./components/menu-item";
// import { MenuItemList } from "./components/menu-item-list";
// import { OpenCloseArrow } from "./components/open-close-arrow";
// import { Counter } from "./model/counter";
// import { Item } from "./model/item";
// import { avaliableIds, initializeIdsPool } from "./utils/item-id-pool";
// import { avaliableRGBs } from "./utils/utils";
// // import { OpenCloseArrow } from "./components/open-close-arrow";
let angleSpeed = 1;
// const counterEl = document.getElementsByClassName(
// "counter"
// )[0]! as HTMLDivElement;
// const itemMenuEl = document.getElementsByClassName(
// "item-menu"
// )[0]! as HTMLDivElement;
// const openCloseMenuEl = document.getElementsByClassName(
// "open-close-menu"
// )[0]! as HTMLDivElement;
// const inputEl = document.querySelector("input")! as HTMLInputElement;
// const addButtonEl = document.getElementsByClassName(
// "new-item-button"
// )[0]! as HTMLButtonElement;
// const removeAllItemsButtonEl = document.getElementsByClassName(
// "remove-all-items-button"
// )[0]! as HTMLButtonElement;
// const MIN_ITEMS = 0;
// const MAX_ITEMS = 16;
let guessItemIndex = Math.floor(Math.random() * items.length);
let segmentAngle = 360 / items.length;
const rotations = 10;
let maxAngle = 360 * rotations + segmentAngle * guessItemIndex;
let delta = 0.2;
let totalAngle = 0;
let animationId: number | null;
// const openCloseItem = new OpenCloseArrow();
// openCloseMenuEl.appendChild(openCloseItem.el);
function startRoulette(){
if(animationId){
resetRouletteAnimation();
}else{
drawRouletteWheel(0);
guessItemIndex = Math.floor(Math.random() * items.length);
segmentAngle = 360 / items.length;
console.log(itemsList.items[guessItemIndex].color);
maxAngle = 360 * rotations + segmentAngle * (guessItemIndex - 1) + segmentAngle * Math.random();
console.log(guessItemIndex);
delta = 0.2
beginAnimateRoulette();
}
}
// const counterItem = new CounterItem(MIN_ITEMS, MAX_ITEMS);
// counterEl.appendChild(counterItem.el);canvasEl
function beginAnimateRoulette(){;
drawRouletteWheel(angleSpeed);
if(totalAngle < maxAngle){
console.log(angleSpeed);
if(angleSpeed > maxAngle / 2){
delta = -delta;
}
angleSpeed += angleSpeed * delta;
totalAngle += angleSpeed;
console.log('TOTAL: ' + totalAngle);
animationId = window.requestAnimationFrame(beginAnimateRoulette);
}else{
window.cancelAnimationFrame(animationId!)
animationId = null;
angleSpeed = 1;
totalAngle = 0;
}
}
// const counter = counterItem.getCounter;
function resetRouletteAnimation(){
if(animationId){
window.cancelAnimationFrame(animationId!)
}
drawRouletteWheel(0);
animationId = null;
angleSpeed = 1;
totalAngle = 0;
delta = 0.2;
}
// const items: MenuItemList = new MenuItemList();
// itemMenuEl.appendChild(items.el);
// configure();
// function configure() {
// initializeIdsPool(MAX_ITEMS);
// }
// addButtonEl.addEventListener("click", () => {
// const id = avaliableIds.pop();
// const name = inputEl.value;
// if (id) {
// if (name) {
// if (counter.increment()) {
// const item = new MenuItem(id, name, avaliableRGBs[MAX_ITEMS % Counter.value]);
// console.log(item.deleteItem);
// item.deleteItem.el.addEventListener('click', () => {
// if(counter.decrement()){
// avaliableIds.push(item.el.id)
// items.removeItem(item);
// counterItem.updateContent();
// }
// })
// items.addItem(item);
// counterItem.updateContent();
// }
// } else {
// alert("Item name cannot be empty!");
// }
// } else {
// alert("Maxium number of elements!");
// }
// });
// removeAllItemsButtonEl.addEventListener("click", () => {
// initializeIdsPool(MAX_ITEMS);
// items.clear();
// counter.clear();
// counterItem.updateContent();
// });
// // const openCloseMenuEl = document.getElementsByClassName(
// // "open-close-menu"
// // )[0]! as HTMLDivElement;
// // const itemNameInput = document.querySelector("input")! as HTMLInputElement;
// // const newItemButtonEl = document.getElementById(
// // "newItemButton"
// // )! as HTMLButtonElement;
// // const removeAllItemsButtonEl = document.getElementById(
// // "removeAllItemsButton"
// // )! as HTMLButtonElement;
// // // const newItemMenu = document.getElementById('new-item-menu')! as HTMLDivElement;
// // menuEl.appendChild(items.el);
// // openCloseMenuEl.appendChild(new OpenCloseArrow().el);
// // const avaliableRGBs = ["255, 0, 0", "0, 255, 0", "0, 0, 255"];
// // const bgOpacity = 0.5;
// // let nextColor = avaliableRGBs[0];
// // const idsPool: number[] = [];
// // initalizeIdsPool();
// // function addNewItem() {
// // if (itemNameInput.value.length > 0 && itemNameInput.value.length <= 32) {
// // if (counter < maxItems && idsPool.length > 0) {
// // const item = new MenuItem(
// // idsPool.pop()!,
// // itemNameInput.value,
// // `rgba(${nextColor}, ${bgOpacity})`
// // );
// // item.deleteItem.el.addEventListener("click", () => {
// // const itemElId = calculateItemElId(item);
// // idsPool.push(itemElId);
// // setNextColor();
// // items.removeItem(item);
// // counter--;
// // updateCounter();
// // });
// // items.addItem(item);
// // counter++;
// // updateCounter();
// // setNextColor();
// // } else {
// // alert("Maximum number of items!");
// // }
// // }
// // }
// // function removeAllItems() {
// // if (items.itemsLength > 0) {
// // items.clear();
// // counter = 0;
// // initalizeIdsPool();
// // nextColor = avaliableRGBs[0];
// // updateCounter();
// // } else {
// // alert("Nothing to clear!");
// // }
// // }
// // function updateCounter() {
// // counterEl.textContent = `${counter}/${maxItems}`;
// // counterEl.animate([
// // { transform: 'scale(1.25)'},
// // { transform: 'scale(1.00)'}
// // ],
// // {
// // duration: 500
// // }
// // );
// // }
// // function setNextColor() {
// // nextColor = avaliableRGBs[counter % avaliableRGBs.length];
// // }
// // function calculateItemElId(item: MenuItem) {
// // return +item.el.id.slice(0, item.el.id.indexOf("-"));
// // }
// // const openCloseArrow = document.getElementById("open-close-arrow")!;
// // let open = true;
// // function rotateOpenCloseArrow() {
// // openCloseArrow.animate([
// // { transform: 'rotate(180deg)' }
// // ],
// // {
// // duration: 500,
// // }
// // );
// // if(open){
// // menuEl.className = 'decreasing';
// // open = false;
// // }else{
// // menuEl.className = 'normal';
// // open = true;
// // }
// // }
// // function initalizeIdsPool() {
// // idsPool.length = 0;
// // for (let i = 0; i < maxItems; i++) {
// // idsPool.push(i);
// // }
// // }
// // newItemButtonEl.addEventListener("click", addNewItem);
// // removeAllItemsButtonEl.addEventListener("click", removeAllItems);
// // openCloseArrow.addEventListener("click", rotateOpenCloseArrow);
canvasEl.addEventListener('click', startRoulette);

View File

@ -1,35 +0,0 @@
// import { Component } from "./base-component";
// export class CounterItem extends Component<HTMLElement>{
// private counter: Counter;
// constructor(min: number, max: number){
// const counterEl = document.createElement('p');
// counterEl.textContent = `${min}/${max}`;
// counterEl.className = 'counter-item';
// super(counterEl);
// this.counter = new Counter(min, max);
// }
// get getCounter(){
// return this.counter;
// }
// updateContent(){
// this.el.textContent = `${Counter.value}/${this.counter.max}`
// this.animate();
// }
// private animate(): void{
// this.el.animate(
// [
// { transform: 'scale(1.25)'},
// { transform: 'scale(1.00)'}
// ],
// {
// duration: 500
// }
// );
// }
// }

View File

@ -10,8 +10,4 @@ export class ItemRemoval extends Component<HTMLImageElement>{
el.id = `${id}-delete`
super(el);
}
configure(){
// TODO: make icon rotation after mouse over event
}
}

View File

@ -18,16 +18,6 @@ export class MenuItem extends Component<HTMLLIElement> {
this.appearAnimation();
}
// registerChangeColorListener(){
// this.el.addEventListener('click', () => {
// const actualColorIndex = avaliableRGBs.indexOf(this.el.style.backgroundColor);
// if(actualColorIndex !== -1){
// const color = avaliableRGBs[(actualColorIndex + 1) % avaliableRGBs.length];
// this.el.style.backgroundColor = color;
// }
// });
// }
appearAnimation(){
this.el.animate(
[{ transform: "scale(1.25)" }, { transform: "scale(1.00)" }],

View File

@ -1,12 +0,0 @@
import { Component } from "./base-component";
export class OpenCloseArrow extends Component<HTMLImageElement> {
constructor() {
const arrow = document.createElement('img');
arrow.src = 'static/right-arrow.png';
arrow.alt = 'open-close-arrow';
arrow.className = 'open-close-arrow';
super(arrow);
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB