HTML5 Canvas Threshold filter Image Tutorial
To apply filter to an Konva.Image
, we have to cache it first with cache()
function. Then apply filter with filters()
function.
To apply a threshold effect to an image with Konva, we can use the Konva.Filters.Threshold
.
The threshold filter converts the image into a black and white image where all pixels above the threshold value become white and all pixels below become black.
Instructions: Slide the control to change threshold value.
For all available filters go to Filters Documentation.
import Konva from 'konva';
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
});
const layer = new Konva.Layer();
stage.add(layer);
const imageObj = new Image();
imageObj.onload = () => {
const image = new Konva.Image({
x: 50,
y: 50,
image: imageObj,
draggable: true,
});
layer.add(image);
image.cache();
image.filters([Konva.Filters.Threshold]);
image.threshold(0.5);
const container = document.createElement('div');
container.style.position = 'absolute';
container.style.top = '20px';
container.style.left = '20px';
const text = document.createElement('span');
text.textContent = 'Threshold: ';
container.appendChild(text);
const slider = document.createElement('input');
slider.type = 'range';
slider.min = '0';
slider.max = '1';
slider.step = '0.01';
slider.value = image.threshold();
slider.style.width = '200px';
slider.addEventListener('input', (e) => {
const value = parseFloat(e.target.value);
image.threshold(value);
layer.batchDraw();
});
container.appendChild(slider);
document.body.appendChild(container);
};
imageObj.src = '/images/lion.png';
import { Stage, Layer, Image } from 'react-konva';
import { useState, useEffect } from 'react';
const App = () => {
const [image, setImage] = useState(null);
const [threshold, setThreshold] = useState(0.5);
useEffect(() => {
const imageObj = new Image();
imageObj.onload = () => {
setImage(imageObj);
};
imageObj.src = '/images/lion.png';
}, []);
return (
<>
<div style={{ position: 'absolute', top: '20px', left: '20px' }}>
<label>Threshold: </label>
<input
type="range"
min="0"
max="1"
step="0.01"
value={threshold}
onChange={(e) => setThreshold(parseFloat(e.target.value))}
style={{ width: '200px' }}
/>
</div>
<Stage width={window.innerWidth} height={window.innerHeight}>
<Layer>
{image && (
<Image
x={50}
y={50}
image={image}
draggable
filters={[Konva.Filters.Threshold]}
threshold={threshold}
/>
)}
</Layer>
</Stage>
</>
);
};
export default App;
<template>
<div>
<div style="position: absolute; top: 20px; left: 20px">
<label>Threshold: </label>
<input
type="range"
min="0"
max="1"
step="0.01"
:value="threshold"
@input="(e) => threshold = parseFloat(e.target.value)"
style="width: 200px"
/>
</div>
<v-stage :config="stageSize">
<v-layer>
<v-image
v-if="image"
:config="{
x: 50,
y: 50,
image: image,
draggable: true,
filters: [Konva.Filters.Threshold],
threshold: threshold,
}"
/>
</v-layer>
</v-stage>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const stageSize = {
width: window.innerWidth,
height: window.innerHeight,
};
const image = ref(null);
const threshold = ref(0.5);
onMounted(() => {
const imageObj = new Image();
imageObj.onload = () => {
image.value = imageObj;
};
imageObj.src = '/images/lion.png';
});
</script>