Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda