Drawing a Line in a html5 canvas using EaselJS

10,141

There are a few issues here. The error you are seeing is because you are adding the Graphics to the Stage, and not the Shape.

The other issue is how the Graphics are modified in the tick:


this.tick = function() {
    graphics.moveTo(100,(dataList[this.index].magnitude)*100); 
    graphics.lineTo(100,(dataList[(this.index)++].magnitude)*100);
    stage.addChild(graphics);
};

You only need to add your Shape to the stage one time, and it will redraw your graphics each time every time the Stage is updated. Your tick call is adding new Graphics instructions every frame, so it will stack all those calls up, and eventually be really slow.

Make sure you clear your Graphics before you draw new things to it, unless you are trying to create an additive effect (and if you are, perhaps look into caching/updateCache to make it performant). Check out the "curveTo" and "updateCache" examples in the GitHub repository for usage.

Once you have added the Shape to the stage instead of the Graphics, feel free to post some follow up questions, and I can try and assist further.

Cheers :)

Share:
10,141
Some Guy Really
Author by

Some Guy Really

Updated on June 21, 2022

Comments

  • Some Guy Really
    Some Guy Really almost 2 years

    I am very new to Easel and HTML5 itself. I am trying to draw a line using on a canvas using EaselJS. The X- Co ordinate is fixedd to 100 and the Y-Co ordinate is got from a array list. The code that i have written is given below. Could please someone let me know where i am going wrong?

    function myFunction(attachPoint)
    {
    //Code for canvas creation is written here.[Not shown];
    //created a stage.
    stage = new createjs.Stage(canvas.domElement());
    //3. create some shapes.MagnitudeLessThanTwo is the array where we get the YAxis Coordinates from
    alert("The lenght before function is"+MagnitudeLessThanTwo.length);
    myShape = new drawLineGraph(MagnitudeLessThanTwo);
    //4. finally add that shape to the stage
    stage.addChild(myShape);
    //5. set up the ticker
    if (!createjs.Ticker.hasEventListener("tick")) { 
    createjs.Ticker.addEventListener("tick", ourTickFunction);
      };
    };
    
    function drawLineGraph(dataList)
    {
    this.index=0;//To keep the track of the index of the array from which we get the Y Axis.
    var graphics = new createjs.Graphics();
    graphics.setStrokeStyle(1);
    graphics.beginStroke("white");
    graphics.moveTo(50,(dataList[this.index].magnitude)*100); 
    graphics.lineTo(50,(dataList[(this.index)++].magnitude)*100);
    createjs.Shape.call(this,graphics);
    this.tick = function() {
    graphics.moveTo(100,(dataList[this.index].magnitude)*100); 
    graphics.lineTo(100,(dataList[(this.index)++].magnitude)*100);
    stage.addChild(graphics);
      };
    };
    drawLineGraph.prototype = new createjs.Shape(); //set prototype
    drawLineGraph.prototype.constructor = drawLineGraph; //fix constructor pointer
    
    
    I am getting the following Error.
    "Object [object Object] has no method 'isVisible'"- This is inside the EaselJS Library.