Skip to content
Advertisement

Create a curve with degree between two lines intersection

I want to make a maple leaf like in the canadian’s flag,but I have a problem to create a curve when the lines intersect so that the curve is only in the intersection and create certain degree between the lines. what I mean is shown in arc A,B,C,etc in the picture I bring down here:

canada flag curve

This is the function I created so far:

JavaScript

Advertisement

Answer

I am going to try to make this as simple as possible but it may take some playing around with to understand. (Final code in last snippet)

First take a look at how arcTo() works https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-arcto-dev

You’ll notice you have a start point and two control points (CP). CP1 is where the lines intersect. CP2 is where the second line starts.

With that said if we take two lines and run an intersect formula on them we can get CP1. CP2 will just be where the second line starts.

In the below snippet the top (first points) of line1 is the top of the leaf. line2’s first points are the tip of the second leaf. For each line x2 and y2 were derived by using the function getCoords(). Since your diagram has a different coordinate system you’ll have to take the angle it gives and subtract it from 360. So line1 has an angle of 63 degrees and line2 is 150 degrees.

Just plug in the numbers and once you get the console.log() for x2 and y2 copy and paste them into the line object. The length doesn;t matter as long as the lines cross.

JavaScript
JavaScript

After you have accurate x1, y1, x2, y2 coordinates for both line we can then run them through the line intersect function to get CP1.

JavaScript
JavaScript

As you see now the function is just console logging the coordinate for you to use as CP1 in the arcTo(). Now you can properly draw you lines. Be aware that arcTo() will draw a straight line connecting to the previous point in the drawing. This means that we don’t need to use ‘lineTo()’ from the previous point to the start of the arc, just from the end of the arc to the next point.

JavaScript
JavaScript

And the final product will be something like this once you go through the whole leaf. The helper functions have been removed as they are not needed now that I have the numbers. Creating your drawing this way will help reduce issues when trying to fill it in.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement