Не работает код на локалке и сервере

Рейтинг: 0Ответов: 1Опубликовано: 21.02.2023

Подскажите пожалуйста, почему не выполняется код на локалке и на сервере, а на codepen.io всё работает (в кодепен не мой код, но нужно такое же)

https://i.sstatic.net/p2yJ4.jpg

https://codepen.io/jaynee/pen/BapQQqM

=====================html======================


<div class="container">
  <div id="wheelOfFortune">
  <canvas id="wheel" width="500" height="500"></canvas>
  <div id="spin"></div>
  </div>
  
  <div>
    <input type="text" id="add-name-input" placeholder="Label"/>
    <input type="text" id="add-color-input" placeholder="Couleur" value="#"/>
    <button type="submit" id="buttonAdd">Ajouter</button>
  </div>
</div>



=====================CSS======================



.container {
  width: 100%;
  text-align: center;
}

#wheelOfFortune {
  display: inline-block;
  position: relative;
  overflow: hidden;
  text-align: center;
}

#wheel {
  display: block;
}

#spin {
  font: 1.5em/0 sans-serif;
  user-select: none;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 50%;
  left: 50%;
  width: 30%;
  height: 30%;
  margin: -15%;
  background: #fff;
  color: #fff;
  box-shadow: 0 0 0 8px currentColor, 0 0px 15px 5px rgba(0, 0, 0, 0.6);
  border: solid 1px #eee;
  border-radius: 50%;
  transition: 0.8s;
}

#spin::after {
  content: "";
  position: absolute;
  top: -17px;
  border: 10px solid transparent;
  border-bottom-color: currentColor;
  border-top: none;
}


=====================JS======================

let sectors = [
  {color:"#D9487D", label:"90"},
  {color:"#F291B5", label:"10"},
  {color:"#A63889", label:"200"},
  {color:"#490F59", label:"50"},
  {color:"#F29F8D", label:"100"},
];

const rand = (m, M) => Math.random() * (M - m) + m;
let tot = sectors.length;
const EL_spin = document.querySelector("#spin");
const ctx = document.querySelector("#wheel").getContext('2d');
const buttonAddEl = document.querySelector("#buttonAdd");
const inputAddEl = document.querySelector("#add-name-input");
const inputAddColor = document.querySelector("#add-color-input");
const dia = ctx.canvas.width;
const rad = dia / 2;
const PI = Math.PI;
const TAU = 2 * PI;
let arc = TAU / sectors.length;

const friction = 0.991; // 0.995=soft, 0.99=mid, 0.98=hard
let angVel = 0; // Angular velocity
let ang = 0; // Angle in radians

const getIndex = () => Math.floor(tot - ang / TAU * tot) % tot;

function drawSector(sector, i) {
  const ang = arc * i;
  ctx.save();
  // COLOR
  ctx.beginPath();
  ctx.fillStyle = sector.color;
  ctx.moveTo(rad, rad);
  ctx.arc(rad, rad, rad, ang, ang + arc);
  ctx.lineTo(rad, rad);
  ctx.fill();
  // TEXT
  ctx.translate(rad, rad);
  ctx.rotate(ang + arc / 2);
  ctx.textAlign = "right";
  ctx.fillStyle = "#fff";
  ctx.font = "bold 30px sans-serif";
  ctx.fillText(sector.label, rad - 10, 10);
  //
  ctx.restore();
};

function rotate() {
  const sector = sectors[getIndex()];
  ctx.canvas.style.transform = `rotate(${ang - PI / 2}rad)`;
  EL_spin.textContent = sector.label;
  EL_spin.style.background = sector.color;
}

function frame() {
  if (!angVel) return;
  angVel *= friction; // Decrement velocity by friction
  if (angVel < 0.002) angVel = 0; // Bring to stop
  ang += angVel; // Update angle
  ang %= TAU; // Normalize angle
  rotate();
}

function engine() {
  frame();
  requestAnimationFrame(engine)
}

// INIT
sectors.forEach(drawSector);
rotate(); // Initial rotation
engine(); // Start engine
EL_spin.addEventListener("click", () => {
  if (!angVel) angVel = rand(0.25, 0.35);
});
buttonAddEl.addEventListener("click", () => {
  const newEl = inputAddEl.value;
  const newColor = inputAddColor.value;
  sectors.push({
    color: newColor, label: newEl
  })
  tot = sectors.length;
  arc = TAU / sectors.length;
  
  sectors.forEach(drawSector);
  setTimeout(() => {
     inputAddEl.value = "";
      inputAddColor.value = "#";
      console.log(sectors);
  }, 500)
})

Ответы

▲ 0

Всё работает.
Ниже код скопированный с Вашего вопроса "один-в-один". Проверяйте свой код на "локалке".

let sectors = [
  {color:"#D9487D", label:"90"},
  {color:"#F291B5", label:"10"},
  {color:"#A63889", label:"200"},
  {color:"#490F59", label:"50"},
  {color:"#F29F8D", label:"100"},
];

const rand = (m, M) => Math.random() * (M - m) + m;
let tot = sectors.length;
const EL_spin = document.querySelector("#spin");
const ctx = document.querySelector("#wheel").getContext('2d');
const buttonAddEl = document.querySelector("#buttonAdd");
const inputAddEl = document.querySelector("#add-name-input");
const inputAddColor = document.querySelector("#add-color-input");
const dia = ctx.canvas.width;
const rad = dia / 2;
const PI = Math.PI;
const TAU = 2 * PI;
let arc = TAU / sectors.length;

const friction = 0.991; // 0.995=soft, 0.99=mid, 0.98=hard
let angVel = 0; // Angular velocity
let ang = 0; // Angle in radians

const getIndex = () => Math.floor(tot - ang / TAU * tot) % tot;

function drawSector(sector, i) {
  const ang = arc * i;
  ctx.save();
  // COLOR
  ctx.beginPath();
  ctx.fillStyle = sector.color;
  ctx.moveTo(rad, rad);
  ctx.arc(rad, rad, rad, ang, ang + arc);
  ctx.lineTo(rad, rad);
  ctx.fill();
  // TEXT
  ctx.translate(rad, rad);
  ctx.rotate(ang + arc / 2);
  ctx.textAlign = "right";
  ctx.fillStyle = "#fff";
  ctx.font = "bold 30px sans-serif";
  ctx.fillText(sector.label, rad - 10, 10);
  //
  ctx.restore();
};

function rotate() {
  const sector = sectors[getIndex()];
  ctx.canvas.style.transform = `rotate(${ang - PI / 2}rad)`;
  EL_spin.textContent = sector.label;
  EL_spin.style.background = sector.color;
}

function frame() {
  if (!angVel) return;
  angVel *= friction; // Decrement velocity by friction
  if (angVel < 0.002) angVel = 0; // Bring to stop
  ang += angVel; // Update angle
  ang %= TAU; // Normalize angle
  rotate();
}

function engine() {
  frame();
  requestAnimationFrame(engine)
}

// INIT
sectors.forEach(drawSector);
rotate(); // Initial rotation
engine(); // Start engine
EL_spin.addEventListener("click", () => {
  if (!angVel) angVel = rand(0.25, 0.35);
});
buttonAddEl.addEventListener("click", () => {
  const newEl = inputAddEl.value;
  const newColor = inputAddColor.value;
  sectors.push({
    color: newColor, label: newEl
  })
  tot = sectors.length;
  arc = TAU / sectors.length;
  
  sectors.forEach(drawSector);
  setTimeout(() => {
     inputAddEl.value = "";
      inputAddColor.value = "#";
      console.log(sectors);
  }, 500)
})
.container {
  width: 100%;
  text-align: center;
}

#wheelOfFortune {
  display: inline-block;
  position: relative;
  overflow: hidden;
  text-align: center;
}

#wheel {
  display: block;
}

#spin {
  font: 1.5em/0 sans-serif;
  user-select: none;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 50%;
  left: 50%;
  width: 30%;
  height: 30%;
  margin: -15%;
  background: #fff;
  color: #fff;
  box-shadow: 0 0 0 8px currentColor, 0 0px 15px 5px rgba(0, 0, 0, 0.6);
  border: solid 1px #eee;
  border-radius: 50%;
  transition: 0.8s;
}

#spin::after {
  content: "";
  position: absolute;
  top: -17px;
  border: 10px solid transparent;
  border-bottom-color: currentColor;
  border-top: none;
}
<div class="container">
  <div id="wheelOfFortune">
  <canvas id="wheel" width="500" height="500"></canvas>
  <div id="spin"></div>
  </div>
  
  <div>
    <input type="text" id="add-name-input" placeholder="Label"/>
    <input type="text" id="add-color-input" placeholder="Couleur" value="#"/>
    <button type="submit" id="buttonAdd">Ajouter</button>
  </div>
</div>