I have SVG image like the tag below in div
<div id="MainDiv">
<div id="annotationText">
<svg id="circle" width="50" height="50">
<circle cx="25" cy="25" r="20" stroke="green" stroke-width="4" fill="yellow" fill-opacity="0.0"/>
</svg>
</div>
</div>
I am making is draggable and resizable. draggable is working how to make it resizable, I tried but it is not working.
makeDragableCircle('#annotationText',jQuery('#MainDiv'));
function makeDragableCircle(selector,obj){
var height=obj.height();
var width=obj.width();
var objdiv=jQuery( selector );
jQuery( selector ).draggable({
containment: obj,
drag: function( event, ui ) {
var cleft=ui.position.left*100/width;
var top=ui.position.top*100/height;
jQuery(event.target).attr('data-offsetx',cleft);
jQuery(event.target).attr('data-offsety',top);
}
}).resizable({
alsoResize: "#"+circle,
aspectRatio: 1.0
});
}
As I mentioned in my comment, resizable, is designed for box models like <div>
, or <span>
. You can use it on the <svg>
element, but it will treat it like an HTML element.
If you want to make use of it to manipulate the structure of a SVG object, you need to roll your own a bit.
$(function() {
function makeDragableCircle(selector, obj) {
var height = obj.height();
var width = obj.width();
var objdiv = $(selector);
var circle = $("#circle", objdiv);
$(selector).draggable({
containment: obj,
drag: function(event, ui) {
var cleft = ui.position.left * 100 / width;
var top = ui.position.top * 100 / height;
$(event.target).attr('data-offsetx', cleft);
$(event.target).attr('data-offsety', top);
}
}).resizable({
aspectRatio: 1.0,
containment: obj,
minWidth: 40,
minHeight: 40,
resize: function(e, ui) {
circle.attr({
width: ui.size.width,
height: ui.size.height
});
$("circle", circle).attr({
cx: Math.round(ui.size.width / 2) - 2,
cy: Math.round(ui.size.height / 2) - 2,
r: Math.round(ui.size.width / 2) - 4
});
}
});
}
makeDragableCircle('#annotationText', $('#mainDiv'));
});
#mainDiv {
width: 400px;
height: 200px;
border: 1px dashed #eee;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="mainDiv">
<div id="annotationText">
<svg id="circle" width="50" height="50">
<circle cx="25" cy="25" r="20" stroke="green" stroke-width="4" fill="yellow" fill-opacity="0.0" />
</svg>
</div>
</div>
As you see, when you drag, it all moves. When you resize, we adjust the SVG size and we adjust properties of the <circle>
.
Hope that helps.