我对画架和HTML5本身很陌生。我想用画笔在画布上画一条线。X坐标固定为100,Y坐标从数组列表中获得。我写的代码如下。有人能告诉我哪里出错了吗?

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.

最佳答案

这里有一些问题。您看到的错误是因为您正在将图形添加到阶段,而不是形状。
另一个问题是如何在勾号中修改图形:


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

您只需要将形状添加到阶段中一次,每次更新阶段时,它都会重新绘制图形。你的tick调用会在每帧中添加新的图形指令,所以它会将所有这些调用叠加起来,最终会非常慢。
在绘制新对象之前,请确保清除图形,除非尝试创建附加效果(如果是,请查看缓存/更新缓存以使其具有性能)。查看GitHub存储库中的“curveTo”和“updateCache”示例以了解用法。
一旦你把形状添加到舞台上,而不是图形,请随时发布一些后续问题,我可以尝试和进一步协助。
干杯:)

09-25 16:00