Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!