jQuery: extract top-most SVG element from HTML string with multiple nodes - javascript

This question and this question are similar, but the answers do not yield a clean, consistent method for finding the top most SVG element among different HTML strings.
The goal is to extract the top-most SVG element from a HTML string.
$(htmlString).find("svg") does not work.
$($.parseHTML(htmlString)) only generates an array of jQuery objects, but the goal is to turn the htmlString into one jQuery object where you can execute find and retrieve the top-most SVG element.
Example HTML string:
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Generator: Adobe Illustrator 18.1.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 424 424" style="enable-background:new 0 0 424 424;" xml:space="preserve" width="512px" height="512px">
<g>
<g>
<path d="M35,89C15,89,0,74,0,54s15-36,35-36h353c20,0,36,16,36,36s-16,35-36,35H35z" fill="#7c7a7d"/>
<path d="M388,176c20,0,36,16,36,36s-16,35-36,35H35c-20,0-35-15-35-35s15-36,35-36H388z" fill="#7c7a7d"/>
<path d="M388,335c20,0,36,15,36,35s-16,36-36,36H35c-20,0-35-16-35-36s15-35,35-35H388z" fill="#7c7a7d"/>
</g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
</svg>
Result of $($.parseHTML(htmlString)), where htmlString is the string above:
$($.parseHTML(htmlString))
w.fn.init(6) [comment, text, comment, text, svg#Capa_1, text]
0: comment
1: text
2: comment
3: text
4: svg#Capa_1
5: text
length: 6

You cannot use find on the result because the markup you give to jQuery doesn't represent a tree: Doctype and comments nodes are siblings of your <svg>.
Thus, you need filter the jQuery entries in order to keep only the svg node:
const $svg = $(`<?xml version="1.0" encoding="utf-8"?>
<!-- comment1 -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 424 424">
<g>
<g>
<path d="M35,89C15,89,0,74,0,54s15-36,35-36h353c20,0,36,16,36,36s-16,35-36,35H35z" fill="#7c7a7d"/>
<path d="M388,176c20,0,36,16,36,36s-16,35-36,35H35c-20,0-35-15-35-35s15-36,35-36H388z" fill="#7c7a7d"/>
<path d="M388,335c20,0,36,15,36,35s-16,36-36,36H35c-20,0-35-16-35-36s15-35,35-35H388z" fill="#7c7a7d"/>
</g>
</g>
</svg>`)
.filter((i, el) => $(el).is('svg'));
console.log($svg.attr('id'));
$svg.find('path').attr('fill', 'red');
$('body').append($svg);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Now, you may also want to use a native DOMParser, which might be better at handling namespaces than jQuery. From there, you will have an XMLDocument, whose documentElement will be your <svg> node.
You will then be able to work on it from jQuery:
const str = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 424 424">
<g>
<g>
<path d="M35,89C15,89,0,74,0,54s15-36,35-36h353c20,0,36,16,36,36s-16,35-36,35H35z" fill="#7c7a7d"/>
<path d="M388,176c20,0,36,16,36,36s-16,35-36,35H35c-20,0-35-15-35-35s15-36,35-36H388z" fill="#7c7a7d"/>
<path d="M388,335c20,0,36,15,36,35s-16,36-36,36H35c-20,0-35-16-35-36s15-35,35-35H388z" fill="#7c7a7d"/>
</g>
</g>
</svg>`;
const doc = (new DOMParser).parseFromString(str, 'image/svg+xml');
// use the second argument (context) of jQuery()
const $svg = $('svg', doc);
console.log($svg.attr('id'));
$svg.find('path').attr('fill', 'red');
$('body').append($svg);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Related

svg won't show up in firefox but loads in chrome?

I am loading an 404.svg at certain scenarios into a div dynamically.
The svg is getting loaded in chrome but it never does in Firefox for some reason and I haven't checked it in other browsers.
I got the svg from some free SVG providing websites and this is the inside code of svg (can't upload svg directly here stackoverflow limitation ) :
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Generator: Adobe Illustrator 19.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 503.322 503.322" style="enable-background:new 0 0 503.322 503.322;" xml:space="preserve">
<g>
<rect x="182.194" y="408.711" style="fill:#95A5A5;" width="138.847" height="77.017"/>
<path style="fill:#3F5C6C;" d="M166.617,0.195H34.712C15.534,0.195,0,15.121,0,43.195v338.441c0,8.548,15.534,23.474,34.712,23.474
H468.61c19.178,0,34.712-14.926,34.712-23.474V43.195c0-28.073-15.534-42.999-34.712-42.999H166.617z"/>
<g>
<path style="fill:#BDC3C7;" d="M138.847,468.415h225.627c14.378,0,26.034,11.656,26.034,26.034v8.678H112.814v-8.678
C112.814,480.071,124.469,468.415,138.847,468.415z"/>
<path style="fill:#BDC3C7;" d="M503.322,365.364v27.509c-0.43,18.754-15.957,33.621-34.712,33.237H34.712
C15.957,426.495,0.43,411.627,0,392.873v-27.509H503.322z"/>
</g>
<path style="fill:#D25627;" d="M346.612,69.228h45.932c18.28,0.912,32.386,16.428,31.557,34.712v86.78
c0.829,18.284-13.276,33.8-31.556,34.712H108.538c-18.28-0.912-32.385-16.428-31.556-34.712v-86.78
c-0.829-18.284,13.276-33.8,31.556-34.712H346.612z"/>
<g>
<path style="fill:#ECF0F1;" d="M198.475,156.008h-8.678v-8.678c0-4.793-3.885-8.678-8.678-8.678c-4.793,0-8.678,3.885-8.678,8.678
v8.678h-19.754l27.288-47.763c2.366-4.16,0.917-9.449-3.238-11.823c-4.155-2.374-9.447-0.937-11.83,3.213l-34.712,60.746
c-1.537,2.685-1.527,5.986,0.026,8.662c1.553,2.676,4.414,4.323,7.508,4.321h34.712v17.356c0,4.793,3.885,8.678,8.678,8.678
c4.793,0,8.678-3.885,8.678-8.678v-17.356h8.678c4.793,0,8.678-3.885,8.678-8.678
C207.153,159.893,203.267,156.008,198.475,156.008z"/>
<path style="fill:#ECF0F1;" d="M363.356,156.008h-8.678v-8.678c0-4.793-3.885-8.678-8.678-8.678s-8.678,3.885-8.678,8.678v8.678
h-19.754l27.288-47.763c2.366-4.16,0.917-9.449-3.238-11.823c-4.155-2.374-9.447-0.937-11.83,3.213l-34.712,60.746
c-1.537,2.685-1.527,5.986,0.026,8.662c1.553,2.676,4.414,4.323,7.508,4.321h34.712v17.356c0,4.793,3.885,8.678,8.678,8.678
s8.678-3.885,8.678-8.678v-17.356h8.678c4.793,0,8.678-3.885,8.678-8.678C372.034,159.893,368.149,156.008,363.356,156.008z"/>
<path style="fill:#ECF0F1;" d="M259.22,95.262h-17.356c-14.372,0.016-26.018,11.662-26.034,26.034v52.068
c0.016,14.372,11.662,26.018,26.034,26.034h17.356c14.372-0.016,26.018-11.662,26.034-26.034v-52.068
C285.239,106.925,273.592,95.278,259.22,95.262z M267.898,173.364c-0.005,4.791-3.887,8.673-8.678,8.678h-17.356
c-4.791-0.005-8.673-3.887-8.678-8.678v-52.068c0.005-4.791,3.887-8.673,8.678-8.678h17.356c4.791,0.005,8.673,3.887,8.678,8.678
V173.364z"/>
</g>
<path style="fill:#D25627;" d="M137.763,268.822l-34.712,52.068h-8.678c-9.574-0.028-17.328-7.782-17.356-17.356v-17.356
c0.028-9.574,7.782-17.328,17.356-17.356H137.763z"/>
<g>
<path style="fill:#E57E25;" d="M424.136,286.178v17.356c-0.028,9.574-7.782,17.328-17.356,17.356h-43.39l34.712-52.068h8.678
C416.353,268.85,424.108,276.604,424.136,286.178z"/>
<polygon style="fill:#E57E25;" points="181.153,268.822 146.441,320.889 103.051,320.889 137.763,268.822 "/>
</g>
<polygon style="fill:#D25627;" points="224.542,268.822 189.831,320.889 146.441,320.889 181.153,268.822 "/>
<polygon style="fill:#E57E25;" points="267.932,268.822 233.22,320.889 189.831,320.889 224.542,268.822 "/>
<polygon style="fill:#D25627;" points="311.322,268.822 276.61,320.889 233.22,320.889 267.932,268.822 "/>
<polygon style="fill:#E57E25;" points="354.712,268.822 320,320.889 276.61,320.889 311.322,268.822 "/>
<polygon style="fill:#D25627;" points="398.102,268.822 363.39,320.889 320,320.889 354.712,268.822 "/>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
</svg>
and this is how I dynamically add the svg:
imgDiv.innerHTML = `<img class="image" id="cardImage" src="404.svg" alt="404" style="box-shadow: none"/>`;
how to load the SVG properly into all browsers ?

Trying to change the color of an svg using an onclick event. Its saying it cant assign the color because its underfined

heres my svg file, its just a face as I am practicing
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 720 720" style="enable-background:new 0 0 720 720;" xml:space="preserve">
<g id="skin">
<circle class="skin" cx="364.42" cy="383" r="278"/>
</g>
<g id="mouth">
<path class="mouth" d="M172.92,383c127.67,0,255.33,0,383,0c0,105.05-86.45,191.5-191.5,191.5S172.92,488.05,172.92,383z"/>
</g>
<g id="hair">
<path id = "hair" class = "hair" d="M107.4,276.86c-2.76-50.59,10.76-81.24,24.2-100.13C189.57,95.31,331.92,112,341.92,57.91
c2.04-11.03-2.07-21.46-6.93-29.61c18.47-6.31,81.92-25.39,151.28,3.28c94.59,39.09,121.88,137.21,127.56,160.84
c7.93,32.98,7.18,61.32,5.46,79.42c-38.79-64.73-78.17-85.57-107.42-92.42c-43.32-10.15-60.72,11.26-139.64,11.71
c-67.77,0.39-78.13-15.27-119.49-10.14C216.75,185.45,165.19,204.69,107.4,276.86z"/>
</g>
<g id="eyes">
<g>
<circle class = "eyes" cx="251.17" cy="271.25" r="52.4"/>
<circle class = "eyes" cx="477.67" cy="271.25" r="52.4"/>
</g>
</g>
</svg>
I'm trying to change the hair to blue using a button and an onclick event here:
<button class="blueButton" onclick="hair.style.fill='blue';"></button>
it does not change the color so I was wondering if anyone knew where I was going wrong with this...
Thank you!
You can't just reference the hair element. Instead, call document.getElementById('hair') to get it.
<button class="blueButton" onclick="document.getElementById('hair').style.fill='blue';">Button</button>
Here is a fiddle you can see it work in https://jsfiddle.net/679nLv3p/
You have two SVG objects with the same id hair. Remove the group one.
<g>
<path id = "hair" class = "hair" d="M107.4,276.86c-2.76-50.59,10.76-81.24,24.2-100.13C189.57,95.31,331.92,112,341.92,57.91
c2.04-11.03-2.07-21.46-6.93-29.61c18.47-6.31,81.92-25.39,151.28,3.28c94.59,39.09,121.88,137.21,127.56,160.84
c7.93,32.98,7.18,61.32,5.46,79.42c-38.79-64.73-78.17-85.57-107.42-92.42c-43.32-10.15-60.72,11.26-139.64,11.71
c-67.77,0.39-78.13-15.27-119.49-10.14C216.75,185.45,165.19,204.69,107.4,276.86z"/>
</g>
Working jsbin example

How to get parent group content from svg using javascript?

I want to get the content of <g> tag .
I have tried getElementsByTagName('g') but it returns all <g> in an array , I want just content of outer <g> .
Here is my svg
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" viewBox="0 0 500 500" style="enable-background:new 0 0 500 500;" xml:space="preserve">
<style type="text/css">
.st0{font-family:'MyriadPro-Regular';}
.st1{font-size:117.1799px;}
.st2{fill:#17519D;}
.st3{fill:#E1232E;}
.st4{fill:#FAFDF9;}
</style>
<g>
<text transform="matrix(1 0 0 1 161.3171 175.1665)" class="st0 st1">text</text>
<g id="xe_o3c0">
<g id="xe_6ipa">
<g id="xe_zj3t">
<path id="xe_ce0v" pointer-events="bounding-box" class="st2" d=".4l1."/>
</g>
</g>
</g>
<image style="overflow:visible;" width="728" height="625" xlink:href="http://magento1924.inkxe.com//xetool/assets/magento1924_inkxe_com/images/designs/16.jpg" transform="matrix(0.3073 0 0 0.3073 35.3171 216.8965)">
</image>
</g>
</svg>
Why don't you use id as the selector?
getElementsById('xe_6ipa')
To get the first group element in the SVG, you can use:
getElementsByTagName('g').item(0)
document.querySelector('g') will return the first outer instance that it finds, and returns the node instead of a NodeList.

Add a prefix to mutliple elements from the parent attribute

I'm trying to figure out how to add a unique prefix to ID's and other reference links.
I have multiple identical SVGs on a page. These SVGs serve as a wrapper (they're devices - laptop, phone, etc.) which have image link that is inserted into the SVG after load. The problem is, the ID's for these svgs are identical so they all conflict with each other. What I'm trying to do (but am open to better solutions) is to insert a unique ID to each id,xlink:href, url(#, etc. but pass over the <image> href attribute and fill and stroke attributes.
EDIT: I added/tweaked the code provided by #Temani, which got me closer to my desired result, however, I'm now getting all the ID's added to each SVG element. Formatted the code to be executable.
Desired output:
<div class="device" data-screen="[[IMAGE TO USE]]" data-name"[[UNIQUE ID]]">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="635" height="420" viewBox="0 0 635 420">
<defs>
<path id="[[UNIQUE ID]]-path-1"/>
<rect id="[[UNIQUE ID]]-path-3" />
<pattern id="[[UNIQUE ID]]-pattern-4">
<use transform="scale(13.1875)" xlink:href="#[[UNIQUE ID]]-image-5"/>
</pattern>
<image id="[[UNIQUE ID]]-image-5" href="[[IMAGE LINK THAT IS INSERTED AFTER LOAD]]"/>
</defs>
<g fill="none" fill-rule="evenodd">
<rect fill="#fff" stroke="#2D8EFF" />
<g >
<mask id="[[UNIQUE ID]]-mask-2" fill="#fff">
<use xlink:href="#[[UNIQUE ID]]-path-1"/>
</mask>
<g mask="url(#[[UNIQUE ID]]-mask-2)">
<mask id="mask-6" fill="#fff">
<use xlink:href="#[[UNIQUE ID]]-path-3"/>
</mask>
</g>
</g>
</g>
</svg>
</div>
Right now - both ID's (first and second) gets added to each SVG element. I also
$('.device').each(function() {
//we get the needed id using $(this) that refer to actual device
var id = $(this).data('name');
//we check all the element with ID
$(this).find("path, rect, pattern, image, mask").each(function() {
//now (this) refer to the actual element
if($(this).attr("id"))
$(this).attr("id", id+"-"+$(this).attr("id"));
});
//we update the <use>
$(this).find("use").each(function() {
//now (this) refer to the actual element
$(this).attr("xlink:href","#"+id+"-"+$(this).attr("xlink:href").substring(1));
});
// Also, hoping to combine g[mask], u[fill], etc into 1 function.
// Basically any attribute that starts with "url(#"
$(this).find("g[mask^='url']").each(function() {
$(this).attr("mask","url(#"+id+"-"+$(this).attr("mask").substring(5));
});
$(this).find("use[fill^='url']").each(function() {
$(this).attr("fill","url(#"+id+"-"+$(this).attr("fill").substring(5));
});
$(this).find("use[filter^='url']").each(function() {
$(this).attr("filter","url(#"+id+"-"+$(this).attr("filter").substring(5));
});
// grab data-screen value
var data = $(this).data('screen');
// replace this with the link inside svg
if (data != '') {
$(this).find("svg defs image").attr("href", data).attr("xlink:href", data);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="device" data-name="first" data-screen="image-1.png">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
<defs>
<rect id="w-circle-stroke-a" width="24" height="24"/>
<path id="w-circle-stroke-b" d="M12,2 C17.52,2 22,6.48 22,12 C22,17.52 17.52,22 12,22 C6.48,22 2,17.52 2,12 C2,6.48 6.48,2 12,2 Z M12,3.81818182 C7.48415409,3.81818182 3.81818182,7.48415409 3.81818182,12 C3.81818182,16.5158459 7.48415409,20.1818182 12,20.1818182 C16.5158459,20.1818182 20.1818182,16.5158459 20.1818182,12 C20.1818182,7.48415409 16.5158459,3.81818182 12,3.81818182 Z M10.5553177,13.4773237 L15.155405,8.80967806 C15.5597962,8.4027095 16.222261,8.39598875 16.6350615,8.79466684 C16.6382917,8.79778661 16.6600317,8.81952282 16.7002813,8.85987545 C17.0999062,9.26113743 17.0999062,9.90402237 16.7002813,10.3052843 L10.5553177,16.5 L7.29971874,13.2228714 C6.90252847,12.8240541 6.8997633,12.1859262 7.29348277,11.7837778 L7.33224151,11.7441893 C7.73340831,11.3344341 8.39555055,11.3228774 8.8111776,11.7183766 C8.81566955,11.722651 9.39704957,12.3089667 10.5553177,13.4773237 Z"/>
<image id="image-5" href="image-to-be-inserted.jpg"/>
</defs>
<g fill="none" fill-rule="evenodd">
<mask id="w-circle-stroke-c" fill="#fff">
<use xlink:href="#w-circle-stroke-b"/>
</mask>
<g fill="#2D8EFF" mask="url(#w-circle-stroke-c)">
<rect width="24" height="24"/>
</g>
<use fill="url(#pattern-4)" xlink:href="#path-3"/>
<use fill="#000" filter="url(#filter-10)" xlink:href="#path-9"/>
</g>
</svg>
</div>
<div class="device" data-name="second" data-screen="image-1.png">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
<defs>
<rect id="w-circle-stroke-a" width="24" height="24"/>
<path id="w-circle-stroke-b" d="M12,2 C17.52,2 22,6.48 22,12 C22,17.52 17.52,22 12,22 C6.48,22 2,17.52 2,12 C2,6.48 6.48,2 12,2 Z M12,3.81818182 C7.48415409,3.81818182 3.81818182,7.48415409 3.81818182,12 C3.81818182,16.5158459 7.48415409,20.1818182 12,20.1818182 C16.5158459,20.1818182 20.1818182,16.5158459 20.1818182,12 C20.1818182,7.48415409 16.5158459,3.81818182 12,3.81818182 Z M10.5553177,13.4773237 L15.155405,8.80967806 C15.5597962,8.4027095 16.222261,8.39598875 16.6350615,8.79466684 C16.6382917,8.79778661 16.6600317,8.81952282 16.7002813,8.85987545 C17.0999062,9.26113743 17.0999062,9.90402237 16.7002813,10.3052843 L10.5553177,16.5 L7.29971874,13.2228714 C6.90252847,12.8240541 6.8997633,12.1859262 7.29348277,11.7837778 L7.33224151,11.7441893 C7.73340831,11.3344341 8.39555055,11.3228774 8.8111776,11.7183766 C8.81566955,11.722651 9.39704957,12.3089667 10.5553177,13.4773237 Z"/>
<image id="image-5" href="image-to-be-inserted.jpg"/>
</defs>
<g fill="none" fill-rule="evenodd">
<mask id="w-circle-stroke-c" fill="#fff">
<use xlink:href="#w-circle-stroke-b"/>
</mask>
<g fill="#2D8EFF" mask="url(#w-circle-stroke-c)">
<rect width="24" height="24"/>
</g>
<use fill="url(#pattern-4)" xlink:href="#path-3"/>
<use fill="#000" filter="url(#filter-10)" xlink:href="#path-9"/>
</g>
</svg>
</div>
The issue is that you don't have to use $('.device') when inside and you need to refer to $(this) and since you have 2 each() nested you need to pay attention to the scope of this.
$('.device').each(function() {
//we get the needed id using $(this) that refer to actual device
var id = $(this).data('name');
//we check all the element with ID
$(this).find("path, rect, pattern, image, mask").each(function() {
//now (this) refer to the actual element
if($(this).attr("id"))
$(this).attr("id", id+"-"+$(this).attr("id"));
});
//we update the <use>
$(this).find("use").each(function() {
//now (this) refer to the actual element
$(this).attr("xlink:href","#"+id+"-"+$(this).attr("xlink:href").substring(1));
});
// grab data-screen value
var data = $(this).data('screen');
// replace this with the link inside svg
if (data != '') {
$(this).find("svg defs image").attr("href", data).attr("xlink:href", data);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="device" data-screen="[[IMAGE TO USE]]" data-name="[[UNIQUE ID]]">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="635" height="420" viewBox="0 0 635 420">
<defs>
<path id="path-1"/>
<rect id="path-3" />
<pattern id="pattern-4">
<use transform="scale(13.1875)" xlink:href="#image-5"/>
</pattern>
<image id="image-5" href="[[IMAGE LINK THAT IS INSERTED AFTER LOAD]]"/>
</defs>
<g fill="none" fill-rule="evenodd">
<rect fill="#fff" stroke="#2D8EFF" />
<g >
<mask id="mask-2" fill="#fff">
<use xlink:href="#path-1"/>
</mask>
<g mask="url(#mask-2)">
<mask id="mask-6" fill="#fff">
<use xlink:href="#path-3"/>
</mask>
</g>
</g>
</g>
</svg>
</div>

After dynamically adding element to SVG the item is not visible

I am creating a custom map that is drawn in an SVG. I want to add a polygon to this map,
however after adding the polygon it is not drawn. If I paste the complete page into an .html file and open it, it does show (http://peterelzinga.eu/map/test.html).
The code for adding the polygon to my SVG:
var svg = file_get_contents("18/135160/86183.svg");
var parser = new DOMParser();
var data = parser.parseFromString(svg, "text/xml");
data = data.firstChild;
console.log(data);
data.setAttribute ("x", d);
data.setAttribute ("y", e);
document.getElementById('front').appendChild(data);
The SVG element after adding the polygon:
<svg id="map" xmlns="http://www.w3.org/2000/svg" version="1.1" width="512" height="512">
<g id="back">
<image xlink:href="12/2110/1345.png" x="0" y="0" width="256" height="256"></image>
</g>
<g id="front">
<g width="256" height="256" x="0" y="0">
<polyline fill="#FFFFFF" stroke="#000000" stroke-miterlimit="10" points="256,85.333 209,100.667 230.334,158.334 143,194.334 160.667,248.667 221.667,223.667 256,241.334 " onclick="alert('St Jansdal')"></polyline>
</g>
</g>
</svg>
Does anyone know why this happens?
From Mozilla docs: https://developer.mozilla.org/en-US/docs/Web/API/DOMParser
You should specify the content type as "image/svg+xml" to get a SVGDocument.
The problem is that your generated nodes are not SVG nodes, but XML nodes.
I had a similar problem solved by switching from createElement to createElementNS. See answer: jquery's append not working with svg element?
Why the above won't work is still a mistery to me. However, i did manage to resolve the problem by using the following solution:
The javascript function to load and add an new svg element - which contains our polyline - to the main element:
var api = new XMLHttpRequest;
api.open("GET", a+"/"+b+"/"+c+".svg", false);
api.send("");
if( api.status == 200 ) {
var parser = new DOMParser();
var data = parser.parseFromString(api.responseText, "text/xml");
data = data.firstChild;
console.log(data);
data.setAttribute("x", d);
data.setAttribute("y", e);
document.getElementById('front').appendChild(data);
}
Now instead of only having the polyline element in the .svg file, I have put a complete svg element in the file:
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="256px" height="256px" viewBox="0 0 256 256" enable-background="new 0 0 256 256" xml:space="preserve">
<polyline fill="#FFFFFF" points="0,97 45,81.333 51.667,100.667 13.667,117.333 31,164 142.167,119.5 127.167,75.001
172.667,53.341 152.5,0 0,0 "/>
</svg>
And this does work. By setting the X and Y values of the I can position the svg in the correct spot so the polyline is drawn in the right place.
The endresult:
<svg id="map" xmlns="http://www.w3.org/2000/svg" version="1.1" width="512" height="512">
<g id="back">
<image xlink:href="18/135160/86183.png" x="0" y="0" width="256" height="256"></image>
<image xlink:href="18/135161/86183.png" x="256" y="0" width="256" height="256"></image>
<image xlink:href="18/135160/86184.png" x="0" y="256" width="256" height="256"></image>
<image xlink:href="18/135161/86184.png" x="256" y="256" width="256" height="256"></image>
</g>
<g id="front">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0" y="0" width="256px" height="256px" viewBox="0 0 256 256" enable-background="new 0 0 256 256" xml:space="preserve">
<polyline name="St. Jansdal" fill="#FFFFFF" points="256,84.75 209,100.75 230.75,157.5 142.5,194.25 160.75,248.5 221.75,223.75 256,241 "></polyline>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="256" y="0" width="256px" height="256px" viewBox="0 0 256 256" enable-background="new 0 0 256 256" xml:space="preserve">
<polyline name="St. Jansdal" fill="#FFFFFF" points="0,85.336 6.333,82.669 44,180.336 86,200.669 135.333,181.002 155.667,138.669 136.667,82.669 190,63.669 202,94.336 256,76.002 256,137.336 219.333,151.336 184.333,219.669 188.333,228.669 231.333,252.669 256,243.336 256,256 30,256 0,240.669 "></polyline>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="256" y="256" width="256px" height="256px" viewBox="0 0 256 256" enable-background="new 0 0 256 256" xml:space="preserve">
<polyline name="St. Jansdal" fill="#FFFFFF" points="31.25,0 78.5,24 82.75,29.75 82.75,34.75 81.5,40 74.5,52.5 0,80.75 0,134.25 64.25,110 87.25,120 120.75,207.5 133.5,212.25 133.5,206 139.25,199.75 150,197.75 154,198.75 162,202.25 165.25,191.5 122.5,76.25 129.5,61.75 135.25,55 138.75,53.25 141.5,53.25 144.5,53.75 235.25,93.75 239.5,103.75 256,97.75 256,0 "></polyline>
</svg>
</g>
</svg>

Categories

Resources