I wanted to implement a slider control that changes the brightness of the image, much like the one shown at this link but through Html5 canvas :
I want to control brightness of image by using input slider on html and define on javascript.
any idea how is possible to change the Brightness of this image with javascript.
// Dispatch a change event to file input element.
$(document).ready(function() {
$('#imgfile').change(function(e) {
var file = e.target.files[0],
imageType = /image.*/;
if (!file.type.match(imageType))
return;
// Get the uploaded file from the event handler and get a data URL by using the FileReader object.
var reader = new FileReader();
reader.onload = fileOnload;
reader.readAsDataURL(file);
});
// Use some jQuery to trigger click on canvas to input
$('#canvas').on('click', function() {
$('#imgfile').trigger('click');
//show the slider after click to choose the image
$('#slider').show();
});
// Make an img element with the data URL and draw it on the canvas.
function fileOnload(e) {
//Like this in javascript make an image
// img = new Image();
//img.onload = imageLoaded;
var $img = $('<img>', { src: e.target.result });
$img.load(function() {
var canvas = $('#canvas')[0];
var context = canvas.getContext('2d');
context.drawImage(this, 0, 0, canvas.width, canvas.height );
});
}
var slider = $('#slider').change(function(){
console.log(this.value);
})
});
body {
text-align: center;
width: 100%;
}
canvas {
border: 1px solid black;
margin: 50px auto;
}
.container {
margin: 0 auto;
}
@media(max-width:1200px) {
.container {
margin: 0 50px;
}
#canvas {
width: 100%;
height: auto;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/style.css">
<script src="https://cdn.rawgit.com/konvajs/konva/1.7.6/konva.min.js"></script>
<script src="js/jquery.js" charset="utf-8"></script>
<script src="js/main.js" charset="utf-8"></script>
<title>Brightness-Adjustment</title>
</head>
<body>
<div class="container">
<canvas id="canvas" width="600" height="384">
<!-- <img src="https://www.smashingmagazine.com/wp-content/uploads/2015/06/10-dithering-opt.jpg" width="150" height="150" alt=""/> -->
</canvas>
<form action='#' onsubmit="return false;">
<input type='file' id='imgfile' style="display: none;"/>
<input id="slider" type="range" min="-1" max="1" step="0.05" value="0" style="display: none;">
</form>
</div>
</body>
</html>