<?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 Anurag Hazra on Medium]]></title>
        <description><![CDATA[Stories by Anurag Hazra on Medium]]></description>
        <link>https://medium.com/@anuraghazra?source=rss-bf2e65f6d437------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*4mYwcb9KLDmraACGsQzDMg.png</url>
            <title>Stories by Anurag Hazra on Medium</title>
            <link>https://medium.com/@anuraghazra?source=rss-bf2e65f6d437------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 04:17:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anuraghazra/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[Lessons learned from building reusable UI components]]></title>
            <link>https://engineering.razorpay.com/lessons-learned-from-building-reusable-ui-components-bb34289cd438?source=rss-bf2e65f6d437------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb34289cd438</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[design-systems]]></category>
            <category><![CDATA[user-interface]]></category>
            <dc:creator><![CDATA[Anurag Hazra]]></dc:creator>
            <pubDate>Wed, 02 Nov 2022 12:01:19 GMT</pubDate>
            <atom:updated>2022-11-03T08:05:01.829Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Cover picture: various UI elements layered together, text says “Razorpay”" src="https://cdn-images-1.medium.com/max/1024/1*04HksQDbAeWKXBLB6maEiw.jpeg" /></figure><p>Writing reusable components that work seamlessly across different platforms such as web &amp; react-native, or different browsers and devices tends to have more than a few footguns associated with them. From API design to accessibility there’s a huge spectrum of edge cases to be handled while building a component that suffices a variety of use cases.</p><p>I’ve been writing reusable components in React for a while now. Writing components, designing developer-friendly APIs &amp; brainstorming about how the components will suffice the product’s use cases are intriguing problems to solve.</p><h3>Intro</h3><p>Currently, we are developing Razorpay&#39;s design system called Blade which we&#39;ve been iterating over for quite a while now. It’s open source so you can check the <a href="https://github.com/razorpay/blade">source here</a>.</p><p>Previously I&#39;ve helped build <a href="https://github.com/adaptui/react">Renderlesskit</a> (now AdaptUI) and I&#39;ve also worked with and contributed to other design systems/component libraries like <a href="https://chakra-ui.com/">ChakraUI</a> &amp; <a href="https://github.com/ariakit/ariakit">Ariakit</a>.</p><p>I wanted to share some of my learnings from building these libraries to help you design better APIs.</p><p>Both Blade and AdaptUI have very different scopes, use cases &amp; business requirements. In fact, you can say Blade &amp; AdaptUI are completely opposite to each other. One focuses on constrained design to enforce certain rules &amp; the other one on flexibility.</p><h3>Lesson 1: The Spectrum Of Flexibility &amp; Constraints</h3><p>The most obvious aspect of design systems is that they evolve and change significantly based on the organization’s use cases and goals.</p><p>Design systems walk this “Spectrum” and lean towards either being more flexible or constrained.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y9w6f27NlVP64pK7oZqERg.png" /><figcaption>The Spectrum Of Flexibility &amp; Constraints</figcaption></figure><p><strong>But how do you choose between a flexible and a constrained system?</strong></p><p>There are various factors:</p><p>1. Product &amp; Brands<br>2. Scope of The Design System</p><p><strong>1. Product &amp; Brands</strong></p><p>First and foremost the products that an organization is trying to build can have a major impact on where your design system is going to be on the spectrum of flexibility &amp; constraints.</p><p>At Razorpay we have different product offerings which need to be consistent both in design &amp; functionality to provide a coherent experience for the users. We also have different verticals with different branding like Payments &amp; Banking which have to provide a similar experience but have distinct appearances, plus we also support various modes like Payment Dark mode &amp; Banking Light modes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-HxVwRoobG1Xj2W3csHcZw.png" /></figure><p>To ensure a similar experience across all products Blade leans more towards a constrained system which results in consistency in the experience.</p><p>While on other hand AdaptUI’s products were defined by the clients that Timeless.co worked with, and each client could have a different product, brand &amp; feel to it which needed to be addressed. Due to this reason, AdaptUI’s core goal was to be as flexible as possible to be able to cover all future use cases.</p><p>So depending on your organization’s products it’s crucial to make the right decision.</p><p><strong>2. Scope of The Design System</strong></p><p>Before jumping into building the design system, defining the scope is important.</p><ul><li>Does the design system support multiple platforms? Web, mobile apps, Watch, TV, etc.?</li><li>Does the design system need to support other frameworks? React, Angular, Vue, Svelte, etc.?</li><li>Should we provide other means of consuming tokens? CSS Vars? SASS/LESS tokens?</li></ul><p>While some of them you can tackle if the requirement arises, setting the scope early makes it easier to decide what kind of system we will be needing in the future - flexible or constrained?</p><p>These are the various factors which you can use to decide what kind of system is best for you.</p><h3>Lesson 2: API Design is HARD</h3><p>Designing developer-friendly APIs while catering to all the use cases and requirements is tremendously hard.</p><p>While at a glance even a Button component might look simple but when you go deep and solve for business requirements it gets exponentially harder to think of all the edge cases.</p><p>And then there’s “The Spectrum”, you might build a component with a very open &amp; flexible API and you might think it’s intuitive enough that users will use it as intended, but the sad reality is that most of the time if you don’t constrain the API in some way, introducing footguns are inevitable.</p><p>Designing Open APIs is important but it’s also essential that you nudge the developers to use it correctly by putting necessary guardrails in a certain way.</p><p>Let’s take an example of an &lt;Alert /&gt;:</p><figure><img alt="Image of blade Alert component" src="https://cdn-images-1.medium.com/max/1024/1*6KzfEDB7V4FJpOXVAUTJ9A.png" /></figure><p>A more open and flexible API may look like this:</p><figure><img alt="&lt;Alert&gt; &lt;AlertTitle&gt;International Payments Only&lt;/AlertTitle&gt; &lt;AlertDescription&gt; Currently you can only accept payments in international currencies. &lt;Link href=”https://razorpay.com&quot;&gt;Know More&lt;/Link&gt; &lt;/AlertDescription&gt; &lt;AlertPrimaryAction onClick={() =&gt; {}}&gt; Pay now &lt;/AlertPrimaryAction&gt; &lt;AlertSecondaryAction href=”https://razorpay.com&quot;&gt; Cancel &lt;/AlertSecondaryAction&gt; &lt;/Alert&gt;" src="https://cdn-images-1.medium.com/max/1024/1*3dDxHcEI0ZkKp9nzVOuzsA.png" /><figcaption>flexible Alert component API</figcaption></figure><p>This API is extremely flexible. Where users can literally change anything and everything they want to.</p><ul><li>Users can change the order of any of the JSX elements like AlertTitle, AlertDescription, PrimaryAction, SecondaryAction</li><li>Users can add any random JSX inside the AlertDescription</li><li>Users can pass href or onClick to the action buttons and it will render accordingly</li></ul><p>Basically, consumers can do anything and in a design system this can be problematic depending on where you want to be in “The Spectrum”.</p><p>Let’s change the API to be more constrained now:</p><figure><img alt="&lt;Alert title=”International Payments Only” primaryAction={{ label: “Pay now”, onClick: () =&gt; {} }} secondaryAction={{ label: “Cancel”, onClick: () =&gt; {} }} &gt; Currently you can only accept payments in international currencies. &lt;/Alert&gt;" src="https://cdn-images-1.medium.com/max/1024/1*rY3tJX4Oz9EER0FuJeqTcA.png" /><figcaption>Constrained Alert component API</figcaption></figure><p>There you have it. Now users can only have control over a few things and as a design system, we are being more consistent and constrained.</p><ul><li>Users are now bound to provide strings for the title and description.</li><li>Users cannot reorder any JSX elements as they like.</li><li>Users cannot mess up the primary and secondary action buttons.</li><li>Users cannot change any styling.</li></ul><p>It all depends on where you want to be In “The Spectrum”,</p><p>Blade sits on the far right of the spectrum and is more constrained and AdaptUI sits on the far left which is more flexible.</p><h3>Lesson 3: Low-Level APIs, High-Level Abstractions</h3><p>I love the idea that you build low-level APIs/Primitives and using them as a foundation, you can build high-level abstractions.</p><p>Surma has a beautiful article about this topic where he talks about <a href="https://surma.dev/things/cost-of-convenience/">“Layered Architecture”</a></p><h3>Anurag Hazra ⚛ on Twitter: &quot;&quot;Layered architectures&quot; is amazing. APIs can be created like a Ladder and each time you climb the ladder the abstractions get more opinionated and constrained thus covering wide use cases. if you want to be in a superposition of flexibility &amp; consistency this is the way. https://t.co/rziDGGFjy6 / Twitter&quot;</h3><p>&quot;Layered architectures&quot; is amazing. APIs can be created like a Ladder and each time you climb the ladder the abstractions get more opinionated and constrained thus covering wide use cases. if you want to be in a superposition of flexibility &amp; consistency this is the way. https://t.co/rziDGGFjy6</p><p>IMO this is a great way to architect your design system components if you are at least sitting somewhat close to the middle of “The Spectrum” or you can even be in a superposition of flexibility and constraint by exposing those low-level APIs to the user.</p><p>I learned about this pattern while I was working on AdaptUI, where our whole architecture was built around the notion of reusable hooks and primitives which can then be used to create higher-level abstractions. We even had two repositories — one which holds the primitives and core component logic (<a href="https://github.com/adaptui/react">AdaptUI/react</a>) and then the actual design system (<a href="https://github.com/adaptui/react-tailwind">AdaptUI/react-tailwind</a>)</p><p>I’ve talked about this pattern in detail in this interview at SurviveJS: <a href="https://survivejs.com/blog/renderlesskit-interview/">Renderlesskit React — Collection of composable headless hooks — Interview with Anurag Hazra</a></p><p>Let me show you a practical example. In <a href="https://medium.com/p/bb34289cd438#aba9">lesson 2</a> we saw how the Alert API can be built with two different goals in mind, Flexible vs Constrained.</p><p>We can introduce the “Layered Architecture” in that component too.<br>We can build the flexible low-level API first and on top of that we can build the high-level API:</p><figure><img alt="const Alert = ({ title, primaryAction, secondaryAction, children }) =&gt; { return ( &lt;AlertTitle&gt;{title}&lt;/AlertTitle&gt; &lt;AlertDescription&gt;{children}&lt;/AlertDescription&gt; {primaryAction &amp;&amp; ( &lt;AlertPrimaryAction onClick={primaryAction.onClick}&gt; {primaryAction.label} &lt;/AlertPrimaryAction&gt; )} {secondaryAction &amp;&amp; ( &lt;AlertPrimaryAction onClick={secondaryAction.onClick}&gt; {secondaryAction.label} &lt;/AlertPrimaryAction&gt; )} ) }" src="https://cdn-images-1.medium.com/max/1024/1*olviErjApvHz9IFuklLK8A.png" /><figcaption>Alert component API build with the low-level building blocks</figcaption></figure><p>Blade is a different beast altogether because it is a multi-platform design system.</p><p>It isn’t just for the web but it also works seamlessly on native too (without react-native-web). And we have patterns/abstractions which we’ve built to provide the same developer experience and look and feel to our components.</p><p>Recently we wrote the <a href="https://github.com/razorpay/blade/blob/master/packages/blade/src/components/Checkbox/_decisions/decisions.md">API decisions for Checkbox</a> which we released. While implementing it, we designed the architecture ahead of time to minimise the amount of effort to make it truly cross-platform. Thus internally we opted for a more low-level hook-based architecture for the Checkbox and built high-level abstractions on top of it.</p><p><a href="https://github.com/razorpay/blade/pull/544#issue-1301661653">See the Checkbox Architecture in this PR</a></p><p>We created useCheckbox &amp; useCheckboxGroup low-level hooks which work seamlessly across both platforms and then we use them to create higher-level components for each platform.</p><figure><img alt="Illustration of Checkbox Architecture" src="https://cdn-images-1.medium.com/max/1024/1*U0Nh_FaViqzOWOUbVRKK8w.png" /><figcaption>The architecture of the Checkbox Component</figcaption></figure><p>It’s a robust pattern.</p><h3>Lesson 4: Accessibility</h3><p>Probably the most important part of any design system or component library is ensuring Accessibility.</p><p>Building accessible components are hard. Therefore, baking accessibility right in the design system makes it easy for other developers/designers to build accessible products without much hassle, which in turn provides a better experience for the users.</p><p><strong>But why is accessibility important?</strong></p><p>At a surface level if your foundational components are inaccessible then the products that you build with those foundations will naturally result in an inaccessible experience for the users. But there’s more to it than that</p><p>Making our products accessible is important for two reasons:</p><ul><li>It’s the right thing to do. We believe that everyone deserves to be able to use our products, regardless of their abilities.</li><li>It’ll help build trust with our customers. By making our products accessible, we can expand our potential customer base and make our products more appealing to a wider range of people.</li></ul><p>In 2020, A 29-year-old banker who is visually impaired opened 2 petitions to make popular delivery apps accessible because they couldn’t order from them due to their inaccessible apps. Over 15,000 &amp; 27,000 people signed the petition. Later these apps listened to the people and improved their accessibility.</p><p>Following <a href="https://www.w3.org/WAI/ARIA/apg/patterns/">APG patterns</a>, testing accessibility, and ensuring that components work with the keyboard as well as screen readers is crucial for a component library since these are the foundations which will be used to build the products, A case study by W3C shows that building accessible digital products also have a <a href="https://www.w3.org/WAI/business-case/">positive business impact</a>.</p><p>At Razorpay we ensure that our products are accessible to as many people as possible, hence for Blade, we wrote a detailed <a href="https://github.com/razorpay/blade/blob/master/rfcs/2022-04-09-accessibility.md">Accessibility RFC</a> which covers most of the important parts of how we can build accessible components.</p><p>In Lesson 2, We talked about how API design can be hard. Turns out that you can even enforce Accessibility through good API design, and improve the developer experience.</p><p>For example, Say we have a Button component which can take an icon as a prop and the user can omit the children prop to make it look like an Icon only Button, but the problem is now the Button isn’t accessible to screen readers since it has no accessible label associated with it.</p><p>To solve this issue, we can leverage TypeScript to enforce that if the icon prop is present and the children prop is absent, make accessibilityLabel prop mandatory.</p><p><a href="https://tsplay.dev/WY31gN">TypeScript playground example</a>:</p><figure><img alt="Screenshot of 3 Button component showing how the TypeScript throws error depending on the icon prop. See the typescript playground attached above." src="https://cdn-images-1.medium.com/proxy/0*-5lmeGpPzq1xXajj" /><figcaption>TypeScript is throwing error in the 3rd button</figcaption></figure><p>All in all, building accessible components is probably the most important lesson I learnt while tinkering with reusable components. It did not just help me build better components but also helped me be a better FrontEnd engineer by teaching me to care more about accessibility in general.</p><h3>Lesson 5: Interoperability isn’t free</h3><p>While building Blade we’ve realized that building frameworks which are truly cross-platform is hard. We expect things to “just work” but in reality, some parts are scattered and disconnected from the system.</p><figure><img alt="Illustration with title “Interoperability in systems” showing exception is that things will just work but in reality there are disconnected pieces." src="https://cdn-images-1.medium.com/max/1024/1*_adkXp8KA6LCptH7TsBoDw.png" /></figure><p>Building components in a way that works on both platforms seamlessly is difficult, and more often than not it requires platform-specific code and special abstractions.</p><p>I’ve experimented with various ways to ease the process of creating cross-platform components and also proposed APIs like a polymorphic &lt;Box /&gt; component which can help us build these components.</p><h3>Anurag Hazra ⚛ on Twitter: &quot;Kinda digging this cross-platform polymorphic Box component. Just a POC, I&#39;ve been thinking of reducing the friction of writing cross-platform code in blade design system at @RazorpayEngg a lot lately. pic.twitter.com/hu6OHaSpk7 / Twitter&quot;</h3><p>Kinda digging this cross-platform polymorphic Box component. Just a POC, I&#39;ve been thinking of reducing the friction of writing cross-platform code in blade design system at @RazorpayEngg a lot lately. pic.twitter.com/hu6OHaSpk7</p><p>As you may have noticed this pattern goes hand in hand with Lesson 3, where we talked about low-level APIs with high-level abstractions, Where low-level APIs can be used to create high-level components that are truly cross-platform.</p><p>Though In blade for now we took the simplified approach to only have a simple internal &lt;Box /&gt; component since the above approach still has more than a few edge cases to handle.</p><h3>Fin</h3><p>Building design systems and designing complex APIs are very interesting challenges in themselves. While we continue to evolve and improve Blade we hope that you find some of these learnings useful. That’s all.</p><p>Checkout:</p><ul><li><a href="https://github.com/razorpay/blade">Blade Design System</a></li><li><a href="https://github.com/adaptui/react">AdaptUI</a></li></ul><h3>Come Work With Us 🚀</h3><p>If the work we do excites you, we are actively hiring for our engineering team. We are always looking out for great folks. Reach out to us at <a href="/service/mailto:tech-hiring@razorpay.com">tech-hiring@razorpay.com</a> or head over to our <a href="https://razorpay.com/jobs/">jobs page</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb34289cd438" width="1" height="1" alt=""><hr><p><a href="https://engineering.razorpay.com/lessons-learned-from-building-reusable-ui-components-bb34289cd438">Lessons learned from building reusable UI components</a> was originally published in <a href="https://engineering.razorpay.com/">Razorpay Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making a Verlet Physics Engine in JavaScript]]></title>
            <link>https://medium.com/better-programming/making-a-verlet-physics-engine-in-javascript-1dff066d7bc5?source=rss-bf2e65f6d437------2</link>
            <guid isPermaLink="false">https://medium.com/p/1dff066d7bc5</guid>
            <category><![CDATA[javascript-frameworks]]></category>
            <category><![CDATA[physics-engine]]></category>
            <category><![CDATA[physics]]></category>
            <category><![CDATA[verlet]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Anurag Hazra]]></dc:creator>
            <pubDate>Thu, 16 May 2019 15:46:31 GMT</pubDate>
            <atom:updated>2019-07-05T10:17:46.482Z</atom:updated>
            <content:encoded><![CDATA[<h4>Taking a look under the hood of a physics engine</h4><p>Have you ever wondered if you can make your own physics engine in JavaScript? If so, you have come to the right place. We are going to build a Physics engine from scratch in JavaScript.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Pc5hJ1_RuRY_Ugdh" /><figcaption>Photo by <a href="https://unsplash.com/@cakirchoff?utm_source=medium&amp;utm_medium=referral">Chad Kirchoff</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Before we start, I should mention that this tutorial assumes you have a good understanding of Vectors.</p><p>Don’t worry if you do not yet have this understanding — Vectors are simple: <a href="https://anuraghazra.github.io/CanvasFun/src/lib/Vector.js">get the Vector.js</a></p><h3><strong>What Is Verlet Physics?</strong></h3><p>According to Wikipedia:</p><blockquote><strong>Verlet integration</strong> is a numerical method used to integrate Newton’s equations of motion. It is frequently used to calculate trajectories of particles in molecular dynamics simulations and computer graphics. The algorithm was first used in 1791 by Delambre and has been rediscovered many times since then, most recently by Loup Verlet in the 1960s for use in molecular dynamics.</blockquote><p>In simple terms, Verlet physics is just a system of connected dots.</p><h4>In a Verlet system, we have 2 main components:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*k5B1cW0drtG6GNhBMW9Z5A.png" /><figcaption>A Box. Connected With Dots And Sticks</figcaption></figure><ol><li>Points (dots)</li><li>Constraints (sticks(</li></ol><h4>Dots</h4><p>The dots have very simple physics applied to them.</p><p>We have to keep track of the dots’ current and old <strong>positions </strong>to add the physics behavior to them — you&#39;ll see this when we actually implement it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef77764f064dae0d2080fba7d8c31250/href">https://medium.com/media/ef77764f064dae0d2080fba7d8c31250/href</a></iframe><p>We have the basic setup, now let&#39;s render the dots <em>and </em>make them move.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24afe2f1b87599b520d1ccf5dcc1aba6/href">https://medium.com/media/24afe2f1b87599b520d1ccf5dcc1aba6/href</a></iframe><p>The update function will update the position and handle the physics of the dot.</p><p>Basically, to do Verlet integration we have to calculate velocity based on dots old position.</p><p>In the first line, we are <strong>subtract the current position from the old position to get the desired velocity. </strong>After calculating the velocity, we apply friction to the dots so they come to rest instead of sliding forever.</p><p>Then, we update the old position by saying this.oldpos.setXY(this.pos.x, this.pos.y)<strong> </strong>and add the velocity and gravity to position.</p><p>We also want to make them stay inside the canvas so we have to add some checks. We will also add another function: constrain():</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c10cdb46f3209aa0e1b8e68a4a537805/href">https://medium.com/media/c10cdb46f3209aa0e1b8e68a4a537805/href</a></iframe><p>Let’s add the render method too:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16ccc89045a5c83a669b7ebaaff33f79/href">https://medium.com/media/16ccc89045a5c83a669b7ebaaff33f79/href</a></iframe><p>Setting up:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5de8e92be84ed0b812e6814c1bc6dc8b/href">https://medium.com/media/5de8e92be84ed0b812e6814c1bc6dc8b/href</a></iframe><p>We added a lot more dots in random positions and then update() constrain() render() them.</p><p>Let&#39;s see what that looks like:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fanuraghazra%2Fembed%2Fpreview%2FVOpJJR%3Fheight%3D600%26slug-hash%3DVOpJJR%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2Fanuraghazra%2Ffull%2FVOpJJR&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fm.cdpn.io%2Fscreenshot-coming-soon-small.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/7044fbf3769ea60c2c5fe35f739bb74a/href">https://medium.com/media/7044fbf3769ea60c2c5fe35f739bb74a/href</a></iframe><p>Nice — it&#39;s just a start but finally we have something.</p><p>Now we are going to add the sticks!</p><h4>Sticks</h4><p>Sticks are the core of Verlet physics. Sticks make sure that the dots don’t get too far or too close to each other — they constrain dots to a certain distance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*VuR7pK6avYSxN6eXMqHYqQ.png" /><figcaption><a href="https://slsdo.github.io/blob-family/#constraint">https://slsdo.github.io/blob-family/#constraint</a></figcaption></figure><p><strong>Stick.js</strong> class is fairly simple. It will take two <strong>Dots </strong>as an argument and a length. but if the length is not defined we will calculate the length based on the dot’s position.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7352bf30e8f9079b500f7be8825355af/href">https://medium.com/media/7352bf30e8f9079b500f7be8825355af/href</a></iframe><p>Now let&#39;s add the actual core of the algorithm. This resolves and updates the dot’s position based on the stick’s distance, ultimately constraining it to a certain distance from all other dots.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fe0e615786bd773909e521027ed929d/href">https://medium.com/media/3fe0e615786bd773909e521027ed929d/href</a></iframe><p>Okay, I think we are good to go! Let’s add the render function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44a7e53d77e53e4ed5c8beba5e7eb136/href">https://medium.com/media/44a7e53d77e53e4ed5c8beba5e7eb136/href</a></iframe><p>Setting up:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a35df6607ecd1fc419d5cb142b842d21/href">https://medium.com/media/a35df6607ecd1fc419d5cb142b842d21/href</a></iframe><p>Let’s see the results:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fanuraghazra%2Fembed%2Fpreview%2FqGmWmZ%3Fheight%3D600%26slug-hash%3DqGmWmZ%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2Fanuraghazra%2Ffull%2FqGmWmZ&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fm.cdpn.io%2Fscreenshot-coming-soon-small.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb505a4a5d364a4ceb57e48ec679d500/href">https://medium.com/media/fb505a4a5d364a4ceb57e48ec679d500/href</a></iframe><p>As you can see, we have a box! Well, a box that acts like it’s made of Jello, anyway. That’s because in a single frame, one update per stick is not enough to make the dots rest at their length. We have to iterate the simulation as many times as we can — the more the iterations, more the rigid box will be.</p><p>Adding these lines to the existing code will make the box rigid:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86350fcc714cf79b7e3648c76f131aa3/href">https://medium.com/media/86350fcc714cf79b7e3648c76f131aa3/href</a></iframe><p>Okay, let&#39;s take a look at an updated example:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fanuraghazra%2Fembed%2Fpreview%2FgJWpmX%3Fheight%3D600%26slug-hash%3DgJWpmX%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2Fanuraghazra%2Ffull%2FgJWpmX&amp;image=https%3A%2F%2Fscreenshot.codepen.io%2F1747435.gJWpmX.small.2c7aa292-c1f4-42cb-9a52-b30dcfe3e558.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5144a16b8a137585d538eada06abbc1/href">https://medium.com/media/e5144a16b8a137585d538eada06abbc1/href</a></iframe><p>Looks amazing, doesn’t it?</p><h3>Entity.js: managing dots and sticks in one place</h3><p>Okay, now we have Dot.js and Stick.js. Both are working nicely but the problem is we don&#39;t have much control over <em>how </em>we use them.</p><p>We will create an Entity Class which will easily handle the Updates and Renders of the Verlet Object. I&#39;m going to paste the whole code here — it’s nothing special:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af3c4a77760262523c84535e76922c3d/href">https://medium.com/media/af3c4a77760262523c84535e76922c3d/href</a></iframe><p>Using the Entity Class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1fefeb96cb0fe15828f7adbae350f31/href">https://medium.com/media/d1fefeb96cb0fe15828f7adbae350f31/href</a></iframe><p>Yes, this is looking pretty clean and manageable!</p><p>Let&#39;s take a look at the final result:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fanuraghazra%2Fembed%2Fpreview%2FJqNdQW%3Fheight%3D600%26slug-hash%3DJqNdQW%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2Fanuraghazra%2Ffull%2FJqNdQW&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fm.cdpn.io%2Fscreenshot-coming-soon-small.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a4156b554851e228863ac327b01d6f8/href">https://medium.com/media/0a4156b554851e228863ac327b01d6f8/href</a></iframe><p>Now we can create lots of fun Verlet Shapes with this Entity.js class!</p><h3>Verly.js: A physics engine I wrote</h3><p>Verly.js is a Robust Verlet physics engine I wrote. It has many cool features:</p><ol><li>Attraction — Repulsion behavior</li><li>Basic Shapes</li><li>Box, Hexagon, Cloth, Rope, Ragdoll</li><li>Cloth Tearing</li><li>Typography and Text</li></ol><p>With Verly.js you can create a tearable cloth in just 15 lines of code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a8ee78e1a1a2d8222ce1a01ad8d0233/href">https://medium.com/media/4a8ee78e1a1a2d8222ce1a01ad8d0233/href</a></iframe><p>Demo: <a href="https://anuraghazra.github.io/Verly.js/">https://anuraghazra.github.io/Verly.js/</a></p><p>Source-code: <a href="https://github.com/anuraghazra/Verly.js">https://github.com/anuraghazra/Verly.js</a></p><p>Examples: <a href="https://anuraghazra.github.io/Verly.js/examples">https://anuraghazra.github.io/Verly.js/examples</a></p><p>Take a look at some of the examples in my <strong>Verly.js</strong> Physics Engine which I created some time ago. I added almost everything in that engine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/262/1*alaeeMAe-pnmA08SKb2R4g.png" /><figcaption>Spherical Shapes <a href="https://anuraghazra.github.io/Verly.js/examples">anuraghazra.github.io</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*k13KJb85HxqbqTHvY994uw.png" /><figcaption>Attraction Repulsion Behavior <a href="https://anuraghazra.github.io/Verly.js/examples">anuraghazra.github.io</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/133/1*MMj1DB0-6UXqAuZgyijlHw.png" /><figcaption>RagDoll <a href="https://anuraghazra.github.io/Verly.js/examples">anuraghazra.github.io</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/339/1*NbJsaxITUjGfx8v8YdUb-Q.png" /><figcaption><a href="https://anuraghazra.github.io/Verly.js/examples">anuraghazra.github.io</a></figcaption></figure><p>Codepen examples:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fanuraghazra%2Fembed%2Fpreview%2FvwmLKo%3Fheight%3D600%26slug-hash%3DvwmLKo%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2Fanuraghazra%2Ffull%2FvwmLKo&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fm.cdpn.io%2Fscreenshot-coming-soon-small.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/23b650fbed5071c4553a9a02d1df4399/href">https://medium.com/media/23b650fbed5071c4553a9a02d1df4399/href</a></iframe><p>Verly.js’s API is easy to use and flexible because of its Entity Component Structure.</p><p>Thanks for reading — I hope you learned something!</p><p><strong>Other places to learn about Verlet physics:</strong></p><ol><li><a href="https://www.youtube.com/watch?v=3HjO_RGIjCU">Keith Peter’s CodingMath Verlet Physics Videos</a>.</li><li>Amazing Article At DataGenetic <a href="http://datagenetics.com/blog/july22018/index.html">http://datagenetics.com/blog/july22018/index.html</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1dff066d7bc5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/making-a-verlet-physics-engine-in-javascript-1dff066d7bc5">Making a Verlet Physics Engine in JavaScript</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>