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

时间:2013-07-19 20:05:20

标签: javascript

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

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();
  });
}

使用亮度设置覆盖原始图像的新图像。所以最终,我最终只得到一个纯白色或黑色的图像。任何帮助将不胜感激!提前谢谢!

4 个答案:

答案 0 :(得分:1)

您可以使用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 + ')');

    }
};

Live Demo

答案 1 :(得分:1)

要阅读完整的实施说明,请访问此网站 How to Create an Image Brightness Control Slider

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>

答案 2 :(得分:0)

//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。

答案 3 :(得分:-1)

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

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

这种方法的工作方式是将图像与一些比例相等的元素重叠,并根据滑块/文本输入值使用不透明度。对于值>,该元素的背景颜色将是白色。 50,并且黑色表示值&lt; 50。

Link to 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;
}