JointJS creating custom Shapes, Diamond, Hexagon
Asked Answered
C

2

7

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

Cloudberry answered 21/1, 2014 at 16:20 Comment(0)
M
6

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.

Missioner answered 23/1, 2014 at 0:35 Comment(0)
C
3

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'
        }
    });
Cloudberry answered 23/1, 2014 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.