javascriptgojs

How do I add a shape at a particular x, y position within a Go.JS diagram?


I am using Go.JS and trying to create a function that will fill a user defined shape with other shapes. To do this, I want to check if a square falls within the larger shape's geometry.containsPoint(), and if so draw a shape at those exact points using another function. I have been reading the documentation, and it seems that Go.JS doesn't really like to use x,y coordinates when creating its shapes. A go.geometry has startX and startY which would meet my needs, but I have struggled to connect a custom rectangle geometry to my new shape. How else can I create a rectangle at a certain point in my diagram? Everything I have tried thusfar has failed.

This is the current state of my function, I have a failed attempt to add a geometry in there (despite the fact that the constructor is wrong), but this version shows what I was attempting.

    //New function for adding shapes at a specific x,y
    function addNode(nodeType, color, text= " ", xcord, ycord, size){
        var node = new go.Node("Auto")
            .add(new go.Shape(nodeType, {
                geometry: new go.Geometry(startX=xcord, startY=ycord, endX=xcord+size, endY=ycord+size),
                width: size,
                height: size,
                fill: color,
                strokewidth: 3
            }))
            .add(new go.TextBlock(text, {
                margin: 5
            }))
        node.location = (xcord, ycord);
        diagram.add(node);
    }

Solution

  • node.location = (xcord, ycord);
    

    needs to be

    node.location = new go.Point(xcord, ycord);