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

165
Visualizações
How to solve this canvas fillStyle problem?

class Entity {
  constructor(name, type = 'player') {
    this.name = name,
      this.type = type
  }
  newObject(name, ...pvals) {
    this[name] = {}
    if (pvals) {
      for (var i = 0; i < pvals.length; i += 2) {
        this[name][pvals[i]] = pvals[i + 1]
      }
    }
  }
}

const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d')

canvas.height = window.innerHeight - 20
canvas.width = window.innerWidth

var frame = new Entity('frame', 'game-object')
var button = new Entity('button', 'player-component')

const radius = 70
var speed = 3

frame.newObject('$', 'r', radius)
button.newObject('$', 'r', radius / 3)

function updateFrame() {
  ctx.fillStyle = 'black'
  ctx.arc((canvas.width / 2), (canvas.height / 2), frame.$.r, 0, Math.PI * 2)
  ctx.fill()
  ctx.fillStyle = 'red'
  ctx.arc((canvas.width / 2), (canvas.height / 2), button.$.r, 0, Math.PI * 2)
  ctx.fill()
}

updateFrame()
<canvas></canvas>

As far as I know it should print big black circle in the middle of the canvas and on top of that a red small circle. But it just prints a big red circle. I just can't figure it out.

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

0

Just like @Teemu points out in the comment "Begin the paths" you must use the ctx.beginPath() between your arcs when you are changing colors

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/beginPath#examples

I simplified a lot of your code to show just the problem, you should do the same when you are troubleshooting class Entity was just a distraction and we can reproduce without it

const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d')

ctx.beginPath()
ctx.fillStyle = 'black'
ctx.arc(50, 50, 20, 0, Math.PI * 2)
ctx.fill()

ctx.beginPath()
ctx.fillStyle = 'red'
ctx.arc(50, 50, 10, 0, Math.PI * 2)
ctx.fill()
<canvas></canvas>

about 4 years ago · Juan Pablo Isaza Relatório

0

You should add these 2 lines:

ctx.beginPath();
ctx.closePath();

function updateFrame() {
  ctx.beginPath();
  ctx.fillStyle = 'black'
  ctx.arc((canvas.width / 2), (canvas.height / 2), frame.$.r, 0, Math.PI * 2)
  ctx.closePath();
  ctx.fill();
  ctx.beginPath();
  ctx.fillStyle = 'red'
  ctx.arc((canvas.width / 2), (canvas.height / 2), button.$.r, 0, Math.PI * 2)
  ctx.closePath();
  ctx.fill()
}
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