Modify Shape Color on Click

Instructions: Click on a shape to change its color

Konva Modify Shape Color on Clickview raw
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/[email protected]/konva.min.js"></script>
<meta charset="utf-8" />
<title>Konva Modify Shape Color on Click Demo</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f0f0f0;
}
</style>
</head>
<body>
<div id="container"></div>
<script>
var width = window.innerWidth;
var height = window.innerHeight;

var stage = new Konva.Stage({
container: 'container',
width: width,
height: height,
});

var layer = new Konva.Layer();

var triangle = new Konva.RegularPolygon({
x: 80,
y: 120,
sides: 3,
radius: 50,
fill: '#00D2FF',
stroke: 'black',
strokeWidth: 4,
});

triangle.on('click', function () {
var fill = this.fill() == 'yellow' ? '#00D2FF' : 'yellow';
this.fill(fill);
});

layer.add(triangle);

var circle = new Konva.Circle({
x: 180,
y: 120,
radius: 50,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
});

circle.on('click', function () {
var fill = this.fill() == 'red' ? '#00d00f' : 'red';
this.fill(fill);
});

layer.add(circle);
stage.add(layer);
</script>
</body>
</html>
Enjoying Konva? Please consider to support the project.