jonitjs图形嵌套

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <div id="paper-reparenting"></div>
        
        <!-- dependencies -->
        <!-- http://resources.jointjs.com/docs/jointjs/v3.0/joint.html#dia.Paper.events -->
        <script src="node_modules/jquery/dist/jquery.js"></script>
        <script src="node_modules/lodash/lodash.js"></script>
        <script src="node_modules/backbone/backbone.js"></script>
        <script src="node_modules/jointjs/dist/joint.js"></script>
        <script>
            (function() {
            
            var graph = new joint.dia.Graph;
            var paper = new joint.dia.Paper({ 
                el: $('#paper-reparenting'), 
                width: 1000, 
                height: 1000, 
                gridSize: 1, 
                model: graph ,
            });
            
            var r1 = new joint.shapes.basic.Rect({
                position: { x: 20, y: 20 },
                size: { width: 200, height: 200 },
                attrs: { rect: { fill: '#E74C3C' }, text: { text: 'El A' } }
            });
            var r2 = new joint.shapes.basic.Rect({
                position: { x: 270, y: 30 },
                size: { width: 100, height: 80 },
                attrs: { rect: { fill: '#F1C40F' }, text: { text: 'El B' } }
            });
            
            graph.addCells([r1, r2]);
                
            // First, unembed the cell that has just been grabbed by the user.
            paper.on('cell:pointerdown', function(cellView, evt, x, y) {
                
                var cell = cellView.model;
            
                if (!cell.get('embeds') || cell.get('embeds').length === 0) {
                    // Show the dragged element above all the other cells (except when the
                    // element is a parent).
                    cell.toFront();
                }
                
                if (cell.get('parent')) {
                    graph.getCell(cell.get('parent')).unembed(cell);
                }
            });
            
            // When the dragged cell is dropped over another cell, let it become a child of the
            // element below.
            paper.on('cell:pointerup', function(cellView, evt, x, y) {
            
                var cell = cellView.model;
                var cellViewsBelow = paper.findViewsFromPoint(cell.getBBox().center());
            
                if (cellViewsBelow.length) {
                    // Note that the findViewsFromPoint() returns the view for the `cell` itself.
                    var cellViewBelow = _.find(cellViewsBelow, function(c) { return c.model.id !== cell.id });
                
                    // Prevent recursive embedding.
                    if (cellViewBelow && cellViewBelow.model.get('parent') !== cell.id) {
                        cellViewBelow.model.embed(cell);
                    }
                }
            });
                
            }());
        </script>
    </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值