<?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 Muhammad Ahmed on Medium]]></title>
        <description><![CDATA[Stories by Muhammad Ahmed on Medium]]></description>
        <link>https://medium.com/@ahmedkhann?source=rss-b145da09cdce------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*xBqpRI-qBrrUhHCn7vRB1g.jpeg</url>
            <title>Stories by Muhammad Ahmed on Medium</title>
            <link>https://medium.com/@ahmedkhann?source=rss-b145da09cdce------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 15:26:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ahmedkhann/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[Standardizing Front-end Starter Packs @Emumba]]></title>
            <link>https://medium.com/emumba/standardizing-front-end-starter-packs-emumba-a70c49a2e8ad?source=rss-b145da09cdce------2</link>
            <guid isPermaLink="false">https://medium.com/p/a70c49a2e8ad</guid>
            <category><![CDATA[nx]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[frontend-tools]]></category>
            <dc:creator><![CDATA[Muhammad Ahmed]]></dc:creator>
            <pubDate>Sun, 24 Mar 2024 09:13:37 GMT</pubDate>
            <atom:updated>2024-03-25T09:49:55.497Z</atom:updated>
            <content:encoded><![CDATA[<p>Embracing Standardized Practices for Quality and Innovation at Emumba</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I94gYZ3MuTvQ3wusT0bzkQ.png" /></figure><h3>Problem Statement</h3><p>Almost every team at Emumba follows its own set of practices. We don’t have any way to standardize the set of practices or tools that should be used for the project. Due to this, there are lapses in the engineering quality, resulting in a lot of technical debt. We cannot govern and evangelize this because we don’t have any “<strong>standardized rules</strong>” or any reference point on which we have a consensus of all stakeholders.</p><h4>Making Things Easier with Starter Packs and Clear Rules</h4><p>At first, I thought using starter packs would fix our problem. Starter packs help, but they only solve part of the issue. We realized we also needed some clear rules and to agree on using certain tools and libraries that everyone should stick to. Here are a few examples:</p><ul><li><strong>How We Name Things:</strong> We should use camel case for naming variables. This means the first word is all lower case and the first letter of each new word is capitalized, like thisIsAnExample.</li><li><strong>Choosing Libraries Wisely:</strong> Try not to use a whole library just for one small thing. For example, don’t add lodash or ramda if you’re only going to use one function from it. This helps keep our project size smaller.</li><li><strong>Design Choices: </strong>Let’s use Material UI &amp; AntD for our designs.</li><li><strong>Handling APIs:</strong> For managing APIs, it’s good to use useSWR or react-query.</li></ul><p>These are just a few rules, and there might be more!</p><p>Of course, sometimes a team might need to do things a bit differently. For example, if a team wants to use ChakraUI instead of Material UI &amp; AntD, they can talk to the architects’ committee. The committee will see if there’s a good reason for the change.</p><p>After setting these rules, the next step is to make things easier by automating them. We could use a starter pack or a special tool (CLI) for this.</p><p>We evaluated two options in this case:</p><h4>Creating a Custom CLI Tool: A Road Not Taken</h4><p>We had an idea to make things even easier for everyone starting new projects. We thought about making a special tool that would help set up new projects quickly. This tool would work with create-react-app, create-vite-app, and angular-starter-scripts to give people a ready-to-go package.</p><p>Here’s how it would work: The tool would ask you a few questions, like:</p><ul><li>What’s the name of your project?</li><li>Is this going to be a library or a standalone app? Will it be part of a bigger project (a monorepo) or just by itself?</li><li>If it’s a monorepo, which system should organize it? (We thought about using Lerna or Turborepo.)</li><li>Do you want to use React or Angular for the front end?</li><li>Which tool do you prefer for bundling your code, Vite or Webpack?</li><li>Do you need CI (Continuous Integration) scripts included?</li></ul><p>…and a few more based on what you need.</p><p>However, we realized this tool would be a bit complicated to learn and use. It would also require us to put in a lot of effort to create and then keep updating it in the future. Because of these challenges, we decided not to go ahead with this idea.</p><h4>Discovering Nx: The Solution We Were Looking For</h4><p>While searching for alternatives to our starter pack idea, I stumbled upon a fantastic tool that looks like it could solve our problem. So, what is Nx exactly?</p><p>Nx is a smart building tool made for modern JavaScript development. It’s really good for projects where you want to keep all your work in one place (called a mono repo), which means you can manage several projects under a single roof. Nx is great because it’s very flexible; it can work with a bunch of different tools and frameworks like Angular, React, Next.js, and NestJS.</p><p>I tried making a new React app with Nx and it was super fast. I just had to pick a few options and in no time, my app was ready to go. Here’s an example of how I did it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MoXXeLYM0pn_pKgs" /><figcaption>Creating a standalone react app using Nx CLI</figcaption></figure><p><strong>How Generators Make Nx Even Better</strong></p><p>Nx uses something called generators to make our work a lot easier. Generators are like helpful robots that do tasks for us. They can create new parts of our project, like components, services, and modules. They can even set up whole new apps or libraries inside our mono repo. This is great because it saves us time and makes sure everything in our project looks and works the same way, following the rules we set.</p><p>Want to see some examples? Check out these links:</p><ul><li><a href="https://nx.dev/packages/react/generators/redux">Nx React Redux Generator</a></li><li><a href="https://nx.dev/packages/react#generators">More Nx React Generators</a></li></ul><p>There’s a whole bunch of these generators in Nx, and we can use them to make our project better.</p><h4><strong>Introducing Emumba Nx Plugin:</strong></h4><p>After looking at our options, we decided Nx was the best choice for us. Why? Because it lets us customize pretty much everything. We were able to create our own templates for libraries and follow the best practices we had already set. Then, we used Nx’s custom generators to automatically make the starting point for our projects based on these templates.</p><p>We set up a special plan (or “schema”) for how things should be done. When someone starts a new project and picks their options, our custom generators use this plan to create exactly what they need.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/0*yW6_94CHRl561lPs" /></figure><p>This way, we don’t have to spend a lot of time keeping things up to date. Nx does most of the work for us. We only need to check now and then to make sure our templates are still good to go.</p><p>Want to know more? Check out our project repository at <a href="https://github.com/emumba-com/emumba-nx-boilerplate-plugin">Emumba’s Nx Boilerplate Plugin</a>.</p><h3>Exciting News: Open Source Contributions Welcome at Emumba!</h3><p>Great news! Emumba is now open for open-source contributions, especially for our Emumba Nx plugin. We’re excited to see what you can bring to the table.</p><p>We’ve laid out all the guidelines and the code of conduct in our repository. So, if you’re interested in contributing, feel free to take a look, fork the repo, and submit a pull request. We can’t wait to see your ideas and work together to make something great.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a70c49a2e8ad" width="1" height="1" alt=""><hr><p><a href="https://medium.com/emumba/standardizing-front-end-starter-packs-emumba-a70c49a2e8ad">Standardizing Front-end Starter Packs @Emumba</a> was originally published in <a href="https://medium.com/emumba">Emumba</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Your Front-end needs a BFF!]]></title>
            <link>https://medium.com/emumba/your-front-end-needs-a-bff-6e6fbce6afdd?source=rss-b145da09cdce------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e6fbce6afdd</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[backend-for-frontend]]></category>
            <category><![CDATA[full-stack]]></category>
            <category><![CDATA[mach]]></category>
            <category><![CDATA[frontend]]></category>
            <dc:creator><![CDATA[Muhammad Ahmed]]></dc:creator>
            <pubDate>Wed, 22 Mar 2023 09:54:48 GMT</pubDate>
            <atom:updated>2023-04-17T11:55:17.283Z</atom:updated>
            <content:encoded><![CDATA[<p>Confused ?!?, BFF means <strong>Back-end for Front-end.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wf2i2-frUSfLICUoySWq_g.png" /></figure><h3><strong>Problem with microservices</strong></h3><p>Microservices have become widely popular in these recent years. Almost every project is moving towards it. Companies with giant <strong>monoliths</strong> plan to convert parts of monoliths to small microservices.</p><p>But… As we grow, we often add <strong>complexity</strong> to microservices by adding unnecessary <strong>endpoints</strong> and <strong>business logic</strong>.</p><p>Imagine a scenario below…</p><p><strong>Front-end &amp; Back-end Dilemma:</strong></p><p>For example, we have a <strong>data table</strong> that we need to visualize. Data has to come from <strong>multiple data sources</strong>. And then, the data also needs to be <strong>transformed</strong> according to the UI Design.</p><p>Now, <strong>Front-end Engineer</strong> will go to the <strong>Back-end engineer</strong> to provide the API. The Back-end engineer will go to other devs to provide the required <strong>interfaces</strong> to get data from <strong>other services</strong>. It will visually look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*ZkDbvXf-8PsX38NCvz1klg.png" /></figure><p>Meanwhile, an argument starts about whether the <strong>data transformations</strong> should be done on <strong>Back-end</strong> or <strong>Front-end</strong>.</p><p><strong>Back-end Engineer</strong> says it will add extra complexity on the API Level.</p><p><strong>Front-end Engineer</strong> says it will slow down the page performance. Data will take <strong>a lot of time</strong> to load. And there will be <strong>slow response</strong> times.</p><p>This is a <strong>classical</strong> Back-end x Front-end dialogue. And if things heat up, you will even find them fighting for this like teenage kids 😄.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RFRRKqCOxVINNgA9TREwcQ.jpeg" /><figcaption>Generated with Mid Journey v5</figcaption></figure><p>Let me tell you something…</p><p>Both of them are <strong>right</strong> about their arguments!</p><p>But then, what should be the other way?</p><p>When there is no other <strong>alternative</strong>, Front-end devs usually have to pay the price. This is one of those situations where the Front-end dev says “<a href="https://blog.emumba.com/i-hate-front-end-development-2b7a9ff4704e"><strong>I hate Front-end development</strong></a>”. 😄. <em>(Check out the blog by </em><a href="https://medium.com/u/ba0f6f3fd01"><em>Ahmed Waleed</em></a><em> in the link)</em></p><p>Luckily, I have a <strong>solution</strong> to all this fuss. i-e <strong>BFF (Backend for Frontend)</strong></p><h3>What is BFF (Backend for Front-end)?</h3><p>In simple terms, it is a Back-end for all Front-end-related <strong>data transformations</strong> and other <strong>performance-related</strong> non-UI things. Usually, we tend to use <strong>web workers</strong>, but they obviously have some limitations.</p><p>Let&#39;s <strong>reimagine</strong> the earlier example with <strong>BFF</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7PV8UC0OfUC8gAR6A7OwWg.png" /></figure><p>Now, as we see, we have a <strong>BFF</strong> that takes care of all the <strong>data fetching</strong> and <strong>transformations</strong>, <strong>Front-end</strong> is much <strong>lighter</strong> weight now as it is only responsible for <strong>UI-related operations</strong>.</p><p><strong>Benefits of using BFF:</strong></p><ol><li>It hides the overall <strong>complexity</strong> of multiple microservices from the Front-end. Front-end only needs to deal with the BFF.</li><li>Keeps the microservices <strong>simple</strong>. It means that a microservice should do a <strong>defined job</strong>. It shouldn’t be responsible for handling <strong>extra complexity</strong> related to data transformations.</li><li>It takes the <strong>heavy load</strong> away from the Front-end, making it more <strong>performant</strong>.</li><li>Last but not least, It prevents Back-end &amp; Front-end devs from <strong>fighting</strong> each other. 😄</li></ol><p><strong>Who should take ownership of the BFF?</strong></p><p>Usually, the Front-end team is also responsible for <strong>maintaining</strong> the BFF because there are times when there is more than <strong>one client for a single app</strong>. In this case, there will be a <strong>single BFF</strong> but there can be <strong>multiple</strong> clients, For example, a web app, mobile app &amp; desktop app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tNAHI7wZOuNQDJkyGsKbvg.png" /></figure><p>But then, there is a question arises that there will be an <strong>overhead</strong> of maintaining a separate <strong>node.js app</strong> for BFF. How to cater this extra <strong>responsibility</strong>?</p><p><strong>Enter; Full-Stack Frameworks</strong></p><p>This is where <strong>Full-stack frameworks </strong>come in handy. e.g Next.js, Nest.js, Nuxt.js, etc.</p><p>We often <strong>argue</strong> to not use these frameworks because we don’t have full-stack apps. We have <strong>separate</strong> microservice-based back-ends and separate front-end <strong>clients</strong>.</p><p>But, as the BFF becomes more common, these frameworks will become more <strong>mainstream</strong>. These frameworks provide a proper <strong>ecosystem</strong> to support a Back-end for your Front-end.</p><p>We can even render some parts of our pages on the <strong>server</strong> and other parts on the <strong>client</strong>. This will totally <strong>change the way</strong> we think about <strong>designing</strong> our client apps.</p><h4>Conclusion</h4><p><strong>React</strong> is evolving day by day. In the next <strong>4–5 years</strong>, we will be using full-stack frameworks in our <strong>routine</strong> development. So, it is <strong>better</strong> to be well <strong>prepared</strong> for that. Otherwise, <strong>ChatGPT</strong> will replace us 😜. Just Kidding 😄.</p><p>If you want to study more about this, do check out these videos:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FWk5Ln_WRKTE%3Fstart%3D467%26feature%3Doembed%26start%3D467&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DWk5Ln_WRKTE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FWk5Ln_WRKTE%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1ce016bbaf44ae71b6e8f04b563905b/href">https://medium.com/media/e1ce016bbaf44ae71b6e8f04b563905b/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FSSo-z16wEnc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DSSo-z16wEnc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FSSo-z16wEnc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f2d0dc8c30c701ec7d828fff96d9d88/href">https://medium.com/media/7f2d0dc8c30c701ec7d828fff96d9d88/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e6fbce6afdd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/emumba/your-front-end-needs-a-bff-6e6fbce6afdd">Your Front-end needs a BFF!</a> was originally published in <a href="https://medium.com/emumba">Emumba</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A ride into the world of Graphics]]></title>
            <link>https://medium.com/emumba/a-ride-into-the-world-of-graphics-4be80ddce528?source=rss-b145da09cdce------2</link>
            <guid isPermaLink="false">https://medium.com/p/4be80ddce528</guid>
            <category><![CDATA[webgl]]></category>
            <category><![CDATA[svg]]></category>
            <category><![CDATA[graphics]]></category>
            <category><![CDATA[svg-animation]]></category>
            <dc:creator><![CDATA[Muhammad Ahmed]]></dc:creator>
            <pubDate>Fri, 12 Jul 2019 11:44:21 GMT</pubDate>
            <atom:updated>2019-11-15T12:08:53.376Z</atom:updated>
            <content:encoded><![CDATA[<p>Sit tight! It’s going to be a bumpy ride.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sWpV1j5hkIQo6cCR" /><figcaption>Photo by <a href="https://unsplash.com/@qa9de?utm_source=medium&amp;utm_medium=referral">Mark Asthoff</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Graphics always gave me a hard time but it also fascinated me all the same. The more I wanted to get away from it, the more it came naturally to me. And now, it is one of my strongest skills. From <strong>Unity3D</strong> to <strong>SVG</strong> to <strong>WebGL</strong>, it has been a roller coaster ride for me and still continues to be that way.</p><p>Today, I will tell you some of my technical experiences in this “roller coaster” ride. Are you ready?</p><h3>A Tale of Inexperience</h3><p>My first encounter with graphics was way back in 2014. This was a time when I was very excited about graphics and felt that it was a very <strong>COOL</strong> thing to do. Lost in that enthusiasm, I started an ambitious project of simulating brain surgery. This project was for medical students who were learning neurosurgery. The simulations had to be interactive and very accurate. So, I took help from a neurosurgeon for its authenticity. My enthusiasm didn’t end there, I also decided to add Virtual Reality just for that little extra spice!</p><p>Imagine you aboard a roller coaster ride but forgot to wear the safety gear. Well, that’s what happened to me. People do things in excitement without knowing its consequences. It’s human psychology! But that was too much. I fell, and I fell badly.</p><p>Days before my demo, I found out that the animations I built cannot be run on a mobile device due to lack of hardware capacity. This is where I got very frustrated and eventually started to lose interest in graphics. I didn’t know what to do with the project. I had simulations ready but no way to integrate it. I tried several techniques like optimizing the mesh structure of 3D-Models but that was of no use. The simulations were highly complex and not supported on mobile hardware.</p><p>I was very worried until one day…</p><h3>Arrival of Comic Book Renderer</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*1iGsfLIdby1L6OM931Ql9g.gif" /><figcaption>Sample Flip book animation</figcaption></figure><p>I was watching a movie and there I saw a kid doing the flip book animation <strong><em>(like the one you see above)</em></strong>. I was sleepy at that point so I didn’t pay attention to that. But the next morning, suddenly I got an idea of what is now called “<strong>Comic Book Renderer</strong>”.</p><p>The main idea of this renderer can be summed up into three points:</p><ul><li><strong>Identify the frames: </strong>Extract/Load all frames from an animation/video or from raw animation data.</li><li><strong>Transform data:</strong> Transform the data according to your requirements.</li><li><strong>Async rendering:</strong> Make a thread cycle (asynchronous) to load the frames one by one at a certain speed/interval (fps) so that it feels like an animation/video.</li></ul><p>That’s it! This is the basic idea of the renderer. The idea is so generic that the applications are immense. Some applications that I have implemented are as below:</p><ol><li>We can add interactivity by pausing the thread at a certain frame and then asking the user to do some task <strong>(e.g. drag something to the image)</strong> and based on the result a new branch of animation is executed. This gives the user a feeling as if he/she is in a simulation. This is what I have implemented in my project to complete it and the renderer was very efficient in terms of performance.</li><li>You can also bind different events to the image frames. For example, you can bind scroll event to frames. Now, on scroll change, you can change the frames asynchronously so that it feels like it is moving with scroll. This <strong>UX</strong> is more demanding in modern day websites.</li><li>Applications in raw animations: <strong>SVG and WebGL</strong>. These are quite complex problems that I solved using comic book renderer. So, I will explain them in separate sections below.</li></ol><h3>The Horror of Moving SVG</h3><p>Every time we hear the word <strong>SVG</strong>, we think of an image which will not pixelate when we zoom into it. I also used to think that this is very easy as we have to use some shapes, give them some coordinates and we’re done! But, what about animations? Can we do that in SVG ? This was the same question I remember asking myself a few months back.</p><p>It turns out that we can do that very easily using <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/animateTransform"><strong>animateTransform</strong></a><strong> </strong>element of SVG, provided that we have animation sequence values.</p><blockquote>e.g (sequence of translation of object [x1,y1,x2,y2, … , xN, yN])</blockquote><p>And we also have some other properties like type of transformation (rotation, translation etc), duration, repeat count etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*5ukyII4xxsTKT7hOZC2X5A.gif" /><figcaption>Example of a simple animation using animateTransform</figcaption></figure><p>In the above animation, we have simply set repeat count to <strong>infinite, </strong>and then gave the animation a sequence of <strong>( 0,120 0,0 )</strong>. This means that first go to <strong>0,120 </strong>and then come back to <strong>0,0</strong>. As, we have set repeat count to <strong>infinite, </strong>it will never stop.</p><p>Simple, yes? But there is a twist!</p><h3>The Moving Frame Problem</h3><p>Imagine that you have to move 50 balls like above in one go. First, you have to calculate each and every coordinate you have to give in the animation sequence values. And let’s say you have calculated all that, but it will be hard-coded for just 50 balls.</p><p>Sounds complex? Let’s add some more spice to it.</p><p>The positioning data for balls is coming dynamically from the server. It means it’s all live animation. You will get data from server after specific interval of time and you have to animate it in real time.</p><p>This is where the real fun starts. Now, before writing any code, first we have to understand how the server is talking to us. <strong>(We are using a websocket based server)</strong></p><ol><li>First, it gives us the initial positions of the balls.</li><li>Then after a 1 sec interval, it gives us 10 positions for each ball at which a ball has to translate/move.</li></ol><p>Now, lets see how we can write a parser. The positions that the server is returning are not coordinates, they are points on a <strong>100x100</strong> grid. We have to translate that into our coordinate system.</p><p>Let’s say we have a <strong>500x500 SVG grid</strong>, then we have to multiply each of our position to 500, so that it maps onto the SVG. And as I am using react, I managed all my coordinates in the component’s state. Now, every time the state changes, it will re-render the points.</p><p>First thing done! Now, we have to calculate the animation sequence. This is tricky now, as we have to loop through every item in the positions and then normalize that on our SVG grid.</p><p>But this will work the first time only! Any idea why?</p><p>Server is returning us the next positions and after the end of each animation, a ball will be on the final position of last animation. Let me show you via diagram.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/505/1*573CPBSJa7b8lvKtth4Pcg.png" /></figure><p>So, it creates a very annoying jerk in the animation making it not-so-smooth. So, we have to also find a way to preserve the last point and then append it in the next animation. For that, we have to keep the previous animation state in memory.</p><h4>Its all about how you design a single frame</h4><p>Now, as we feed these values to <strong>animateTransform</strong> of each point, it will start animating the point frame by frame. In the first frame, only values in bold will be executed.</p><p>(<strong>1</strong>,2,3,4,5,6,7,8,9,10)<br>(<strong>1</strong>,2,3,4,5,6,7,8,9,10)<br>(<strong>1</strong>,2,3,4,5,6,7,8,9,10)<br>(<strong>1</strong>,2,3,4,5,6,7,8,9,10)<br>(<strong>1</strong>,2,3,4,5,6,7,8,9,10)</p><p>In the second frame, the second column values will be rendered (values in bold).</p><p>(1,<strong>2</strong>,3,4,5,6,7,8,9,10)<br>(1,<strong>2</strong>,3,4,5,6,7,8,9,10)<br>(1,<strong>2</strong>,3,4,5,6,7,8,9,10)<br>(1,<strong>2</strong>,3,4,5,6,7,8,9,10)<br>(1,<strong>2</strong>,3,4,5,6,7,8,9,10)</p><p>Now, the way web rendering works is that when you give it some values to animate, it will request an animation frame from the browser context one by one to render it.</p><p>Now, you can observe that I have arranged the values frame by frame without thinking about their direction and other parameters. This approach abstracts away all this information. When you have translated the actual coordinates then the direction and other parameters don’t matter.</p><p>The only thing that matters is…</p><blockquote>How you design a single frame</blockquote><h4>Queuing the SVG Performance</h4><p>Now, as we are adding things to the parser, it will become very costly to perform all the calculations on runtime. Imagine we are receiving data of 100 balls, each ball has 10 positions that we have to animate and we have to preserve the previous positions of 100 balls.</p><p><strong>BOOM! </strong>It will explode at some point.</p><p>Also, as we will be receiving new data every second, so doing all these calculations and animations in 1 second is not feasible.</p><p>This is where I fulfilled my long lasting dream of making a queuing system in JS. Sidenote: I am still working to make it a <strong>general purpose queuing engine</strong>. Still a lot of work remaining.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*MFzZ0o5nG_HgZ62xvrz9Uw.png" /><figcaption>Queuing Engine Pipeline</figcaption></figure><p>Now, let me explain how this queuing mechanism works.</p><ol><li>A Web Socket server (API) feeds data to the dispatcher after some specific interval.</li><li>Dispatcher sends that to data processor which transforms raw data to animation data.</li><li>After processing, it puts the data to a queue that is managed by your application state (e.g. Redux)</li><li>Now, if we look at the other side, UI is showing some animation and it takes a specific time to show an animation. So, it pulls out one entry from the queue every time an animation ends.</li></ol><p>This mechanism made the animations more controllable. For example, I can control the speed of animation and control the amount the animation data to be distributed in the queue.</p><p>Like I can dump all animation information in one queue item, which will cause a lot of load on front-end and it will affect the memory consumption and speed. On the other hand, I can divide them into small items, which will be a lot more efficient and fast.</p><p>This queuing engine, again, is a generic idea and can be implemented in various data pipeline problems. This also somehow relates to the stream processing architecture we already have for data processing pipelines.</p><h3><strong>Conclusion</strong></h3><p>So, this was the roller coaster ride that I wanted to share with everyone. I am still going through this but now I am enjoying it. The joy of solving such complex problems inspires me. Now, if i look back… Actually, I don’t look back now. It’s all about what I can do moving forward.</p><p>About this Comic book renderer and Queuing Engine: I have used them for my various projects in WebGL and Canvas rendering. If you understand the concept of these ideas, you can apply it anywhere.</p><p>At the end,</p><blockquote>It’s all about how you design a single frame.</blockquote><p>Special Thanks to <a href="https://medium.com/u/ba0f6f3fd01">Ahmed Waleed</a> &amp; <a href="https://medium.com/u/47f9bf0865c0">Haris Hasan</a> for reviewing the article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4be80ddce528" width="1" height="1" alt=""><hr><p><a href="https://medium.com/emumba/a-ride-into-the-world-of-graphics-4be80ddce528">A ride into the world of Graphics</a> was originally published in <a href="https://medium.com/emumba">Emumba</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 let users highlight your HTML?]]></title>
            <link>https://medium.com/emumba/how-to-let-users-highlight-your-html-c1013abf68fb?source=rss-b145da09cdce------2</link>
            <guid isPermaLink="false">https://medium.com/p/c1013abf68fb</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[highlighting]]></category>
            <category><![CDATA[document-object-model]]></category>
            <dc:creator><![CDATA[Muhammad Ahmed]]></dc:creator>
            <pubDate>Mon, 11 Jun 2018 08:27:05 GMT</pubDate>
            <atom:updated>2018-06-11T08:36:32.274Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0XreXqgR5IVm5CO61nmHRA.png" /></figure><p>Medium has been a very popular blog for quite some time and it still is. I have been introduced to this blog 2 months ago. Since then, I found this blog to be more interesting in terms of user experience and for some of its very unique features. One of my favorite feature is <strong>text highlighting</strong> &amp; taking <strong>private notes</strong> on highlighted text. Today, we are going to see how we can implement text highlighting.</p><p>As a starter, you just need to have basic knowledge of <strong>javascript</strong> and know how we can access <strong>DOM elements</strong> using javascript.</p><p>Now, from the looks of it, this is a very simple feature to implement. You just have to get the user selected text by simple javascript DOM function <strong>(window.getSelection()). </strong>After that, you just have to wrap a div having a dark background color <strong>(Predefined jQuery function). </strong>Simple Right ?</p><p>Well, Sadly this is not that simple. The above technique will only work if we have plain text enclosed in a single div. In blogs like medium, the text is enclosed in a nested structure of DOM. So, the problem here is quite complex.</p><p>Now, before arbitrarily approaching different solutions, we have to critically analyze our problem.</p><h4><strong>Problem Definition:</strong></h4><p>Our end goal is to <strong>highlight/unhighlight</strong> the text that a user select from within the blog post. For this, we have to address the following questions in our desired solution.</p><ol><li>How to get the <strong>text selected</strong> by the user?</li><li>How to <strong>highlight</strong> the selected text? Given that blog text is enclosed in the nested DOM structure.</li><li>How to <strong>restrict user</strong> from selecting already highlighted text?</li><li>How to <strong>remove highlight</strong> from already highlighted text?</li></ol><p>Our problem doesn’t end here. We don’t know the pattern of nested DOM structure because every blog writer has its own way to write a blog article. So, we have to come up with a generic solution that can handle any type of nested structure.</p><p>If we see, question 1 and 3 are quite simple. For question 1, the solution is using the plain javascript DOM function <strong>(window.getSelection()). </strong>For question 3, there is a simple <strong>CSS property</strong> that prevent user selection <strong>( user-select : none ).</strong></p><p>The main problem we have is question 2 and 4. As i have mentioned before that we can’t <strong>predict the nested DOM structure</strong> of a given blog. A paragraph can have italic sections, bold sections, hyperlinks sections etc. Example is shown below:</p><blockquote><strong>&lt;p&gt;</strong></blockquote><blockquote>Lorem Ipsum <strong>&lt;a href=”abc.com”&gt;</strong>has been the<strong>&lt;/a&gt;</strong> industry’s standard dummy text ever since the 1500s, when an unknown printer <strong>&lt;b&gt;</strong> took a galley <strong>&lt;/b&gt;</strong>of type and scrambled <strong>&lt;i&gt;</strong>it to<strong>&lt;/i&gt;</strong> make a type specimen book. It has survived not only <strong>&lt;a href=”abc.com”&gt;</strong>five centuries<strong>&lt;/a&gt;</strong></blockquote><blockquote><strong>&lt;/p&gt;</strong></blockquote><p>Looking at the above example, a user can select text from anywhere. For example:</p><p>It can be:</p><blockquote><em>Lorem Ipsum </em><strong><em>&lt;a href=”abc.com”&gt;</em></strong><em>has</em></blockquote><blockquote><strong><em>OR</em></strong></blockquote><blockquote>when an unknown printer <strong>&lt;b&gt;</strong> took a</blockquote><blockquote><strong><em>OR</em></strong></blockquote><blockquote>and scrambled <strong>&lt;i&gt;</strong>it to<strong>&lt;/i&gt;</strong> make a type specimen book. It has survived not only <strong>&lt;a href=”abc.com”&gt;</strong>five cen</blockquote><p>Looking at the situation, we see that we have keep a track of every <strong>opening and closing tag</strong> within the selected text. Otherwise, we will end up messing up the whole DOM structure.</p><p>In reality, we can face a lot more complex structures. There can be a situation where we have to <strong>select multiple paragraphs</strong>, half hyperlinks and so on. The possibilities are endless. And even if we succeed to highlight a selected text, we also have to <strong>keep a reverse path open</strong>. It means that if we have to remove a highlight from a text, we can be able to do so.</p><p>Now, i think the problem we have to solve is quite understood and we should move on towards the possible solutions.</p><h4><strong>Possible Solutions:</strong></h4><p>We will use the javascript range object in construction of the solution. So, before proceeding towards the solution, we must understand the javascript <strong>RANGE</strong> object. A range object contains the whole DOM tree information for the current selected text. The information contains its parent, child elements, its position in the DOM etc. We can access this range object by using selection object i-e <strong>(window.getSelection().getRangeAt(0))</strong>.</p><p>Now, moving on towards the solutions. Suppose we have the given blog post:</p><blockquote><strong>&lt;p&gt;</strong></blockquote><blockquote>Lorem Ipsum <strong>&lt;a href=”abc.com”&gt;</strong>has been the<strong>&lt;/a&gt;</strong> industry’s standard dummy text ever since the 1500s, when an unknown printer <strong>&lt;b&gt;</strong> took a galley <strong>&lt;/b&gt;</strong>of type and scrambled <strong>&lt;i&gt;</strong>it to<strong>&lt;/i&gt;</strong> make a type specimen book. It has survived not only <strong>&lt;a href=”abc.com”&gt;</strong>five centuries<strong>&lt;/a&gt;</strong></blockquote><blockquote><strong>&lt;/p&gt;</strong></blockquote><p>Now, Suppose i want to highlight the following:</p><blockquote><em>Lorem Ipsum </em><strong><em>&lt;a href=”abc.com”&gt;</em></strong><em>has been</em></blockquote><p>We know that how we can get the range object. Through this object we can access the whole tree information.</p><p><strong>Solution # 1:</strong></p><p>We can start <strong>wrapping our selected text range</strong> and if we have any non-closed child at the end of range. We will close it first and then close our highlight wrapper. The resulting blog post will look like this:</p><blockquote><strong>&lt;p&gt;</strong></blockquote><blockquote><strong>&lt;span class=”highlight”&gt;</strong>Lorem Ipsum <strong>&lt;a href=”abc.com”&gt;</strong>has been<strong>&lt;/a&gt;&lt;/span&gt;</strong> the<strong>&lt;/a&gt;</strong> industry’s standard dummy text ever since the 1500s, when an unknown printer <strong>&lt;b&gt;</strong> took a galley <strong>&lt;/b&gt;</strong>of type and scrambled <strong>&lt;i&gt;</strong>it to<strong>&lt;/i&gt;</strong> make a type specimen book. It has survived not only <strong>&lt;a href=”abc.com”&gt;</strong>five centuries<strong>&lt;/a&gt;</strong></blockquote><blockquote><strong>&lt;/p&gt;</strong></blockquote><p>Now, if we see carefully, we have a problem. We have a anchor tag <strong>( &lt;a&gt; ) </strong>after span who has no parent. To solve this problem, we have to add a starting tag of the same child element present in the highlight <strong>&lt;span&gt;</strong> element.</p><p>Now, the correct output shall look like this:</p><blockquote><strong>&lt;p&gt;</strong></blockquote><blockquote><strong>&lt;span class=”highlight”&gt;</strong>Lorem Ipsum <strong>&lt;a href=”abc.com”&gt;</strong>has been<strong>&lt;/a&gt;&lt;/span&gt;&lt;a href=”abc.com”&gt;</strong> the<strong>&lt;/a&gt;</strong> industry’s standard dummy text ever since the 1500s, when an unknown printer <strong>&lt;b&gt;</strong> took a galley <strong>&lt;/b&gt;</strong>of type and scrambled <strong>&lt;i&gt;</strong>it to<strong>&lt;/i&gt;</strong> make a type specimen book. It has survived not only <strong>&lt;a href=”abc.com”&gt;</strong>five centuries<strong>&lt;/a&gt;</strong></blockquote><blockquote><strong>&lt;/p&gt;</strong></blockquote><p>Hurray!!! Looks like we have solved the problem. Well, thats is not quite true. Let me explain, We have highlighted the text successfully, but what about <strong>remove the highlight</strong> from the text? . Other than removing the highlight span tag ( &lt;span class=”highlight”&gt; ) we also have to <strong>remove all the manually altered child</strong> element tags that we added to avoid DOM destructuring. i-e (&lt;a href=”abc.com”&gt; )</p><p>This can become very costly task in terms of computation. This is because that other than the selected text range, we also have to extract the outer text range object to access its tree structure for manipulation.</p><p>The illustrated example is a very simple. If we think of situations where we have 10 to 12 incomplete child tags, even highlighting can be a very costly task.</p><p>The above solution is very much appropriate but not efficient for large DOM structures. So, i have to discard this solution.</p><p><strong>Solution # 2:</strong></p><p>Let’s take a look on the text portion we need to highlight:</p><blockquote><em>Lorem Ipsum </em><strong><em>&lt;a href=”abc.com”&gt;</em></strong><em>has been</em></blockquote><p>Initially, we have only one text selection range for this selection. We will divide this range into several safe ranges. <strong>Safe ranges </strong>are those ranges who have no incomplete closing child elements.</p><p>To get safe ranges, we have to traverse the full DOM tree in the current selected text range. We have the find all the incomplete child element nodes and then split the range on the incomplete child nodes.</p><p>In this particular example, we get 2 safe ranges in our selected text.</p><blockquote><em>Lorem Ipsum </em><strong>AND </strong><em>has been</em></blockquote><p>Now we can wrap these safe ranges separately with highlighted span. The resulting blog post will look like this:</p><blockquote><strong>&lt;p&gt;</strong></blockquote><blockquote><strong>&lt;span class=”highlight”&gt;</strong>Lorem Ipsum <strong>&lt;/span&gt;&lt;a href=”abc.com”&gt;&lt;span class=”highlight”&gt;</strong>has been<strong>&lt;/span&gt;</strong> the<strong>&lt;/a&gt;</strong> industry’s standard dummy text ever since the 1500s, when an unknown printer <strong>&lt;b&gt;</strong> took a galley <strong>&lt;/b&gt;</strong>of type and scrambled <strong>&lt;i&gt;</strong>it to<strong>&lt;/i&gt;</strong> make a type specimen book. It has survived not only <strong>&lt;a href=”abc.com”&gt;</strong>five centuries<strong>&lt;/a&gt;</strong></blockquote><blockquote><strong>&lt;/p&gt;</strong></blockquote><p>Now, to remove highlight from text we just need to remove the highlighted span tag (<strong>&lt;span class=”highlight”&gt;</strong>) . Nothing else!</p><p>Now, In my opinion this solution is more suitable for all kinds of nested DOM structures. There is only one time computation effort to find the safe ranges. After that, there is no expensive effort. We just need to wrap and unwrap the safe ranges.</p><p>Atlast! We have a winner. It is none other than Solution # 2 ( Safe Ranges Solution )</p><h4><strong>BONUS ! Persisting the highlighted text in the browser local storage</strong></h4><p>Well this is quite easy, there is a javascript library called “<strong>rangy</strong>”. It is used to serialize and deserialize the Range object. We just need to serialize our safe ranges and save it in the localstorage. Upon page reload, we just need to fetch them and deserialize them. After that you just need to wrap the safe ranges by highlighted span tag (<strong>&lt;span class=”highlight”&gt;</strong>)</p><h4>Link to live demo:</h4><p><a href="https://wikimedium-server.firebaseapp.com/wiki/Stack_Overflow">https://wikimedium-server.firebaseapp.com/wiki/Stack_Overflow</a></p><p>Just Open this link and select a text to highlight it. Enjoy!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c1013abf68fb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/emumba/how-to-let-users-highlight-your-html-c1013abf68fb">How to let users highlight your HTML?</a> was originally published in <a href="https://medium.com/emumba">Emumba</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>