HTML5 Canvas 色相、饱和度和亮度滤镜图像教程

要对 Konva.Node 应用滤镜,我们首先需要使用 cache() 函数对其进行缓存。然后使用 filters() 函数应用滤镜。

要使用 Konva 更改图像的色相、饱和度和亮度成分,我们可以使用 Konva.Filters.HSL

说明:滑动控件以更改 HSL 值。

有关所有可用滤镜,请访问 滤镜文档

Konva 色相、饱和度和亮度图像演示view raw
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/konva@9.3.18/konva.min.js"></script>
<meta charset="utf-8" />
<title>Konva HSL Image Demo</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f0f0f0;
}

#controls {
position: absolute;
top: 20px;
left: 20px;
}
</style>
</head>

<body>
<div id="container"></div>
<div id="controls">
hue:
<input id="hue" type="range" min="0" max="259" step="1" value="150" />
saturation:
<input
id="saturation"
type="range"
min="-2"
max="10"
step="0.5"
value="0"
/>
luminance:
<input
id="luminance"
type="range"
min="-2"
max="2"
step="0.1"
value="0"
/>
</div>
<script>
Konva.Image.fromURL('../../../assets/lion.png', function (lion) {
var stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
});

var layer = new Konva.Layer();

lion.position({
x: 50,
y: 50,
});
lion.cache();
lion.filters([Konva.Filters.HSL]);
layer.add(lion);
stage.add(layer);

var sliders = ['hue', 'saturation', 'luminance'];
sliders.forEach(function (attr) {
var slider = document.getElementById(attr);
function update() {
lion[attr](parseFloat(slider.value));
}
slider.oninput = update;
update();
});
});
</script>
</body>
</html>