개요
- knovaJS는 canvas API를 보다 쉽게 쓸 수 있도록 도와주는 라이브러리이다.
- Stage, Layer, Group, Shape으로 이루어져 있다.
stage
- 화면상에 보이는 캔버스 크기 및 이벤트를 관리
layer
group
shape
- Layer에 그려지는 다양한 모양
- ex) 사각형, 원, 이미지, 스프라이트, 텍스트, 선, 다각형, 정다각형, 경로, 별 등
example
const {innerWidth: width, innerHeight: height} = window;
const stage = new Konva.Stage({
container: 'container',
width,
height,
});
const layer = new Konva.Layer();
stage.add(layer);
const greenRectOption = {
x: 20,
y: 20,
width: 100,
height: 50,
fill: 'green',
stroke: '#3a3a3a',
strokeWidth: 4,
cornerRadius: [0, 0, 40, 40],
shadowBlur: 10,
}
const greenRect = new Konva.Rect(greenRectOption);
layer.add(greenRect);
const redCircleOption = {
x: 170,
y: 60,
radius: 25,
fill: 'red',
}
const redCircle = new Konva.Circle(redCircleOption);
layer.add(redCircle);
const yellowOvalOption = {
x: 250,
y: 150,
radiusX: 100,
radiusY: 50,
fill: 'yellow',
}
const yellowOval = new Konva.Ellipse(yellowOvalOption);
layer.add(yellowOval);
const redLineOption = {
points: [5, 70, 140, 23, 250, 60, 300, 20],
stroke: 'red',
strokeWidth: 15,
lineCap: 'round',
lineJoin: 'round',
}
const redLine = new Konva.Line(redLineOption);
redLine.move({
x: 0,
y: 5,
});
layer.add(redLine);
const textOption = {
x: 10,
y: 10,
fontFamily: 'Calibri',
fontSize: 24,
text: '',
fill: 'black',
}
const text = new Konva.Text(textOption);
layer.add(text);
function writeMessage(message) {
text.text(message);
}
const triangleOption = {
x: 80,
y: 120,
sides: 3,
radius: 80,
fill: '#00D2FF',
stroke: 'black',
strokeWidth: 4,
}
const triangle = new Konva.RegularPolygon(triangleOption);
layer.add(triangle);
triangle.on('mousemove', function () {
var mousePos = stage.getPointerPosition();
var x = mousePos.x - 190;
var y = mousePos.y - 40;
writeMessage('x: ' + x + ', y: ' + y);
});
triangle.on('mouseout', function () {
writeMessage('Mouseout triangle');
});
triangle.on('touchmove', function () {
var touchPos = stage.getPointerPosition();
var x = touchPos.x - 190;
var y = touchPos.y - 40;
writeMessage('x: ' + x + ', y: ' + y);
});
let rectX = stage.width() / 2 - 50;
let rectY = stage.height() / 2 - 25;
const rectOption = {
x: rectX,
y: rectY,
width: 100,
height: 50,
fill: '#00D2FF',
stroke: 'black',
strokeWidth: 4,
draggable: true,
}
const box = new Konva.Rect(rectOption);
layer.add(box);
const group = new Konva.Group({
draggable: true,
});
const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
for (var i = 0; i < 6; i++) {
const box = new Konva.Rect({
x: i * 30 + 100,
y: i * 18 + 400,
width: 100,
height: 50,
name: colors[i],
fill: colors[i],
stroke: 'black',
strokeWidth: 4,
});
group.add(box);
}
layer.add(group);
- transformer(move, resize, rotate등)
const tr = new Konva.Transformer({});
tr.nodes([group]);
layer.add(tr);
const randomGroup = new Konva.Group({
x: 500,
y: 500,
draggable: true,
});
layer.add(randomGroup);
const transformer = new Konva.Transformer();
layer.add(transformer);
transformer.nodes([randomGroup]);
document.getElementById('button').addEventListener('click', addShape);
function addShape() {
randomGroup.add(
new Konva.Circle({
x: Math.random() * 100,
y: Math.random() * 100,
radius: Math.random() * 100,
fill: Konva.Util.getRandomColor(),
stroke: 'black',
strokeWidth: Math.random() * 10,
})
);
transformer.forceUpdate();
}
참고자료
https://adjh54.tistory.com/m/14