{"id":11896,"date":"2021-04-28T07:51:14","date_gmt":"2021-04-28T07:51:14","guid":{"rendered":"https:\/\/www.thecoderschool.com\/blog\/?p=11896"},"modified":"2022-10-13T20:49:52","modified_gmt":"2022-10-13T20:49:52","slug":"svgs","status":"publish","type":"post","link":"https:\/\/www.thecoderschool.com\/blog\/svgs\/","title":{"rendered":"SVGs"},"content":{"rendered":"<h2><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-11901\" src=\"https:\/\/www.thecoderschool.com\/blog\/wp-content\/uploads\/2021\/04\/SVG-300x297.png\" alt=\"\" width=\"300\" height=\"297\" srcset=\"https:\/\/www.thecoderschool.com\/blog\/wp-content\/uploads\/2021\/04\/SVG-300x297.png 300w, https:\/\/www.thecoderschool.com\/blog\/wp-content\/uploads\/2021\/04\/SVG-150x150.png 150w, https:\/\/www.thecoderschool.com\/blog\/wp-content\/uploads\/2021\/04\/SVG.png 674w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/h2>\n<p><strong data-rich-text-format-boundary=\"true\">By Daniel K., Age 14<\/strong><\/p>\n<h2><span style=\"font-weight: 400;\">In web design (and in all computer graphics), there are two types of images:<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Raster graphics and vector graphics. The difference between the two is that while raster graphics usually work better for images, they get pixelated or blotchy if you zoom in too much. However, vector graphics use math to display images. Although vector graphics don\u2019t work for images, they can make illustrations. However, the best part about vector graphics is that you can zoom in however much you want and have the image stay sharp. Websites use vector graphics in the form of &lt;svg&gt; tags.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">However, the best thing about SVGs in websites is that you can control how they are displayed. For instance, you could animate their colors with CSS or even change their appearance with Javascript.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To learn how you would go about doing that, we first need to understand a bit about SVGs. SVG files are a lot like HTML. If you open a .svg file in a text editor, you will see something that looks a lot like HTML code, but with some new tags. This will be useful later. There are two ways to add an SVG to your HTML: use an image tag that points to a .svg file, or embed the file straight in the HTML.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The first type of SVG is the simplest to implement, but offers none of the cool tricks I\u2019m about to show you. You can implement it by using an IMG tag with a source ending in .svg (<\/span><i><span style=\"font-weight: 400;\">&lt;img src=\u201dsomeDirectory\/example.svg\u201d&gt;<\/span><\/i><span style=\"font-weight: 400;\">).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The second type of SVG, however, takes the contents of the .svg file and copy-pastes them directly onto the HTML page. This works since SVGs are formatted a lot like HTML and almost all major browsers support SVGs. This way of adding SVGs to your webpage is much better as it allows you to directly modify the SVG data using Javascript.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">An SVG consists of an <\/span><i><span style=\"font-weight: 400;\">&lt;svg&gt;<\/span><\/i><span style=\"font-weight: 400;\"> tag, which contains all of the metadata for the SVG. This isn\u2019t very important for our purposes, so act as if that tag is a <\/span><i><span style=\"font-weight: 400;\">&lt;div&gt;<\/span><\/i><span style=\"font-weight: 400;\"> containing the actual drawing. Inside the <\/span><i><span style=\"font-weight: 400;\">&lt;svg&gt;<\/span><\/i><span style=\"font-weight: 400;\"> tag, you will find one or more <\/span><i><span style=\"font-weight: 400;\">&lt;g&gt;<\/span><\/i><span style=\"font-weight: 400;\"> tags. These tags mainly handle the layers for the SVG. You won\u2019t need those either, unless you\u2019re planning on transforming entire SVG layers with the <\/span><i><span style=\"font-weight: 400;\">transform=\u201dmatix()\u201d<\/span><\/i><span style=\"font-weight: 400;\"> attribute. The thing we\u2019re looking for is inside the <\/span><i><span style=\"font-weight: 400;\">&lt;g&gt;<\/span><\/i><span style=\"font-weight: 400;\"> tag. This is where the main graphics-drawing takes place.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Although you may see some <\/span><i><span style=\"font-weight: 400;\">&lt;rect&gt;<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><i><span style=\"font-weight: 400;\">&lt;ellipse&gt;<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><i><span style=\"font-weight: 400;\">&lt;polygon&gt;<\/span><\/i><span style=\"font-weight: 400;\">, and <\/span><i><span style=\"font-weight: 400;\">&lt;polyline&gt;<\/span><\/i><span style=\"font-weight: 400;\"> tags, most SVGs are made primarily out of <\/span><i><span style=\"font-weight: 400;\">&lt;path&gt;<\/span><\/i><span style=\"font-weight: 400;\"> tags. All drawing tags can contain a <\/span><i><span style=\"font-weight: 400;\">fill <\/span><\/i><span style=\"font-weight: 400;\">attribute, as well as several other attributes specific to drawing a shape. For instance, the <\/span><i><span style=\"font-weight: 400;\">&lt;line&gt;<\/span><\/i><span style=\"font-weight: 400;\"> tag has <\/span><i><span style=\"font-weight: 400;\">x1<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><i><span style=\"font-weight: 400;\">y1<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><i><span style=\"font-weight: 400;\">x2<\/span><\/i><span style=\"font-weight: 400;\">, and <\/span><i><span style=\"font-weight: 400;\">y2<\/span><\/i><span style=\"font-weight: 400;\"> attributes that determine the two edges of that line, while the <\/span><i><span style=\"font-weight: 400;\">&lt;path&gt;<\/span><\/i><span style=\"font-weight: 400;\"> tag has a <\/span><i><span style=\"font-weight: 400;\">\u201cd\u201d<\/span><\/i><span style=\"font-weight: 400;\"> attribute that dictates the shape drawn.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Now comes the fun part: modifying the SVG. Since each drawing tag in an SVG is like an HTML tag, we can add IDs to them. Then, when the user does something, we can access them through CSS and apply styling to the shapes as you would normally.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To do more complex things with the shape, for instance change it with JS, we can use the <\/span><i><span style=\"font-weight: 400;\">&lt;animate&gt;<\/span><\/i><span style=\"font-weight: 400;\"> tag. When an event is triggered, we can add the animate tag inside the drawing tag, like this:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">&lt;g&gt;<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">&lt;path d=\u201dsome value\u201d&gt;<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">&lt;<\/span><\/i><b><i>animate<\/i><\/b><i><span style=\"font-weight: 400;\">&gt;<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">&lt;\/path&gt;<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">&lt;\/g&gt;<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">We can then add an <\/span><i><span style=\"font-weight: 400;\">attributeName<\/span><\/i><span style=\"font-weight: 400;\"> attribute to the animate tag in order to specify the attribute that should be animated, in this case <\/span><i><span style=\"font-weight: 400;\">\u201cd\u201d<\/span><\/i><span style=\"font-weight: 400;\">. We can also give the animate tag a <\/span><i><span style=\"font-weight: 400;\">values<\/span><\/i><span style=\"font-weight: 400;\"> attribute, which specifies the attribute values to animate, separated by semicolons. In this case, we can specify <\/span><i><span style=\"font-weight: 400;\">\u201csome value;another value\u201d<\/span><\/i><span style=\"font-weight: 400;\"> since we want the path\u2019s <\/span><i><span style=\"font-weight: 400;\">d<\/span><\/i><span style=\"font-weight: 400;\"> attribute to animate from some value to another value. Specifying a <\/span><i><span style=\"font-weight: 400;\">dur<\/span><\/i><span style=\"font-weight: 400;\"> attribute for the animation allows you to customize the duration the animation lasts.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Congratulations, you can now animate SVGs. When making websites, you can apply this in interesting ways, such as making animated buttons or backgrounds.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>By Daniel K., Age 14 In web design (and in all computer graphics), there are two types of images: Raster graphics and vector graphics. The difference between the two is that while raster graphics usually work better for images, they get pixelated or blotchy if you zoom in too much. However, vector graphics use math &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.thecoderschool.com\/blog\/svgs\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;SVGs&#8221;<\/span><\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[69,65],"class_list":["post-11896","post","type-post","status-publish","format-standard","hentry","category-coder-blog","tag-student-showcase","tag-tips-tricks","entry"],"_links":{"self":[{"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/posts\/11896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/comments?post=11896"}],"version-history":[{"count":5,"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/posts\/11896\/revisions"}],"predecessor-version":[{"id":11904,"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/posts\/11896\/revisions\/11904"}],"wp:attachment":[{"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/media?parent=11896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/categories?post=11896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thecoderschool.com\/blog\/wp-json\/wp\/v2\/tags?post=11896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}