I want to do things with those elements using a JavaScript script, similarly as you would do in an HTML file. I created the following script:
console.log("I am alive!");
var circle = document.getElementById('circle');
circle.style.fill = "#0000ff"; // Make it blue.
console.log(circle);
and embedded it in the SVG file using Inkscape→File→Document properties→Scripting→External scripts→my_script.js. It prints I am alive! but then circle is null. Inspired by this link I also tried
console.log("I am alive!");
console.log(svgDocument.getElementById("circle"));
which prints Uncaught ReferenceError: svgDocument is not defined. Following this link I tried
console.log("I am alive!");
var svgObject = document.getElementById('svg-object').contentDocument;
var element = svgObject.getElementById('circle');
console.log(element);
and
console.log("I am alive!");
var svgObject = document.getElementById('svg5').contentDocument;
var element = svgObject.getElementById('circle');
console.log(element);
and
console.log("I am alive!");
var svgObject = document.getElementById('svg5');
var element = svgObject.getElementById('circle');
console.log(element);
but they all fail.
What is the way of writing this JavaScript such that when I open the SVG with a web browser this works?
I have a simple drawing I created using Inkscape, this is the Inkscape file:
and looks like this:
I want to do things with those elements using a JavaScript script, similarly as you would do in an HTML file. I created the following script:
and embedded it in the SVG file using Inkscape→File→Document properties→Scripting→External scripts→
my_script.js
. It printsI am alive!
but thencircle
isnull
. Inspired by this link I also triedwhich prints
Uncaught ReferenceError: svgDocument is not defined
. Following this link I triedand
and
but they all fail.
What is the way of writing this JavaScript such that when I open the SVG with a web browser this works?
There is an extension that may be useful: Extensions>Web>Javascript>Set Attributes.
I am very rusty with javascript ( I wasn't very good in the first place )
Javascript is event driven.
If the script is not linked to an event, -- or is being triggered before the svg has fully loaded it will not work.
I've added an onload to the <svg> tag of the attached svg. ( You could do this by attaching events handlers instead )
Also I have used CDATA, because svg rejects < characters in the code and crashes.
I can't remember the scope of javascript in an svg ( whether or no document.GetElementById applies to the parent html or not )
------------------------------------------
If you hit F5 to reload the document, each time the circle fills with randomly change.
Ahh seems like the Inkscape website will not allow me to attach an svg with a script in it.
https://www.raincloud.co.uk/test/StudioFibonacci-Cartoon-peacock_random_colours.svg
<script>
<![CDATA[
function randomColorCircles() {
circleCollection = document.getElementsByTagName("circle");
console.log(document)
for (var i=0; i < circleCollection.length; i++) {
randomRGB = `rgb(${(Math.floor(Math.random() * 256))}, ${(Math.floor(Math.random() * 256))}, ${(Math.floor(Math.random() * 256))})`;
circleCollection[i].style.fill = randomRGB;
}
}
]]>
</script>