Zoom Image on Hover

Instructions: Hover over an Image.

Konva Zoom Image on Hover Demoview raw
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/[email protected]/konva.min.js"></script>
<meta charset="utf-8" />
<title>Konva Zoom Image on Hover 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();

stage.add(layer);

var imageObj = new Image();
imageObj.onload = function () {
var backgroundImage = new Konva.Image({
x: 0,
y: 0,
width: width,
height: height,
image: imageObj,
});
layer.add(backgroundImage);
};

imageObj.src = '/assets/space.jpg';

var zoomLevel = 2;
layer.on('mouseenter', function () {
layer.scale({
x: zoomLevel,
y: zoomLevel,
});
});

layer.on('mousemove', function (e) {
var pos = stage.getPointerPosition();
layer.x(-pos.x);
layer.y(-pos.y);
});

layer.on('mouseleave', function () {
layer.x(0);
layer.y(0);
layer.scale({
x: 1,
y: 1,
});
});
</script>
</body>
</html>
Enjoying Konva? Please consider to support the project.