You can also use oninput
, so it updates dynamically
window.onload = function () {
var context = document.getElementById('img').getContext('2d');
var base_image = document.querySelector('.fj');
var canvas = document.querySelector('.canvas');
var h2 = document.getElementById('demo');
var brightVal = document.getElementById('b');
context.drawImage(base_image, 0, 0, base_image.width, base_image.height, 0, 0, canvas.width, canvas.height);
document.getElementById('bri').oninput = function () {
var amount = this.value + '%';
brightVal.innerHTML = amount;
var img = document.getElementById('img');
img.setAttribute('style', 'filter:brightness(' + amount + ');');
}
}
<canvas id="img" class="canvas">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Cheadle_Hulme_Arches.jpg/324px-Cheadle_Hulme_Arches.jpg" class="fj">
</canvas>
<input type="range" min="-300" max="500" value="100" class="slider" id="bri">
<h2 id="demo">Value: <span id="b">0</span></h2>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…