<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ola B</title>
    <description>The latest articles on DEV Community by Ola B (@vakme).</description>
    <link>https://dev.to/vakme</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F216661%2Fd3182017-b0fd-45db-aa51-8aa3e5758225.png</url>
      <title>DEV Community: Ola B</title>
      <link>https://dev.to/vakme</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vakme"/>
    <language>en</language>
    <item>
      <title>Know Your WebDev Math: Translating coordinates with transformation matrix</title>
      <dc:creator>Ola B</dc:creator>
      <pubDate>Wed, 10 Jun 2026 20:01:48 +0000</pubDate>
      <link>https://dev.to/vakme/know-your-webdev-math-translating-coordinates-with-transformation-matrix-17e9</link>
      <guid>https://dev.to/vakme/know-your-webdev-math-translating-coordinates-with-transformation-matrix-17e9</guid>
      <description>&lt;p&gt;When I needed to pass Calculus and Algebra during my Computer Science degree, I used the formula known to every Polish student called 'ZZZ' (Zakuć, Zdać, Zapomnieć) - Cram, Pass, Forget. It means &lt;em&gt;memorizing just enough to pass the exam and then not caring if you'll remember any of it later&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Fast forward a few years into my web developer career and I needed to implement a &lt;code&gt;mouseMove&lt;/code&gt; event on the SVG element. Every move I made with my mouse made the element's position shoot off into the void. I solved this issue using the transformation matrix. &lt;/p&gt;

&lt;p&gt;The more I worked with SVGs and graphs, the more I realized that the math I tried to cram and forget shows up in everything that I do. From the coordinates to graph layouts, drag-and-drop to animations - we just don't call it 'math'. &lt;/p&gt;

&lt;p&gt;This series attempts to show you that math. Each article takes one concept (that we had to cram in class) and shows exactly where it lives in real frontend code, with real examples I've built in production or in side projects. I don't want to show you mathematical proofs, I will explain a bit of theory with a demonstration how to use it when &lt;strong&gt;you&lt;/strong&gt; are the one who has to position elements just the right way, resolve conflicts and overlaps or draw a line from one point to another.&lt;/p&gt;

&lt;p&gt;Welcome to &lt;strong&gt;Know Your WebDev Math&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Part 2: Translating coordinates with transformation matrix
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This is the problemI already hinted at in the intro to the Know Your WebDev Math series. No big requirements this time - just trying to build drag-and-drop of SVG elements inside a viewport. &lt;/p&gt;

&lt;p&gt;If you try to pass coordinates directly from drag-and-drop mouse event to SVG, this will happen: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6cjk180quuxsd7xpww32.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6cjk180quuxsd7xpww32.gif" alt=" " width="800" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Points will be moved seemingly independently from the moves of the mouse cursor.&lt;/p&gt;

&lt;p&gt;And how we want it to look like? I believe like this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2b1pg7zd37vp5xrdjbbq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2b1pg7zd37vp5xrdjbbq.gif" alt=" " width="800" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is the difference between the first example and the second? &lt;strong&gt;A single line of code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;matrixTransform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inverse&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in this article, I will explain, why.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution: Transformation matrices
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Theory: what is a transformation matrix?
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;(Last time we talked about triangles. Today's starting topic: matrices)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A matrix is a mathematical object that structures numbers (or other objects) in rows and columns. Matrices (especially square matrices) have multiple usages in algebra, numerical analysis and geometry, thanks to their properties. One of matrices used in geometry is a &lt;strong&gt;transformation matrix&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A transformation matrix is a structure that describes how to move, scale, rotate, or skew points in space. In the context of 2D graphics (which is where we live as web developers working with SVGs), it's a 3×3 matrix (3 rows, 3 columns) where third row is always &lt;code&gt;[0, 0, 1]&lt;/code&gt;. The remaining 6 values - &lt;code&gt;[a, b, c, d, e, f]&lt;/code&gt; - describe all possible 2D transformations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;| a  c  e |
| b  d  f |
| 0  0  1 |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;a&lt;/code&gt; and &lt;code&gt;d&lt;/code&gt; handle &lt;strong&gt;scaling&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; handle &lt;strong&gt;rotation and skewing&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;e&lt;/code&gt; and &lt;code&gt;f&lt;/code&gt; handle &lt;strong&gt;translation&lt;/strong&gt; (moving)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You don't need to memorize this. What matters is understanding that this matrix encodes the transformation necessary, to move the point from one system of coordinates to another. In our case: the coordinate system of the screen, and the coordinate system inside the SVG viewport.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want to go deeper, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Matrix_math_for_the_web" rel="noopener noreferrer"&gt;MDN's Matrix math for WebGL&lt;/a&gt; and the &lt;a href="https://docs.aspose.com/svg/net/drawing-basics/transformation-matrix/" rel="noopener noreferrer"&gt;Aspose SVG transformation docs&lt;/a&gt; both have excellent explanations.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Another special type of matrix is &lt;strong&gt;identity matrix&lt;/strong&gt;. This is a matrix that has 1 on the diagonal and 0 everywhere else.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;| 1  0  0 |
| 0  1  0 |
| 0  0  1 |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we use an identity matrix as a transformation matrix - nothing will happen. Nic. Zilch. Nada.&lt;/p&gt;

&lt;h3&gt;
  
  
  Working with points and matrices in JavaScript
&lt;/h3&gt;

&lt;p&gt;The browser exposes two classes from the &lt;a href="https://www.w3.org/TR/geometry-1/" rel="noopener noreferrer"&gt;Geometry Interfaces Module&lt;/a&gt; that we'll need:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;DOMPoint&lt;/code&gt;&lt;/strong&gt; - represents a 2D (or 3D) point:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DOMPoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;DOMMatrix&lt;/code&gt;&lt;/strong&gt; - represents a 2D or 3D transformation matrix. You can create one manually, but in practice you'll get it from the browser (more on that in a moment):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DOMMatrix&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The useful part is that &lt;code&gt;DOMPoint&lt;/code&gt; has a method called &lt;code&gt;matrixTransform&lt;/code&gt; - it applies a given matrix to the point and returns the transformed coordinates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;transformedPoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;point&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;matrixTransform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These two classes allow us to represent points and matrices - mathematical structures - in JS environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  The SVG viewport and &lt;code&gt;viewBox&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Here's where the coordinate problem actually comes from.&lt;/p&gt;

&lt;p&gt;An SVG element lives in the browser page and has a size in screen pixels, let's say &lt;code&gt;600×600px&lt;/code&gt;. But the SVG also has its own internal coordinate system, defined by the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/viewBox" rel="noopener noreferrer"&gt;&lt;code&gt;viewBox&lt;/code&gt; attribute&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;svg&lt;/span&gt; &lt;span class="na"&gt;viewBox=&lt;/span&gt;&lt;span class="s"&gt;"0 0 1000 1000"&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"600"&lt;/span&gt; &lt;span class="na"&gt;height=&lt;/span&gt;&lt;span class="s"&gt;"600"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells the SVG: "my internal coordinate system goes from 0 to 1000 in both x and y, but render it into a 600×600 pixel box on screen." Every point you draw at &lt;code&gt;(500, 500)&lt;/code&gt; in SVG coordinates will appear at &lt;code&gt;(300, 300)&lt;/code&gt; screen pixels - because 500 out of 1000 = 50%, and 50% of 600 is 300.&lt;/p&gt;

&lt;p&gt;Now here's the problem: when your mouse fires a &lt;code&gt;mousemove&lt;/code&gt; event, it gives you &lt;strong&gt;screen coordinates&lt;/strong&gt; - pixels relative to the top-left of the browser viewport. If you naively pass those into your SVG element's position, you try to apply external coordinates to internal system, and things can get 'jumpy'.&lt;/p&gt;

&lt;p&gt;The fix is to &lt;strong&gt;convert from screen coordinates into SVG coordinates&lt;/strong&gt;. And that's exactly what the transformation matrix is for.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;getScreenCTM()&lt;/code&gt; - getting the matrix
&lt;/h3&gt;

&lt;p&gt;We don't need to guess how the transformation matrix should look - SVG elements expose a method called &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/SVGGraphicsElement/getScreenCTM" rel="noopener noreferrer"&gt;&lt;code&gt;getScreenCTM()&lt;/code&gt;&lt;/a&gt; (CTM stands for &lt;strong&gt;Current Transformation Matrix&lt;/strong&gt;). It returns an object of the &lt;code&gt;DOMMatrix&lt;/code&gt; class that describes the transformation from the SVG's coordinate system to the screen's coordinate system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;svgElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getScreenCTM&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think of it like this: &lt;code&gt;m&lt;/code&gt; knows how to go &lt;strong&gt;from SVG to screen&lt;/strong&gt;. Apply &lt;code&gt;m&lt;/code&gt; to an SVG point, and you get where it appears on screen.&lt;/p&gt;

&lt;p&gt;But we want to go the other way, &lt;strong&gt;from screen to SVG&lt;/strong&gt;. We have a mouse position in screen coordinates, and we need the equivalent SVG coordinates.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inverting the matrix - reversing the translation
&lt;/h3&gt;

&lt;p&gt;To go in the opposite direction, we need to invert the matrix. Inverting a transformation matrix produces a new matrix that undoes exactly what the original does.&lt;/p&gt;

&lt;p&gt;Mathematically, if A is our matrix and A&lt;sup&gt;-1&lt;/sup&gt; is inverted matrix A, multiplying them both will give us &lt;strong&gt;identity matrix&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;And I remember that because I had to retake the test on inverting matrices during algebra course in uni.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So our &lt;code&gt;m&lt;/code&gt; matrix will transform from SVG to screen, and we need to find &lt;code&gt;mInversed&lt;/code&gt; that will transform from screen to SVG.&lt;/p&gt;

&lt;p&gt;Fortunately for me and my algebra skills, &lt;code&gt;DOMMatrix&lt;/code&gt; gives us this for free:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mInverse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inverse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that brings us back to the one-liner from the intro:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;matrixTransform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inverse&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We take the point &lt;code&gt;p&lt;/code&gt; (in screen coordinates from the mouse event), apply the inverted CTM, and get back the correct position in SVG coordinates. &lt;/p&gt;

&lt;h2&gt;
  
  
  Calculations
&lt;/h2&gt;

&lt;p&gt;Let's walk through exactly what happens in the final code. It's a Vue 3 app with an SVG element that has draggable endpoints.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up the SVG and drag events
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;svg&lt;/span&gt;
  &lt;span class="na"&gt;viewBox=&lt;/span&gt;&lt;span class="s"&gt;"0 0 1000 1000"&lt;/span&gt;
  &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;pointerMove=&lt;/span&gt;&lt;span class="s"&gt;"onDrag($event)"&lt;/span&gt;
  &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;pointerUp=&lt;/span&gt;&lt;span class="s"&gt;"onDragStop()"&lt;/span&gt;
  &lt;span class="na"&gt;ref=&lt;/span&gt;&lt;span class="s"&gt;"container"&lt;/span&gt;
&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;circle&lt;/span&gt;
    &lt;span class="na"&gt;r=&lt;/span&gt;&lt;span class="s"&gt;"4"&lt;/span&gt;
    &lt;span class="na"&gt;:cx=&lt;/span&gt;&lt;span class="s"&gt;"start.x"&lt;/span&gt;
    &lt;span class="na"&gt;:cy=&lt;/span&gt;&lt;span class="s"&gt;"start.y"&lt;/span&gt;
    &lt;span class="na"&gt;fill=&lt;/span&gt;&lt;span class="s"&gt;"purple"&lt;/span&gt;
    &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;pointerDown=&lt;/span&gt;&lt;span class="s"&gt;"onDragStart('start')"&lt;/span&gt;
    &lt;span class="na"&gt;ref=&lt;/span&gt;&lt;span class="s"&gt;"start"&lt;/span&gt;
  &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;circle&lt;/span&gt;
    &lt;span class="na"&gt;r=&lt;/span&gt;&lt;span class="s"&gt;"4"&lt;/span&gt;
    &lt;span class="na"&gt;:cx=&lt;/span&gt;&lt;span class="s"&gt;"end.x"&lt;/span&gt;
    &lt;span class="na"&gt;:cy=&lt;/span&gt;&lt;span class="s"&gt;"end.y"&lt;/span&gt;
    &lt;span class="na"&gt;fill=&lt;/span&gt;&lt;span class="s"&gt;"purple"&lt;/span&gt;
    &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;pointerDown=&lt;/span&gt;&lt;span class="s"&gt;"onDragStart('end')"&lt;/span&gt;
    &lt;span class="na"&gt;ref=&lt;/span&gt;&lt;span class="s"&gt;"end"&lt;/span&gt;
  &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/svg&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;pointerMove&lt;/code&gt; is on the SVG itself (not the circles) - this way we don't lose tracking if the mouse moves faster than the element. The &lt;code&gt;ref&lt;/code&gt; attributes give us direct access to the DOM nodes, which we'll need to call &lt;code&gt;getScreenCTM()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The drag handler
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;onDrag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dragging&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DOMPoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientY&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dragging&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$refs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getScreenCTM&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;matrixTransform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inverse&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dragging&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;end&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$refs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getScreenCTM&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;matrixTransform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inverse&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;new DOMPoint(e.clientX, e.clientY)&lt;/code&gt; - wrap the mouse's screen coordinates in a &lt;code&gt;DOMPoint&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this.$refs.start.getScreenCTM()&lt;/code&gt; - get the transformation matrix of the dragged element. This gives us the full picture of how the SVG is scaled, translated, and positioned relative to the screen.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;m.inverse()&lt;/code&gt; - inverting the matrix - now the matrix translates screen → SVG.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;p.matrixTransform(m.inverse())&lt;/code&gt; - apply the inverted matrix to our screen-space point, getting the equivalent SVG-space coordinates.&lt;/li&gt;
&lt;li&gt;Update the reactive data, and Vue re-renders the circle at the correct position.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Notice that we call &lt;code&gt;getScreenCTM()&lt;/code&gt; on the &lt;strong&gt;circle element&lt;/strong&gt;, not the SVG root. This is intentional - it accounts for any additional transforms applied to the element itself (rotation, scaling, nesting inside a &lt;code&gt;&amp;lt;g&amp;gt;&lt;/code&gt; with a transform, etc.). The matrix you get is cumulative for the full chain from that element to the screen.&lt;/p&gt;

&lt;p&gt;You can play with the full example here:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/vakme/embed/NPdNeyK?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;p&gt;With these ~3 lines of setup, you get perfectly tracked drag-and-drop in any SVG, regardless of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how the SVG is scaled or positioned on the page&lt;/li&gt;
&lt;li&gt;whether the &lt;code&gt;viewBox&lt;/code&gt; matches the rendered pixel size&lt;/li&gt;
&lt;li&gt;whether the SVG is inside a scrolled container&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The matrix handles all of this for you. &lt;/p&gt;

&lt;p&gt;One thing I want to highlight about Screen CTM: notice that the math itself is invisible. You're not computing scale factors manually, not applying CSS transforms, not doing trigonometry. You just ask the DOM for the matrix it's already maintaining internally, invert it, and apply it. The browser has been tracking this all along and &lt;code&gt;getScreenCTM()&lt;/code&gt; just allows you to use the existing transformation. The geometry is already there. You just need to know that &lt;strong&gt;you can use it and what to use it for&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is why we need math in programming - even if I failed my first matrix inversion test, I knew that I could use matrix to transform a point position and I could start to look for matrixes in JS.&lt;/p&gt;

&lt;p&gt;Next up: Bézier curves — the math behind every smooth path in CSS, SVG, and canvas, and why those control point handles in your design tools are doing exactly what the formula says.&lt;/p&gt;

&lt;p&gt;If you've run into SVG nightmares of your own (and I know you have), drop them in the comments!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>vue</category>
      <category>svg</category>
    </item>
    <item>
      <title>Know Your WebDev Math: Dynamic positioning with trigonometry</title>
      <dc:creator>Ola B</dc:creator>
      <pubDate>Fri, 29 May 2026 14:09:33 +0000</pubDate>
      <link>https://dev.to/vakme/know-your-webdev-math-dynamic-positioning-with-trigonometry-2co6</link>
      <guid>https://dev.to/vakme/know-your-webdev-math-dynamic-positioning-with-trigonometry-2co6</guid>
      <description>&lt;p&gt;When I needed to pass Calculus and Algebra during my Computer Science degree, I used the formula known to every Polish student called 'ZZZ' (Zakuć, Zdać, Zapomnieć) - Cram, Pass, Forget. It means &lt;em&gt;memorizing just enough to pass the exam and then not caring if you'll remember any of it later&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Fast forward a few years into my web developer career and I needed to implement a &lt;code&gt;mouseMove&lt;/code&gt; event on the SVG element. Every move I made with my mouse made the element's position shoot off into the void. I solved this issue using the transformation matrix. &lt;/p&gt;

&lt;p&gt;The more I worked with SVGs and graphs, the more I realized that the math I tried to cram and forget shows up in everything that I do. From the coordinates to graph layouts, drag-and-drop to animations - we just don't call it 'math'. &lt;/p&gt;

&lt;p&gt;This series attempts to show you that math. Each article takes one concept (that we had to cram in class) and shows exactly where it lives in real frontend code, with real examples I've built in production or in side projects. I don't want to show you mathematical proofs, I will explain a bit of theory with a demonstration how to use it when &lt;strong&gt;you&lt;/strong&gt; are the one who has to position elements just the right way, resolve conflicts and overlaps or draw a line from one point to another.&lt;/p&gt;

&lt;p&gt;Welcome to &lt;strong&gt;Know Your WebDev Math&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Part 1: Dynamic positioning with trigonometry
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I play a ton of DnD. And in my longest and favourite campaign (Curse of Strahd) the relations within the party and between the party and important NPCs (Ireena) are extremely important. We keep an Excel spreadsheet describing the current relationship status (from 'love' to 'hate' to 'it's complicated'). And I had an idea of creating an interactive diagram of those relations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Requirements
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Every character avatar and name needs to be visible. &lt;/li&gt;
&lt;li&gt;Every character needs to be connected to every other character.&lt;/li&gt;
&lt;li&gt;The color of the connection symbolizes the relationship status.&lt;/li&gt;
&lt;li&gt;Hovering over a character's avatar should show description of what they think of others and what others think of them.&lt;/li&gt;
&lt;li&gt;The number of characters is not fixed.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Analysis
&lt;/h3&gt;

&lt;p&gt;Graph is the best structure to show elements and their relations, so I decided to implement my diagram in React Flow.&lt;br&gt;
To ensure good readability of the graph I decided to use circular graph layout:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F41j9ho1fl8yu7q59v9rn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F41j9ho1fl8yu7q59v9rn.png" alt="Circular graph layout" width="321" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It means that each character avatar should be positioned on the edge of the circle.&lt;br&gt;
Only one question remained: &lt;strong&gt;How to dynamically position elements in the circle?&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Solution: Trigonometry
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Theory: sine and cosine
&lt;/h3&gt;

&lt;p&gt;This is a right-angled triangle.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs1n6ei77ycftsv83bdbh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs1n6ei77ycftsv83bdbh.png" alt="Right angled triangle" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Bear with me for a moment, I know you know what a triangle is. All of this will matter in a second)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It has 3 sides: opposite, adjacent and hypotenuse. It also has an acute angle, let's call this angle α.&lt;/p&gt;

&lt;p&gt;We can calculate sine of α - sin(α) - by dividing the length of the opposite side by the length of hypotenuse side: &lt;code&gt;sin(α) = opposite / hypotenuse&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Same with cosine of α - cos(α) - we need to divide the length of the adjacent side by the length of the hypotenuse side: &lt;code&gt;cos(α) = adjacent / hypotenuse&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Sine and cosine are ratios, which means they stay exactly the same for every specific angle value. The sine of 30 degree angle is always 0.5 - it doesn't matter if the opposite side of a triangle has a length of 2 or 200.&lt;/p&gt;
&lt;h3&gt;
  
  
  How is it going to help us?
&lt;/h3&gt;

&lt;p&gt;We want to position elements on the edge of the circle in equal distances from each other - we need to calculate x and y position of the centre of each element.&lt;/p&gt;

&lt;p&gt;Let's go back to our circular graph. If we draw the line from the centre of the graph to the centre of one of the nodes, we can create a right-angled triangle in which the lengths of adjacent and opposite sides are our x and y, respectively.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqib6qzcu9hpr9nrer9xn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqib6qzcu9hpr9nrer9xn.png" alt="Circular graph layout with a triangle" width="321" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We need to find the lengths of sides in our triangle. And we can do that using sine and cosine.&lt;/p&gt;
&lt;h3&gt;
  
  
  Calculations
&lt;/h3&gt;

&lt;p&gt;The easiest side is hypotenuse - it's the radius of our layout circle, let's call this variable &lt;code&gt;radius&lt;/code&gt;. It can be arbitrary, just adjust it to the size of your nodes. &lt;br&gt;
The center of the graph (point &lt;code&gt;C&lt;/code&gt;) can also be arbitrary, let's assume variable &lt;code&gt;c&lt;/code&gt; of type &lt;code&gt;Point&lt;/code&gt;: &lt;code&gt;type Point = {x: number; y: number}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To calculate the value of x - adjuscent side of triangle - let's change the cosine equation: &lt;br&gt;
&lt;code&gt;adjacent = cos(α) * hypotenuse&lt;/code&gt;.&lt;br&gt;
And to calculate the value of y - opposite side - let's change the sine equation the same way:&lt;br&gt;
&lt;code&gt;opposite = sin(α) * hypotenuse&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can calculate the sine and cosine values for specific angle α using functions from JavaScript Math library: &lt;code&gt;Math.sin&lt;/code&gt; and &lt;code&gt;Math.cos&lt;/code&gt;. Those functions take radian angle value as parameter. &lt;/p&gt;
&lt;h4&gt;
  
  
  So how can we calculate radian value of α?
&lt;/h4&gt;

&lt;p&gt;We know that our graph is going to have &lt;code&gt;n&lt;/code&gt; elements. And radian value of 360 degree angle is 2π.&lt;/p&gt;

&lt;p&gt;So our α is: &lt;code&gt;α = 2π / n&lt;/code&gt; or in JS: &lt;code&gt;const alpha = (2 * Math.PI) / n&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  Final calculation
&lt;/h4&gt;

&lt;p&gt;Now, we need to iterate over each node and calculate its position (adjusting for center point &lt;code&gt;c&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updatedNodes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;angle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;alpha&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;angle&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;angle&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;};&lt;/span&gt;
     &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nodes above are graph nodes from React Flow, so if I'll set &lt;code&gt;position&lt;/code&gt; property in the node object, React Flow will automatically place them for me. &lt;/p&gt;

&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;p&gt;Behold, an example with Baldur's Gate 3 characters!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3trijhxa9crzvlnvklyc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3trijhxa9crzvlnvklyc.png" alt="Example" width="800" height="594"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Additionally, you can adjust your &lt;code&gt;radius&lt;/code&gt; variable based on the number of nodes in the graph to have fully dynamic graph, growing with the number of characters you want to have in your D&amp;amp;D (or DaggerHeart, or Call of Cthulhu or any other system) session. &lt;/p&gt;

&lt;p&gt;I decided to start &lt;strong&gt;Know Your WebDev Math&lt;/strong&gt; series with simple trigonometry, because sine and cosine are, for me, the symbols of 'that stupid math I won't ever use in my life' approach. Turns out, trigonometry was just waiting for me to build something worthy using it on.&lt;/p&gt;

&lt;p&gt;If you play D&amp;amp;D or have a graph layout problem, drop it in the comments!&lt;/p&gt;

&lt;p&gt;Next up: the tower of Babel of different coordinate systems, and how transformation matrix acts as a translator.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>reactflow</category>
      <category>react</category>
    </item>
    <item>
      <title>Create elegant OpenAPI spec documentation with Rapi Doc and Vitepress</title>
      <dc:creator>Ola B</dc:creator>
      <pubDate>Sat, 23 Nov 2024 17:32:44 +0000</pubDate>
      <link>https://dev.to/vakme/create-elegant-openapi-spec-documentation-with-rapi-doc-and-vitepress-21pk</link>
      <guid>https://dev.to/vakme/create-elegant-openapi-spec-documentation-with-rapi-doc-and-vitepress-21pk</guid>
      <description>&lt;p&gt;I recently had to create a documentation page supporting OpenAPI spec documentation. What's an OpenAPI spec documentation? A page, either self-hosted or included in your API management platform, that allows users to check what endpoints, methods, webhooks, etc., are available based on OpenAPI JSON or YAML.&lt;/p&gt;

&lt;p&gt;I needed to find a balance between needing as many customization options as possible and using ready-to-go tools for quick setup and deployment.&lt;/p&gt;

&lt;p&gt;And I found &lt;a href="https://rapidocweb.com/index.html" rel="noopener noreferrer"&gt;Rapi Doc&lt;/a&gt; - a web component that can be embedded anywhere.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxdkus496pfeb1ppbxiaj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxdkus496pfeb1ppbxiaj.png" alt=" " width="800" height="531"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the component ready, I needed a tool to write documentation that supported custom components.&lt;/p&gt;

&lt;p&gt;So I chose &lt;a href="https://vitepress.dev/" rel="noopener noreferrer"&gt;Vitepress&lt;/a&gt;. And I had two tools that I wanted to merge. How did it go? Let's find out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running app in dev mode
&lt;/h2&gt;

&lt;p&gt;I'll skip the story of Vitepress setup - you can find the instructions on their main page.&lt;/p&gt;

&lt;p&gt;I also created a custom RapiDoc.vue component where I embedded my &lt;code&gt;rapi-doc&lt;/code&gt; web component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt; &lt;span class="na"&gt;setup&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rapidoc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;rapi-doc&lt;/span&gt;
      &lt;span class="na"&gt;spec-url = &lt;/span&gt;&lt;span class="s"&gt;"https://petstore.swagger.io/v2/swagger.json"&lt;/span&gt;
      &lt;span class="na"&gt;render-style = &lt;/span&gt;&lt;span class="s"&gt;"read"&lt;/span&gt;
      &lt;span class="na"&gt;style = &lt;/span&gt;&lt;span class="s"&gt;"height:100vh; width:100%"&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;/rapi-doc&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;style&lt;/span&gt; &lt;span class="na"&gt;scoped&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;style&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I also embedded this custom component in a &lt;code&gt;api-docs.md&lt;/code&gt; page &lt;em&gt;(yes, you can embed Vue Components in Markdown, I love Vitepress for it!)&lt;/em&gt; so I could see it in my Vitepress documentation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
sidebar: false
layout: page
---

&amp;lt;script setup&amp;gt;
import RapiDoc from './components/RapiDoc.vue';
&amp;lt;/script&amp;gt;

&amp;lt;RapiDoc /&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I ran &lt;code&gt;yarn docs:dev&lt;/code&gt; expecting everything to go smoothly (I followed the instructions from both documentations, so it should be fine, right?)...&lt;/p&gt;

&lt;p&gt;And I got this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshlu28gc8y9yh90fnbwb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshlu28gc8y9yh90fnbwb.png" alt="Start infinite loop" width="800" height="511"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And my browser froze.&lt;/p&gt;

&lt;p&gt;Woohoo, long live the infinite loop!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What happened?&lt;/strong&gt; So, since &lt;code&gt;rapi-doc&lt;/code&gt; is a web component, I need to explicitly tell Vue compiler to not parse it. To just leave it alone.&lt;/p&gt;

&lt;p&gt;And inside my &lt;code&gt;config.mts&lt;/code&gt; file I needed to add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vitepress&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// https://vitepress.dev/reference/site-config&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="na"&gt;vue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;compilerOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;isCustomElement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rapi-doc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We just need to check for custom elements and inform Vue "hey, this tag is off-limits".&lt;/p&gt;

&lt;p&gt;So, we have it, it runs!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu0u0o68fmsqcl17ld4y2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu0u0o68fmsqcl17ld4y2.png" alt="App in dev mode" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And then I tried to build it so I could set up deployment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building the app
&lt;/h2&gt;

&lt;p&gt;I ran &lt;code&gt;yarn docs:build&lt;/code&gt; command. And I immediately (&lt;em&gt;wow, Vite, you're quick!&lt;/em&gt;) got this error:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbaj3mardxolmdih66mbn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbaj3mardxolmdih66mbn.png" alt="Vite build error" width="472" height="111"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This error means that during build-time, Vite couldn't access a &lt;code&gt;self&lt;/code&gt; property. This can also happen if you try to access browser API (e.g., window) from server (in Nuxt or any other SSR framework, for example).&lt;/p&gt;

&lt;p&gt;So what we can do? We can import it dynamically in runtime!&lt;/p&gt;

&lt;p&gt;Let's change our import from this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt; &lt;span class="na"&gt;setup&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rapidoc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt; &lt;span class="na"&gt;setup&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;onMounted&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;onMounted&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rapidoc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now build should pass with no issues! Enjoy you API spec docs!&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Dark Mode
&lt;/h2&gt;

&lt;p&gt;Vitepress comes with dark mode, working out-of-the-box. But how can we make our RapiDoc documentation reacting to mode changes?&lt;/p&gt;

&lt;p&gt;We can use Vitepress core composable - &lt;code&gt;useData&lt;/code&gt;. It contains &lt;code&gt;isDark&lt;/code&gt; property with information if the darkmode is enabled or not.&lt;/p&gt;

&lt;p&gt;So let's use it inside the script section in the SFC:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt; &lt;span class="na"&gt;setup&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useData&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vitepress&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isDark&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;light&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when we have a &lt;code&gt;theme&lt;/code&gt; ref, we can pass it to the &lt;code&gt;rapi-doc&lt;/code&gt; Web Component via attribute binding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;rapi-doc&lt;/span&gt;
      &lt;span class="na"&gt;spec-url = &lt;/span&gt;&lt;span class="s"&gt;"https://petstore.swagger.io/v2/swagger.json"&lt;/span&gt;
      &lt;span class="na"&gt;render-style = &lt;/span&gt;&lt;span class="s"&gt;"read"&lt;/span&gt;
      &lt;span class="na"&gt;:theme=&lt;/span&gt;&lt;span class="s"&gt;"theme"&lt;/span&gt;
      &lt;span class="na"&gt;style = &lt;/span&gt;&lt;span class="s"&gt;"height:100vh; width:100%"&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;/rapi-doc&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to add one more thing for dark mode to work correctly - responding to theme change.&lt;/p&gt;

&lt;p&gt;Let's add a watcher to our &lt;code&gt;script&lt;/code&gt; section:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;watch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isDark&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isDark&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;isDark&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dark&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;light&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And voila, you created API docs that react to theme changes!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0swhnmb9i1rz4snacyjv.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0swhnmb9i1rz4snacyjv.gif" alt=" " width="760" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>vue</category>
      <category>webcomponents</category>
    </item>
    <item>
      <title>Animate your SVG text with animejs (and Vue)</title>
      <dc:creator>Ola B</dc:creator>
      <pubDate>Thu, 18 May 2023 21:56:07 +0000</pubDate>
      <link>https://dev.to/vakme/animate-your-svg-text-with-animejs-64p</link>
      <guid>https://dev.to/vakme/animate-your-svg-text-with-animejs-64p</guid>
      <description>&lt;p&gt;Are you looking for a non-standard portfolio page welcome text? Do you want to create an animated website to propose to your loved one? &lt;/p&gt;

&lt;p&gt;Say no more!&lt;/p&gt;

&lt;p&gt;In this #showdev I'll teach you how to create stunning SVG animation with an equally stunning &lt;code&gt;animejs&lt;/code&gt; library!&lt;/p&gt;

&lt;p&gt;The result will look like this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fvakme.github.io%2Fhowdy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fvakme.github.io%2Fhowdy.gif" alt="Gif of result" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: This post will show you how to create a line drawing animation in animejs. If you want it in a condensed form, straight from the source, visit &lt;a href="https://animejs.com/documentation/#lineDrawing" rel="noopener noreferrer"&gt;the official documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;To create a stunning animated SVG image, first, you will need SVG to animate. There are many tools to do that, available both online and offline. You can, for example, start with generating text using &lt;a href="https://github.com/kartsims/easysvg" rel="noopener noreferrer"&gt;easysvg&lt;/a&gt; (written in php, but a great tool nonetheless). &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Important: Every letter in your SVG should be a separate &lt;code&gt;&amp;lt;path&amp;gt;&lt;/code&gt; tag! Here paths mark the outlines of the letters.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You will also need a container to run the animation in. It can be anything, pure JS script or a more advanced app. In my case, I'm using a very simple Vue.js app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preparing your SVG
&lt;/h2&gt;

&lt;p&gt;To make our SVG look classy, we need to tweak it a bit: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To provide it with an "outlined" look, set &lt;code&gt;fill&lt;/code&gt; attribute in every &lt;code&gt;&amp;lt;path&amp;gt;&lt;/code&gt; tag to &lt;code&gt;transparent&lt;/code&gt; and set &lt;code&gt;stroke&lt;/code&gt; attribute to any color you want.&lt;/li&gt;
&lt;li&gt;Add some extra elements for flavor - directly in SVG code (if you're the pro and a math nerd) or use some editor to do it.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Use my SVG, if you want: &lt;a href="https://github.com/Vakme/vakme.github.io/blob/main/src/assets/howdy.svg" rel="noopener noreferrer"&gt;link to github file&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Animating
&lt;/h2&gt;

&lt;p&gt;First, install &lt;code&gt;animejs&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/juliangarnier" rel="noopener noreferrer"&gt;
        juliangarnier
      &lt;/a&gt; / &lt;a href="https://github.com/juliangarnier/anime" rel="noopener noreferrer"&gt;
        anime
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      JavaScript animation engine
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Anime.js&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;
  
    
    &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fjuliangarnier%2Fanime%2F.%2Fassets%2Fimages%2Fanimejs-v4-logo-animation.gif" class="article-body-image-wrapper"&gt;&lt;img alt="Anime.js V4 logo animation" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fjuliangarnier%2Fanime%2F.%2Fassets%2Fimages%2Fanimejs-v4-logo-animation.gif" width="560"&gt;&lt;/a&gt;
  
&lt;/p&gt;

&lt;p&gt;
  &lt;strong&gt;
  &lt;em&gt;Anime.js&lt;/em&gt; is a fast, multipurpose and lightweight JavaScript animation library with a simple, yet powerful API.&lt;br&gt;
  It works with CSS properties, SVG, DOM attributes and JavaScript Objects
  &lt;/strong&gt;
&lt;/p&gt;

&lt;p&gt;
  &lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/7b364eaced6898c1ce124242f4fff52f80c98f1228257031f4ca5202663fd558/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f646d2f616e696d656a733f7374796c653d666c61742d737175617265266c6f676f3d6e706d"&gt;&lt;img alt="NPM Downloads" src="https://camo.githubusercontent.com/7b364eaced6898c1ce124242f4fff52f80c98f1228257031f4ca5202663fd558/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f646d2f616e696d656a733f7374796c653d666c61742d737175617265266c6f676f3d6e706d"&gt;&lt;/a&gt;
  &lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/b5148e620e83ea2448dd1a4ca2b77c83816bcdaebf4b7b28011b9a5a4c186858/68747470733a2f2f696d672e736869656c64732e696f2f6a7364656c6976722f6e706d2f686d2f616e696d656a733f7374796c653d666c61742d737175617265266c6f676f3d6a7364656c69766572"&gt;&lt;img alt="jsDelivr hits (npm)" src="https://camo.githubusercontent.com/b5148e620e83ea2448dd1a4ca2b77c83816bcdaebf4b7b28011b9a5a4c186858/68747470733a2f2f696d672e736869656c64732e696f2f6a7364656c6976722f6e706d2f686d2f616e696d656a733f7374796c653d666c61742d737175617265266c6f676f3d6a7364656c69766572"&gt;&lt;/a&gt;
  &lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/430c32e279f3c06ce8715ab9f975e7f230c61f6d05a19fcf634276aeaceee4cd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73706f6e736f72732f6a756c69616e6761726e6965723f7374796c653d666c61742d737175617265266c6f676f3d676974687562"&gt;&lt;img alt="GitHub Sponsors" src="https://camo.githubusercontent.com/430c32e279f3c06ce8715ab9f975e7f230c61f6d05a19fcf634276aeaceee4cd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73706f6e736f72732f6a756c69616e6761726e6965723f7374796c653d666c61742d737175617265266c6f676f3d676974687562"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Sponsors&lt;/h2&gt;
&lt;/div&gt;

&lt;p&gt;Anime.js is 100% free and is only made possible with the help of our sponsors.
Help the project become sustainable by sponsoring us on &lt;a href="https://github.com/sponsors/juliangarnier" rel="noopener noreferrer"&gt;GitHub Sponsors&lt;/a&gt;.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;Platinum sponsors&lt;/h3&gt;

&lt;/div&gt;
&lt;p&gt;
&lt;a href="https://ice.io/?ref=animejs" rel="nofollow noopener noreferrer"&gt;
  
    
    &lt;/a&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fjuliangarnier%2Fanime%2F.%2Fassets%2Fsponsors%2Fice-open-network-logomark-dark.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fjuliangarnier%2Fanime%2F.%2Fassets%2Fsponsors%2Fice-open-network-logomark-dark.png" width="250"&gt;&lt;/a&gt;
  

&lt;a href="https://go.warp.dev/anime" rel="nofollow noopener noreferrer"&gt;
  
    
    &lt;/a&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fjuliangarnier%2Fanime%2F.%2Fassets%2Fsponsors%2Fwarp-logomark-dark.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fjuliangarnier%2Fanime%2F.%2Fassets%2Fsponsors%2Fwarp-logomark-dark.png" width="250"&gt;&lt;/a&gt;
  

&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;Silver sponsors&lt;/h3&gt;

&lt;/div&gt;

&lt;p&gt;
&lt;a href="https://www.lambdatest.com?utm_source=animeJS&amp;amp;utm_medium=organic&amp;amp;utm_campaign=july_08&amp;amp;utm_term=sk&amp;amp;utm_content=opensource" rel="nofollow noopener noreferrer"&gt;
  
    
    &lt;/a&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fjuliangarnier%2Fanime%2F.%2Fassets%2Fsponsors%2Flambdatest-logomark-dark.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fjuliangarnier%2Fanime%2F.%2Fassets%2Fsponsors%2Flambdatest-logomark-dark.png" width="150"&gt;&lt;/a&gt;
  

&lt;a href="https://inspatialapp.com/?ref=animejs" rel="nofollow noopener noreferrer"&gt;
  
    
    &lt;/a&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fjuliangarnier%2Fanime%2F.%2Fassets%2Fsponsors%2Finspatial-logomark-dark.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fjuliangarnier%2Fanime%2F.%2Fassets%2Fsponsors%2Finspatial-logomark-dark.png" width="150"&gt;&lt;/a&gt;
  

&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Usage&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;Anime.js V4 works by importing ES modules like so:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;div class="table-wrapper-paragraph"&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;table&gt;

&lt;tbody&gt;

&lt;tr&gt;

  &lt;td&gt;

&lt;div class="highlight highlight-source-js notranslate position-relative overflow-auto js-code-highlight"&gt;
&lt;pre&gt;&lt;span class="pl-k"&gt;import&lt;/span&gt; &lt;span class="pl-kos"&gt;{&lt;/span&gt;
  &lt;span class="pl-s1"&gt;animate&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-s1"&gt;stagger&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
&lt;span class="pl-kos"&gt;}&lt;/span&gt; &lt;span class="pl-k"&gt;from&lt;/span&gt; &lt;span class="pl-s"&gt;'animejs'&lt;/span&gt;&lt;span class="pl-kos"&gt;;&lt;/span&gt;

&lt;span class="pl-en"&gt;animate&lt;/span&gt;&lt;span class="pl-kos"&gt;(&lt;/span&gt;&lt;span class="pl-s"&gt;'.square'&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-kos"&gt;{&lt;/span&gt;
  &lt;span class="pl-c1"&gt;x&lt;/span&gt;: &lt;span class="pl-c1"&gt;320&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-c1"&gt;rotate&lt;/span&gt;: &lt;span class="pl-kos"&gt;{&lt;/span&gt; &lt;span class="pl-c1"&gt;from&lt;/span&gt;: &lt;span class="pl-c1"&gt;-&lt;/span&gt;&lt;span class="pl-c1"&gt;180&lt;/span&gt; &lt;span class="pl-kos"&gt;}&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-c1"&gt;duration&lt;/span&gt;: &lt;span class="pl-c1"&gt;1250&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-c1"&gt;delay&lt;/span&gt;: &lt;span class="pl-en"&gt;stagger&lt;/span&gt;&lt;span class="pl-kos"&gt;(&lt;/span&gt;&lt;span class="pl-c1"&gt;65&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt; &lt;span class="pl-kos"&gt;{&lt;/span&gt; &lt;span class="pl-c1"&gt;from&lt;/span&gt;: &lt;span class="pl-s"&gt;'center'&lt;/span&gt; &lt;span class="pl-kos"&gt;}&lt;/span&gt;&lt;span class="pl-kos"&gt;)&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-c1"&gt;ease&lt;/span&gt;: &lt;span class="pl-s"&gt;'inOutQuint'&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-c1"&gt;loop&lt;/span&gt;: &lt;span class="pl-c1"&gt;true&lt;/span&gt;&lt;span class="pl-kos"&gt;,&lt;/span&gt;
  &lt;span class="pl-c1"&gt;alternate&lt;/span&gt;: &lt;span class="pl-c1"&gt;true&lt;/span&gt;
&lt;span class="pl-kos"&gt;}&lt;/span&gt;&lt;span class="pl-kos"&gt;)&lt;/span&gt;&lt;span class="pl-kos"&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;/div&gt;
  &lt;/td&gt;
  &lt;td&gt;
    &lt;a rel="noopener noreferrer" href="https://github.com/juliangarnier/anime/./assets/images/usage-example-result.gif"&gt;&lt;img alt="Anime.js code example" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fjuliangarnier%2Fanime%2F.%2Fassets%2Fimages%2Fusage-example-result.gif"&gt;&lt;/a&gt;
  &lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;V4 Documentation&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;The full documentation is available &lt;a href="https://animejs.com/documentation" rel="nofollow noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;V3 Migration guide&lt;/h2&gt;

&lt;/div&gt;

&lt;p&gt;You can find the v3 to v4 migration guide…&lt;/p&gt;&lt;/div&gt;


&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/juliangarnier/anime" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;Then add animejs animation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;anime&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;targets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;svg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;strokeDashoffset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;anime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setDashoffset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;easing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;easeInOutBack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;250&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;normal&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy? Let me break it down line by line!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="nx"&gt;targets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;svg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this line, we point to every element we want to animate, in this case to an array of every child element of our SVG. You may need to adjust it a little to the structure of your SVG.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="nx"&gt;strokeDashoffset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;anime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setDashoffset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what makes animation happen. It uses &lt;code&gt;stroke-dashoffset&lt;/code&gt; attribute. If you want to know more about animating SVGs using this attribute, I recommend &lt;a href="https://css-tricks.com/svg-line-animation-works/" rel="noopener noreferrer"&gt;this CSS Tricks article&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="nx"&gt;easing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;easeInOutBack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I wanted to add a little "bouncing" at the beginning and the end of the animation of every letter. If you want to test different easing functions on your own, I recommend &lt;a href="https://easings.net/" rel="noopener noreferrer"&gt;easings.net&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;250&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I wanted to slightly delay every animated path, so I passed a function that multiplies the delay by the index of an element in the table I passed in &lt;code&gt;targets&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="nx"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;normal&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can choose from &lt;code&gt;normal&lt;/code&gt;, &lt;code&gt;reverse&lt;/code&gt;, and &lt;code&gt;alternate&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Embed this... somewhere
&lt;/h2&gt;

&lt;p&gt;Although animejs can be used in Vanilla, I really wanted to use it in my Vue.js app.&lt;/p&gt;

&lt;p&gt;I used Vue.js transitions to do that. &lt;a href="https://vuejs.org/guide/built-ins/transition.html" rel="noopener noreferrer"&gt;More on them.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First, template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;Transition&lt;/span&gt; &lt;span class="na"&gt;appear&lt;/span&gt; &lt;span class="na"&gt;:css=&lt;/span&gt;&lt;span class="s"&gt;"false"&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;enter=&lt;/span&gt;&lt;span class="s"&gt;"onEnter"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"howdy"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Your SVG goes here. I used v-html because I'm lazy ¯\_(ツ)_/¯  --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;v-html=&lt;/span&gt;&lt;span class="s"&gt;"howdy"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/Transition&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You can use &lt;a href="https://blog.logrocket.com/using-svg-and-vue-js-a-complete-guide/" rel="noopener noreferrer"&gt;SVG Component&lt;/a&gt; to embed your SVG on a page.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I wanted my animation to start on a component render, so I added &lt;code&gt;appear&lt;/code&gt; keyword to the transitions. I also needed to tell Vue this animation would be purely JS, so I used &lt;code&gt;:css="false"&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Now I only need to write &lt;code&gt;onEnter&lt;/code&gt; callback:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onEnter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;anime&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;targets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;svg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;strokeDashoffset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;anime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setDashoffset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;easing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;easeInOutBack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;250&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;normal&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;done&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;onEnter&lt;/code&gt; uses the Transition Element as the first argument and &lt;code&gt;done()&lt;/code&gt; function as the second. We have to call &lt;code&gt;done()&lt;/code&gt; function at the end of the application to signal to Vue that the animation ended. We can use &lt;code&gt;complete&lt;/code&gt; hook from animejs to do it.&lt;br&gt;
And we want to animate only descendants of our Transition element, so we can specify &lt;code&gt;targets&lt;/code&gt; using the first argument.&lt;/p&gt;

&lt;p&gt;And... that's it! Sit back and enjoy your animated text!&lt;/p&gt;

&lt;p&gt;If you have any questions, I'm happy to answer :) If there's anything to be improved in this short post, let me know, and I'll gladly fix that!&lt;/p&gt;

&lt;p&gt;And if you want to see this animation in action, you can visit &lt;a href="https://vakme.github.io/" rel="noopener noreferrer"&gt;my page&lt;/a&gt; (I have to warn you though, this is still in a WIP stage).&lt;/p&gt;

&lt;p&gt;All the best,&lt;/p&gt;

&lt;p&gt;Aleksandra (or Ola, for short)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>animejs</category>
      <category>showdev</category>
      <category>vue</category>
    </item>
  </channel>
</rss>
