rotation & try display text names

This commit is contained in:
p-wojt 2022-01-03 22:15:48 +01:00
parent a8ce06c3b8
commit 559865834a
2 changed files with 51 additions and 40 deletions

View File

@ -30,6 +30,12 @@ const canvasWrapper = document.getElementsByClassName(
)[0]! as HTMLDivElement; )[0]! as HTMLDivElement;
const MAXIMUM_SIZE = 16; const MAXIMUM_SIZE = 16;
canvasEl.width = canvasWrapper.offsetWidth;
canvasEl.height = canvasWrapper.offsetHeight;
const x = canvasEl.width / 2;
const y = canvasEl.height / 2;
const radius = canvasEl.height / 2;
const animationFPSRate = 30;
counterEl.textContent = `0/${MAXIMUM_SIZE}`; counterEl.textContent = `0/${MAXIMUM_SIZE}`;
@ -152,22 +158,23 @@ function lightDarkMode() {
}); });
} }
const arrowImage = document.createElement('img');
arrowImage.src = 'static/arrow-down.png';
arrowImage.alt = 'arrow';
function drawRouletteWheel(angle: number) { function drawRouletteWheel(angle: number) {
canvasEl.width = canvasWrapper.offsetWidth; const segmentWidth = 360 / items.length;
canvasEl.height = canvasWrapper.offsetHeight;
const x = canvasEl.width / 2;
const y = canvasEl.height / 2;
const radius = canvasEl.height / 2;
ctx.clearRect(0, 0, canvasEl.width, canvasEl.height);
let startAngle = angle; let startAngle = angle;
let endAngle = segmentWidth + startAngle;
ctx.clearRect(0, 0, canvasEl.width, canvasEl.height);
ctx.beginPath(); ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2, true); ctx.arc(x, y, radius, 0, Math.PI * 2, true);
ctx.stroke(); // let xtemp = x + Math.cos(totalAngle/2) * radius/2;
const segmentWidth = 360 / items.length; // let ytemp = y + Math.sin(totalAngle/2) * radius/2;
let endAngle = segmentWidth + startAngle;
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
ctx.beginPath(); ctx.beginPath();
ctx.lineTo(x, y) ctx.lineTo(x, y)
ctx.font = 'bold 24px verdana, sans-serif';
ctx.arc( ctx.arc(
x, x,
y, y,
@ -176,52 +183,56 @@ function drawRouletteWheel(angle: number) {
(endAngle * Math.PI) / 180, (endAngle * Math.PI) / 180,
false false
); );
ctx.fillText(itemsList.items[i].name, x + Math.cos(segmentWidth * i ) * radius / 2, y + Math.sin(segmentWidth * i + angle) * radius / 2 );
ctx.lineTo(x, y) ctx.lineTo(x, y)
ctx.fillStyle = itemsList.items[i].color; ctx.fillStyle = itemsList.items[i].color;
ctx.fill(); ctx.fill();
if (items.length !== 1) ctx.stroke(); if (items.length !== 1) {
ctx.stroke();
}
ctx.drawImage(arrowImage, x - 32, -32, 64, 64);
startAngle += segmentWidth; startAngle += segmentWidth;
endAngle += segmentWidth; endAngle += segmentWidth;
// xtemp += Math.cos(totalAngle/2) * radius/2;
// ytemp += Math.sin(totalAngle/2) * radius/2;
// console.log(ytemp);
} }
} }
let angleSpeed = 1;
let guessItemIndex = Math.floor(Math.random() * items.length); let guessItemIndex = Math.floor(Math.random() * items.length);
let segmentAngle = 360 / items.length; let segmentAngle = 360 / items.length;
const rotations = 10; const rotations = 3;
let maxAngle = 360 * rotations + segmentAngle * guessItemIndex; let maxAngle = 360 * rotations + segmentAngle * guessItemIndex;
let delta = 0.2; let angleSpeed = 0;
let totalAngle = 0; let totalAngle = 0;
let animationId: number | null; let animationId: number | null;
let startAngle = 1;
// const maxAngleSpeed = segmentAngle;
function startRoulette(){ function startRoulette(){
if(animationId){ if(animationId){
resetRouletteAnimation(); //resetRouletteAnimation();
}else{ }else{
drawRouletteWheel(0);
guessItemIndex = Math.floor(Math.random() * items.length); 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); console.log(guessItemIndex);
delta = 0.2 segmentAngle = 360 / items.length;
maxAngle = 360 * rotations + ((items.length - 1) - guessItemIndex) * segmentAngle + Math.random() * segmentAngle;
beginAnimateRoulette(); beginAnimateRoulette();
} }
} }
function beginAnimateRoulette(){; function beginAnimateRoulette(){
drawRouletteWheel(angleSpeed);
if(totalAngle < maxAngle){ if(totalAngle < maxAngle){
console.log(angleSpeed); drawRouletteWheel(angleSpeed);
if(angleSpeed > maxAngle / 2){ angleSpeed += startAngle;
delta = -delta; totalAngle += startAngle;
} setTimeout(() => {
angleSpeed += angleSpeed * delta; animationId = window.requestAnimationFrame(beginAnimateRoulette);
totalAngle += angleSpeed; }, 1000 / animationFPSRate);
console.log('TOTAL: ' + totalAngle);
animationId = window.requestAnimationFrame(beginAnimateRoulette);
}else{ }else{
window.cancelAnimationFrame(animationId!) window.cancelAnimationFrame(animationId!)
animationId = null; animationId = null;
@ -230,15 +241,15 @@ function beginAnimateRoulette(){;
} }
} }
function resetRouletteAnimation(){ // function resetRouletteAnimation(){
if(animationId){ // if(animationId){
window.cancelAnimationFrame(animationId!) // window.cancelAnimationFrame(animationId!)
} // }
drawRouletteWheel(0); // drawRouletteWheel(0);
animationId = null; // animationId = null;
angleSpeed = 1; // angleSpeed = 1;
totalAngle = 0; // totalAngle = 0;
delta = 0.2; // delta = 0.2;
} // }
canvasEl.addEventListener('click', startRoulette); canvasEl.addEventListener('click', startRoulette);

BIN
static/arrow-down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB