Skip to content
Advertisement

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.

Advertisement

Answer

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