HTML5 Canvas Emboss 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.
Instructions: Slide the controls to change emboss values.
For all available filters go to Filters Documentation.
- Vanilla
- React
- Vue
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.Emboss]); image.embossStrength(0.5); image.embossWhiteLevel(0.5); image.embossDirection('top-left'); image.embossBlend(0.5); // create sliders const strengthSlider = document.createElement('input'); strengthSlider.type = 'range'; strengthSlider.min = '0'; strengthSlider.max = '1'; strengthSlider.step = '0.1'; strengthSlider.value = image.embossStrength(); strengthSlider.style.position = 'absolute'; strengthSlider.style.top = '20px'; strengthSlider.style.left = '20px'; const whiteLevelSlider = document.createElement('input'); whiteLevelSlider.type = 'range'; whiteLevelSlider.min = '0'; whiteLevelSlider.max = '1'; whiteLevelSlider.step = '0.1'; whiteLevelSlider.value = image.embossWhiteLevel(); whiteLevelSlider.style.position = 'absolute'; whiteLevelSlider.style.top = '45px'; whiteLevelSlider.style.left = '20px'; const blendSlider = document.createElement('input'); blendSlider.type = 'range'; blendSlider.min = '0'; blendSlider.max = '1'; blendSlider.step = '0.1'; blendSlider.value = image.embossBlend(); blendSlider.style.position = 'absolute'; blendSlider.style.top = '70px'; blendSlider.style.left = '20px'; // add labels const strengthLabel = document.createElement('div'); strengthLabel.textContent = 'Strength'; strengthLabel.style.position = 'absolute'; strengthLabel.style.top = '20px'; strengthLabel.style.left = '200px'; const whiteLevelLabel = document.createElement('div'); whiteLevelLabel.textContent = 'White Level'; whiteLevelLabel.style.position = 'absolute'; whiteLevelLabel.style.top = '45px'; whiteLevelLabel.style.left = '200px'; const blendLabel = document.createElement('div'); blendLabel.textContent = 'Blend'; blendLabel.style.position = 'absolute'; blendLabel.style.top = '70px'; blendLabel.style.left = '200px'; // add event listeners strengthSlider.addEventListener('input', (e) => { image.embossStrength(parseFloat(e.target.value)); layer.batchDraw(); }); whiteLevelSlider.addEventListener('input', (e) => { image.embossWhiteLevel(parseFloat(e.target.value)); layer.batchDraw(); }); blendSlider.addEventListener('input', (e) => { image.embossBlend(parseFloat(e.target.value)); layer.batchDraw(); }); // add elements to the page document.body.appendChild(strengthSlider); document.body.appendChild(whiteLevelSlider); document.body.appendChild(blendSlider); document.body.appendChild(strengthLabel); document.body.appendChild(whiteLevelLabel); document.body.appendChild(blendLabel); }; 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 [strength, setStrength] = useState(0.5); const [whiteLevel, setWhiteLevel] = useState(0.5); const [blend, setBlend] = useState(0.5); const [image] = useImage('https://new.konvajs.org/assets/lion.png', 'anonymous'); const imageRef = useRef(null); useEffect(() => { if (image) { imageRef.current.cache(); } }, [image]); return ( <> <div style={{ position: 'absolute', top: '20px', left: '20px' }}> <div> <label>Strength: </label> <input type="range" min="0" max="1" step="0.1" value={strength} onChange={(e) => setStrength(parseFloat(e.target.value))} /> </div> <div style={{ marginTop: '10px' }}> <label>White Level: </label> <input type="range" min="0" max="1" step="0.1" value={whiteLevel} onChange={(e) => setWhiteLevel(parseFloat(e.target.value))} /> </div> <div style={{ marginTop: '10px' }}> <label>Blend: </label> <input type="range" min="0" max="1" step="0.1" value={blend} onChange={(e) => setBlend(parseFloat(e.target.value))} /> </div> </div> <Stage width={window.innerWidth} height={window.innerHeight}> <Layer> <Image ref={imageRef} x={50} y={50} image={image} draggable filters={[Konva.Filters.Emboss]} embossStrength={strength} embossWhiteLevel={whiteLevel} embossDirection="top-left" embossBlend={blend} /> </Layer> </Stage> </> ); };
<template> <div> <div style={{ position: 'absolute', top: '20px', left: '20px' }}> <div v-for="control in controls" :key="control.label" style="margin: 10px"> <label>{{ control.label }}: </label> <input type="range" min="0" max="1" step="0.1" :value="control.value" @input="(e) => updateValue(control.prop, parseFloat(e.target.value))" style="width: 200px" /> </div> </div> <v-stage :config="stageSize"> <v-layer> <v-image ref="imageNode" :config="{ x: 50, y: 50, image: image, draggable: true, filters: [Konva.Filters.Emboss], embossStrength: embossValues.strength, embossWhiteLevel: embossValues.whiteLevel, embossDirection: 'top-left', embossBlend: embossValues.blend, }" /> </v-layer> </v-stage> </div> </template> <script setup> import { ref, computed, watch, nextTick } from 'vue'; import { useImage } from 'vue-konva'; import Konva from 'konva'; const stageSize = { width: window.innerWidth, height: window.innerHeight, }; const imageNode = ref(null); const [image] = useImage('https://new.konvajs.org/assets/lion.png', 'anonymous'); const embossValues = ref({ strength: 0.5, whiteLevel: 0.5, blend: 0.5, }); const controls = computed(() => [ { label: 'Strength', prop: 'strength', value: embossValues.value.strength }, { label: 'White Level', prop: 'whiteLevel', value: embossValues.value.whiteLevel }, { label: 'Blend', prop: 'blend', value: embossValues.value.blend }, ]); const updateValue = (prop, value) => { embossValues.value[prop] = value; }; watch(image, async (newImage) => { if (newImage) { await nextTick(); imageNode.value.getNode().cache(); } }); </script>