How to draw lines on canvas


Arun Kumar

I want to draw some lines inside a circle on canvas in the following way.

I don't know how to draw lines like below. But I have basic knowledge of drawing lines and arcs on canvas. how to proceed?

enter image description here

username

You could use a bezier curve with control points as suggested in the comments, however, since those control points cannot pass through the points you define, and you always need to define two control points, it's hard to control (no pun intended).

In order to implement a line through points using actual points , you need to use a cardinal spline .

They don't have built-in support, but for a while I implemented JavaScript and canvas ( code can be downloaded from here under the MIT license ).

This way you can simply define three points as the minimum value (to get a simple curve) and the function will take care of drawing a smooth curve between the points where the tension value is set.

For example, if you define the following three points:

var pts = [10,100, 200,50, 390,100];

If we want to illustrate the points (for comparison), obviously you get a simple polyline :

Line count

Use a cardinal spline with the same three pointswill provide you with :

Cardinal splines

The following code produces the above curve (without the red dots showing the point coordinates):

ctx.beginPath();
ctx.curve(pts);
ctx.stroke();

Now it is simply a matter of moving the points around (in particular the center point) and the curve will adopt. Adding a tension slider for the user can be an advantage:

Increasing the tension to for example 0.8 give you this result:

ctx.curve(pts, 0.8);

higher tension

and lowering it to for example 0.3 will reduce the smoothness to this:

ctx.curve(pts, 0.3);

reduce tension

There are also other parameters (see the link at top for documentation) and you can have "unlimited" number of points in the point array in case you want to add super-fine control.

The implementation extends the canvas context but you can extract the method and use it separately if you are faint at heart. :-)

Implementing this for a circle

I hope I am interpreting your drawing correctly here... to use the above for a circle you would simply need to do the following:

  • Define the range, the side of the circle you want the lines to be drawn into.
  • Define the steps, ie. the space between each line.

Lets say you wanted to draw lines between -70° and 70° and maximum 5 lines you could do something like this:

var ctx = canvas.getContext('2d'),
    cx = canvas.width * 0.5,
    cy = canvas.height * 0.5,
    pts,
    startAngle = -70,
    endAngle = 70,
    lines = 5,
    angle,
    range,
    steps,
    radius = 90,
    delta = 15,
    x, y,
    i;

ctx.lineWidth = 3;
ctx.strokeStyle = '#059';

/// draw arc
ctx.beginPath();
ctx.arc(cx, cy, radius, 0, 2 * Math.PI);
ctx.stroke();

/// calculate angle range normalized to 0 degrees
startAngle = startAngle * Math.PI / 180;
endAngle = endAngle * Math.PI / 180;

range = endAngle  - startAngle;
steps = range / (lines + 1);

/// calculate point at circle (vertical only)
for(i = 1; i <= lines; i++) {
    pts = [];
    /// right side
    x = cx + radius * Math.cos(startAngle + steps * i);
    y = cy + radius * Math.sin(startAngle + steps * i);
    
    pts.push(x, y);

    /// center
    pts.push(cx, y + delta * ((y - cy)/ cy));
    
    /// flip for left side
    x = cx - (x - cx);

    pts.push(x, y);

    ///  draw curve
    ctx.beginPath();
    ctx.curve(pts, 0.8);
    ctx.stroke();
}

Which would result in this:

Earth

Fiddle here

It's now just a matter of playing around with the values (delta for example) and to calculate the horizontal row - I'll leave that as an exercise for the OP:

Using ellipse side to draw the lines

That being said - if you intended the globe to be more um, circular :-S, you could also have used a function to calculate part of an ellipse and draw that as lines instead. If would be about the same implementation as above here but with a sub function to calculate the ellipse between left and right side using the difference between the line and middle point as radius.

For example:

/// calculate point at circle (vertical only)
for(i = 1; i <= lines; i++) {
    pts = [];
    /// right side
    x = cx + radius * Math.cos(startAngle + steps * i);
    y = cy + radius * Math.sin(startAngle + steps * i);
    
    pts.push(cx - radius, cy);
    pts.push(cx, y);   
    pts.push(cx + radius, cy);

    ///  draw ellipse side
    ctx.beginPath();
    drawEllipseSide(pts, true);
    ctx.stroke();
}

Then in the method (only vertical shown):

function drawEllipseSide(pts, horizontal) {

    var radiusX,
        radiusY,
        cx, cy,
        x, y,
        startAngle,
        endAngle,
        steps = Math.PI * 0.01,
        i = 0;
    
    if (horizontal) {
    
        radiusX = Math.abs(pts[4] - pts[0]) * 0.5;
        radiusY = pts[3] - pts[1];
        cx = pts[2];
        cy = pts[1];
        startAngle = 0;
        endAngle = Math.PI;
        
        x = cx + radiusX * Math.cos(startAngle);
        y = cy + radiusY * Math.sin(startAngle);
        
        ctx.moveTo(x, y);
        
        for(i = startAngle + steps; i < endAngle; i += steps) {
            x = cx + radiusX * Math.cos(i);
            y = cy + radiusY * Math.sin(i);
            ctx.lineTo(x, y);
        }
    }
}

Resulting in this (I cheat a bit in the final drawing to give a more clear picture (no pun) of what the end result will be if you continue down these lines (no pun either, I am pun-cursed) given here):

Earth

Fiddle here

My code-OCD kicked in :-P but you should at least have a few options here to go with. Study the code to see how the vertical lines are calculated and adopt that for horizontal ones.

Hope this helps!

Related


How to draw lines on canvas?

monkey 334 I've read some tutorials on the internet but I can't seem to find any way to show me the lines Can someone help? i try to do p = Canvas(height = 600, width = 800).place(x=0,y=0) p.create_rectangle(50, 25, 150, 75, fill="blue") Unfortunately, it did

HTML Canvas - how to draw very thin lines?

terminal I created a project using the JavaScript library P5.js and used its built-in line() and ellipse() functions to draw lines and circles on the canvas. I'm now trying to remove the entire P5.js and its functions from my project to keep pure JavaScript bu

How to draw thinner but sharper lines in html canvas?

Evil Guardian I have the following javascript code to draw a graph table. But the problem is when I print the output, the thin lines don't look clear. The problem is visible when zooming the html page. I wish the lines were clearer. But the width should be the

How to draw random/irregular lines in XAML canvas?

Karedia Noorsil I have a canvas in XAML where I want to draw random irregular (hand drawn) lines using C#. This is what I want : http://jsfiddle.net/GfGVE/9/ HTML Canvas but in XAML C# I want the line to be drawn horizontally. Xml: <Canvas Grid.Column="1" x:Na

How to draw only vertical and horizontal lines (canvas)

Worstein Azatian I want to make a drawing tool with html5 canvas, it can only draw horizontal and vertical lines For example, although I will drag the mouse, it has to draw vertical or horizontal lines. Below I will show a picture where I will show what I need

how draw only vertical and horizontal lines (Canvas)

VostanAzatyan I want to make one drawing tool with html5 canvas, that can draw only horizontal and vertical lines For example despite which way I will drag the mouse it must draw vertical or horizontal line. Below I will show one image where i will show what i

How to draw only vertical and horizontal lines (canvas)

Worstein Azatian I want to make a drawing tool with html5 canvas, it can only draw horizontal and vertical lines For example, it has to draw vertical or horizontal lines despite which way I will drag the mouse. Below I will show an image where I will show what

How to draw only vertical and horizontal lines (canvas)

Worstein Azatian I want to make a drawing tool with html5 canvas, it can only draw horizontal and vertical lines For example, it has to draw vertical or horizontal lines despite which way I will drag the mouse. Below I will show an image where I will show what

Draw parallel lines on canvas

Ma Ansi: I am trying to draw parallel lines on a canvas. One of the lines is fixed. The user enters the distance between the two lines, so the second line is positioned as such. I am new to javascript. Please help me how to change the position of the second li

Draw parallel lines on canvas

Ma Ansi: I am trying to draw parallel lines on a canvas. One of the lines is fixed. The user enters the distance between the two lines, so the second line is positioned as such. I am new to javascript. Please help me how to change the position of the second li

Draw multiple lines on the canvas

username I need to draw two lines on the canvas, the first starts from the top and the second starts after 75% of the screen. I try the code below, but when I draw the second line, it affects the first line strokeWidth. How to create two separate lines: void

draw lines on the last canvas

Nikhil I am new to javascript and canvas programming. I have a function drawLines(canvasIndex, startPosition) responsible for drawing lines on the canvas. It accepts two parameters canvasIndex which represents the canvas I want to draw on, and startPosition wh

Draw parallel lines on canvas

Ma Ansi I am trying to draw parallel lines on a canvas. One of the lines has been fixed. The user enters the distance between the two lines, thereby positioning the second line accordingly. I am new to JavaScript. Please help how should I change the position o

Draw parallel lines on canvas

Ma Ansi: I am trying to draw parallel lines on a canvas. One of the lines is fixed. The user enters the distance between the two lines, so the second line is positioned as such. I am new to javascript. Please help me how to change the position of the second li

Draw multiple lines on the canvas

username I need to draw two lines on the canvas, the first starts from the top and the second starts after 75% of the screen. I try the code below, but when I draw the second line, it affects the first line strokeWidth. How to create two separate lines: void

draw lines on the last canvas

Nikhil I am new to javascript and canvas programming. I have a function drawLines(canvasIndex, startPosition) responsible for drawing lines on the canvas. It accepts two parameters canvasIndex which represents the canvas I want to draw on, and startPosition wh

How to draw 1 pixel lines using Javafx Canvas?

Jock Smith: I've been googling and found some related questions/posts, but none solved my problem. I am using the following method to draw lines directly on the canvas (JavaFX): gc.setStroke(color); gc.setLineWidth(lineWidth); gc.strokeLine(startX, startY, end

How to draw 1 pixel lines using Javafx Canvas?

jock smith I've been googling and found some related questions/posts, but none solved my problem. I draw lines directly on the canvas (JavaFX) using: gc.setStroke(color); gc.setLineWidth(lineWidth); gc.strokeLine(startX, startY, endX, endY); I want lines of 1

How to draw sharp curves and diagonal lines on Canvas in Android

pat The image below shows the output on Canvas. Curves and slashes are not as sharp as horizontal or vertical straight lines. Me android:hardwareAccelerated="true"and ANTI_ALIAS_FLAGon Canvasas well. See code below. How can I improve the quality of these curve