yalpsid eman
yalpsid eman

Reputation: 3432

How can I change the brightness of only part of an image (html/css/js)?

I have an image with tags underneath that correspond to different parts of the image. For instance, the image might be a dog, and one of the tags is 'nose', which corresponds to the portion of the image around the dogs nose. When the user hovers over a tag, I want the brightness around this corresponding part of the image to increase. How can I do this?

function fixLabel(c) {
  var x_coor = document.getElementById('x').textContent;
  var y_coor = document.getElementById('y').textContent;

  var x2_coor = document.getElementById('x2').textContent;
  var y2_coor = document.getElementById('y2').textContent;

  var width = document.getElementById('w').textContent;
  var height = document.getElementById('h').textContent;

  var tag = document.createElement('input'); // Create a `input` element,
  tag.setAttribute('type', 'text'); // Set it's `type` attribute,
  tag.setAttribute('name', 'loc:' + round_2_decimals(x_coor) + "-" + round_2_decimals(y_coor) + "-" +
    round_2_decimals(x2_coor) + "-" + round_2_decimals(y2_coor) + "-" + round_2_decimals(width) +
    "-" + round_2_decimals(height));

  /**
   *Here's the area: How do I attach a mouseover function so the image's 
   *brightness increases whenever I hover over the tag?
   */
  tag.onmouseover = function() {};

  var br = document.createElement('br'); // Create a `br` element,
  var button_div = document.getElementById('fix_label_area');
  button_div.appendChild(br);
  button_div.appendChild(tag);
  button_div.appendChild(br);
};
<div>
  <p id='x'></p>
  <p id='y'></p>
  <p id='x2'></p>
  <p id='y2'></p>
  <p id='w'></p>
  <p id='h'></p>
  <br/>
  <button id='fix_label' onclick="fixLabel()">Fix Label Here</button>
</div>

<form action="" method="POST" id="canvas">
  <div id='img_area'>
    <img src="someImageFile.jpg" id="imageId" width="350" height="350" />
  </div>
  <div id='label_area'>
    <input type="submit" name="submit" value="Label" id='input'>
  </div>
  <div id='fix_label_area'></div>
</form>

Upvotes: 2

Views: 825

Answers (1)

epascarello
epascarello

Reputation: 207521

Personally Canvas and SVG are more powerful, but you can use CSS to clip a circle and opacity to dim the original. But you need to check the browser support to make sure it covers the browsers you need.

div.holder {
  position: relative;
}

img.main {
  opacity: .4;
}

img.circle {
    position: absolute;
    clip-path: circle(50px at 405px 135px);
}
<div class="holder">
  <img class="circle" src="https://placedog.net/500" />
  <img class="main" src="https://placedog.net/500" />
</div>

Upvotes: 4

Related Questions