HTML5 Canvas Contrast filter Image Tutorial
To apply filter to an Konva.Node
, we have to cache it first with cache()
function. Then apply filter with filters()
function.
To change contrast of an image with Konva, we can use the Konva.Filters.Contrast
.
Instructions: Slide the control to change contrast value.
For all available filters go to Filters Documentation.
- Vanilla
- React
- Vue
import Konva from 'konva'; const width = window.innerWidth; const height = window.innerHeight; const stage = new Konva.Stage({ container: 'container', width: width, height: height, }); 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.Contrast]); image.contrast(30); const slider = document.createElement('input'); slider.type = 'range'; slider.min = '-100'; slider.max = '100'; slider.value = image.contrast(); slider.style.position = 'absolute'; slider.style.top = '20px'; slider.style.left = '20px'; slider.addEventListener('input', (e) => { const value = parseInt(e.target.value); image.contrast(value); layer.batchDraw(); }); document.body.appendChild(slider); }; imageObj.src = 'https://new.konvajs.org/assets/lion.png'; imageObj.crossOrigin = 'anonymous';
import { Stage, Layer, Image } from 'react-konva'; import { useState, useEffect, useRef } from 'react'; import useImage from 'use-image'; const App = () => { const [contrast, setContrast] = useState(30); const [image] = useImage('https://new.konvajs.org/assets/lion.png', 'anonymous'); const imageRef = useRef(null); useEffect(() => { if (image) { imageRef.current.cache(); } }, [image]); return ( <> <Stage width={window.innerWidth} height={window.innerHeight}> <Layer> <Image ref={imageRef} x={50} y={50} image={image} draggable filters={[Konva.Filters.Contrast]} contrast={contrast} /> </Layer> </Stage> <input type="range" min="-100" max="100" value={contrast} onChange={(e) => setContrast(parseInt(e.target.value))} style={{ position: 'absolute', top: '20px', left: '20px' }} /> </> ); };
<template> <div> <v-stage :config="stageSize"> <v-layer> <v-image ref="imageNode" :config="{ x: 50, y: 50, image: image, draggable: true, filters: [Konva.Filters.Contrast], contrast: contrast, }" /> </v-layer> </v-stage> <input type="range" min="-100" max="100" :value="contrast" @input="handleSlider" style="position: absolute; top: 20px; left: 20px" /> </div> </template> <script setup> import { ref, watch, nextTick } from 'vue'; import { useImage } from 'vue-konva'; import Konva from 'konva'; const stageSize = { width: window.innerWidth, height: window.innerHeight, }; const contrast = ref(30); const imageNode = ref(null); const [image] = useImage('https://new.konvajs.org/assets/lion.png', 'anonymous'); watch(image, async (newImage) => { if (newImage) { await nextTick(); imageNode.value.getNode().cache(); } }); const handleSlider = (e) => { contrast.value = parseInt(e.target.value); }; </script>