<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Arthur Khokhlov on Medium]]></title>
        <description><![CDATA[Stories by Arthur Khokhlov on Medium]]></description>
        <link>https://medium.com/@arthur.khokhlov?source=rss-23aecf3c1272------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*L0gcvi8FkNfSmw21Gza2NQ.jpeg</url>
            <title>Stories by Arthur Khokhlov on Medium</title>
            <link>https://medium.com/@arthur.khokhlov?source=rss-23aecf3c1272------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 00:05:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arthur.khokhlov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to create isometric diagrams using SVG]]></title>
            <link>https://medium.com/@arthur.khokhlov/how-to-create-isometric-diagrams-using-svg-591924144941?source=rss-23aecf3c1272------2</link>
            <guid isPermaLink="false">https://medium.com/p/591924144941</guid>
            <category><![CDATA[javascript-library]]></category>
            <category><![CDATA[diagramming]]></category>
            <category><![CDATA[svg]]></category>
            <category><![CDATA[frontend-development]]></category>
            <dc:creator><![CDATA[Arthur Khokhlov]]></dc:creator>
            <pubDate>Thu, 17 Aug 2023 14:46:01 GMT</pubDate>
            <atom:updated>2023-08-23T10:20:34.541Z</atom:updated>
            <content:encoded><![CDATA[<p>There are various methods of visualizing three-dimensional objects in two-dimensional space. For example, most 3D graphics engines use perspective projection as the main form of projection. This is because perspective projection is an excellent representation of the real world, in which objects become smaller with increasing distance. But when the relative position of objects is not important, and for a better understanding of the size of objects, you can use parallel projections. They are more common in engineering and architecture, where it is important to maintain parallel lines. Since the birth of computer graphics, these projections have been used to render 3D scenes when 3D rendering hardware acceleration was not possible. Recently, various forms of parallel projections have become a style choice for digital artists, and they are used to display objects in infographics and in digital art in general.</p><p>The purpose of this article is to show how to create and manipulate isometric views in SVG, and how to define these objects using, in particular, the <a href="https://www.jointjs.com/?utm_source=medium&amp;utm_medium=referral">JointJS</a> library. To illustrate SVG’s capabilities in creating parallel projections, we will use isometric projection as an example. This projection is one of the dominant projection types, because it allows you to maintain the relative scale of objects along all axes.</p><h3>Isometric projection</h3><p>Let’s define what isometric projection is. First of all, it is a parallel type of projection in which all lines from a “camera” are parallel. It means that the scale of an object does not depend on the distance between the “camera” and the object. And specifically, in isometric (means “equal measure” in greek) projection, scaling along each axis is the same. This is achieved by defining equal angles between all axes.</p><p>In the following image, you can see how axes are positioned in isometric projection. Keep in mind that in this article we will be using a left-handed coordinate system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jCXEjZJe23Cm7KiwjZZBvg.png" /></figure><p>One of the features of the isometric projection is that it can be deconstructed in three different 2D projections: top, side and front projections. For example, cuboid can be represented by three rectangles on each 2D projection and then combined into one isometric view. The next image represents separate projections of an object using the left-handed coordinate system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D1wn7xyBvUD-uBDjZUGCkA.png" /><figcaption>Separate views of the orthographic projection</figcaption></figure><p>Then we can combine them into one isometric view:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/891/1*4lB3p8a5DSI7fOmBn3GQ4Q.png" /><figcaption>Isometric view of the example object</figcaption></figure><p>The challenge with SVG is that it contains 2D objects which are located on one XY-plane. But we can overcome this by combining all projections in one plane, and then separately applying transformation to every object.</p><h3>SVG isometric view transformations</h3><p>In 3D, to create an isometric view, we can move the camera to a certain position, but SVG is purely a 2D format, so we have to create a workaround to build such a view. We recommend reading <a href="https://design.tutsplus.com/tutorials/how-to-create-advanced-isometric-illustrations-using-the-ssr-method--vector-1058">Cody Walker’s article</a> that presents a method for creating isometric representations from 2D object views — top, side and front projections. Based on the article, we need to create transformations for each 2D projection of the object separately.</p><p>First we need to rotate our plane by 30 degrees. And then we will skew our 2D image by -30 degrees. This transformation will align our axes with the axes of the isometric projection.</p><p>Then we need to use a scale operator to scale our 2D projection down vertically by 0.8602. We need to do it due to the fact of isometric projection distortion.</p><p>Let’s introduce some SVG features that will help us implement isometric projection. The SVG specification allows users to specify a particular transformation in the transform <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform">attribute</a> of an SVG element. This attribute helps us apply linear transformation to the SVG element. To transform 2D projection into an isometric view, we need to apply <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform#scale">scale</a>, <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform#rotate">rotate</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform#skewx">skew</a> operators.</p><p>To represent the transformation in code, we can use the <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMMatrixReadOnly">DOMMatrixReadOnly</a> object, which is a browser API to represent the transformation matrix. Using this interface, we can create a matrix as follows:</p><pre>const isoMatrix = new DOMMatrixReadOnly()<br>    .rotate(30)<br>    .skewX(-30)<br>    .scale(1, 0.8602);</pre><p>This interface allows building a transformation matrix using our values, and then we can apply the resulting value to thetransform <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform">attribute</a> using the matrix <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform#matrix">function</a>.</p><p>In SVG, we can present only one 2D space at a time, so for our conversion, we will be using top projection as a base projection. This is mostly because axes in this projection correspond with axes in a normal SVG viewport.</p><p>To demonstrate SVG possibilities, we will be using the <a href="https://www.jointjs.com/?utm_source=medium&amp;utm_medium=referral">JointJS</a> library. We defined a rectangular grid in the XY-plane with a cell width of 20. Let’s define SVG for the elements on the top projection from the example. To properly render this object, we need to specify two polygons for two levels of our object. Also, we can apply a translate transformation for our element in 2D space using DOMMatrix:</p><pre>// Translate transformation for Top1 Element<br>const matrix2D = new DOMMatrixReadOnly()<br>    .translate(200, 200);</pre><pre>&lt;!--Top1 element--&gt;<br>&lt;polygon joint-selector=&quot;body&quot; id=&quot;v-4&quot; <br>  stroke-width=&quot;2&quot; stroke=&quot;#333333&quot; fill=&quot;#ff0000&quot; <br>  fill-opacity=&quot;0.7&quot; points=&quot;0,0 60,0 60,20 40,20 40,60 0,60&quot; <br>  transform=&quot;matrix(1,0,0,1,200,200)&quot;&gt;<br>&lt;/polygon&gt;<br><br>&lt;!--Top2 element--&gt;<br>&lt;polygon joint-selector=&quot;body&quot; id=&quot;v-6&quot; <br>  stroke-width=&quot;2&quot; stroke=&quot;#333333&quot; fill=&quot;#ff0000&quot; <br>  fill-opacity=&quot;0.7&quot; points=&quot;0,0 20,0 20,40 0,40&quot; <br>  transform=&quot;matrix(1,0,0,1,240,220)&quot;&gt;<br>&lt;/polygon&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/284/1*khp_JwpfA6At9KHbDLIINg.png" /><figcaption>2D top projection</figcaption></figure><p>Then we can apply our isometric matrix to our elements. Also, we will add a translate transformation to position elements in the right place:</p><pre>const isoMatrix = new DOMMatrixReadOnly()<br>    .rotate(30)<br>    .skewX(-30)<br>    .scale(1, 0.8602);<br><br>const top1Matrix = isoMatrix.translate(200, 200);<br>const top2Matrix = isoMatrix.translate(240, 220);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*u-oQShDd8kPNlQcXLYqt_Q.png" /><figcaption>Isometric view without height adjustment</figcaption></figure><p>For simplicity, let’s assume that our element’s base plane is located on the XY-plane. Therefore, we need to translate the top view, so it will be viewed as it is located on the top of the object. To do it, we can just translate the projection by its Z coordinate on the scaled SVG space as follows. Top1 element has elevation 80, so we should translate it by (-80, -80). Similarly, Top2 element has elevation 40. We can just apply these translations to our existing matrix:</p><pre>const top1MatrixWithHeight = top1Matrix.translate(-80, -80);<br>const top2MatrixWithHeight = top1Matrix.translate(-40, -40);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*JAv0Jm4oFfXd2uvjhDNGRA.png" /><figcaption>Final isometric view of top projection</figcaption></figure><p>In the end, we will have the following transform attributes for Top1 and Top2 elements. Note that they differ only in the two last values, which represent the translate transformation:</p><pre>// Top1 element<br>transform=&quot;matrix(0.8660254037844387,0.49999999999999994,-0.8165000081062317,0.47140649947346464,5.9,116.6)&quot;<br><br>// Top2 element<br>transform=&quot;matrix(0.8660254037844387,0.49999999999999994,-0.8165000081062317,0.47140649947346464,26.2,184.9)&quot;</pre><p>To create an isometric view of side and front projections, we need to make a <a href="https://en.wikipedia.org/wiki/Net_(polyhedron)">net</a> so we can place all projections on 2D SVG space. Let’s create a net by attaching side and front views similar to the classic cube net:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/444/1*XM6EaOD7gcOKY27KN0PTyw.png" /></figure><p>Then we need to skewX side and front projections by 45 degrees. It will allow us to align the Z axis for all projections. After this transformation, we will get following image:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/440/1*GlhDjU2CFImTM8GHf3q4wQ.png" /><figcaption>Prepared 2D projection</figcaption></figure><p>Then we can apply our isoMatrix to this object:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*A-CX5n4BHpBxi3XcgBESmg.png" /><figcaption>Isometric projection without depth adjustments</figcaption></figure><p>In every projection, there are parts that have a different 3rd coordinate value. Therefore, we need to adjust this depth coordinate for every projection as we did with the top projection and its Z coordinate. In the end, we will get the following isometric view:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*Z0pFxIsrKasYDmDm2FvkCQ.png" /><figcaption>Final isometric view of the object</figcaption></figure><h3>Using JointJS for the Isometric Diagram</h3><p><a href="https://www.jointjs.com/">JointJS</a> allows us to create and manipulate such objects with ease due to its elements framework and wide set of tools. Using JointJS, we can define and control isometric objects to build powerful isometric diagrams.</p><p>Remember the basic isometric transformation from the beginning of the article?</p><pre>const isoMatrix = new DOMMatrixReadOnly()<br>    .rotate(30)<br>    .skewX(-30)<br>    .scale(1, 0.8602);</pre><p>In the JointJS library, we can apply this transformation to the whole object which stores all SVG elements, and then simply apply the object-specific transformations on top of this.</p><h4>Isometric grid rendering</h4><p>JointJS has great capabilities in the rendering of custom SVG markup. Utilizing JointJS, we can generate a path which is aligned to an untransformed grid, and have it transformed automatically with the grid thanks to the global paper transformation that we mentioned previously. You can see the grid, and how we interpret the coordinate system in the demo below. Note that we can dynamically change the paper transformation which allows us to change the view on the fly:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fjointjs%2Fembed%2Fpreview%2Ff8ea3c83fc17b9f438bb1e95d91d11a9%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3Df8ea3c83fc17b9f438bb1e95d91d11a9&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fjointjs%2Fpen%2FpoQzZJZ%2Ff8ea3c83fc17b9f438bb1e95d91d11a9%3Feditors%3D0010&amp;image=https%3A%2F%2Fshots.codepen.io%2Fusername%2Fpen%2FpoQzZJZ%2Ff8ea3c83fc17b9f438bb1e95d91d11a9-512.jpg%3Fversion%3D1685983634&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/ffd135d78b88d04d392b686520f8408d/href">https://medium.com/media/ffd135d78b88d04d392b686520f8408d/href</a></iframe><h4>Creating a custom isometric SVG element</h4><p>Here we show a custom SVG Isometric shape in JointJS. In our example, we use the isometricHeight property to store information about a third dimension, and then use it to render our isometric object. The following snippet shows how you can call the custom createIsometricElement function to alter object properties:</p><pre>const element = createIsometricElement({<br>    isometricHeight: GRID_SIZE * 3,<br>    size: { width: GRID_SIZE * 3, height: GRID_SIZE * 6 },<br>    position: { x: GRID_SIZE * 6, y: GRID_SIZE * 6 }<br>});</pre><p>In the following demo, you can see that our custom isometric element can be moved like an ordinary element on the isometric grid. You can change dimensions by altering parameters of the createIsometricElement function in the source code (when you click “Edit on CodePen”):</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fjointjs%2Fembed%2Fpreview%2F72e6d9213d8ebab5f16fcfe34c6a99e9%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3D72e6d9213d8ebab5f16fcfe34c6a99e9&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fjointjs%2Fpen%2FeYPxLgo%2F72e6d9213d8ebab5f16fcfe34c6a99e9&amp;image=https%3A%2F%2Fshots.codepen.io%2Fusername%2Fpen%2FeYPxLgo%2F72e6d9213d8ebab5f16fcfe34c6a99e9-512.jpg%3Fversion%3D1684934305&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae242d6edb3e7c222f9952ba5755db8b/href">https://medium.com/media/ae242d6edb3e7c222f9952ba5755db8b/href</a></iframe><h4>Z-index calculation in isometric diagrams</h4><p>One of the problems with an isometric view is placing elements respective to their relative position. Unlike in a 2D plane, in an isometric view objects have perceived height and can be placed one behind the other. We can achieve this behavior in SVG by placing them into the DOM in the right order. To define the order in our case, we can use the JointJS z attribute which allows sending the correct element to the background, so that it can be overlapped/hidden by the other element as expected. You can find more information about this problem in a great <a href="https://mazebert.com/forum/news/isometric-depth-sorting--id775/">article by Andreas Hager</a>.</p><p>We decided to sort the elements using the <a href="https://en.wikipedia.org/wiki/Topological_sorting">topological sorting algorithm</a>. The algorithm consists of two steps. First, we need to create a special graph, and then we need to use a depth-first search for that graph to find the correct order of elements.</p><p>As the first step, we need to populate the initial graph — for each object we need to find all objects behind it. We can do that by comparing the positions of their bottom sides. Let’s illustrate this step with images — let’s, for example, take three elements which are positioned like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*XLhj554bFMIv8neTbV6GmQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*DlumUyf1DdSjJexnqRO_FA.png" /></figure><p>We have marked the bottom side of each object in the second image. Using this data, we will create a graph structure which will model topological relations between elements. In the image, you can see how we define the points on the bottom side — we can find the relative position of all elements by comparing aMax and bMin points. We define that if the x and y coordinates of point bMin are less than the coordinates of point aMax , then object b is located behind object a.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7MeQjNTrlkZ7upiJ2aq-iQ.png" /><figcaption>Algorithm data in a 2D space</figcaption></figure><p>Comparing the three elements from our previous example, we can produce the following graph:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/391/1*p2gElaCQPENK4pPH1FN-GA.png" /><figcaption>Topological graph</figcaption></figure><p>After that, we need to use a <a href="https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search">variation of the depth-first search algorithm</a> to find the correct rendering order. A depth-first search allows us to visit graph nodes according to the visibility order, starting from the most distant one. Here is a library-agnostic example of the algorithm:</p><pre>const sortElements = (elements: Rect[]) =&gt; {<br>    const nodes = elements.map((el) =&gt; {<br>        return {<br>            el: el,<br>            behind: [],<br>            visited: false,<br>            depth: null,<br>        };<br>    });<br><br>    for (let i = 0; i &lt; nodes.length; ++i) {<br>        const a = nodes[i].el;<br>        const aMax = aBBox.bottomRight();<br><br>        for (let j = 0; j &lt; nodes.length; ++j) {<br>            if (i != j) {<br>                const b = nodes[j].el;<br>                const bMin = bBBox.topLeft();<br>                if (bMin.x &lt; aMax.x &amp;&amp; bMin.y &lt; aMax.y) {<br>                    nodes[i].behind.push(nodes[j]);<br>                }<br>            }<br>        }<br>    }<br><br>    const sortedElements = depthFirstSearch(nodes);<br>    return sortedElements;<br>};<br><br>const depthFirstSearch = (nodes) =&gt; {<br>    let depth = 0;<br>    let sortedElements = [];<br><br>    const visitNode = (node) =&gt; {<br>        if (!node.visited) {<br>            node.visited = true;<br><br>            for (let i = 0; i &lt; node.behind.length; ++i) {<br>                if (node.behind[i] == null) {<br>                    break;<br>                } else {<br>                    visitNode(node.behind[i]);<br>                    delete node.behind[i];<br>                }<br>            }<br><br>            node.depth = depth++;<br>            sortedElements.push(node.el);<br>        }<br>    };<br><br>    for (let i = 0; i &lt; nodes.length; ++i) {<br>        visitNode(nodes[i]);<br>    }<br><br>    return sortedElements;<br>};</pre><p>This method can be implemented easily using the JointJS library — in the following CodePen, we use a special JointJS event to recalculate z-indexes of our elements whenever the position of an element is changed. As outlined above, we use a special z property of the element model to specify rendering order and assign it during the depth-first traversal. (Note that the algorithm’s behavior is undefined in the case of intersecting elements, due to the nature of implementation of isometric objects.)</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fjointjs%2Fembed%2Fpreview%2Fe06f0a38d57651c6eb3502a6ce6dd1e2%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3De06f0a38d57651c6eb3502a6ce6dd1e2&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fjointjs%2Fpen%2FZEqPyyX%2Fe06f0a38d57651c6eb3502a6ce6dd1e2&amp;image=https%3A%2F%2Fshots.codepen.io%2Fusername%2Fpen%2FZEqPyyX%2Fe06f0a38d57651c6eb3502a6ce6dd1e2-512.jpg%3Fversion%3D1685017365&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/dda971c91b8289f6741b4b7c044dd50c/href">https://medium.com/media/dda971c91b8289f6741b4b7c044dd50c/href</a></iframe><h4>The JointJS demo</h4><p>We have created a JointJS demo which combines all of these methods and techniques, and also allows you to easily switch between 2D and isometric SVG markup. Crucially, as you can see, the powerful features of JointJS (which allow us to move elements, to connect them with links, and to create tools to edit them, among others) work just as well in the isometric view as they do in 2D.</p><p>You can see the demo <a href="https://www.jointjs.com/demos/isometric-diagram?utm_source=medium&amp;utm_medium=referral">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jLjKMIeN-WLnje5PoYrrzw.png" /></figure><p>Throughout this article, we used our open-source <a href="https://www.jointjs.com/?utm_source=medium&amp;utm_medium=referral">JointJS</a> library for illustration. However, since you were so thorough with your exploration,<strong> we would like to extend to you </strong><a href="https://www.jointjs.com/free-trial?utm_source=medium&amp;utm_medium=referral"><strong>an invitation to our no-commitment 30-day trial of JointJS+</strong></a>, an advanced commercial extension of JointJS. It will allow you to experience additional powerful tools for creating delightful diagrams.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=591924144941" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to write a simple JSON visualizer using JointJS+]]></title>
            <link>https://medium.com/front-end-weekly/how-to-write-a-simple-json-visualizer-using-jointjs-fb40b89d4e7?source=rss-23aecf3c1272------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb40b89d4e7</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[diagrams]]></category>
            <category><![CDATA[diagramming-library]]></category>
            <category><![CDATA[diagramming]]></category>
            <dc:creator><![CDATA[Arthur Khokhlov]]></dc:creator>
            <pubDate>Tue, 01 Nov 2022 12:37:22 GMT</pubDate>
            <atom:updated>2022-11-02T16:44:58.290Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NVQGC1aoPeWSOT-A8voiAg.png" /></figure><p>The JSON format — as a widely used form of data storage and representation — lacks a simple and convenient view of complex data, forcing developers to use external tools for data visualization and thus more productive work.</p><p>At <a href="https://www.jointjs.com/">JointJS</a>, we decided to regularly share code samples to help you create visual applications faster and with less effort, and one of them is a <strong>simple JSON visualizer</strong>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Ff%2F788447%3Fas_embed%3Dtrue&amp;dntp=1&amp;display_name=Upscribe&amp;url=https%3A%2F%2Fupscri.be%2F788447%2F&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/18214d8333adbb5668f31791a3202b60/href">https://medium.com/media/18214d8333adbb5668f31791a3202b60/href</a></iframe><p>This gives us the opportunity to connect with other developers and diagramming enthusiasts, hear their ideas and solve their pain points. We often hear them complain about the lack of an intuitive way to visualize their JSON data.</p><p>That’s why we created this simple demo application that demonstrates how to use a built-in automatic layout to position nodes and add table-like shapes to organize object properties into tables where values have different styles depending on their type.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fjointjs%2Fembed%2Fpreview%2FWNJBMxQ%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DWNJBMxQ&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fjointjs%2Fpen%2FWNJBMxQ&amp;image=https%3A%2F%2Fshots.codepen.io%2Fjointjs%2Fpen%2FWNJBMxQ-512.jpg%3Fversion%3D1666171493&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b0299c03732c2fd7a87dd3742324f2a/href">https://medium.com/media/7b0299c03732c2fd7a87dd3742324f2a/href</a></iframe><p>We release new demos every Wednesday to help developers get inspired and bring their ideas to life faster. If you want to learn with us, <a href="https://codepen.io/jointjs">follow us on CodePen</a> or <a href="https://github.com/clientIO/joint/discussions">join the conversation on Github</a>.</p><p>Happy diagramming!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb40b89d4e7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-weekly/how-to-write-a-simple-json-visualizer-using-jointjs-fb40b89d4e7">How to write a simple JSON visualizer using JointJS+</a> was originally published in <a href="https://medium.com/front-end-weekly">Frontend Weekly</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create nice looking curves in SVG with fixed tangents]]></title>
            <link>https://medium.com/front-end-weekly/how-to-create-nice-looking-curves-in-svg-with-fixed-tangents-721821834d80?source=rss-23aecf3c1272------2</link>
            <guid isPermaLink="false">https://medium.com/p/721821834d80</guid>
            <category><![CDATA[svg-curves]]></category>
            <category><![CDATA[cubic-bézier-curves]]></category>
            <category><![CDATA[svg]]></category>
            <category><![CDATA[catmull-rom-splines]]></category>
            <dc:creator><![CDATA[Arthur Khokhlov]]></dc:creator>
            <pubDate>Thu, 12 May 2022 16:58:46 GMT</pubDate>
            <atom:updated>2022-06-09T19:11:15.717Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*ziYOmWCr79DVhHwvMq78Xw.gif" /></figure><h3>Introduction</h3><p>‍During the development of various applications there is a need to generate curves. In the web environment, the SVG format is most often used for such purposes. At the moment the SVG specification only works with quadratic and cubic Bézier curves. But in the case of a curve that goes through many points, we need a method for smoothly merging segments to create a nice looking curve. The solution to this problem is to generate Bézier curves using continuity conditions C2 and C1. One of the many examples is <a href="https://www.particleincell.com/2012/bezier-splines/">here</a>.</p><p>‍But in our case of connectors for the <a href="https://www.jointjs.com/opensource">JointJS</a> open-source library, there is a restriction that the start and end tangents must be defined so that the curve starts and ends perpendicularly (in the default situation). And in this case the usual implementation leads to very twisted curves in certain situations. A Bézier curve works in terms of control points, but this is not very convenient in the case of manipulations with tangents.‍</p><p>So we decided to use Catmull-Rom splines with subsequent conversion to cubic Bézier curves, which are supported by the SVG format. Catmull-Rom splines are not currently supported by the SVG standard, but there are <a href="https://www.w3.org/TR/2015/WD-SVG2-20150409/paths.html#PathDataCatmullRomCommand">plans</a> to add them.</p><p>Catmull-Rom splines are types of cubic splines that define tangents at each point using the previous and next spline points. Unlike a Bézier curve, which has different orders (quadratic, cubic, etc.), a Catmull-Rom spline is defined only for 4 control points, so its segment is always cubic. The curve is drawn only from points <em>P</em> to <em>P₂</em> while points <em>P₀</em> and <em>P₃</em> are used to define tangents at points <em>P₁</em> and <em>P₂</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/642/1*nQN5FEyES7aM7ZkPyOkwjw.png" /></figure><p>Catmull-Rom splines can be defined in terms of tension. This parameter governs the behavior of the curve at the control points. The higher the tension, the shorter the tangents at the start and end points.‍</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Ff%2F788447%3Fas_embed%3Dtrue&amp;dntp=1&amp;display_name=Upscribe&amp;url=https%3A%2F%2Fupscri.be%2F788447%2F&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/18214d8333adbb5668f31791a3202b60/href">https://medium.com/media/18214d8333adbb5668f31791a3202b60/href</a></iframe><p>A segment of the Catmull-Rom spline can be combined with other segments to obtain a smooth curve through an arbitrary number of points. To ensure a smooth curve, we must assume that at each point the tangents of neighboring segments must be collinear. But we must also adjust the length of the tangent vectors to control the overall flow of the curve.</p><p>In our use case the most important parameter which we want to control is tangents at the interpolated points. A <a href="https://www.researchgate.net/publication/41391547_Modeling_Shapes_for_Pattern_Recognition_A_Simple_Low-Cost_Spline-based_Approach">common formula</a> for a tangent at a particular point is <em>tᵢ=τ(pᵢ−pᵢ˗₁)+(1−τ)(pᵢ˖₁−pᵢ)</em>, where τ is tension and <em>pᵢ </em>is corresponding to control points.</p><p>Using this formula we can derive the coordinates of the control points of the curve segments. All we need to know are the tangents for each point through which our curve will pass.</p><h3>Main algorithm</h3><p>To draw a curve according to our algorithm, we need a list of points <em>P₀…Pₙ</em> through which the curve will pass, as well as the source and target directions <em>Dₛ, Dₜ</em> (or tangents <em>t₀</em>, <em>tₙ</em>). For a tension <em>t</em> we are using the default value of 0.5 (it is the most suitable in our case). In our case we know the desired tangents or directions at the start and end points. In the case where only directions are given, we use a formula to determine the length of the tangent as a function of the distance to the nearest point. Here we introduce the coefficient k — this coefficient is the ratio of the distance between the points to the length of the tangent:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LIDBoPPv9e9Lh155DJfSYA.png" /></figure><p>In <a href="https://www.jointjs.com/opensource">JointJS</a> we are using 0.6 as the default value. After this we are calculating the angle <em>θ</em> between the direction and a vector to the nearest point for every direction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fNF2uysbc22YZmx1PawWoQ.png" /></figure><p>If <em>θₛ</em> is greater than 45 degrees, we adjust the length of the tangent so that the curve behaves correctly at certain angles. To control this behavior we use coefficient <em>kθ</em> (in <a href="https://www.jointjs.com/opensource">JointJS</a> the default is 80) :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yaHr5puULHwmCwbh3ixkHw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*NmNrjQh-Y1egJqaPAuKYhw.png" /></figure><p>You can see how different parameters affect curves in this CodePen:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fjointjs%2Fembed%2Fpreview%2F0f2b710452b695059f61438e71330e89%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3D0f2b710452b695059f61438e71330e89&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fjointjs%2Fpen%2FeYyjbYP%2F0f2b710452b695059f61438e71330e89&amp;image=https%3A%2F%2Fshots.codepen.io%2Fjointjs%2Fpen%2FeYyjbYP%2F0f2b710452b695059f61438e71330e89-512.jpg%3Fversion%3D1649761472&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/68dc18c2773bdcdb4f1dda933390d44f/href">https://medium.com/media/68dc18c2773bdcdb4f1dda933390d44f/href</a></iframe><p>To construct all curve segments, we need to find corresponding tangents at each point. At each point <em>i</em> we need to calculate two tangents <em>tᵢ¹</em>, <em>tᵢ²</em> for two adjacent segments of a spline.</p><p>The direction of the tangent at a particular point is calculated from the previous and subsequent points. But for continuity and smoothness instead of first and last points <em>P₀</em>, <em>Pₙ</em> we use corresponding points with added tangents <em>t₀</em>, <em>tₙ</em>. First we look for vectors to the current point:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0HIPfvitiRtLlPxv_X5s0Q.png" /></figure><p>and find angle <em>θᵥ</em> between these vectors.</p><p>Then we find a unit vector <em>t</em>, which corresponds to the “tangent” of an angle <em>θᵥ</em> (see the picture below). To find vector t we need to rotate vector <em>Vᵢ²</em> counterclockwise (clockwise if a determinant of (<em>Vᵢ¹</em>, <em>Vᵢ²</em>) is less than 0) by (π−θ<em>ᵥ)/2</em> degrees. To find the lengths of the tangents for certain segments we use the following formulas:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T6EEDPsjeYWq11YL4BA8Gw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*gyldcj0jcqP75Oboo2vQDg.png" /></figure><p>After obtaining all tangents we can construct the Catmull-Rom spline in terms of control points. For each <em>i</em>-th segment of the total <em>n</em> segments of a spline (the number of points minus one) we create 4 control points as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3y_VmvhChKFS0leh4hyVbQ.png" /></figure><h3>Conversion to Bezier curves</h3><p>To convert Catmull-Rom curves into Bézier curves, we use the formulas from this <a href="https://rdcu.be/cKyYS">article</a>. Thus, in our terms we obtain this transformation for each segment:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p2unHs-74BSrL20LChGI2A.png" /></figure><p>where <em>B₀</em> and <em>B₃</em> are starting and ending points and <em>B₁</em> and <em>B₂</em> are middle control points of a cubic Bézier curve.</p><h3>Conclusion</h3><p>All in all, this approach provides a fast and customizable way to create beautiful curves using SVG. We work with Catmull-Rom splines because it makes it easier to manipulate tangents, which is our main goal, and the conversion to Bézier curves is very smooth.</p><p>And the result? See it yourself:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fjointjs%2Fembed%2Fpreview%2FJjOXJyG%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DJjOXJyG&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fjointjs%2Fpen%2FJjOXJyG&amp;image=https%3A%2F%2Fassets.codepen.io%2F7589991%2Finternal%2Fscreenshots%2Fpens%2FJjOXJyG.custom.png%3Fversion%3D1643804413&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/e985a7af1aa2487414380159da3c2d99/href">https://medium.com/media/e985a7af1aa2487414380159da3c2d99/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=721821834d80" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-weekly/how-to-create-nice-looking-curves-in-svg-with-fixed-tangents-721821834d80">How to create nice looking curves in SVG with fixed tangents</a> was originally published in <a href="https://medium.com/front-end-weekly">Frontend Weekly</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>