Giter VIP home page Giter VIP logo

bpmn-js-example-custom-rendering's Introduction

This example is part of our ๐Ÿ““ custom elements guide. Checkout the final result here.

bpmn-js Example: Custom Rendering

An example of creating custom rendering for bpmn-js. Custom rendering allows you to render any shape or connection the way you want.

About

This example renders bpmn:Task and bpmn:Event elements differently.

Screenshot

Creating a Custom Renderer

In order to render bpmn:Task and bpmn:Event elements differently we'll create a custom renderer. By extending BaseRenderer we make sure our renderer will be called whenever a shape or connection is to be rendered. Note that we also specify a priority higher than the default priority of 1000 so our renderer will be called first.

const HIGH_PRIORITY = 1500;

export default class CustomRenderer extends BaseRenderer {
  constructor(eventBus) {
    super(eventBus, HIGH_PRIORITY);

    ...
  }
  ...
}

Whenever our renderer is called we need to decide whether we want to render an element or if the default renderer should render it. We're only interested in rendering bpmn:Task and bpmn:Event elements:

canRender(element) {

  // only render tasks and events (ignore labels)
  return isAny(element, [ 'bpmn:Task', 'bpmn:Event' ]) && !element.labelTarget;
}

Once we've decided to render an element depending on the element's type our renderers drawShape or drawConnection will be called. Since we only render shapes, we don't need to implement drawConnection. We don't want to render tasks and events entirely different, so we'll let the default renderer do the heavy lifting of rendering the shape and then change it afterward:

drawShape(parentNode, element) {
    const shape = this.bpmnRenderer.drawShape(parentNode, element);

    if (is(element, 'bpmn:Task')) {
      const rect = drawRect(parentNode, 100, 80, TASK_BORDER_RADIUS, '#52B415');

      prependTo(rect, parentNode);

      svgRemove(shape);

      return shape;
    }

    const rect = drawRect(parentNode, 30, 20, TASK_BORDER_RADIUS, '#cc0000');

    svgAttr(rect, {
      transform: 'translate(-20, -10)'
    });

    return shape;
  }

If the element is a bpmn:Task we render the task first and then replace its rectangle with our own rectangle. Therefore, we don't have to render labels and markers ourselves.

In the case of bpmn:Event elements we let the default renderer render the element first before we render an additional rectangle on top of it.

You can also decide to take care of the rendering entirely on your own without using the default renderer at all.

Finally, since we are rendering shapes we need to implement a getShapePath method which will be called whenever a connection is to be cropped:

getShapePath(shape) {
  if (is(shape, 'bpmn:Task')) {
    return getRoundRectPath(shape, TASK_BORDER_RADIUS);
  }

  return this.bpmnRenderer.getShapePath(shape);
}

See the entire renderer here.

Next, let's add our custom renderer to bpmn-js.

Adding the Custom Renderer to bpmn-js

When creating a new instance of bpmn-js we need to add our custom renderer using the additionalModules property:

import BpmnModeler from 'bpmn-js/lib/Modeler';

import customRendererModule from './custom';

const bpmnModeler = new BpmnModeler({
  additionalModules: [
    customRendererModule
  ]
});

Our custom renderer will now render all task and event shapes.

Run the Example

You need a NodeJS development stack with npm installed to build the project.

To install all project dependencies execute

npm install

To start the example execute

npm start

To build the example into the public folder execute

npm run all

License

MIT

bpmn-js-example-custom-rendering's People

Contributors

nikku avatar philippfromme avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.