{"id":4822,"date":"2020-08-26T18:33:00","date_gmt":"2020-08-26T18:33:00","guid":{"rendered":"https:\/\/azoora.com\/blog\/?p=4822"},"modified":"2020-08-17T18:37:07","modified_gmt":"2020-08-17T18:37:07","slug":"svg-js-easy-creation-and-manipulation-of-svg-elements","status":"publish","type":"post","link":"https:\/\/azoora.com\/blog\/code\/svg-js-easy-creation-and-manipulation-of-svg-elements\/","title":{"rendered":"SVG.Js: Easy Creation And Manipulation Of SVG Elements"},"content":{"rendered":"\n<h2>Description:<\/h2>\n\n\n\n<p>SVG.js is a tiny JavaScript library that makes it easier to create and manipulate SVG elements within the document.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2>How to use it:<\/h2>\n\n\n\n<p>1. Download and load the SVG.js library in the document.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;script src=\".\/lib\/svg.js\"&gt;&lt;\/script&gt;<\/pre>\n\n\n\n<p>2. Create a new SVG object and insert it into an element you specify.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;div id=\"app\"&gt;&lt;\/div&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">const svg = SVG('#app');<\/pre>\n\n\n\n<p>3. Append new elements &amp; attributes to the SVG node. In this example, we\u2019re going to create an SVG square with a smooth scale animation.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">svg.$()\n  .attr('width', 500)\n  .attr('height', 500)\n  .append('g')\n  .attr('transform', 'scale(1, 1)')\n  .attr('transform', { type: 'scale', start: {x: 0, y: 0}, stop: {x: 5, y: 5}, duration: 2000, frequency: 100 })\n  .append('rect')\n  .attr('x', 0)\n  .attr('y', 0)\n  .attr('width', 50)\n  .attr('height', 50)<\/pre>\n\n\n\n<p>4. Manipulate the SVG element with the following API.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Returns a reference to this SVG object.\nSVG.noConflict();\n\n\/\/ Converts a SVG transform attributes string to an object.\nSVG.transformAttrToObj(transform);\n\n\/\/ Converts a SVG transform attributes string to an string.\nSVG.transformAttrToStr(transform);\n\n\/\/ Draws an arc path.\ndraw.getArc(startAngle, stopAngle, outerRadius, innerRadius);\n\n\/\/ Draws polyline paths\n\/\/ shape: a set of array of points (x, y) defining  the polygonal line\n\/\/ closed: true if it is a polygon\ndraw.getMultipolyline(shape, closed);<\/pre>\n\n\n\n<p>5. Public Chaining Methods.<\/p>\n\n\n\n<ul><li>firstParent: moves to the svg node<\/li><li>parent: moves to the parent<\/li><li>previous: selects the previous sibbling element<\/li><li>next: selects the next sibbling element<\/li><li>select: selects an SVG element<\/li><li>append: appends an SVG element and selects it<\/li><li>appendBefore: appends a new SVG el. before the reference SVG el.<\/li><li>appendAfter: appends a new SVG el. after the reference SVG el.<\/li><li>appendHTML: appends a foreignObject to svg and selects it<\/li><li>replace: replaces the current SVG element<\/li><li>remove: removes the given SVG element<\/li><li>removeChild: removes the passed-in child element<\/li><li>replaceChild: replaces a child by another<\/li><li>removeAllChilds: removes all the childs from the selected element<\/li><li>empty: removes all the childs from the selected element<\/li><li>listen: attaches an event listener to the SVG element<\/li><li>listenOnce: attaches a fired once event listener to the SVG element<\/li><li>unlisten: removes an event listener to the SVG element<\/li><li>alink: adds a link attribute to the SVG selected element<\/li><li>attr: adds attributes to the selected SVG element<\/li><li>rmattr: removes the given attribute from the selected SVG element<\/li><li>text: adds text to the selected SVG element<\/li><li>addClass: adds a class value to the selected SVG element<\/li><li>removeClass: removes a class value to the selected SVG element<\/li><li>toggleClass: toggles a class value to the selected SVG element<\/li><\/ul>\n\n\n\n<p>6. &nbsp;Non Chaining public methods.<\/p>\n\n\n\n<ul><li>query: returns the first matching element or null<\/li><li>getElement: returns the selected SVG element<\/li><li>children: returns the children HTMLCollection<\/li><li>getAttribute: returns the attribute value<\/li><li>getComputedStyle: returns the style applied to this element<\/li><li>getPropertyValue: returns the value of the specified property<\/li><li>getSize: returns the width and height of this element<\/li><li>getBbox: returns the bounding boxes<\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><strong>Author<\/strong>: jclo<\/p><p><strong>Official Page<\/strong>: <a href=\"https:\/\/github.com\/jclo\/svg\">Link<\/a><\/p><p><strong>Download<\/strong>: <a href=\"https:\/\/github.com\/jclo\/svg\/archive\/master.zip\">Link<\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Description: SVG.js is a tiny JavaScript library that makes it easier to create and manipulate SVG elements within the document.<\/p>\n","protected":false},"author":2,"featured_media":4823,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[4,145,12],"tags":[25,146,87,90],"jetpack_featured_media_url":"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/Easy-Creation-And-Manipulation-Of-SVG-Elements-SVG.js.png","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p7FQPL-1fM","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/4822"}],"collection":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/comments?post=4822"}],"version-history":[{"count":1,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/4822\/revisions"}],"predecessor-version":[{"id":4824,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/4822\/revisions\/4824"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media\/4823"}],"wp:attachment":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media?parent=4822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/categories?post=4822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/tags?post=4822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}