浮雕滤镜

获取Konva最新的信息

给图片添加滤镜前,我们必须先使用cache()方法将它缓存起来,然后使用filter()方法添加滤镜。

说明:拖动滑杆调节浮雕效果。

查看所有滤镜 Filters Documentation.

Konva Emboss Image Demoview raw
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/konva@4.0.18/konva.min.js"></script>
<meta charset="utf-8" />
<title>Konva Emboss 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">
Strength:
<input
id="Strength"
type="range"
min="0"
max="1"
step="0.1"
value="0.5"
/>
WhiteLevel:
<input
id="WhiteLevel"
type="range"
min="0"
max="1"
step="0.1"
value="0.5"
/>
Direction:
<select id="Direction">
<option value="top" selected>top</option>
<option value="top-left">top-left</option>
<option value="top-right">top-right</option>
<option value="left">left</option>
<option value="right">right</option>
<option value="bottom">bottom</option>
<option value="bottom-left">bottom-left</option>
<option value="bottom-right">bottom-right</option>
</select>
Blend:
<input id="Blend" type="checkbox" checked />
</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.Emboss]);
layer.add(lion);
stage.add(layer);

var sliders = ['Strength', 'WhiteLevel', 'Direction'];
sliders.forEach(function(attr) {
var slider = document.getElementById(attr);
function update() {
lion['emboss' + attr](slider.value);
console.log(slider.value);
layer.batchDraw();
}
slider.oninput = update;
update();
});
var slider = document.getElementById('Blend');
function update() {
lion.embossBlend(slider.checked);
console.log(slider.checked);
layer.batchDraw();
}
slider.onchange = update;
update();
});
</script>
</body>
</html>