简体   繁体   中英

What is the alternate for drawPolygon in create JS ? Has this function been changed to something else?

Hope you're doing well and day has been good for you. My scenario is, we used create js Create JS library for drawing shapes in our old Angular JS project and the code is below:

polygonGraphics(){
 var g = new createjs.Graphics();
 g.setStrokeStyle(2);
 g.beginStroke(createJS.Graphics.getRGB(255,0,0))
 g.beginFill("#34343");
 g.drawPolygon(0,0,customData)
}

This above code works fine in that old Angular Js project.

Now when I try to use this code in my new Angular 2 project, the compiler says, g.drawPolygon does not exist. Has this been changed to something else in any of its versions? I tried finding but didn't get much info on this.

Any help would be appreciated. Thank you in advance

I certainly have found what code has been used. Here is the complete working code drawPolygon and this is what I need. Do you have any idea, how can I add this code in my typescript file so that it can work by using g.drawPolygon()? Here is the code and working example is in the link.

    (createjs.Graphics.Polygon = function(x, y, points) {
    this.x = x;
    this.y = y;
    this.points = points;
}).prototype.exec = function(ctx) {
    // Start at the end to simplify loop
    var end = this.points[this.points.length - 1];
    ctx.moveTo(end.x, end.y);
    this.points.forEach(function(point) {
        ctx.lineTo(point.x, point.y);
    });
};
createjs.Graphics.prototype.drawPolygon = function(x, y, args) {
    var points = [];
    if (Array.isArray(args)) {
        args.forEach(function(point) {
            point = Array.isArray(point) ? {x:point[0], y:point[1]} : point;
            points.push(point);
        });
    } else {
        args = Array.prototype.slice.call(arguments).slice(2);
        var px = null;
        args.forEach(function(val) {
            if (px === null) {
                px = val;
            } else {
                points.push({x: px, y: val});
                px = null;
            }
        });
    }
    return this.append(new createjs.Graphics.Polygon(x, y, points));
};

Thank you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM