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

216
Vistas
how to change circle fill based on background color with javascript?

Basically I want the circle to invert color when the slider moves it across the flag (green when on white background, white when on green rect).

function move() {
  let slider = document.querySelector("#slider");
  let circle = document.querySelector("#player");
  circle.setAttribute("cx", slider.value);
}
<h1>Flag</h1>
<svg width="375" height="375">
  <g id="flag">
    <rect x="0" y="0" width="125" height="375" fill="green"/>
    <rect x="250" y="0" width="125" height="375" fill="green"/>
  </g>
  <circle id="player" cx="187.5" cy="187.5" r="10" fill="green" />
</svg><br>
<input id="slider" type="range" min="0" max="375" value="150" style="width:370px" oninput="move()">

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There isn't an easy way to do this with SVG filters or blend modes, etc. You probably have to just do the switch yourself. See below.

function move() {
  let slider = document.querySelector("#slider");
  let circle = document.querySelector("#player");
  circle.setAttribute("cx", slider.value);
  circle.setAttribute('fill', (slider.value >= 125 && slider.value < 250) ? 'green' : 'white');
}
<h1>Flag</h1>
<svg width="375" height="375">
  <g id="flag">
    <rect x="0" y="0" width="125" height="375" fill="green"/>
    <rect x="250" y="0" width="125" height="375" fill="green"/>
  </g>
  <circle id="player" cx="187.5" cy="187.5" r="10" fill="green"/>
</svg><br>
<input id="slider" type="range" min="0" max="375" value="150" style="width:370px" oninput="move()">

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