179 lines
6.5 KiB
HTML
179 lines
6.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
|
|
<link rel="stylesheet" href="style.css">
|
|
<title>DrawDiagrams</title>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="toolbar">
|
|
<p>Kształty</p>
|
|
<div class="shape-wrapper toolbar-wrapper">
|
|
<div id="shape-rectangle" class="shape" data-type="rectangle"></div>
|
|
<div id="shape-ellipse" class="shape" data-type="ellipse"></div>
|
|
<div id="shape-diamond" class="shape" data-type="diamond"></div>
|
|
<div id="shape-text" class="shape" data-type="text">Tekst</div>
|
|
</div>
|
|
</div>
|
|
<div class="canvas-container">
|
|
<div id="paper"></div>
|
|
</div>
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
|
|
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/@joint/core@4.0.1/dist/joint.js"></script>
|
|
<script>
|
|
$(document).ready(function () {
|
|
//init paper - Start
|
|
const graph = new joint.dia.Graph();
|
|
const paper = new joint.dia.Paper({
|
|
el: $('#paper'),
|
|
model: graph,
|
|
width: window.innerWidth,
|
|
height: window.innerHeight,
|
|
gridSize: 20,
|
|
});
|
|
//init paper - END
|
|
|
|
//create shape - START
|
|
function createRectangle(paperX, paperY) {
|
|
const rect = new joint.shapes.standard.Rectangle({
|
|
position: { x: paperX - 40, y: paperY - 24 },
|
|
size: { width: 80, height: 48 },
|
|
attrs: {
|
|
body: {
|
|
fill: 'transparent',
|
|
stroke: '#000000',
|
|
strokeWidth: 1
|
|
},
|
|
label: {
|
|
text: ''
|
|
}
|
|
}
|
|
});
|
|
graph.addCell(rect);
|
|
}
|
|
|
|
function createDiamond(paperX, paperY) {
|
|
const rect = new joint.shapes.standard.Rectangle({
|
|
position: { x: paperX - 24, y: paperY - 24 },
|
|
size: { width: 48, height: 48 },
|
|
angle: 45,
|
|
attrs: {
|
|
body: {
|
|
fill: 'transparent',
|
|
stroke: '#000000',
|
|
strokeWidth: 1
|
|
},
|
|
label: {
|
|
text: ''
|
|
}
|
|
}
|
|
});
|
|
graph.addCell(rect);
|
|
}
|
|
|
|
function createEllipse(paperX, paperY) {
|
|
const rect = new joint.shapes.standard.Ellipse({
|
|
position: { x: paperX - 40, y: paperY - 24 },
|
|
size: { width: 80, height: 48 },
|
|
attrs: {
|
|
body: {
|
|
fill: 'transparent',
|
|
stroke: '#000000',
|
|
strokeWidth: 1
|
|
},
|
|
label: {
|
|
text: ''
|
|
}
|
|
}
|
|
});
|
|
graph.addCell(rect);
|
|
}
|
|
|
|
function createText(paperX, paperY) {
|
|
const textBlock = new joint.shapes.standard.TextBlock({
|
|
position: { x: paperX - 40, y: paperY - 24 },
|
|
size: { width: 80, height: 48 },
|
|
attrs: {
|
|
body: {
|
|
fill: 'transparent',
|
|
stroke: '#000000',
|
|
strokeWidth: 1,
|
|
strokeDasharray: '5,5'
|
|
},
|
|
label: {
|
|
text: 'Tekst...',
|
|
fill: '#000000'
|
|
}
|
|
}
|
|
});
|
|
graph.addCell(textBlock);
|
|
}
|
|
//create shape - END
|
|
|
|
//init draggalble - START
|
|
$('.shape').draggable({
|
|
helper: function () {
|
|
const $clone = $(this).clone();
|
|
$clone.css({
|
|
opacity: 0.8,
|
|
cursor: 'grabbing',
|
|
borderColor: '#000',
|
|
color: '#000',
|
|
});
|
|
return $clone;
|
|
},
|
|
cursor: 'move',
|
|
revert: 'invalid',
|
|
appendTo: 'body',
|
|
zIndex: 1000
|
|
});
|
|
|
|
$('#paper').droppable({
|
|
accept: '.shape',
|
|
drop: function (event, ui) {
|
|
const shapeType = ui.helper.data('type');
|
|
|
|
const containerOffset = $(this).offset();
|
|
const x = event.pageX - containerOffset.left;
|
|
const y = event.pageY - containerOffset.top;
|
|
|
|
const scale = paper.scale();
|
|
const translate = paper.translate();
|
|
const paperX = (x - translate.tx) / scale.sx;
|
|
const paperY = (y - translate.ty) / scale.sy;
|
|
|
|
if (shapeType === 'rectangle') {
|
|
createRectangle(paperX, paperY);
|
|
} else if (shapeType === 'ellipse') {
|
|
createEllipse(paperX, paperY);
|
|
} else if (shapeType === 'diamond') {
|
|
createDiamond(paperX, paperY);
|
|
} else if (shapeType === 'text') {
|
|
createText(paperX, paperY);
|
|
}
|
|
}
|
|
});
|
|
//init draggable - END
|
|
|
|
//add text to text block - START
|
|
paper.on('element:pointerdblclick', function (elementView) {
|
|
if (elementView.model.attributes.type === 'standard.TextBlock') {
|
|
const currentText = elementView.model.attr('label/text');
|
|
const newText = prompt('Edytuj tekst:', currentText);
|
|
if (newText != null) {
|
|
elementView.model.attr('label/text', newText);
|
|
}
|
|
}
|
|
});
|
|
//add text to text block - END
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html> |