JQuery-ui resizable: resizing does not work for dynamically generated SVG element

I have an SVG <rect>

inside <div>

and want to drag and drop it and resize it. When I create elements statically in HTML, for example:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/start/jquery-ui.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {  

        $('div').draggable({
            handle: 'rect'
        }).resizable({
            aspectRatio: 1.0
        });
    });
</script>
</head>
<body>    
    <div style="width:400px; height:400px; border:solid thin #888; padding:10px; border-radius:4px; background-color:#ccc;">
        <svg xmlns="http://www.w3.org/2000/svg" version="1.0" viewBox="0 0 400 400">
            <rect x="0" y="0" width="200" height="200" style="fill:#FF0000" />
        </svg>
    </div>
</body>

      

everything is working. <rect>

is dragged and resized along with <div>

, but when I generate the elements dynamically like this:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/start/jquery-ui.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {

        $('body').append('<div style="width:400px; height:400px; border:solid thin #888; padding:10px; border-radius:4px; background-color:#ccc;">');

        var svg = document.createElementNS('http://www.w3.org/2000/svg', "svg");
        svg.setAttributeNS(null, "viewbox", "0 0 400 400");
        $('div').append(svg);

        var square = document.createElementNS('http://www.w3.org/2000/svg', "rect");
        square.setAttributeNS(null, "width", "200");
        square.setAttributeNS(null, "height", "200");
        square.setAttributeNS(null, "x", "0");
        square.setAttributeNS(null, "y", "0");
        square.setAttributeNS(null, "style", "fill:#FF0000");
        $('svg').append(square);

        $('div').draggable({
            handle: 'rect'
        }).resizable({
            aspectRatio: 1.0
        });
    });
</script>
</head>
<body>    
</body>
</html>

      

just drag and drop continues to work for <div>

and to <rect>

. Resizing is done for only <div>

, <rect>

does not resize at all.

What happened?

+2


source to share


1 answer


SVG is case sensitive in the static case when you use the correct "viewBox". In the dynamic case, you don't.

    svg.setAttributeNS(null, "viewbox", "0 0 400 400");

      



it should be

    svg.setAttributeNS(null, "viewbox", "0 0 400 400");

      



+4


source







All Articles