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

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

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 Denunciar

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 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