Animate drawing of path on HTML5 canvas











up vote
1
down vote

favorite












My problem is how to animate the drawing of a path between two points.



Consider a curved line or path between two points, A & B. I can draw this easily on the canvas using the line drawing functions in Konvajs.



However, what I actually want is to animate the revealing of the line so that it starts from point A and progressively draws to point B. The reveal should be animated so I can apply pleasing easings.



As a comparable example, see the brief video on this site https://coggle.it/ where the video shows the creation of a new box and the line draws to connect it to the old.










share|improve this question
























  • Welcome - I can see that you have tried to explain what you need help with and I am sorry but I don't quite understand what you are after. StackOverflow is a problem solving site so you need to write a precise problem you need solved. If you need general knowledge then fire up Google. Can you explain more about what you need please ?
    – Vanquished Wombat
    Nov 8 at 22:26










  • @VanquishedWombat I need to make line with animation like on this site coggle.it, so then you press add button, the line will appear with an animation
    – Roxane Amory Lorch
    Nov 9 at 7:36










  • Hi - I edited your question so that it is easier to understand in the context of Konvajs. Also, please learn from this style of how to pose a question on SO. You could improve the question much more and solicit a faster answer if you include a small amount of code that shows what you have tried. Even better, add a working snippet which is a bit of JS that works right here in the page on SO - see other questions for Konvajs which include snippets as examples that you can cut & paste.
    – Vanquished Wombat
    Nov 9 at 7:56










  • @VanquishedWombat Ok! Thank you very much!
    – Roxane Amory Lorch
    Nov 9 at 8:37

















up vote
1
down vote

favorite












My problem is how to animate the drawing of a path between two points.



Consider a curved line or path between two points, A & B. I can draw this easily on the canvas using the line drawing functions in Konvajs.



However, what I actually want is to animate the revealing of the line so that it starts from point A and progressively draws to point B. The reveal should be animated so I can apply pleasing easings.



As a comparable example, see the brief video on this site https://coggle.it/ where the video shows the creation of a new box and the line draws to connect it to the old.










share|improve this question
























  • Welcome - I can see that you have tried to explain what you need help with and I am sorry but I don't quite understand what you are after. StackOverflow is a problem solving site so you need to write a precise problem you need solved. If you need general knowledge then fire up Google. Can you explain more about what you need please ?
    – Vanquished Wombat
    Nov 8 at 22:26










  • @VanquishedWombat I need to make line with animation like on this site coggle.it, so then you press add button, the line will appear with an animation
    – Roxane Amory Lorch
    Nov 9 at 7:36










  • Hi - I edited your question so that it is easier to understand in the context of Konvajs. Also, please learn from this style of how to pose a question on SO. You could improve the question much more and solicit a faster answer if you include a small amount of code that shows what you have tried. Even better, add a working snippet which is a bit of JS that works right here in the page on SO - see other questions for Konvajs which include snippets as examples that you can cut & paste.
    – Vanquished Wombat
    Nov 9 at 7:56










  • @VanquishedWombat Ok! Thank you very much!
    – Roxane Amory Lorch
    Nov 9 at 8:37















up vote
1
down vote

favorite









up vote
1
down vote

favorite











My problem is how to animate the drawing of a path between two points.



Consider a curved line or path between two points, A & B. I can draw this easily on the canvas using the line drawing functions in Konvajs.



However, what I actually want is to animate the revealing of the line so that it starts from point A and progressively draws to point B. The reveal should be animated so I can apply pleasing easings.



As a comparable example, see the brief video on this site https://coggle.it/ where the video shows the creation of a new box and the line draws to connect it to the old.










share|improve this question















My problem is how to animate the drawing of a path between two points.



Consider a curved line or path between two points, A & B. I can draw this easily on the canvas using the line drawing functions in Konvajs.



However, what I actually want is to animate the revealing of the line so that it starts from point A and progressively draws to point B. The reveal should be animated so I can apply pleasing easings.



As a comparable example, see the brief video on this site https://coggle.it/ where the video shows the creation of a new box and the line draws to connect it to the old.







javascript canvas html5-canvas konvajs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 at 11:50









Vanquished Wombat

3,70811336




3,70811336










asked Nov 8 at 18:43









Roxane Amory Lorch

215




215












  • Welcome - I can see that you have tried to explain what you need help with and I am sorry but I don't quite understand what you are after. StackOverflow is a problem solving site so you need to write a precise problem you need solved. If you need general knowledge then fire up Google. Can you explain more about what you need please ?
    – Vanquished Wombat
    Nov 8 at 22:26










  • @VanquishedWombat I need to make line with animation like on this site coggle.it, so then you press add button, the line will appear with an animation
    – Roxane Amory Lorch
    Nov 9 at 7:36










  • Hi - I edited your question so that it is easier to understand in the context of Konvajs. Also, please learn from this style of how to pose a question on SO. You could improve the question much more and solicit a faster answer if you include a small amount of code that shows what you have tried. Even better, add a working snippet which is a bit of JS that works right here in the page on SO - see other questions for Konvajs which include snippets as examples that you can cut & paste.
    – Vanquished Wombat
    Nov 9 at 7:56










  • @VanquishedWombat Ok! Thank you very much!
    – Roxane Amory Lorch
    Nov 9 at 8:37




















  • Welcome - I can see that you have tried to explain what you need help with and I am sorry but I don't quite understand what you are after. StackOverflow is a problem solving site so you need to write a precise problem you need solved. If you need general knowledge then fire up Google. Can you explain more about what you need please ?
    – Vanquished Wombat
    Nov 8 at 22:26










  • @VanquishedWombat I need to make line with animation like on this site coggle.it, so then you press add button, the line will appear with an animation
    – Roxane Amory Lorch
    Nov 9 at 7:36










  • Hi - I edited your question so that it is easier to understand in the context of Konvajs. Also, please learn from this style of how to pose a question on SO. You could improve the question much more and solicit a faster answer if you include a small amount of code that shows what you have tried. Even better, add a working snippet which is a bit of JS that works right here in the page on SO - see other questions for Konvajs which include snippets as examples that you can cut & paste.
    – Vanquished Wombat
    Nov 9 at 7:56










  • @VanquishedWombat Ok! Thank you very much!
    – Roxane Amory Lorch
    Nov 9 at 8:37


















Welcome - I can see that you have tried to explain what you need help with and I am sorry but I don't quite understand what you are after. StackOverflow is a problem solving site so you need to write a precise problem you need solved. If you need general knowledge then fire up Google. Can you explain more about what you need please ?
– Vanquished Wombat
Nov 8 at 22:26




Welcome - I can see that you have tried to explain what you need help with and I am sorry but I don't quite understand what you are after. StackOverflow is a problem solving site so you need to write a precise problem you need solved. If you need general knowledge then fire up Google. Can you explain more about what you need please ?
– Vanquished Wombat
Nov 8 at 22:26












@VanquishedWombat I need to make line with animation like on this site coggle.it, so then you press add button, the line will appear with an animation
– Roxane Amory Lorch
Nov 9 at 7:36




@VanquishedWombat I need to make line with animation like on this site coggle.it, so then you press add button, the line will appear with an animation
– Roxane Amory Lorch
Nov 9 at 7:36












Hi - I edited your question so that it is easier to understand in the context of Konvajs. Also, please learn from this style of how to pose a question on SO. You could improve the question much more and solicit a faster answer if you include a small amount of code that shows what you have tried. Even better, add a working snippet which is a bit of JS that works right here in the page on SO - see other questions for Konvajs which include snippets as examples that you can cut & paste.
– Vanquished Wombat
Nov 9 at 7:56




Hi - I edited your question so that it is easier to understand in the context of Konvajs. Also, please learn from this style of how to pose a question on SO. You could improve the question much more and solicit a faster answer if you include a small amount of code that shows what you have tried. Even better, add a working snippet which is a bit of JS that works right here in the page on SO - see other questions for Konvajs which include snippets as examples that you can cut & paste.
– Vanquished Wombat
Nov 9 at 7:56












@VanquishedWombat Ok! Thank you very much!
– Roxane Amory Lorch
Nov 9 at 8:37






@VanquishedWombat Ok! Thank you very much!
– Roxane Amory Lorch
Nov 9 at 8:37














3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










Here is a potential answer (special thanks to @markov00 re same technique in SVG). It works by manipulating the path dashOffset and dash attributes. There is an excellent explanation of the technique here in a post by Jake Archibald which also includes an interactive experiment with sliders which I found very useful.



I have tried to make the demo as lightweight as possible and just show the technique - though I added a slider and some UI to help understand the process. The use of jquery is only for the UI parts which are not needed for the technique.



Couple of points:




  • The demo here uses a path from 3 straight line segments. But I tried curves and combination paths and the technique works in those cases too - so any path should work.

  • I found that using a close-path command (z) on the path caused the path length function to be short on the true distance. This appears as an amount of the path remaining stroked or gapped at either end, with the size depending on the jump between first & last to close the path.

  • The path length is virtually always going to be decimal so don't try to do everything as integers as you will ultimately find your stroke is slightly overlong or short.


To adopt this for animation and easing etc, take the couple of lines from the slider change event and stick them inside the frame callback, manipulating the maths to suit your case.






// Set up the canvas / stage
var stage = new Konva.Stage({container: 'container1', width: 320, height: 180});

// Add a layer
var layer = new Konva.Layer({draggable: false});
stage.add(layer);

// show where the start of the path is.
var circle = new Konva.Circle({
x: 66,
y: 15,
radius: 5,
stroke: 'red'
})
layer.add(circle);

// draw a path.
var path = new Konva.Path({
x: 0,
y: 0,
data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
stroke: 'green'
});

// get the path length and set this as the dash and dashOffset.
var pathLen = path.getLength();
path.dashOffset(pathLen);
path.dash([pathLen]);

layer.add(path)
stage.draw();

// Some UI bits
$('#dist').attr('max', parseInt(pathLen)); // set slider max to length of path
$('#pathLen').html('Path : ' + pathLen); // display path length

// jquery event listener on slider change
$('#dist').on('input', function(){

// compute the new dash lenth as original path length - current slider value.
// Means that dashLen initially = path len and moves toward zero as slider val increases.
var dashLen = pathLen - $(this).val();;
path.dashOffset(dashLen); // set new value
layer.draw(); // refresh the layer to see effect

// update the UI elements
$('#dashLen').html('Dash: ' + dashLen);
$('#pathPC').html(parseInt(100-(100 * (dashLen/pathLen)), 10) + '%');

})

.info 
{
padding-left: 20px;

}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
<div class="slidecontainer">
<input class='slider' id='dist' type="range" min="0" max="100" value="0" class="slider" id="myRange"/>
<span class='info' id='pathPC'></span>
<span class='info' id='pathLen'></span>
<span class='info' id='dashLen'></span>
</div>
<div id='container1' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
<div id='img'></div>








share|improve this answer























  • Thank you very much!
    – Roxane Amory Lorch
    Nov 9 at 19:01


















up vote
1
down vote













My solution with animation:






    var width = window.innerWidth;
var height = window.innerHeight;

// Set up the canvas / stage
var stage = new Konva.Stage({
container: 'container',
width: width,
height: height
});

// Add a layer
var layer = new Konva.Layer({
draggable: false
});
stage.add(layer);

// show where the start of the path is.
var circle = new Konva.Circle({
x: 66,
y: 15,
radius: 5,
stroke: 'red'
})
layer.add(circle);

// draw a path.
var path = new Konva.Path({
x: 0,
y: 0,
data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
stroke: 'green'
});

// get the path length and set this as the dash and dashOffset.
var pathLen = path.getLength();
path.dashOffset(pathLen);
path.dash([pathLen]);

// make some animation with stop
var anim = new Konva.Animation(function (frame) {
var dashLen = pathLen - frame.time / 5;
path.dashOffset(dashLen);
if (dashLen < 0) {
anim.stop();
}
}, layer);

anim.start();

layer.add(path)
stage.draw();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
<div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
<div id='img'></div>








share|improve this answer























  • Nice one - I switched your code into a snippet. Take note for your next post.
    – Vanquished Wombat
    Nov 10 at 8:35


















up vote
1
down vote













And here is an alternative to @Roxane's animated version but using a tween.






var width = window.innerWidth;
var height = window.innerHeight;

// Set up the canvas / stage
var stage = new Konva.Stage({
container: 'container',
width: width,
height: height
});

// Add a layer
var layer = new Konva.Layer({
draggable: false
});
stage.add(layer);

// show where the start of the path is.
var circle = new Konva.Circle({
x: 66,
y: 15,
radius: 5,
stroke: 'red'
})
layer.add(circle);

// draw a path.
var path = new Konva.Path({
x: 0,
y: 0,
data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
stroke: 'green'
});

// get the path length and set this as the dash and dashOffset.
var pathLen = path.getLength();
path.dashOffset(pathLen);
path.dash([pathLen]);

layer.add(path); // have to add to layer for tweening.

// create the tween
var tween = new Konva.Tween({
node: path,
dashOffset: 0,
easing: Konva.Easings['BounceEaseOut'],
duration: 1.5
});
tween.play(); // execute the tween

stage.draw();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
<div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
<div id='img'></div>








share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214214%2fanimate-drawing-of-path-on-html5-canvas%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Here is a potential answer (special thanks to @markov00 re same technique in SVG). It works by manipulating the path dashOffset and dash attributes. There is an excellent explanation of the technique here in a post by Jake Archibald which also includes an interactive experiment with sliders which I found very useful.



    I have tried to make the demo as lightweight as possible and just show the technique - though I added a slider and some UI to help understand the process. The use of jquery is only for the UI parts which are not needed for the technique.



    Couple of points:




    • The demo here uses a path from 3 straight line segments. But I tried curves and combination paths and the technique works in those cases too - so any path should work.

    • I found that using a close-path command (z) on the path caused the path length function to be short on the true distance. This appears as an amount of the path remaining stroked or gapped at either end, with the size depending on the jump between first & last to close the path.

    • The path length is virtually always going to be decimal so don't try to do everything as integers as you will ultimately find your stroke is slightly overlong or short.


    To adopt this for animation and easing etc, take the couple of lines from the slider change event and stick them inside the frame callback, manipulating the maths to suit your case.






    // Set up the canvas / stage
    var stage = new Konva.Stage({container: 'container1', width: 320, height: 180});

    // Add a layer
    var layer = new Konva.Layer({draggable: false});
    stage.add(layer);

    // show where the start of the path is.
    var circle = new Konva.Circle({
    x: 66,
    y: 15,
    radius: 5,
    stroke: 'red'
    })
    layer.add(circle);

    // draw a path.
    var path = new Konva.Path({
    x: 0,
    y: 0,
    data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
    stroke: 'green'
    });

    // get the path length and set this as the dash and dashOffset.
    var pathLen = path.getLength();
    path.dashOffset(pathLen);
    path.dash([pathLen]);

    layer.add(path)
    stage.draw();

    // Some UI bits
    $('#dist').attr('max', parseInt(pathLen)); // set slider max to length of path
    $('#pathLen').html('Path : ' + pathLen); // display path length

    // jquery event listener on slider change
    $('#dist').on('input', function(){

    // compute the new dash lenth as original path length - current slider value.
    // Means that dashLen initially = path len and moves toward zero as slider val increases.
    var dashLen = pathLen - $(this).val();;
    path.dashOffset(dashLen); // set new value
    layer.draw(); // refresh the layer to see effect

    // update the UI elements
    $('#dashLen').html('Dash: ' + dashLen);
    $('#pathPC').html(parseInt(100-(100 * (dashLen/pathLen)), 10) + '%');

    })

    .info 
    {
    padding-left: 20px;

    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
    <div class="slidecontainer">
    <input class='slider' id='dist' type="range" min="0" max="100" value="0" class="slider" id="myRange"/>
    <span class='info' id='pathPC'></span>
    <span class='info' id='pathLen'></span>
    <span class='info' id='dashLen'></span>
    </div>
    <div id='container1' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
    <div id='img'></div>








    share|improve this answer























    • Thank you very much!
      – Roxane Amory Lorch
      Nov 9 at 19:01















    up vote
    2
    down vote



    accepted










    Here is a potential answer (special thanks to @markov00 re same technique in SVG). It works by manipulating the path dashOffset and dash attributes. There is an excellent explanation of the technique here in a post by Jake Archibald which also includes an interactive experiment with sliders which I found very useful.



    I have tried to make the demo as lightweight as possible and just show the technique - though I added a slider and some UI to help understand the process. The use of jquery is only for the UI parts which are not needed for the technique.



    Couple of points:




    • The demo here uses a path from 3 straight line segments. But I tried curves and combination paths and the technique works in those cases too - so any path should work.

    • I found that using a close-path command (z) on the path caused the path length function to be short on the true distance. This appears as an amount of the path remaining stroked or gapped at either end, with the size depending on the jump between first & last to close the path.

    • The path length is virtually always going to be decimal so don't try to do everything as integers as you will ultimately find your stroke is slightly overlong or short.


    To adopt this for animation and easing etc, take the couple of lines from the slider change event and stick them inside the frame callback, manipulating the maths to suit your case.






    // Set up the canvas / stage
    var stage = new Konva.Stage({container: 'container1', width: 320, height: 180});

    // Add a layer
    var layer = new Konva.Layer({draggable: false});
    stage.add(layer);

    // show where the start of the path is.
    var circle = new Konva.Circle({
    x: 66,
    y: 15,
    radius: 5,
    stroke: 'red'
    })
    layer.add(circle);

    // draw a path.
    var path = new Konva.Path({
    x: 0,
    y: 0,
    data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
    stroke: 'green'
    });

    // get the path length and set this as the dash and dashOffset.
    var pathLen = path.getLength();
    path.dashOffset(pathLen);
    path.dash([pathLen]);

    layer.add(path)
    stage.draw();

    // Some UI bits
    $('#dist').attr('max', parseInt(pathLen)); // set slider max to length of path
    $('#pathLen').html('Path : ' + pathLen); // display path length

    // jquery event listener on slider change
    $('#dist').on('input', function(){

    // compute the new dash lenth as original path length - current slider value.
    // Means that dashLen initially = path len and moves toward zero as slider val increases.
    var dashLen = pathLen - $(this).val();;
    path.dashOffset(dashLen); // set new value
    layer.draw(); // refresh the layer to see effect

    // update the UI elements
    $('#dashLen').html('Dash: ' + dashLen);
    $('#pathPC').html(parseInt(100-(100 * (dashLen/pathLen)), 10) + '%');

    })

    .info 
    {
    padding-left: 20px;

    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
    <div class="slidecontainer">
    <input class='slider' id='dist' type="range" min="0" max="100" value="0" class="slider" id="myRange"/>
    <span class='info' id='pathPC'></span>
    <span class='info' id='pathLen'></span>
    <span class='info' id='dashLen'></span>
    </div>
    <div id='container1' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
    <div id='img'></div>








    share|improve this answer























    • Thank you very much!
      – Roxane Amory Lorch
      Nov 9 at 19:01













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    Here is a potential answer (special thanks to @markov00 re same technique in SVG). It works by manipulating the path dashOffset and dash attributes. There is an excellent explanation of the technique here in a post by Jake Archibald which also includes an interactive experiment with sliders which I found very useful.



    I have tried to make the demo as lightweight as possible and just show the technique - though I added a slider and some UI to help understand the process. The use of jquery is only for the UI parts which are not needed for the technique.



    Couple of points:




    • The demo here uses a path from 3 straight line segments. But I tried curves and combination paths and the technique works in those cases too - so any path should work.

    • I found that using a close-path command (z) on the path caused the path length function to be short on the true distance. This appears as an amount of the path remaining stroked or gapped at either end, with the size depending on the jump between first & last to close the path.

    • The path length is virtually always going to be decimal so don't try to do everything as integers as you will ultimately find your stroke is slightly overlong or short.


    To adopt this for animation and easing etc, take the couple of lines from the slider change event and stick them inside the frame callback, manipulating the maths to suit your case.






    // Set up the canvas / stage
    var stage = new Konva.Stage({container: 'container1', width: 320, height: 180});

    // Add a layer
    var layer = new Konva.Layer({draggable: false});
    stage.add(layer);

    // show where the start of the path is.
    var circle = new Konva.Circle({
    x: 66,
    y: 15,
    radius: 5,
    stroke: 'red'
    })
    layer.add(circle);

    // draw a path.
    var path = new Konva.Path({
    x: 0,
    y: 0,
    data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
    stroke: 'green'
    });

    // get the path length and set this as the dash and dashOffset.
    var pathLen = path.getLength();
    path.dashOffset(pathLen);
    path.dash([pathLen]);

    layer.add(path)
    stage.draw();

    // Some UI bits
    $('#dist').attr('max', parseInt(pathLen)); // set slider max to length of path
    $('#pathLen').html('Path : ' + pathLen); // display path length

    // jquery event listener on slider change
    $('#dist').on('input', function(){

    // compute the new dash lenth as original path length - current slider value.
    // Means that dashLen initially = path len and moves toward zero as slider val increases.
    var dashLen = pathLen - $(this).val();;
    path.dashOffset(dashLen); // set new value
    layer.draw(); // refresh the layer to see effect

    // update the UI elements
    $('#dashLen').html('Dash: ' + dashLen);
    $('#pathPC').html(parseInt(100-(100 * (dashLen/pathLen)), 10) + '%');

    })

    .info 
    {
    padding-left: 20px;

    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
    <div class="slidecontainer">
    <input class='slider' id='dist' type="range" min="0" max="100" value="0" class="slider" id="myRange"/>
    <span class='info' id='pathPC'></span>
    <span class='info' id='pathLen'></span>
    <span class='info' id='dashLen'></span>
    </div>
    <div id='container1' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
    <div id='img'></div>








    share|improve this answer














    Here is a potential answer (special thanks to @markov00 re same technique in SVG). It works by manipulating the path dashOffset and dash attributes. There is an excellent explanation of the technique here in a post by Jake Archibald which also includes an interactive experiment with sliders which I found very useful.



    I have tried to make the demo as lightweight as possible and just show the technique - though I added a slider and some UI to help understand the process. The use of jquery is only for the UI parts which are not needed for the technique.



    Couple of points:




    • The demo here uses a path from 3 straight line segments. But I tried curves and combination paths and the technique works in those cases too - so any path should work.

    • I found that using a close-path command (z) on the path caused the path length function to be short on the true distance. This appears as an amount of the path remaining stroked or gapped at either end, with the size depending on the jump between first & last to close the path.

    • The path length is virtually always going to be decimal so don't try to do everything as integers as you will ultimately find your stroke is slightly overlong or short.


    To adopt this for animation and easing etc, take the couple of lines from the slider change event and stick them inside the frame callback, manipulating the maths to suit your case.






    // Set up the canvas / stage
    var stage = new Konva.Stage({container: 'container1', width: 320, height: 180});

    // Add a layer
    var layer = new Konva.Layer({draggable: false});
    stage.add(layer);

    // show where the start of the path is.
    var circle = new Konva.Circle({
    x: 66,
    y: 15,
    radius: 5,
    stroke: 'red'
    })
    layer.add(circle);

    // draw a path.
    var path = new Konva.Path({
    x: 0,
    y: 0,
    data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
    stroke: 'green'
    });

    // get the path length and set this as the dash and dashOffset.
    var pathLen = path.getLength();
    path.dashOffset(pathLen);
    path.dash([pathLen]);

    layer.add(path)
    stage.draw();

    // Some UI bits
    $('#dist').attr('max', parseInt(pathLen)); // set slider max to length of path
    $('#pathLen').html('Path : ' + pathLen); // display path length

    // jquery event listener on slider change
    $('#dist').on('input', function(){

    // compute the new dash lenth as original path length - current slider value.
    // Means that dashLen initially = path len and moves toward zero as slider val increases.
    var dashLen = pathLen - $(this).val();;
    path.dashOffset(dashLen); // set new value
    layer.draw(); // refresh the layer to see effect

    // update the UI elements
    $('#dashLen').html('Dash: ' + dashLen);
    $('#pathPC').html(parseInt(100-(100 * (dashLen/pathLen)), 10) + '%');

    })

    .info 
    {
    padding-left: 20px;

    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
    <div class="slidecontainer">
    <input class='slider' id='dist' type="range" min="0" max="100" value="0" class="slider" id="myRange"/>
    <span class='info' id='pathPC'></span>
    <span class='info' id='pathLen'></span>
    <span class='info' id='dashLen'></span>
    </div>
    <div id='container1' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
    <div id='img'></div>








    // Set up the canvas / stage
    var stage = new Konva.Stage({container: 'container1', width: 320, height: 180});

    // Add a layer
    var layer = new Konva.Layer({draggable: false});
    stage.add(layer);

    // show where the start of the path is.
    var circle = new Konva.Circle({
    x: 66,
    y: 15,
    radius: 5,
    stroke: 'red'
    })
    layer.add(circle);

    // draw a path.
    var path = new Konva.Path({
    x: 0,
    y: 0,
    data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
    stroke: 'green'
    });

    // get the path length and set this as the dash and dashOffset.
    var pathLen = path.getLength();
    path.dashOffset(pathLen);
    path.dash([pathLen]);

    layer.add(path)
    stage.draw();

    // Some UI bits
    $('#dist').attr('max', parseInt(pathLen)); // set slider max to length of path
    $('#pathLen').html('Path : ' + pathLen); // display path length

    // jquery event listener on slider change
    $('#dist').on('input', function(){

    // compute the new dash lenth as original path length - current slider value.
    // Means that dashLen initially = path len and moves toward zero as slider val increases.
    var dashLen = pathLen - $(this).val();;
    path.dashOffset(dashLen); // set new value
    layer.draw(); // refresh the layer to see effect

    // update the UI elements
    $('#dashLen').html('Dash: ' + dashLen);
    $('#pathPC').html(parseInt(100-(100 * (dashLen/pathLen)), 10) + '%');

    })

    .info 
    {
    padding-left: 20px;

    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
    <div class="slidecontainer">
    <input class='slider' id='dist' type="range" min="0" max="100" value="0" class="slider" id="myRange"/>
    <span class='info' id='pathPC'></span>
    <span class='info' id='pathLen'></span>
    <span class='info' id='dashLen'></span>
    </div>
    <div id='container1' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
    <div id='img'></div>





    // Set up the canvas / stage
    var stage = new Konva.Stage({container: 'container1', width: 320, height: 180});

    // Add a layer
    var layer = new Konva.Layer({draggable: false});
    stage.add(layer);

    // show where the start of the path is.
    var circle = new Konva.Circle({
    x: 66,
    y: 15,
    radius: 5,
    stroke: 'red'
    })
    layer.add(circle);

    // draw a path.
    var path = new Konva.Path({
    x: 0,
    y: 0,
    data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
    stroke: 'green'
    });

    // get the path length and set this as the dash and dashOffset.
    var pathLen = path.getLength();
    path.dashOffset(pathLen);
    path.dash([pathLen]);

    layer.add(path)
    stage.draw();

    // Some UI bits
    $('#dist').attr('max', parseInt(pathLen)); // set slider max to length of path
    $('#pathLen').html('Path : ' + pathLen); // display path length

    // jquery event listener on slider change
    $('#dist').on('input', function(){

    // compute the new dash lenth as original path length - current slider value.
    // Means that dashLen initially = path len and moves toward zero as slider val increases.
    var dashLen = pathLen - $(this).val();;
    path.dashOffset(dashLen); // set new value
    layer.draw(); // refresh the layer to see effect

    // update the UI elements
    $('#dashLen').html('Dash: ' + dashLen);
    $('#pathPC').html(parseInt(100-(100 * (dashLen/pathLen)), 10) + '%');

    })

    .info 
    {
    padding-left: 20px;

    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
    <div class="slidecontainer">
    <input class='slider' id='dist' type="range" min="0" max="100" value="0" class="slider" id="myRange"/>
    <span class='info' id='pathPC'></span>
    <span class='info' id='pathLen'></span>
    <span class='info' id='dashLen'></span>
    </div>
    <div id='container1' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
    <div id='img'></div>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 10 at 13:00

























    answered Nov 9 at 18:51









    Vanquished Wombat

    3,70811336




    3,70811336












    • Thank you very much!
      – Roxane Amory Lorch
      Nov 9 at 19:01


















    • Thank you very much!
      – Roxane Amory Lorch
      Nov 9 at 19:01
















    Thank you very much!
    – Roxane Amory Lorch
    Nov 9 at 19:01




    Thank you very much!
    – Roxane Amory Lorch
    Nov 9 at 19:01












    up vote
    1
    down vote













    My solution with animation:






        var width = window.innerWidth;
    var height = window.innerHeight;

    // Set up the canvas / stage
    var stage = new Konva.Stage({
    container: 'container',
    width: width,
    height: height
    });

    // Add a layer
    var layer = new Konva.Layer({
    draggable: false
    });
    stage.add(layer);

    // show where the start of the path is.
    var circle = new Konva.Circle({
    x: 66,
    y: 15,
    radius: 5,
    stroke: 'red'
    })
    layer.add(circle);

    // draw a path.
    var path = new Konva.Path({
    x: 0,
    y: 0,
    data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
    stroke: 'green'
    });

    // get the path length and set this as the dash and dashOffset.
    var pathLen = path.getLength();
    path.dashOffset(pathLen);
    path.dash([pathLen]);

    // make some animation with stop
    var anim = new Konva.Animation(function (frame) {
    var dashLen = pathLen - frame.time / 5;
    path.dashOffset(dashLen);
    if (dashLen < 0) {
    anim.stop();
    }
    }, layer);

    anim.start();

    layer.add(path)
    stage.draw();

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
    <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
    <div id='img'></div>








    share|improve this answer























    • Nice one - I switched your code into a snippet. Take note for your next post.
      – Vanquished Wombat
      Nov 10 at 8:35















    up vote
    1
    down vote













    My solution with animation:






        var width = window.innerWidth;
    var height = window.innerHeight;

    // Set up the canvas / stage
    var stage = new Konva.Stage({
    container: 'container',
    width: width,
    height: height
    });

    // Add a layer
    var layer = new Konva.Layer({
    draggable: false
    });
    stage.add(layer);

    // show where the start of the path is.
    var circle = new Konva.Circle({
    x: 66,
    y: 15,
    radius: 5,
    stroke: 'red'
    })
    layer.add(circle);

    // draw a path.
    var path = new Konva.Path({
    x: 0,
    y: 0,
    data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
    stroke: 'green'
    });

    // get the path length and set this as the dash and dashOffset.
    var pathLen = path.getLength();
    path.dashOffset(pathLen);
    path.dash([pathLen]);

    // make some animation with stop
    var anim = new Konva.Animation(function (frame) {
    var dashLen = pathLen - frame.time / 5;
    path.dashOffset(dashLen);
    if (dashLen < 0) {
    anim.stop();
    }
    }, layer);

    anim.start();

    layer.add(path)
    stage.draw();

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
    <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
    <div id='img'></div>








    share|improve this answer























    • Nice one - I switched your code into a snippet. Take note for your next post.
      – Vanquished Wombat
      Nov 10 at 8:35













    up vote
    1
    down vote










    up vote
    1
    down vote









    My solution with animation:






        var width = window.innerWidth;
    var height = window.innerHeight;

    // Set up the canvas / stage
    var stage = new Konva.Stage({
    container: 'container',
    width: width,
    height: height
    });

    // Add a layer
    var layer = new Konva.Layer({
    draggable: false
    });
    stage.add(layer);

    // show where the start of the path is.
    var circle = new Konva.Circle({
    x: 66,
    y: 15,
    radius: 5,
    stroke: 'red'
    })
    layer.add(circle);

    // draw a path.
    var path = new Konva.Path({
    x: 0,
    y: 0,
    data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
    stroke: 'green'
    });

    // get the path length and set this as the dash and dashOffset.
    var pathLen = path.getLength();
    path.dashOffset(pathLen);
    path.dash([pathLen]);

    // make some animation with stop
    var anim = new Konva.Animation(function (frame) {
    var dashLen = pathLen - frame.time / 5;
    path.dashOffset(dashLen);
    if (dashLen < 0) {
    anim.stop();
    }
    }, layer);

    anim.start();

    layer.add(path)
    stage.draw();

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
    <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
    <div id='img'></div>








    share|improve this answer














    My solution with animation:






        var width = window.innerWidth;
    var height = window.innerHeight;

    // Set up the canvas / stage
    var stage = new Konva.Stage({
    container: 'container',
    width: width,
    height: height
    });

    // Add a layer
    var layer = new Konva.Layer({
    draggable: false
    });
    stage.add(layer);

    // show where the start of the path is.
    var circle = new Konva.Circle({
    x: 66,
    y: 15,
    radius: 5,
    stroke: 'red'
    })
    layer.add(circle);

    // draw a path.
    var path = new Konva.Path({
    x: 0,
    y: 0,
    data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
    stroke: 'green'
    });

    // get the path length and set this as the dash and dashOffset.
    var pathLen = path.getLength();
    path.dashOffset(pathLen);
    path.dash([pathLen]);

    // make some animation with stop
    var anim = new Konva.Animation(function (frame) {
    var dashLen = pathLen - frame.time / 5;
    path.dashOffset(dashLen);
    if (dashLen < 0) {
    anim.stop();
    }
    }, layer);

    anim.start();

    layer.add(path)
    stage.draw();

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
    <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
    <div id='img'></div>








        var width = window.innerWidth;
    var height = window.innerHeight;

    // Set up the canvas / stage
    var stage = new Konva.Stage({
    container: 'container',
    width: width,
    height: height
    });

    // Add a layer
    var layer = new Konva.Layer({
    draggable: false
    });
    stage.add(layer);

    // show where the start of the path is.
    var circle = new Konva.Circle({
    x: 66,
    y: 15,
    radius: 5,
    stroke: 'red'
    })
    layer.add(circle);

    // draw a path.
    var path = new Konva.Path({
    x: 0,
    y: 0,
    data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
    stroke: 'green'
    });

    // get the path length and set this as the dash and dashOffset.
    var pathLen = path.getLength();
    path.dashOffset(pathLen);
    path.dash([pathLen]);

    // make some animation with stop
    var anim = new Konva.Animation(function (frame) {
    var dashLen = pathLen - frame.time / 5;
    path.dashOffset(dashLen);
    if (dashLen < 0) {
    anim.stop();
    }
    }, layer);

    anim.start();

    layer.add(path)
    stage.draw();

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
    <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
    <div id='img'></div>





        var width = window.innerWidth;
    var height = window.innerHeight;

    // Set up the canvas / stage
    var stage = new Konva.Stage({
    container: 'container',
    width: width,
    height: height
    });

    // Add a layer
    var layer = new Konva.Layer({
    draggable: false
    });
    stage.add(layer);

    // show where the start of the path is.
    var circle = new Konva.Circle({
    x: 66,
    y: 15,
    radius: 5,
    stroke: 'red'
    })
    layer.add(circle);

    // draw a path.
    var path = new Konva.Path({
    x: 0,
    y: 0,
    data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
    stroke: 'green'
    });

    // get the path length and set this as the dash and dashOffset.
    var pathLen = path.getLength();
    path.dashOffset(pathLen);
    path.dash([pathLen]);

    // make some animation with stop
    var anim = new Konva.Animation(function (frame) {
    var dashLen = pathLen - frame.time / 5;
    path.dashOffset(dashLen);
    if (dashLen < 0) {
    anim.stop();
    }
    }, layer);

    anim.start();

    layer.add(path)
    stage.draw();

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
    <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
    <div id='img'></div>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 10 at 9:00

























    answered Nov 9 at 20:15









    Roxane Amory Lorch

    215




    215












    • Nice one - I switched your code into a snippet. Take note for your next post.
      – Vanquished Wombat
      Nov 10 at 8:35


















    • Nice one - I switched your code into a snippet. Take note for your next post.
      – Vanquished Wombat
      Nov 10 at 8:35
















    Nice one - I switched your code into a snippet. Take note for your next post.
    – Vanquished Wombat
    Nov 10 at 8:35




    Nice one - I switched your code into a snippet. Take note for your next post.
    – Vanquished Wombat
    Nov 10 at 8:35










    up vote
    1
    down vote













    And here is an alternative to @Roxane's animated version but using a tween.






    var width = window.innerWidth;
    var height = window.innerHeight;

    // Set up the canvas / stage
    var stage = new Konva.Stage({
    container: 'container',
    width: width,
    height: height
    });

    // Add a layer
    var layer = new Konva.Layer({
    draggable: false
    });
    stage.add(layer);

    // show where the start of the path is.
    var circle = new Konva.Circle({
    x: 66,
    y: 15,
    radius: 5,
    stroke: 'red'
    })
    layer.add(circle);

    // draw a path.
    var path = new Konva.Path({
    x: 0,
    y: 0,
    data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
    stroke: 'green'
    });

    // get the path length and set this as the dash and dashOffset.
    var pathLen = path.getLength();
    path.dashOffset(pathLen);
    path.dash([pathLen]);

    layer.add(path); // have to add to layer for tweening.

    // create the tween
    var tween = new Konva.Tween({
    node: path,
    dashOffset: 0,
    easing: Konva.Easings['BounceEaseOut'],
    duration: 1.5
    });
    tween.play(); // execute the tween

    stage.draw();

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
    <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
    <div id='img'></div>








    share|improve this answer

























      up vote
      1
      down vote













      And here is an alternative to @Roxane's animated version but using a tween.






      var width = window.innerWidth;
      var height = window.innerHeight;

      // Set up the canvas / stage
      var stage = new Konva.Stage({
      container: 'container',
      width: width,
      height: height
      });

      // Add a layer
      var layer = new Konva.Layer({
      draggable: false
      });
      stage.add(layer);

      // show where the start of the path is.
      var circle = new Konva.Circle({
      x: 66,
      y: 15,
      radius: 5,
      stroke: 'red'
      })
      layer.add(circle);

      // draw a path.
      var path = new Konva.Path({
      x: 0,
      y: 0,
      data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
      stroke: 'green'
      });

      // get the path length and set this as the dash and dashOffset.
      var pathLen = path.getLength();
      path.dashOffset(pathLen);
      path.dash([pathLen]);

      layer.add(path); // have to add to layer for tweening.

      // create the tween
      var tween = new Konva.Tween({
      node: path,
      dashOffset: 0,
      easing: Konva.Easings['BounceEaseOut'],
      duration: 1.5
      });
      tween.play(); // execute the tween

      stage.draw();

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
      <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
      <div id='img'></div>








      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        And here is an alternative to @Roxane's animated version but using a tween.






        var width = window.innerWidth;
        var height = window.innerHeight;

        // Set up the canvas / stage
        var stage = new Konva.Stage({
        container: 'container',
        width: width,
        height: height
        });

        // Add a layer
        var layer = new Konva.Layer({
        draggable: false
        });
        stage.add(layer);

        // show where the start of the path is.
        var circle = new Konva.Circle({
        x: 66,
        y: 15,
        radius: 5,
        stroke: 'red'
        })
        layer.add(circle);

        // draw a path.
        var path = new Konva.Path({
        x: 0,
        y: 0,
        data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
        stroke: 'green'
        });

        // get the path length and set this as the dash and dashOffset.
        var pathLen = path.getLength();
        path.dashOffset(pathLen);
        path.dash([pathLen]);

        layer.add(path); // have to add to layer for tweening.

        // create the tween
        var tween = new Konva.Tween({
        node: path,
        dashOffset: 0,
        easing: Konva.Easings['BounceEaseOut'],
        duration: 1.5
        });
        tween.play(); // execute the tween

        stage.draw();

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
        <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
        <div id='img'></div>








        share|improve this answer












        And here is an alternative to @Roxane's animated version but using a tween.






        var width = window.innerWidth;
        var height = window.innerHeight;

        // Set up the canvas / stage
        var stage = new Konva.Stage({
        container: 'container',
        width: width,
        height: height
        });

        // Add a layer
        var layer = new Konva.Layer({
        draggable: false
        });
        stage.add(layer);

        // show where the start of the path is.
        var circle = new Konva.Circle({
        x: 66,
        y: 15,
        radius: 5,
        stroke: 'red'
        })
        layer.add(circle);

        // draw a path.
        var path = new Konva.Path({
        x: 0,
        y: 0,
        data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
        stroke: 'green'
        });

        // get the path length and set this as the dash and dashOffset.
        var pathLen = path.getLength();
        path.dashOffset(pathLen);
        path.dash([pathLen]);

        layer.add(path); // have to add to layer for tweening.

        // create the tween
        var tween = new Konva.Tween({
        node: path,
        dashOffset: 0,
        easing: Konva.Easings['BounceEaseOut'],
        duration: 1.5
        });
        tween.play(); // execute the tween

        stage.draw();

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
        <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
        <div id='img'></div>








        var width = window.innerWidth;
        var height = window.innerHeight;

        // Set up the canvas / stage
        var stage = new Konva.Stage({
        container: 'container',
        width: width,
        height: height
        });

        // Add a layer
        var layer = new Konva.Layer({
        draggable: false
        });
        stage.add(layer);

        // show where the start of the path is.
        var circle = new Konva.Circle({
        x: 66,
        y: 15,
        radius: 5,
        stroke: 'red'
        })
        layer.add(circle);

        // draw a path.
        var path = new Konva.Path({
        x: 0,
        y: 0,
        data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
        stroke: 'green'
        });

        // get the path length and set this as the dash and dashOffset.
        var pathLen = path.getLength();
        path.dashOffset(pathLen);
        path.dash([pathLen]);

        layer.add(path); // have to add to layer for tweening.

        // create the tween
        var tween = new Konva.Tween({
        node: path,
        dashOffset: 0,
        easing: Konva.Easings['BounceEaseOut'],
        duration: 1.5
        });
        tween.play(); // execute the tween

        stage.draw();

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
        <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
        <div id='img'></div>





        var width = window.innerWidth;
        var height = window.innerHeight;

        // Set up the canvas / stage
        var stage = new Konva.Stage({
        container: 'container',
        width: width,
        height: height
        });

        // Add a layer
        var layer = new Konva.Layer({
        draggable: false
        });
        stage.add(layer);

        // show where the start of the path is.
        var circle = new Konva.Circle({
        x: 66,
        y: 15,
        radius: 5,
        stroke: 'red'
        })
        layer.add(circle);

        // draw a path.
        var path = new Konva.Path({
        x: 0,
        y: 0,
        data: 'M66 15 L75 100 L225 120 L100 17 L66 15',
        stroke: 'green'
        });

        // get the path length and set this as the dash and dashOffset.
        var pathLen = path.getLength();
        path.dashOffset(pathLen);
        path.dash([pathLen]);

        layer.add(path); // have to add to layer for tweening.

        // create the tween
        var tween = new Konva.Tween({
        node: path,
        dashOffset: 0,
        easing: Konva.Easings['BounceEaseOut'],
        duration: 1.5
        });
        tween.play(); // execute the tween

        stage.draw();

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.js"></script>
        <div id='container' style="display: inline-block; width: 300px, height: 200px; background-color: silver; overflow: hidden; position: relative;"></div>
        <div id='img'></div>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 12:20









        Vanquished Wombat

        3,70811336




        3,70811336






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214214%2fanimate-drawing-of-path-on-html5-canvas%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)