How to draw a line with animation of drawing?

Hello!
I need to draw a line with two dots.

For example:
var viewer = new Cesium.Viewer('cesiumContainer');

var startPos = Cesium.Cartesian3.fromDegrees(-75.0, 40.0);
var finPos = Cesium.Cartesian3.fromDegrees(-175.0, 40.0);

var line = new Cesium.Entity({
                    name: 'Redline',
                    polyline: {
                        positions: [startPos, finPos],
                        width: 5,
                        material: Cesium.Color.RED, //show : false
                    }

                });
viewer.entities.add(line);

What should I do to draw that line with realtime animation of drawing?

If you’re dealing with a smaller set of points something like this would work:

https://gist.github.com/maikuru/841a821bdb347955dca0 or http://codepen.io/maikuru/pen/EjZXYb

Thank you, Mike.
But I can't understand how to implement the animation, which will operate during the pause.

lets forget code for a second. Can you explain your situation and desired results.

Feel free to be as generic as you want/need to, but I need to know:

Properties: What data you have? (example lat/lon/time)

Scope: How many entities/objects are going to be plotted at any given time?

Presentation: Are the paths going to always drawn startingPoint to only currentPoint or a multi-segmented line starting from startingPoint through each position you received?

If you need to implement some basic drawing, have a looks at:
https://github.com/leforthomas/cesium-drawhelper

I have leverage this with cesium 1.9, it works off of primitives rather than entities, I’ve been using it with some minor changes to deal with callbacks to implement a basic clamp to ground on polylines and polygons.

Look. I have two points, two pins to be exact. If I click on one pin а line should be drawn to another pin. By default line appears instantly. I need to implement progressive rendering, which is not dependent on the speed of the playing or pause.

There really is only 2 options to accomplish this:

  1. SamplePointProperty, which I demonstrated before

  2. Write a custom shader, which is a much more advanced task. If you want to go that route I recommend looking at the existing shaders in the Source for examples.

This is straightforward to do. You need to create a dynamic positions property that is dependent on wall time and not the simulation time. Whether the pins are moving themselves or not doesn’t matter because our positions property will keep a reference to them. You can use Cartesian3’s lerp function to perform the interpolation. Here’s a simple complete example that you can copy and paste into Sandcastle. It creates 2 entities and when you press the go button, it creates a 3rd entity that is a line that grows from the start to the end over a user specified time (in milliseconds). Let me know if any part of the below code is unclear. createLineAnimation is the important part.

var viewer = new Cesium.Viewer(‘cesiumContainer’, {timeline : false, animation : false});

var pinBuilder = new Cesium.PinBuilder();

var bluePin = viewer.entities.add({

name : ‘Blank blue pin’,

position : Cesium.Cartesian3.fromDegrees(-75.170726, 39.9208667),

billboard : {

image : pinBuilder.fromColor(Cesium.Color.ROYALBLUE, 48).toDataURL(),

verticalOrigin : Cesium.VerticalOrigin.BOTTOM

}

});

var questionPin = viewer.entities.add({

name : ‘Question mark’,

position : Cesium.Cartesian3.fromDegrees(-75.1698529, 39.9220071),

billboard : {

image : pinBuilder.fromText(’?’, Cesium.Color.BLACK, 48).toDataURL(),

verticalOrigin : Cesium.VerticalOrigin.BOTTOM

}

});

viewer.zoomTo(viewer.entities);

function createLineAnimation(startEntity, endEntity, duration){

var startTime = performance.now();

return new Cesium.CallbackProperty(function(time, result){

if(!Cesium.defined(result)){

result = ;

}

var now = performance.now();

var start = startEntity.position.getValue(time, result[0]);

var end = endEntity.position.getValue(time, result[1]);

var t = Math.min(1.0, (now - startTime) / duration);

Cesium.Cartesian3.lerp(start, end, t, end);

result[0] = start;

result[1] = end;

result.length = 2;

return result;

}, false);

}

Sandcastle.addToolbarButton(‘Go’, function(){

viewer.entities.add({

polyline: {

positions : createLineAnimation(bluePin, questionPin, 2000)

}

});

});

Thank you! This is what I need!

I just tried this out, it looks great. Thanks for the code! One thing I noticed is that the lines seem to "swim" or "wander" if I use regular lerp so I used the following spherical linear interpolation:

function Slerp(from,to, t, result) {
    var result1 = new Cesium.Cartesian3();
    var result2 = new Cesium.Cartesian3();
    var result3 = new Cesium.Cartesian3();
    var theta = Cesium.Cartesian3.angleBetween(from, to);
    Cesium.Cartesian3.multiplyByScalar(from, Math.sin((1-t)*theta), result1);
    Cesium.Cartesian3.multiplyByScalar(to, Math.sin(t*theta), result2);
    Cesium.Cartesian3.add(result1, result2, result3);
    Cesium.Cartesian3.divideByScalar(result3, Math.sin(theta), result);

    return result;
}

Hi Everyone,
I designed a arrow using velocity orientation property and it travels from point to point. i want to improve the app by making the arrow go lower from point to point. is there any way to do that???

my code is found below…

var target = viewer.entities.add({

name: (sourceCountry + " - " + destCountry),

position: positions,

orientation: new Cesium.VelocityOrientationProperty(positions),

path: {

resolution: 1,

/*material: new Cesium.PolylineGlowMaterialProperty({

glowPower: 0.1,

color: Cesium.Color.YELLOW

}),*/

//material: new Cesium.PolylineArrowMaterialProperty(Cesium.Color.RED), // by tschia attackArrowColour

material: attackArrowColour(devicetype,alert),

width: 20,

//trailTime: 12460*60,

trailTime: duration, // original is duration

leadTime: 0

},

Hi Everyone,
I designed a arrow using velocity orientation property and it travels from point to point. i want to improve the app by making the arrow go lower from point to point. is there any way to do that???

my code is found below…

var target = viewer.entities.add({

name: (sourceCountry + " - " + destCountry),

position: positions,

orientation: new Cesium.VelocityOrientationProperty(positions),

path: {

resolution: 1,

/*material: new Cesium.PolylineGlowMaterialProperty({

glowPower: 0.1,

color: Cesium.Color.YELLOW

}),*/

//material: new Cesium.PolylineArrowMaterialProperty(Cesium.Color.RED), // by tschia attackArrowColour

material: attackArrowColour(devicetype,alert),

width: 20,

//trailTime: 12460*60,

trailTime: duration, // original is duration

leadTime: 0

},


I have attached the image to this discussion to show what i want… i would rather want the red than the yellow…

Cheers,

Abdul

I just tried this out, it looks great. Thanks for the code! One thing I noticed is that the lines seem to “swim” or “wander” if I use regular lerp so I used the following spherical linear interpolation:
function Slerp(from,to, t, result) {
var result1 = new Cesium.Cartesian3();
var result2 = new Cesium.Cartesian3();
var result3 = new Cesium.Cartesian3();
var theta = Cesium.Cartesian3.angleBetween(from, to);
Cesium.Cartesian3.multiplyByScalar(from, Math.sin((1-t)theta), result1);
Cesium.Cartesian3.multiplyByScalar(to, Math.sin(t
theta), result2);
Cesium.Cartesian3.add(result1, result2, result3);
Cesium.Cartesian3.divideByScalar(result3, Math.sin(theta), result);

return result;
}

Hello!
I need to draw a line with two dots.

For example:
var viewer = new Cesium.Viewer(‘cesiumContainer’);

var startPos = Cesium.Cartesian3.fromDegrees(-75.0, 40.0);
var finPos = Cesium.Cartesian3.fromDegrees(-175.0, 40.0);

var line = new Cesium.Entity({
name: ‘Redline’,
polyline: {
positions: [startPos, finPos],
width: 5,
material: Cesium.Color.RED, //show : false
}

            });

viewer.entities.add(line);

What should I do to draw that line with realtime animation of drawing?

Hi there,

Check out this code example: http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Callback%20Property.html&label=Beginner

Hope that helps,

  • Rachel