想要创建一个简单的图像亮度控制滑块

Want to create a simple image brightness control slider

本文关键字:图像 亮度 控制 简单 一个 创建      更新时间:2023-09-26

我想实现一个滑块控件来改变图像的亮度,就像这个链接上显示的一样:

http://camanjs.com/examples/

我是javascript的新手,事实证明这相当困难。所以现在,我正在使用CamanJS库,但不幸的是,我无法复制它。我试着对这个例子进行逆向工程,但天哪,这个例子非常复杂,根本不可读!无论如何,这就是我的实现的问题:

//this is the event handler called when the slider value changes
function brightnessControl(e, ui) {
  //mainImage is the id of the canvas that holds the image
  Caman("#mainImage", function() {
    this.brightness(ui.value);
    this.render();
  });
}

原始图像被具有亮度设置的新图像覆盖。所以最终,我得到的只是一个普通的白色或黑色的图像。如有任何帮助,我们将不胜感激!提前感谢!

您可以使用canvas元素、CSS3过滤器和纯JavaScript:来实现所需的效果

HTML

<input id="bri" type="text" value="1"/>
<canvas id="img"></canvas>

JavaScript

window.onload = function () {
    var context = document.getElementById('img').getContext('2d');
    /* Loading the image at first */
    var base_image = new Image();
    base_image.src = 'http://images.google.com/intl/fr_ALL/images/logos/images_logo_lg.gif';
    context.drawImage(base_image, 0, 0);
    /* Function trigerred when we leave the input */
    document.getElementById('bri').onblur = function () {
        var amount = this.value;
        var img = document.getElementById('img');
        /* We change the brightness of the canvas itself */
        img.setAttribute('style', 'filter:brightness(' + amount + '); -webkit-filter:brightness(' + amount + '); -moz-filter:brightness(' + amount + ')');
    }
};

实时演示

要阅读完整的实现,请查看此网站如何创建图像亮度控制滑块

rangeInput = document.getElementById('range');
container = document.getElementsByClassName('container')[0];
rangeInput.addEventListener("mousemove",function(){
container.style.filter = "brightness(" + rangeInput.value + "%)";
});
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.container{
  background: url(https://codingdebugging.com/wp-content/uploads/2020/07/include-how-to-create-an-image-brightness-control-slider.jpg) no-repeat center;
  min-height: 100vh;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
}
.brightness-box{
  width: 400px;
  height: 60px;
  background: #f9f9f9;
  border-radius: 8px;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brightness-box i{
  margin: 0 10px;
}
#range{
  width: 100%;
  -webkit-appearance: none;
  background: #0a85ff;
  height: 3px;
  outline: none;
}
#range::-webkit-slider-thumb{
  -webkit-appearance: none;
  width: 22px;
  height: 22px;
  background: #333;
  border-radius: 50%;
  cursor: pointer;
}
<!DOCTYPE html>
<html>
<head>
  <title>Brightness Control - Coding Debugging</title>
    <!-- Font Awesome Icon -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css">
</head>
<body>
  <div class="container">
    <div class="brightness-box">
      <i class="far fa-sun"></i>
      <input type="range" id="range" min="10" max="100" value="100">
      <i class="fas fa-sun"></i>
    </div>
  </div>
</body>
</html>

//this is the event handler called when the slider value changes
function brightnessControl(e, ui) {
  //mainImage is the id of the canvas that holds the image
  Caman("#mainImage", function() {
    this.style.filter='brightness(ui.value)';//like this: filter: brightness(0.4)
    //this.brightness(ui.value);
   // this.render(); i don/t know this is useful? you judge it by yourself
  });
}

亮度是从0到1。

以下内容将在CSS 2.1+中使用。请注意,我在本示例中使用HTML5 input type="range"只是为了方便使用。Javascript回退代码也在本例中为不支持此功能的浏览器实现(input type将默认为text)。

最好实现一个自定义滑块,但我相信这个问题是关于亮度控制,而不是滑块。

其工作方式是将图像与一些比例相等的元素重叠,并且不透明度取决于滑块/文本输入值。该元素的背景颜色对于值>50将是白色,而对于值<50.

链接到JS Fiddle

#HTML
<div id="container">
    <div id="brightness"></div>
    <img src="http://placehold.it/400x400" />
</div>
Brightness (0 - 100):<br />
<input type="range" id="controls" value="50" min="0" max="100" maxlength="3">

 

#Javascript
window.onload = function()
{
    var brightness = document.getElementById('brightness');
        controls   = document.getElementById('controls');
    controls.onkeyup = controls.onchange = function()
    {
        var brightness = document.getElementById('brightness'),
            val        = parseInt(this.value) - 50;
        if (val > 50 || val < -50)
        return false;
        brightness.style.backgroundColor = val > 0 ? 'white' : 'black';
        brightness.style.opacity = Math.abs(val/100) * 2;
    }
}

 

#CSS
#container{
    width:400px;
    height:400px;
    margin-bottom:10px;
    border:1px solid rgb(127, 127, 127);
}
#brightness{
    width:400px;
    height:400px;
    background:white;
    position:absolute;
    opacity:0;
}
#controls{
    width:400px;
    height:22px;
    padding:0 5px;
}