Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="paper-container"></div>
<a target="_blank" href="https://www.jointjs.com">
  <svg version="1.2" id="logo" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 280" width="200" height="56">
    <path fill="#DFE6EC" d="m130.71 225.71l-27.28-27.27q0-0.01 0-0.01h76.41v-103.68h27.28c0 0 0 59.4 0 98.19l-32.77 32.77zm330.37-116.97c10.68 10.41 17.29 25.87 17.29 46.13 0 20.26-6.61 35.71-17.29 46.13-10.69 10.41-25.47 15.79-41.91 15.79-16.44 0-31.22-5.38-41.91-15.79-10.68-10.42-17.29-25.87-17.29-46.13 0-20.26 6.61-35.72 17.29-46.13 10.69-10.41 25.47-15.79 41.91-15.79 16.44 0 31.22 5.38 41.91 15.79zm401.41-18.61q-8.23-7.14-22.76-7.15-11.77 0.01-18.3 5.07-6.59 5.11-6.59 14.42 0 6.67 3.47 10.89 3.42 4.18 10.27 7.59 6.77 3.37 20.96 8.6h0.01q14.98 5.85 23.95 10.62 8.92 4.74 15.31 13.26 6.38 8.48 6.38 21.16 0 12.48-6.28 22.05-6.29 9.58-18.03 14.86-11.78 5.29-27.76 5.29-15.99 0-28.09-5.4-12.05-5.39-18.55-15.18-6.49-9.79-6.49-22.71v-7.66h23.37v6.36q-0.01 10.17 8.71 16.92 8.64 6.7 22.95 6.7 13.05-0.01 19.69-5.74 6.69-5.76 6.69-14.84-0.01-6.23-3.69-10.56-3.63-4.29-10.37-7.81-6.67-3.48-20.01-8.7 0 0-0.01 0-14.98-5.64-24.27-10.63-9.23-4.95-15.42-13.46-6.16-8.49-6.16-21.17 0-18.93 13.39-29.9 13.45-11 35.93-10.99 15.77 0 27.86 5.61 12.07 5.6 18.78 15.62 6.7 10.01 6.7 23.13v5.92h-23.37v-4.61q0-10.38-8.27-17.56zm-318.54 18.35h1.52c7.2-9.6 18.63-15.53 34.94-15.53 14.12 0 25.51 4.23 33.38 12.18 7.88 7.95 12.27 19.65 12.27 34.71v74.96h-21.74v-71.71c0-9.84-2.46-17.37-7.24-22.42-4.78-5.03-11.84-7.55-20.88-7.55-10.09 0-18.19 3.05-23.74 9.4-5.05 5.79-7.99 14.26-8.51 25.47v66.53h-21.83v-119.76h21.83zm-194.95-13.73c0 0 0 63.58 0 98.2l-21.85 21.85c-10.29 0-22.53 0-32.81 0l-21.83-21.83q0 0 0 0h54.66v-98.22zm353.46 98.22l-21.83 21.83c0 0-10.89 0-21.8 0l-21.85-21.85v-130.94h21.83v32.74h32.74v21.83h-32.74v76.39h98.31v-130.96h21.83c0 0 0 88.7 0 130.94l-21.85 21.85c-10.29 0-22.53 0-32.81 0 0 0-21.83-21.83-21.83-21.83zm-213.17-98.22h21.83v119.77h-21.83zm-96.8 29.36c-6.6 7.05-10.44 17.44-10.44 30.75 0 13.3 3.84 23.69 10.44 30.74 6.57 7.02 15.86 10.69 26.68 10.69 10.82 0 20.11-3.67 26.68-10.69 6.61-7.05 10.45-17.44 10.45-30.74 0-13.31-3.84-23.7-10.45-30.75-6.57-7.01-15.86-10.69-26.68-10.69-10.82 0-20.11 3.68-26.68 10.69zm-299.99 63.38l-27.28-27.28q0 0 0 0h76.4v-103.69h27.29c0 0 0 59.4 0 98.2l-32.77 32.77zm396.79-125.48h21.82v21.82h-21.82zm-162.11 0h21.82v21.83h-21.82z" />
  </svg>
</a>
              
            
!

CSS

              
                #paper-container {
  position: absolute;
  right: 0;
  top: 0;
  left: 0;
  bottom: 0;
  overflow: scroll;
}

#logo {
  position: absolute;
  bottom: 20px;
  right: 20px;
}

              
            
!

JS

              
                const { dia, elementTools, shapes: defaultShapes, layout, util } = joint;

const Event = dia.Element.define(
    'fta.Event',
    {
        z: 3,
        attrs: {
            root: {
                pointerEvents: 'bounding-box',
            },
            body: {
                strokeWidth: 2,
                stroke: '#ed2637',
                fill: {
                    type: 'pattern',
                    attrs: {
                        width: 12,
                        height: 12,
                        'stroke-width': 2,
                        'stroke-opacity': 0.3,
                        stroke: '#ed2637',
                        fill: 'none',
                    },
                    markup: util.svg`
                        <rect width="12" height="12" fill="#131e29" stroke="none" />
                        <path d="M 0 0 L 12 12 M 6 -6 L 18 6 M -6 6 L 6 18" />
                    `,
                },
            },
            label: {
                textWrap: {
                    height: -20,
                    width: -20,
                    ellipsis: true,
                },
                x: 'calc(w / 2)',
                y: 'calc(h / 2)',
                fontSize: 16,
                fontFamily: 'sans-serif',
                fill: '#ffffff',
                textAnchor: 'middle',
                textVerticalAnchor: 'middle',
            },
        },
    },
    {
        // Prototype
    },
    {
        // Static
        create: function(text) {
            return new this({
                attrs: {
                    label: { text: text },
                },
            });
        },
    }
);

const IntermediateEvent = Event.define(
    'fta.IntermediateEvent',
    {
        size: {
            width: 120,
            height: 150,
        },
        attrs: {
            root: {
                title: 'Intermediate Event',
            },
            body: {
                d: 'M 10 0 H calc(w-10) l 10 10 V calc(h - 90) l -10 10 H 10 l -10 -10 V 10 Z',
                stroke: '#ed2637',
                fill: '#131e29',
            },
            label: {
                textWrap: {
                    height: -90,
                    width: -20,
                },
                fontSize: 16,
                y: 'calc(h / 2 - 40)',
                fill: '#ffffff',
            },
            idBody: {
                width: 'calc(w - 20)',
                height: 30,
                y: 'calc(h - 70)',
                x: 10,
                fill: '#131e29',
                stroke: '#dde6ed',
                strokeWidth: 2,
            },
            idLabel: {
                y: 'calc(h - 55)',
                x: 'calc(w / 2)',
                fontSize: 14,
                fontFamily: 'sans-serif',
                fill: '#ffffff',
                textAnchor: 'middle',
                textVerticalAnchor: 'middle',
            },
            gate: {
                event: 'element:gate:click',
                gateType: 'xor',
                stroke: '#dde6ed',
                fill: {
                    type: 'pattern',
                    attrs: {
                        width: 6,
                        height: 6,
                        'stroke-width': 1,
                        'stroke-opacity': 0.3,
                        stroke: '#dde6ed',
                        fill: 'none',
                    },
                    markup: [
                        {
                            tagName: 'rect',
                            attributes: {
                                width: 6,
                                height: 6,
                                fill: '#131e29',
                                stroke: 'none',
                            },
                        },
                        {
                            tagName: 'path',
                            attributes: {
                                d: 'M 3 0 L 3 6',
                            },
                        },
                    ],
                },
                strokeWidth: 2,
                transform: 'translate(calc(w / 2), calc(h))',
                fillRule: 'nonzero',
                cursor: 'pointer',
            },
        },
    },
    {
        markup: util.svg`
            <path @selector="gate" />
            <path @selector="body" />
            <rect @selector="idBody" />
            <text @selector="label" />
            <text @selector="idLabel" />
        `,
        gateTypes: {
            or: 'M -20 0 C -20 -15 -10 -30 0 -30 C 10 -30 20 -15 20 0 C 10 -6 -10 -6 -20 0',
            xor: 'M -20 0 C -20 -15 -10 -30 0 -30 C 10 -30 20 -15 20 0 C 10 -6 -10 -6 -20 0 M -20 0 0 -30 M 0 -30 20 0',
            and: 'M -20 0 C -20 -25 -10 -30 0 -30 C 10 -30 20 -25 20 0 Z',
            priority_and:
                'M -20 0 C -20 -25 -10 -30 0 -30 C 10 -30 20 -25 20 0 Z M -20 0 0 -30 20 0',
            inhibit: 'M -10 0 -20 -15 -10 -30 10 -30 20 -15 10 0 Z',
            transfer: 'M -20 0 20 0 0 -30 z',
        },
        gate: function(type) {
            if (type === undefined) return this.attr(['gate', 'gateType']);
            return this.attr(['gate'], {
                gateType: type,
                title: type.toUpperCase() + ' Gate',
            });
        },
    },
    {
        attributes: {
            gateType: {
                set: function(type) {
                    const data = this.model.gateTypes[type];
                    return { d: data ? data + ' M 0 -30 0 -80' : 'M 0 0 0 0' };
                },
            },
        },

        create: function(text) {
            const id = Math.random().toString(36).substring(2, 8);
            return new this({
                id,
                attrs: {
                    label: { text },
                    idLabel: {
                        text: `id: ${id}`,
                        annotations: [
                            { start: 4, end: 10, attrs: { fill: '#f6f740' }},
                        ],
                    },
                },
            });
        },
    }
);

const ExternalEvent = Event.define(
    'fta.ExternalEvent',
    {
        size: {
            width: 80,
            height: 100,
        },
        attrs: {
            root: {
                title: 'External Event',
            },
            body: {
                d: 'M 0 20 calc(w / 2) 0 calc(w) 20 calc(w) calc(h) 0 calc(h) Z',
            },
        },
    },
    {
        markup: util.svg`
            <path @selector="body" />
            <text @selector="label" />
        `,
    }
);

const UndevelopedEvent = Event.define(
    'fta.UndevelopedEvent',
    {
        size: {
            width: 140,
            height: 80,
        },
        attrs: {
            root: {
                title: 'Undeveloped Event',
            },
            body: {
                d: 'M 0 calc(h / 2) calc(w / 2) calc(h) calc(w) calc(h / 2) calc(w / 2) 0 Z',
            },
        },
    },
    {
        markup: util.svg`
            <path @selector="body" />
            <text @selector="label" />
        `,
    }
);

const BasicEvent = Event.define(
    'fta.BasicEvent',
    {
        size: {
            width: 80,
            height: 80,
        },
        z: 3,
        attrs: {
            root: {
                title: 'Basic Event',
            },
            body: {
                cx: 'calc(w / 2)',
                cy: 'calc(h / 2)',
                r: 'calc(w / 2)',
            },
        },
    },
    {
        markup: util.svg`
            <circle @selector="body" />
            <text @selector="label" />
        `,
    }
);

const ConditioningEvent = Event.define(
    'fta.ConditioningEvent',
    {
        size: {
            width: 140,
            height: 80,
        },
        z: 2,
        attrs: {
            root: {
                title: 'Conditioning Event',
            },
            body: {
                cx: 'calc(w / 2)',
                cy: 'calc(h / 2)',
                rx: 'calc(w / 2)',
                ry: 'calc(h / 2)',
            },
        },
    },
    {
        markup: util.svg`
            <ellipse @selector="body" />
            <text @selector="label" />
        `,
    }
);

const Link = dia.Link.define(
    'fta.Link',
    {
        attrs: {
            line: {
                connection: true,
                stroke: '#ed2637',
                strokeWidth: 2,
                strokeLinejoin: 'round',
            },
        },
    },
    {
        markup: util.svg`
            <path @selector="line" fill="none" pointer-events="none" />
        `,
    },
    {
        create: function(event1, event2) {
            const source = {
                id: event1.id,
            };
            if (event1.get('type') === 'fta.IntermediateEvent') {
                source.selector = 'gate';
            } else {
                source.selector = 'body';
            }
            if (event2.get('type') === 'fta.ConditioningEvent') {
                source.anchor = { name: 'perpendicular' };
            }
            return new this({
                z: 1,
                source,
                target: {
                    id: event2.id,
                    selector: 'body',
                },
            });
        },
    }
);

const shapes = {
    ...defaultShapes,
    fta: {
        Event,
        ExternalEvent,
        UndevelopedEvent,
        BasicEvent,
        ConditioningEvent,
        Link,
    },
};

// Custom element tools for collapsing and expanding elements.
const ExpandButton = elementTools.Button.extend({
    options: {
        x: 'calc(w / 2 - 35)',
        y: 'calc(h - 15)',
        action: (evt, view, tool) => {
            view.paper.trigger('element:expand', view, evt);
        },
    },
    children() {
        return util.svg`
                <rect @selector="button" fill="#cad8e3" x="-8" y="-8" width="16" height="16" cursor="pointer" />
                <path @selector="icon" fill="none" stroke="#131e29" stroke-width="2" pointer-events="none" />
            `;
    },
    update() {
        elementTools.Button.prototype.update.call(this, arguments);
        this.childNodes.icon.setAttribute('d', this.getIconPath());
    },
    getIconPath() {
        if (this.relatedView.model.get('collapsed')) {
            return 'M -4 0 4 0 M 0 -4 0 4';
        } else {
            return 'M -4 0 4 0';
        }
    },
});

// Custom highlighter that renders a bevelled frame around the highlighted element
const BevelledFrame = dia.HighlighterView.extend({
    tagName: 'path',
    attributes: {
        stroke: '#f6f740',
        'stroke-width': 2,
        fill: 'none',
        'pointer-events': 'none',
    },
    // Method called to highlight a CellView
    highlight({ model }) {
        const { padding = 0, bevel = 10 } = this.options;
        const bbox = model.getBBox();
        // Highlighter is always rendered relatively to the CellView origin
        bbox.x = bbox.y = 0;
        // Increase the size of the highlighter
        bbox.inflate(padding);
        const { x, y, width, height } = bbox;
        this.vel.attr(
            'd',
            `
                M ${x} ${y + bevel}
                L ${x} ${y + height - bevel}
                L ${x + bevel} ${y + height}
                L ${x + width - bevel} ${y + height}
                L ${x + width} ${y + height - bevel}
                L ${x + width} ${y + bevel}
                L ${x + width - bevel} ${y}
                L ${x + bevel} ${y}
                Z
            `
        );
    },
});

const graph = new dia.Graph({}, { cellNamespace: shapes });

const paper = new dia.Paper({
    width: '100%',
    height: '100%',
    model: graph,
    defaultConnectionPoint: { name: 'boundary', args: { offset: 5 }},
    defaultConnector: {
        name: 'straight',
        args: { cornerType: 'line', cornerRadius: 10 },
    },
    defaultRouter: { name: 'orthogonal' },
    async: true,
    interactive: false,
    frozen: true,
    sorting: dia.Paper.sorting.APPROX,
    cellViewNamespace: shapes,
    background: { color: '#131e29' },
    viewport: function(view) {
        const { model } = view;
        if (!view) return true;
        return !model.get('hidden');
    },
});

document.getElementById('paper-container').appendChild(paper.el);

paper.on({
    'element:mouseenter': (elementView) => {
        BevelledFrame.add(elementView, 'root', 'frame', { padding: 10 });
    },
    'element:mouseleave': (elementView) => {
        BevelledFrame.remove(elementView, 'frame');
    },
    'element:gate:click': (elementView) => {
        const element = elementView.model;
        const gateType = element.gate();
        const gateTypes = Object.keys(element.gateTypes);
        const index = gateTypes.indexOf(gateType);
        const newIndex = (index + 1) % gateTypes.length;
        element.gate(gateTypes[newIndex]);
    },
    'element:expand': (elementView) => {
        const element = elementView.model;
        const successorElements = graph.getSuccessors(element);
        const [successor] = successorElements;
        const shouldExpand = !successor.get('hidden');
        const successorCells = graph.getSubgraph([
            element,
            ...successorElements,
        ]);
        successorCells.forEach((cell) => {
            if (cell === element) {
                cell.set({
                    hidden: false,
                    collapsed: shouldExpand,
                });
            } else {
                cell.set({ hidden: shouldExpand });
                if (cell.isElement()) {
                    cell.set({ collapsed: false });
                }
            }
        });
        runLayout(graph);
    },
});

// Original FTA Diagram: https://www.edrawsoft.com/templates/pdf/scaffolding-fall-fault-tree.pdf

const events = [
    IntermediateEvent.create('Fall from Scaffolding').gate('inhibit'),
    IntermediateEvent.create('Fall from the Scaffolding', 'and').gate('and'),
    IntermediateEvent.create('Safety Belt Not Working', 'or').gate('or'),
    IntermediateEvent.create('Fall By Accident', 'or').gate('or'),
    IntermediateEvent.create('Broken By Equipment', 'or').gate('or'),
    IntermediateEvent.create('Did not Wear Safety Belt', 'or').gate('or'),
    UndevelopedEvent.create('Slip and Fall'),
    UndevelopedEvent.create('Lose Balance'),
    UndevelopedEvent.create('Upholder Broken'),
    BasicEvent.create('Safety Belt Broken'),
    BasicEvent.create('Forgot to Wear'),
    ExternalEvent.create('Take off When Walking'),
    ConditioningEvent.create('Height and Ground Condition'),
];

const links = [
    Link.create(events[0], events[1]),
    Link.create(events[1], events[2]),
    Link.create(events[1], events[3]),
    Link.create(events[2], events[4]),
    Link.create(events[2], events[5]),
    Link.create(events[3], events[6]),
    Link.create(events[3], events[7]),
    Link.create(events[4], events[8]),
    Link.create(events[4], events[9]),
    Link.create(events[5], events[10]),
    Link.create(events[5], events[11]),
    Link.create(events[0], events[12]),
];

graph.resetCells(events.concat(links));

runLayout(graph);
addTools(paper, events);

paper.transformToFitContent({
    padding: 15,
    contentArea: graph.getBBox(),
    verticalAlign: 'middle',
    horizontalAlign: 'middle',
});

paper.unfreeze();

// Functions

function runLayout(graph) {
    const autoLayoutElements = [];
    const manualLayoutElements = [];
    graph.getElements().forEach((el) => {
        if (el.get('hidden')) return;
        if (el.get('type') === 'fta.ConditioningEvent') {
            manualLayoutElements.push(el);
        } else {
            autoLayoutElements.push(el);
        }
    });
    // Automatic Layout
    layout.DirectedGraph.layout(graph.getSubgraph(autoLayoutElements), {
        rankDir: 'TB',
        setVertices: true,
    });
    // Manual Layout
    manualLayoutElements.forEach((el) => {
        const [neighbor] = graph.getNeighbors(el, { inbound: true });
        if (!neighbor) return;
        const neighborPosition = neighbor.getBBox().bottomRight();
        el.position(
            neighborPosition.x + 20,
            neighborPosition.y - el.size().height / 2 - 15
        );
    });
    // Make sure the root element of the graph is always at the same position after the layout.
    const rootCenter = { x: 500, y: 100 };
    const [source] = graph.getSources();
    const { width, height } = source.size();
    const diff = source
        .position()
        .difference({
            x: rootCenter.x - width / 2,
            y: rootCenter.y - height / 2,
        });
    graph.translate(-diff.x, -diff.y);
}

function addTools(paper, elements) {
    const toolName = 'expand-tools';
    elements.forEach(function(element) {
        if (element.get('type') !== 'fta.IntermediateEvent') return;
        const view = element.findView(paper);
        if (view.hasTools(toolName)) return;
        const toolsView = new dia.ToolsView({
            name: toolName,
            tools: [new ExpandButton()],
        });
        view.addTools(toolsView);
    });
}

              
            
!
999px

Console