Want to create a simple image brightness control slider

后端 未结 4 580
轻奢々
轻奢々 2021-01-06 09:54

I wanted to implement a slider control that changes the brightness of the image, much like the one shown at this link :

http://camanjs.com/examples/

I am fa

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-06 10:13

    You can achieve the desired effect with a canvas element, CSS3 filter and pure JavaScript:

    HTML

    
    
    

    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

提交回复
热议问题