Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
Is there a way to initialized Canvas api?

I am now making Customize roulette, and players can input the text what they wanted. when I click button, the renderRoulette function is work. And this is the inner context of renderRoulette function

  rouletteCanvas.style.display = "block"; // the initial state of rouletteCanvas's display is 'none'

  // #customize elements are the inputs of the text that users made.
  let customize = document.querySelectorAll("#customize");
  let len = customize.length;
  
  const canvas = document.querySelector(".roulette-panel"); // I want to draw here.
  let width = canvas.width;
  let height = canvas.height;
  const ctx = canvas.getContext("2d");

  // ctx Initialization
  ctx.clearRect(0, 0, width, height);

  const devide = len; 
  const degree = 360;
  const goalDegree = 270 + degree / devide;

  for (let i = 0; i < devide; i++) {
    let json = {
      first: (degree / devide) * i,
      last: (degree / devide) * (i + 1),
      text: `${i + 1}`,
    };
    data.push(json);
  }

  // Draw a circular sector
  data.forEach((item) => {
    ctx.save();

    ctx.beginPath();
    ctx.lineWidth = 2;
    ctx.strokeStyle = "gray";
    ctx.moveTo(width / 2, height / 2);
    ctx.arc(
      width / 2,
      height / 2,
      width / 2,
      degreesToRadians(item.first),
      degreesToRadians(item.last),
      false
    );

    ctx.closePath();
    ctx.stroke();
    ctx.restore();

    ctx.save();
    let half = Math.abs(item.first - item.last) / 2;
    let degg = item.first + half;
    let xx = ((Math.cos(degreesToRadians(degg)) * width) / 2) * 0.7 + width / 2;
    let yy =
      ((Math.sin(degreesToRadians(degg)) * width) / 2) * 0.7 + height / 2;
    let minus = ctx.measureText(item.text).width / 2;

    ctx.font = "bold 15px sans-serif";
    ctx.fillText(item.text, xx - minus, yy);
    ctx.restore();
  });

Every time I click the button, I want to delete all the previously drawn circular sector shapes and redraw them, so I used the code, ctx.clearRect(0, 0, width, height); . However, even if you click the button after, it will be drawn over the existing picture. How do I initialize it?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I get a feeling that the save and restore in your code are causing some problems for you, I tried a simpler version of your code and the ctx.clearRect(0, 0, width, height) works just fine.

Here is the code:

const canvas = document.querySelector(".roulette-panel");
const ctx = canvas.getContext("2d");

let width = height = 100;
const devide = 8;

let data = []
for (let i = 0; i < devide; i++) {
  data.push({
    first: (360 / devide) * i,
    last: (360 / devide) * (i + 1)
  });
}

function draw() {
  ctx.clearRect(0, 0, width, height);
  ctx.beginPath();
  data.forEach((item) => {
    ctx.moveTo(width / 2, height / 2);
    ctx.arc(50, 50, 45,
      item.first * Math.PI / 180,
      item.last * Math.PI / 180
    );
    item.first += 0.5
    item.last += 0.5
  });
  ctx.stroke();
}

setInterval(draw, 40)
<canvas class="roulette-panel"></canvas>

This code uses setInterval to delete all the previously drawn circular sector shapes and draw new one on a new position creating the effect of movement


Here is that same code but with the clearRect commented:

const canvas = document.querySelector(".roulette-panel");
const ctx = canvas.getContext("2d");

let width = height = 100;
const devide = 8;

let data = []
for (let i = 0; i < devide; i++) {
  data.push({
    first: (360 / devide) * i,
    last: (360 / devide) * (i + 1)
  });
}

function draw() {
  //ctx.clearRect(0, 0, width, height);
  ctx.beginPath();
  data.forEach((item) => {
    ctx.moveTo(width / 2, height / 2);
    ctx.arc(50, 50, 45,
      item.first * Math.PI / 180,
      item.last * Math.PI / 180
    );
    item.first += 0.5
    item.last += 0.5
  });
  ctx.stroke();
}

setInterval(draw, 40)
<canvas class="roulette-panel"></canvas>

We can clearly see a different effect

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda