<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/konva@4.0.18/konva.min.js"></script>
<meta charset="utf-8" />
<title>Konva Rect 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 rect = new Konva.Rect({
x: 10,
y: 10,
width:190,
height: 100,
fill: 'red',
stroke: 'black',
strokeWidth: 1,
shadowBlur: 10,
cornerRadius: [0,35,0,5],
draggable: true
})
// 圆
var circle = new Konva.Circle({
x: 1000,
y: 100,
radius: 80,
fill: 'pink',
stroke: 'black',
strokeWidth: 1,
shadowBlur: 10,
cornerRadius: [0,35,0,5],
draggable: true
})
// 椭圆
var ellipse = new Konva.Ellipse({
x: 100,
y: 500,
radiusX: 74,
radiusY: 34,
fill: 'pink',
stroke: 'black',
strokeWidth: 1,
shadowBlur: 10,
cornerRadius: [0,35,0,5],
draggable: true
})
// 扇形
var wedge = new Konva.Wedge({
x: 100,
y: 500,
radius: 34,
angle: 60,
fill: 'pink',
stroke: 'black',
strokeWidth: 1,
shadowBlur: 10,
cornerRadius: [0,35,0,5],
rotation: -10,
draggable: true
})
// 多边形
var line = new Konva.Line({
points: [80,90,400,80,200,600,300,270,66,99],
fill: 'pink',
stroke: 'black',
strokeWidth: 1,
shadowBlur: 10,
closed: true,
draggable: true
})
var rectX = stage.width()
var rectY = stage.height()
var rect1 = new Konva.Rect({
x: rectX,
y: rectY,
width:190,
height: 100,
fill: 'red',
stroke: 'black',
strokeWidth: 1,
shadowBlur: 10,
cornerRadius: [0,35,0,5],
draggable: true
})
rect.on('mouseover',function() {
document.body.style.cursor = 'pointer';
})
rect.on('mouseout', function() {
document.body.style.cursor = 'default';
});
layer.add(rect)
layer.add(rect1)
layer.add(circle)
layer.add(ellipse)
layer.add(wedge)
layer.add(line)
stage.add(layer)
</script>
</body>
</html>
简单的图形绘制,实现拖拽
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://unpkg.com/konva@4.0.18/konva.min.js"></script>
<style>
.title{
width: 100%;
height: 40px;
background: rgb(192, 185, 185);
}
ul {
list-style: none;
display: flex;
}
ul li{
margin-right: 10px;
cursor: pointer;
line-height: 40px;
}
#container{
border: 1px solid red;
}
</style>
</head>
<body>
<div class="title">
<ul>
<li>圆</li>
<li>矩形</li>
</ul>
</div>
<div id="container"></div>
<script>
// 创建舞台
var stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight
})
// 创建图层
var layer = new Konva.Layer()
var container = document.getElementById('container')
const staus = {
noDrag: 0,
darging: 1,
end: 2
}
const info = {
isStaus: staus.end
}
function pos(e) {
return {
x: e.offsetX,
y: e.offsetY
}
}
container.addEventListener('click', e => {
info.isStaus = staus.noDrag
if(info.isStaus === staus.noDrag){
const circle = new Konva.Circle({
x: pos(e).x,
y: pos(e).y,
radius: 80,
fill: 'pink',
stroke: 'black',
strokeWidth: 1,
shadowBlur: 10,
draggable: true
})
layer.add(circle)
stage.add(layer)
}
})
// container.addEventListener('mousedown', e=> {
// })
stage.add(layer)
</script>
</body>
</html>
详细教程参考:文档 | Konva 中文文档 中文API