top of page
  • Writer's pictureSteve

Is SVG animation file format the new Flash?

Updated: Nov 19, 2023

SVG files are similar to GIF and SWF files and yet offer much more functionality.

Here is a simple example of how you can embed an SVG animation image into an HTML page using the "object" tag, and provide a fallback image for older browsers:



  <object data="image.svg" type="image/svg+xml">
  <img src="fallback.png" alt="Fallback Image" />
  </object>
  

In this example, the "object" tag is used to embed the SVG image into the HTML page. The "data" attribute specifies the path to the SVG file, and the "type" attribute specifies the MIME type of the file.


For browsers that support SVG, the image will be displayed using the "object" tag. For older browsers that do not support SVG, a fallback image will be displayed instead, using the "img" tag. The "src" attribute specifies the path to the fallback image, and the "alt" attribute provides a text description of the image for accessibility purposes.


Note that the "object" tag has some advantages over the "img" tag when it comes to SVG. For example, the "object" tag allows you to add interactivity to the SVG image, and also allows you to style the SVG using CSS.



<svg>
  <use href="images/child.svg#shape" x="0" y="0" />
</svg>

In this example above, the "href" attribute of the <use> element specifies the relative path to the child SVG file, starting from the location of the parent SVG file. The "#shape" fragment identifier at the end of the href specifies which element in the child SVG file to use and is the actual ID within the parent SVG.


SVGs can be combine with other SVGs

SVG files can be included within other SVG files using the <use> element, which allows you to reuse and combine SVG elements across multiple files.


Here's an example of how to use the <use> element to import an SVG file into another SVG file:



<svg>
<defs>
<g id="shape"><circle cx="50"cy="50"r="40" /></g>
</defs>
<usehref="shapes.svg#shape"x="0"y="0" />
</svg>

In this example, the <defs> element defines a group with an ID of "shape" that contains a circle element. The <use> element then references the "shape" group in the external SVG file "shapes.svg" using the "href" attribute. The "x" and "y" attributes can be used to position the imported shape within the SVG file.


This technique allows you to reuse and combine SVG elements across multiple files, which can be useful for creating modular and reusable graphics.


It's worth noting that not all SVG editors support the <use> element or external SVG references, so you'll need to check your editor's documentation to see if this is possible.


onclick import of another SVG into a parent SVG

Technically, it is possible to use JavaScript to dynamically load and insert an SVG file into the DOM of an HTML page when an event occurs, such as a click event. This can be done using the XMLHttpRequest (XHR) object in JavaScript to load the SVG file as text, and then parsing the text into an SVG element using the DOMParser object.


Here's an example of how to dynamically load and insert an SVG file into the DOM using JavaScript:


<button onclick="loadSVG()">Load SVG</button>

<div id="svg-container"></div>

<script>
function loadSVG() {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', 'image.svg', true);
  xhr.onload = function() {
    if (xhr.status === 200) {
      var parser = new DOMParser();
      var svg = parser.parseFromString(xhr.responseText, 'image/svg+xml');
      document.getElementById('svg-container').appendChild(svg.documentElement);
    }
  };
  xhr.send();
}
</script>

In this example, the JavaScript function "loadSVG()" is called when the button is clicked. The function creates a new XHR object and opens a GET request to the "image.svg" file. When the response is received, the function uses a DOMParser object to parse the SVG text into an SVG element, and then appends the SVG element to the "svg-container" div in the HTML page.


While this technique can be useful for dynamically loading SVG files into an HTML page, it's worth noting that it can also have performance implications, especially for larger or more complex SVG files. Additionally, some web browsers may have security restrictions that prevent cross-domain XHR requests, which can limit the use of this technique in certain situations.


Create a single SVG file that contains all of your icons and graphics

you can create a single SVG file that contains all of your icons and graphics, and then use the <use> element to selectively include and display specific elements within that SVG file.


To do this, you would need to give each icon or graphic within the SVG file a unique ID attribute. For example, you could define a set of icons within a single <symbol> element, and give each icon a unique ID attribute:


<svg> <defs> <symbol id="icon1"> <!-- Icon 1 definition goes here --> </symbol> <symbol id="icon2"> <!-- Icon 2 definition goes here --> </symbol> <symbol id="icon3"> <!-- Icon 3 definition goes here --> </symbol> </defs> </svg>

In this example, the <symbol> element is used to define a set of icons within the <defs> element of the SVG file. Each icon is given a unique ID attribute ("icon1", "icon2", etc.) that can be used to reference the icon later.


You can then use the <use> element to reference a specific icon within the SVG file and display it within your HTML page:



<svg>
  <use href="icons.svg#icon1" x="0" y="0" />
</svg>

In this example, the <use> element references the "icon1" symbol within the "icons.svg" file, and uses the "x" and "y" attributes to position the icon within the SVG canvas.


By using this technique, you can create a single SVG file that contains all of your icons and graphics, and then selectively include and display specific elements within that file using the <use> element and unique ID attributes.












5 views0 comments
bottom of page