Ondřej Severa
Ondřej Severa

Reputation: 379

HTML Button inside SVG document

I have following problem I want to dynamically create HTML buttons inside SVG document. At first I look over the SVG document, find apropriate reactangle and replace rectangle with <foreignObject>element. I am able to generate following SVG

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   version="1.1"
   width="100%"
   height="100%"
   viewBox="0 0 93.875076 43.283451"
   id="svg20755"
   xml:space="preserve">

    <g id="Button-1" transform="translate(0.5000775,0.50220189)">

       <foreignObject xmlns="http://www.w3.org/2000/svg" x="0" y="0" width="93" height="43">
           <body xmlns="http://www.w3.org/1999/xhtml" style="margin: 0px; height: 100%;">
            <button type="button" style="width:100%; height:100%;" onclick="alert('Button clicked')">
                Juhuhuhu!
            </button>
           </body>
       </foreignObject>

    </g>
</svg>

This is the snippet how I create the element

var ns_HTML = 'http://www.w3.org/1999/xhtml';
var ns_SVG = REX.HELPERS.ns_svg;

var element = <Some code to load an SVG element>

var foreignObject = document.createElementNS(ns_SVG, 'foreignObject');

var body = document.createElementNS(ns_HTML, 'body');
body.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml'); // FF
body.style.margin = '0px';
body.style.height = '100%';        

var button = document.createElementNS(ns_HTML, 'button');    
button.setAttribute('type','button');    
button.setAttribute('style','width:100%; height:100%;');
button.innerHTML = 'Test!';

body.appendChild(button);
foreignObject.appendChild(body);

I have two problems especially in Chrome Browser:

  1. The element <body> did not fill the height of the <foreignObject>
  2. The button did not behave as a button (no reaction on mouseover and mousedown) (It seems that this was fixed in new release of Chrome and Firefox)

JSFiddle here

Upvotes: 7

Views: 3815

Answers (1)

schlebe
schlebe

Reputation: 3715

I don't know why you say <body> element doesn't fill the height of <foreignObject> !?

When I try now in 2020, I see only a button in midlle on screen.

I have added a SVG <rect> element with same size (width and height) than <foreignObject> to have the possibility to compare these 2 objects.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
  xmlns="http://www.w3.org/2000/svg"
  version="2.0"
  width="100%"
  height="100%"
  viewBox="0 0 90 50"
  >

<g transform="translate(1 0)">
    <foreignObject xmlns="http://www.w3.org/2000/svg" x="0" y="0" width="80" height="20">
        <body xmlns="http://www.w3.org/1999/xhtml" style="margin:0px; height:100%;">
            <button type="button" style="width:100%; height:100%;" onclick="alert('Button clicked')">
            Juhuhuhu!
            </button>
        </body>
    </foreignObject>
    <rect x="0" y="25" width="80" height="20" stroke="orange" stroke-width="2" fill="white"/>
</g>

As you can see, they have same size !

enter image description here

I put an image to proove what I say because it is possible that in 1 year what I have written is false !

Upvotes: 2

Related Questions