HTML5 Canvas对比滤镜

时间:2012-06-20 19:09:49

标签: javascript html5 canvas filter contrast

如何使用HTML5 Canvas像素处理功能调整图像的对比度?我已经成功地很容易地改变了图像的亮度,但对比度似乎有点复杂。

有没有人知道我可以遵循的算法,或者可能采用javascript代码我可以从中获取想法?

提前感谢您对此问题的任何帮助。

3 个答案:

答案 0 :(得分:3)

既然你说你已经调整了亮度,听起来就像你需要的那样是对比度调整算法这里是C#方法的线程(看看更新2),代码的逻辑是健全的并且会在任何计划中工作:

Adjust the contrast of an image in C# efficiently - Stack Overflow

答案 1 :(得分:1)

结帐Pixastic提供了很多很棒的效果,您可以使用示例Editor来查看一些功能。 Pixastic brightness/contrast example

下面是一个演示,我掀起了一个对比的马里奥。

<强> Live Demo

如果您不想使用整个库,也请查看pixastic github source code for the contrast method,了解如何在您的代码中实现该功能。

答案 2 :(得分:1)

我将发布所有过滤器的演示。 你可以通过id或class在画布上轻松使用它。 但问题是你不能用画布渲染它意味着你不保存或预览。因为canvas无法渲染样式。

<!-- canvas -->
<html>
<head>
<style>
img {
    width: 33%;
    height: auto;
    float: left;
}
.origional {filter:none;}
.blur {-webkit-filter: blur(4px);filter: blur(4px);}
.brightness {-webkit-filter: brightness(0.30);filter: brightness(0.30);}
.contrast {-webkit-filter: contrast(180%);filter: contrast(180%);}
.grayscale {-webkit-filter: grayscale(100%);filter: grayscale(100%);}
.huerotate {-webkit-filter: hue-rotate(180deg);filter: hue-rotate(180deg);}
.invert {-webkit-filter: invert(100%);filter: invert(100%);}
.opacity {-webkit-filter: opacity(50%);filter: opacity(50%);}
.saturate {-webkit-filter: saturate(7); filter: saturate(7);}
.sepia {-webkit-filter: sepia(100%);filter: sepia(100%);}
.shadow {-webkit-filter: drop-shadow(8px 8px 10px green);filter: drop-shadow(8px 8px 10px green);}
</style>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" type="text/javascript"></script>
<script>
$( document ).ready(function() {
//origional canvas image
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
    var img = document.getElementById("origional");
   ctx.drawImage(img, 0, 0,300,300);

   $('#button').click(function() {
    canvas.classList.add("origional");
    canvas.classList.remove("blur");
    canvas.classList.remove("brightness");
    canvas.classList.remove("contrast");
    canvas.classList.remove("grayscale");
    canvas.classList.remove("huerotate");
    canvas.classList.remove("invert");
    canvas.classList.remove("opacity");
    canvas.classList.remove("saturate");
    canvas.classList.remove("sepia");
    canvas.classList.remove("shadow");
});

   $('#button1').click(function() {
    canvas.classList.add("blur");
    canvas.classList.remove("origional");
    canvas.classList.remove("brightness");
    canvas.classList.remove("contrast");
    canvas.classList.remove("grayscale");
    canvas.classList.remove("huerotate");
    canvas.classList.remove("invert");
    canvas.classList.remove("opacity");
    canvas.classList.remove("saturate");
    canvas.classList.remove("sepia");
    canvas.classList.remove("shadow");
});

   $('#button2').click(function() {
    canvas.classList.add("brightness");
    canvas.classList.remove("blur");
    canvas.classList.remove("origional");
    canvas.classList.remove("contrast");
    canvas.classList.remove("grayscale");
    canvas.classList.remove("huerotate");
    canvas.classList.remove("invert");
    canvas.classList.remove("opacity");
    canvas.classList.remove("saturate");
    canvas.classList.remove("sepia");
    canvas.classList.remove("shadow");
});
   $('#button3').click(function() {
    canvas.classList.add("contrast");
    canvas.classList.remove("blur");
    canvas.classList.remove("brightness");
    canvas.classList.remove("origional");
    canvas.classList.remove("grayscale");
    canvas.classList.remove("huerotate");
    canvas.classList.remove("invert");
    canvas.classList.remove("opacity");
    canvas.classList.remove("saturate");
    canvas.classList.remove("sepia");
    canvas.classList.remove("shadow");
});
   $('#button4').click(function() {
    canvas.classList.add("grayscale");
    canvas.classList.remove("blur");
    canvas.classList.remove("brightness");
    canvas.classList.remove("contrast");
    canvas.classList.remove("origional");
    canvas.classList.remove("huerotate");
    canvas.classList.remove("invert");
    canvas.classList.remove("opacity");
    canvas.classList.remove("saturate");
    canvas.classList.remove("sepia");
    canvas.classList.remove("shadow");
});
   $('#button5').click(function() {
    canvas.classList.add("huerotate");
    canvas.classList.remove("blur");
    canvas.classList.remove("brightness");
    canvas.classList.remove("contrast");
    canvas.classList.remove("grayscale");
    canvas.classList.remove("origional");
    canvas.classList.remove("invert");
    canvas.classList.remove("opacity");
    canvas.classList.remove("saturate");
    canvas.classList.remove("sepia");
    canvas.classList.remove("shadow");
});
   $('#button6').click(function() {
    canvas.classList.add("invert");
    canvas.classList.remove("blur");
    canvas.classList.remove("brightness");
    canvas.classList.remove("contrast");
    canvas.classList.remove("grayscale");
    canvas.classList.remove("huerotate");
    canvas.classList.remove("origional");
    canvas.classList.remove("opacity");
    canvas.classList.remove("saturate");
    canvas.classList.remove("sepia");
    canvas.classList.remove("shadow");
});
   $('#button7').click(function() {
    canvas.classList.add("opacity");
    canvas.classList.remove("blur");
    canvas.classList.remove("brightness");
    canvas.classList.remove("contrast");
    canvas.classList.remove("grayscale");
    canvas.classList.remove("huerotate");
    canvas.classList.remove("invert");
    canvas.classList.remove("origional");
    canvas.classList.remove("saturate");
    canvas.classList.remove("sepia");
    canvas.classList.remove("shadow");
});
   $('#button8').click(function() {
    canvas.classList.add("saturate");
    canvas.classList.remove("blur");
    canvas.classList.remove("brightness");
    canvas.classList.remove("contrast");
    canvas.classList.remove("grayscale");
    canvas.classList.remove("huerotate");
    canvas.classList.remove("invert");
    canvas.classList.remove("opacity");
    canvas.classList.remove("origional");
    canvas.classList.remove("sepia");
    canvas.classList.remove("shadow");
});
   $('#button9').click(function() {
    canvas.classList.add("sapia");
    canvas.classList.remove("blur");
    canvas.classList.remove("brightness");
    canvas.classList.remove("contrast");
    canvas.classList.remove("grayscale");
    canvas.classList.remove("huerotate");
    canvas.classList.remove("invert");
    canvas.classList.remove("opacity");
    canvas.classList.remove("saturate");
    canvas.classList.remove("origional");
    canvas.classList.remove("shadow");
});
   $('#button10').click(function() {
    canvas.classList.add("shadow");
    canvas.classList.remove("blur");
    canvas.classList.remove("brightness");
    canvas.classList.remove("contrast");
    canvas.classList.remove("grayscale");
    canvas.classList.remove("huerotate");
    canvas.classList.remove("invert");
    canvas.classList.remove("opacity");
    canvas.classList.remove("saturate");
    canvas.classList.remove("sepia");
    canvas.classList.remove("origional");
});
});
</script>
</head>
<body>
<h1>
canvas
</h1>
<canvas id="canvas" width="300" height="300" ></canvas>
<br>
<div ><input type="button" id="button" value="origional" />
<input type="button" id="button1" value="blur" />
<input type="button" id="button2" value="brightness" /> 
<input type="button" id="button3" value="contrast" /> 
<input type="button" id="button4" value="grayscale" /> 
<input type="button" id="button5" value="huerotate" /> 
<input type="button" id="button6" value="invert" /> 
<input type="button" id="button7" value="opacity" /> 
<input type="button" id="button8" value="saturate" /> 
<input type="button" id="button9" value="sepia" /> 
<input type="button" id="button10" value="shadow" />  
</div>
<br>
<!-- image -->
<h1>
image
</h1>
<img id="origional" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img id="blur" class="blur" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="brightness" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="contrast" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="grayscale" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="huerotate" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="invert" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="opacity" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="saturate" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="sepia" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="shadow" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
</body>
</html>

此处Live Demo