Question

I m new to jointJS, I need to create custom shapes using JointJS, I have tried creating the diamond shape using the Rectangle, making its height and width same, and then rotate by 45 degrees as follows,

var diamond =  new joint.shapes.basic.Rect({
        position: { x: 100, y: 100 },
        size: { width: 100, height: 100 },
        attrs: { diamond: { width: 100, height: 30 } }
    }); 
    diamond.attr({

        rect: { fill: '#cccccc', 'stroke-width': 2, stroke: 'black' },
        text: {
            text: 'Diamond', fill: '#3498DB',
            'font-size': 18, 'font-weight': 'bold', 
            'font-variant': 'small-caps', 
            'text-transform': 'capitalize'
        }
    });
  diamond.rotate(45);

However, the text present inside the rectangle also gets rotated, Any Ideas how can i proceed.... Also I need to create hexagon with a label... Any help will be much appreciated ....

Thanks In Advance,

Mayuri

Was it helpful?

Solution

There is no need to rotate the whole element. Try to add a transform attribute to joint.dia.basic.Rect model.

rect: { transform: 'rotate(45)' }

The other option would be to use joint.dia.basic.Path model.

var diamond = new joint.shapes.basic.Path({
    size: { width: 100, height: 100 },
    attrs: {
        path: { d: 'M 30 0 L 60 30 30 60 0 30 z' },
        text: {
            text: 'Diamond',
            'ref-y': .5 // basic.Path text is originally positioned under the element
        }
    }
});

In order to achieve a hexagon shape, use the joint.dia.basic.Path model again, but this time use the following path data.

path: { d: 'M 50 0 L 0 20 0 80 50 100 100 80 100 20 z'}

Last but least you can create a custom shape with SVG Polygon in its markup.

OTHER TIPS

Thanks a lot Roman, I followed first solution for diamond and it worked liked a charm!!

here is this for any one looking to make diamond shape using joint.js, I have added the following in joint.js

joint.shapes.basic.Diamond = joint.shapes.basic.Generic.extend({

    markup: '<g class="rotatable"><g class="scalable"><rect/></g><text/></g>',

    defaults: joint.util.deepSupplement({

        type: 'basic.Rect',
        attrs: {
            'rect': { fill: '#FFFFFF', stroke: 'black', width: 1, height: 1,transform: 'rotate(45)' },
            'text': { 'font-size': 14, text: '', 'ref-x': .5, 'ref-y': .5, ref: 'rect', 'y-alignment': 'middle', 'x-alignment': 'middle', fill: 'black', 'font-family': 'Arial, helvetica, sans-serif' }
        }

    }, joint.shapes.basic.Generic.prototype.defaults)
});

And for its implementation as follows,

var diamond =  new joint.shapes.basic.Diamond({
        position: { x: 100, y: 100 },
        size: { width: 100, height: 100 },
        attrs: { diamond: { width: 100, height: 30 } }
    }); 
    diamond.attr({

        rect: { fill: '#cccccc', 'stroke-width': 2, stroke: 'black' },
        text: {
            text: 'Diamond', fill: '#3498DB',
            'font-size': 18, 'font-weight': 'bold', 
            'font-variant': 'small-caps', 
            'text-transform': 'capitalize'
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top