Hacking into Labeled Arrows

Rate this content
Bookmark

Labeled arrows are one of the most widely used features when it comes to whiteboard, isn't it? In this talk, I will be deep-diving into the internals of labeled arrows and how they work under the hood in Canvas and SVG and I will be also sharing my experience building labeled arrows in Excalidraw - A virtual free Open-Source collaborative end-to-end encrypted canvas-based editor for sketching hand-drawn diagrams.

This talk has been presented at React Summit 2024, check out the latest edition of this React Conference.

FAQ

To construct a Labeled Arrow, you need at least three elements: a line, an arrowhead, and text.

Using clearRect is generally faster than clipping in Canvas, especially for multiple iterations or complex paths.

Link a label to an arrow by adding a label ID to the arrow and a container ID to the text element. This creates a one-to-one relationship between the arrow and the label.

To improve performance, batch the drawing calls and use the stroke API only once. Additionally, consider using offscreen canvas and rounding off floating point values.

To export labeled arrows as SVG, convert Canvas APIs to SVG path elements and use masking to remove the overlapping area between the arrow and the text.

You can render arrows in Canvas using the Canvas drawing API, which is divided into rectangles and paths. For arrows, you use the paths API to draw lines and arrowheads.

To render text in Canvas, use the fill text API along with font and fill style settings. For multiline text, split lines individually and render them with vertical line height differences.

Akansha is an open source maintainer and a core maintainer of Excalidraw, ReactJS, and other open source libraries. She also organizes FOSS meetups in Bangalore, India.

FOSS stands for Free and Open Source Software.

A Labeled Arrow is an arrow with text attached to it. It is commonly used in diagrams, communication flows, and architectural diagrams to show directions and crucial information.

Aakansha Doshi
Aakansha Doshi
32 min
14 Jun, 2024

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Labeled Arrows are widely used in conversations, communication, diagrams, and more. The Talk discusses constructing Labeled Arrows, rendering arrows in Canvas, optimizing stroke API calls, using RoughJS for drawing and rendering text, rendering multiline text on Canvas, clearing overlapping area and clipping, comparing clipping and clear rect performance, handling clipping and linking labels to arrows, linking labeled arrows to texts, rotated text and exporting as SVG, masking in SVG and labeling arrows, SVG masking and performance improvements, and optimizing rendering and drawing in Canvas.
Available in Español: Hackeando las Flechas Etiquetadas

1. Introduction to Labeled Arrows

Short description:

Labeled Arrows are arrows with text attached to them. They are widely used in conversations, communication, diagrams, and more. In this part, we will explore how Labeled Arrows are constructed and discuss the requirements for building a prototype, including rendering arrows, linking labels and arrows, supporting multipoint arrows, handling label rotation, and exporting to SVG.

Before I actually start, let me just give a quick intro. So I'm Akansha, I'm an open source maintainer, FOSS basically stands for free and open source software. I'm a core maintainer of Excalidraw, ReactJS, and a couple of more open source libraries. And I also do these FOSS meetups in Bangalore, India to promote the FOSS community in India. And yeah, these are my socials. I would love to connect with all of you and chat about anything frontend, React, open source, Canvas, anything.

So yeah, now let's get started with the talk. So what exactly do I mean by Labeled Arrows? Labeled Arrows are basically when you have an arrow and you have some text attached to it, that is basically a Labeled Arrow. You would have used it in some conversation as you can see the conversation between two friends or some communication, some flow, some architectural diagram to show some directions, some crucial information. It can be anything, but it is like one of the most used features. So since we are going to implement it, so let's look at how this Labeled Arrow is constructed internally.

Now this is a Labeled Arrow. It has a line. It consists of a line. It consists of an arrowhead and a text. So at least when constructing a Labeled Arrow, you have at least three separate different types of elements. And let's make a note of it because we are going to deep dive into this.

Now, as we developers do, like before building any feature, let's just come up with some minimal requirements to build a prototype. So let's look at the list of requirements. So the first is we want to understand how we can render the arrows in Canvas, how we can render the label that is the text on top of the arrow, how we can link the label and the arrow. So in the code base, we are aware of that, hey, this particular label is attached to this particular arrow, and this arrow has this label. So we just need some relationship between these two elements. How should we render the label for multipoint arrows? I'll be discussing this in a while as well. And how should we render the label when we are rotating an arrow? And lastly, we want to support export to SVG as well. So we'll be diving a bit into SVG as well. So let's have this set of requirements. I'll be diving into each of it. And we'll be fulfilling it one by one and have a minimal prototype at the end. So before that, let's understand how we want to structure the elements in the Canvas.

2. Structuring Elements in Canvas

Short description:

To structure elements in Canvas, use a minimal JSON with type, ID, XY coordinates, width, height, points, start arrowhead, and end arrowhead. For understanding the audience, I conducted a survey and found that 63% are beginners and 37% are intermediate. Now let's discuss rendering arrows in Canvas using the drawing API, which consists of rectangles and paths. We will focus on paths and learn how to create a canvas and interact with it.

So before that, let's understand how we want to structure the elements in the Canvas. This is the bare minimum JSON, which you can think of. So it has a type, which is basically the type of the element. Is it an arrow or is it a text? The ID of the element, which you will be using to uniquely represent the element on the Canvas. So every element on the Canvas will have a unique ID. The XY are basically the coordinates to position them on the Canvas. Width and height are the dimensions. Points is basically an important parameter when it comes to arrows because an arrow can have two points, 100 points, 1,000 points, who knows? So it's basically the array of those points. And start arrowhead and end arrowhead are basically the two extreme points of the arrow, and the shape of the arrowhead, you can decide on. You can have a star. You can have a circle, whatever shape you want to come up with. So this is like a minimal JSON. Let's consider this JSON.

So I just shared this in socials to understand the audience and asked them to fill a Google form just to understand what is the understanding of the audience for this talk. And this is what I came up with. So I got like 63% of the audience will be beginners and 37% will be intermediate and similar for SVG. So can I get a quick raise of hands? Who are our beginners in Canvas here? Okay. Awesome. Thank you. Thank you for participating. I think some of you participated in this, and thank you so much.

So yeah, let's actually go to the first requirement now. How can you render the arrows in Canvas? So let's get into the basics first. Canvas drawing API is broadly divided into two categories. One is rectangle and one is paths. Rectangle is for drawing the rectangles. Paths is basically a collection of a lot of points and with which you can draw any complex shape in Canvas. And in this talk, we are going to talk a lot about paths. So coming to the basics, you create a canvas. You get the context so you can interact with the canvas.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Video Editing in the Browser
React Summit 2023React Summit 2023
23 min
Video Editing in the Browser
Top Content
Watch video: Video Editing in the Browser
This Talk discusses the challenges of video editing in the browser and the limitations of existing tools. It explores image compression techniques, including Fourier transform and Huffman encoding, to reduce file sizes. The video codec and frame decoding process are explained, highlighting the importance of keyframes and delta frames. The performance bottleneck is identified as the codec, and the need for specialized hardware for efficient video editing is emphasized. The Talk concludes with a call to create a simplified API for video editing in the browser and the potential for AI-powered video editing.
Handling Data at Scale for React Developers
React Summit 2022React Summit 2022
23 min
Handling Data at Scale for React Developers
This Talk discusses handling data at scale for React developers, including scaling databases and the need for search. It explores different ways to fetch data in React, such as using useEffect, fetch, and setState. The Talk also introduces Suspense for data fetching and how it improves user experience. It covers controlling React Suspense, handling search, and using render-as-you-fetch. The Talk concludes with a discussion on the RFC status and fetching in event handlers.