Draw continuous lines on HTML canvas one after the other

Ramana Venkata

I am trying to write code for the above described problem. I tried finding a solution. This is what I currently have.

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

var drawColorLine = function(start, end, color) {
  var deltaX, deltaY, i = 0,
    currLength = 0,
    isHor, isVert;

  deltaX = end[0] - start[0];
  deltaY = end[1] - start[1];
  context.strokeStyle = color;

  isHor = deltaX === 0 ? 0 : 1;
  isVert = deltaY === 0 ? 0 : 1;

  function draw() {
    context.beginPath();
    context.moveTo(start[0] + currLength * isHor, start[1] + currLength * isVert);

    currLength = currLength + 0.5 * i;
    context.lineTo(start[0] + currLength * isHor, start[1] + currLength * isVert);
    context.stroke();

    if (currLength <= Math.max(deltaX, deltaY)) {
      i = i + 1;
      requestAnimationFrame(draw);
    }
  }
  draw();
};

drawColorLine([40, 40], [100, 40], '#116699');
drawColorLine([40, 40], [40, 100], '#bb11dd');
<canvas id='canvas' width='400' height='400'></canvas>

The problem is both are being drawn at the same time. One should follow after the other. Using promsises is it possible to delay the second function while the first function is getting executed and later execute second function? I tried reading on Promises a bit but I couldn't translate what I understood into code.

Thanks in advance.

Bergi

Yes, you can use promises, altough for learning purposes you might want to write a pure callback solution first.

You'll want to have a look at my rules of thumb for promise development. Let's apply them here:

  1. Every asynchronous function must return a promise.

    These would be drawColorLine, draw, and requestAnimationFrame in your case.

  2. As requestAnimationFrame is a native, primitively asynchronous function that unfortunately still takes a callback, we'll have to promisify it:

    function getAnimationFrame() {
        return new Promise(function(resolve) {
            requestAnimationFrame(resolve); // this promise never gets rejected
            // TODO: cancellation support :-)
        });
    }
    
  3. Everything that follows an asynchronous action goes into a .then() callback:

    function drawColorLine(start, end, color) {
        … // initialisation
    
        function draw() {
            … // do work
            // always return a promise:
            if (/* furter work */) {
                i++;
                return getAnimationFrame().then(draw); // magic happens here :-)
            } else {
                return Promise.resolve(…); // maybe have a path object as eventual result?
                                           // or anything else, including nothing (no arg)
            }
        }
        return draw(); // returns a promise - but don't forget the `return`
    }
    

Voila!

drawColorLine([40, 40], [100, 40], '#116699').then(function() {
    return drawColorLine([40, 40], [40, 100], '#bb11dd');
}).then(console.log.bind(console, "both lines drawn"));

Collected from the Internet

Please contact debug[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

can html canvas draw uploaded images?

From Dev

draw svg file on html canvas programmatically

From Dev

Draw smoother paths in html canvas

From Dev

Draw HTML5 Canvas pulse line

From Dev

HTML 5 Canvas and Draw Circle

From Dev

RXJS draw line on html5 canvas

From Dev

Draw on HTML5 Canvas and get the coordinates

From Dev

how draw only vertical and horizontal lines (Canvas)

From Dev

How to draw parallel lines with canvas PaperJS? (Canvas/Javascript)

From Dev

Draw video on canvas HTML5

From Dev

How do I draw thin but sharper lines in html canvas?

From Dev

HTML 5 Canvas Animation - Move multiple objects one after another

From Dev

Canvas object always draw behind other object

From Dev

Javascript canvas draw text after loading font

From Dev

How to draw an arbitrary shape using html canvas?

From Dev

How to draw a continuous line with mouse on JavaFX canvas?

From Dev

How to Add Rectangle one after other vertically on Canvas in WPF using C#?

From Dev

How to draw curves and straight lines in one image?

From Dev

Changing Pen Color on Canvas after draw

From Dev

how draw only vertical and horizontal lines (Canvas)

From Dev

moving and dropping objects inside one canvas to other canvas in html5

From Dev

showing one line on canvas which I draw before clear canvas

From Dev

align html elements one after the other

From Dev

How to increase draw speed of lines on canvas in Javafx

From Dev

HTML5 Canvas - Draw segments and circles - Different color for one circle

From Dev

Delphi draw lines in a PlotGrid with canvas

From Dev

HTML Canvas - Draw Shape after Interval

From Dev

HTML Canvas - How to draw very thin lines?

From Dev

Unable to draw to canvas in Tkinter after() loop

Related Related

HotTag

Archive