📜 ⬆️ ⬇️

Cross-browser SVG

I continue a series of topics about SVG. Vector SVG elements are correctly displayed in modern browsers, but how to implement support in older versions? We will understand the vector cross-browser compatibility.

image

Previous topics about SVG:


')

Using the object element


If graphics are stored in the .svg file, you can use the object element on the page:

<object data='images/apple.svg'></object> 


When using the following construction, browsers that support SVG will display vector elements correctly, the rest will show the .png file ( Demonstration ):

 <object data='images/apple.svg'> <img src='images/apple.png'/> </object> 




The main disadvantage of this method is the non-scalability of PNG images.

Modernizr


This method uses the Modernizr.js and Raphael.js JavaScript libraries. First of all, you need to convert SVG-files into a format that is supported by Raphael. The ReadySetRaphael.js tool will help us with this . Connecting Modernizr.js:

 <script type="text/javascript" src="scripts/modernizr.js"></script> 


Then, in the case of a non-supporting browser, you need to connect Raphael.js, as well as the svg.js file, which turned out after conversion.

 if (!Modernizr.inlinesvg) { document.write( '<script type="text/javascript" src="scripts/raphael.js"><\/script>', '<script type="text/javascript" src="scripts/svg.js"><\/script>' ); } 


Now you just add the HTML markup. In the code below, the SVG file is included directly in the HTML code of the page in order to make Raphael work:

 <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="500px" height="280px" viewBox="0 0 500 280" enable-background="new 0 0 500 280" xml:space="preserve"> <path fill="#333333" d="M296.908,120.622c-8.77,6.201-13.158,13.676-13.158,22.41c0,10.458,5.425,18.479,16.262,24.076 c-2.908,8.435-7.122,15.764-12.65,22.009c-5.516,6.243-10.553,9.368-15.11,9.368c-2.147,0-5.075-0.718-8.794-2.133l-1.782-0.687 c-3.646-1.416-6.854-2.133-9.656-2.133c-2.641,0-5.535,0.555-8.679,1.665l-2.237,0.807l-2.818,1.154 c-2.218,0.884-4.468,1.326-6.725,1.326c-5.328,0-11.208-4.387-17.642-13.161c-9.273-12.567-13.905-26.264-13.905-41.085 c0-10.538,2.886-19.02,8.678-25.46c5.78-6.432,13.446-9.658,22.979-9.658c3.566,0,6.897,0.653,10,1.958l2.129,0.865l2.238,0.92 c1.992,0.84,3.601,1.264,4.825,1.264c1.569,0,3.316-0.364,5.231-1.094l2.929-1.151l2.19-0.804c3.483-1.262,7.34-1.896,11.555-1.896 C282.777,109.183,290.814,112.996,296.908,120.622z M273.238,82.575c0.108,1.344,0.167,2.378,0.167,3.102 c0,6.628-2.412,12.442-7.237,17.443c-4.823,5-10.438,7.494-16.837,7.494c-0.189-1.493-0.29-2.563-0.29-3.212 c0-5.635,2.239-10.924,6.726-15.864c4.482-4.939,9.671-7.838,15.575-8.678C271.754,82.787,272.395,82.696,273.238,82.575z"/> </svg> <div id="applelogo"></div> 


Recalling the previous article , add text under the SVG file:

 <text x="210" y="250">This is SVG</text> 


Here's what happened ( Demonstration ):



Svgweb


Another tool for implementing SVG support for older browsers is the SVGWeb JavaScript library. After connecting it to the page, you can position the SVG elements like this:

 <script type="image/svg+xml"> <svg ...>...</svg> </script> 


In XHTML you must use CDATA:

 <script type="image/svg+xml"><![CDATA[ <svg ...>...</svg> ]]></script> 


Conclusion


The described examples of cross-browser SVG graphics are not ideal, however, they are suitable as a solution for most problems with the correct display of vector graphics in older browsers.

Used materials:

Source: https://habr.com/ru/post/159947/


All Articles