Using predefined SVG file for creating a custom JointJS shape with ports

百般思念 提交于 2019-12-02 07:37:08

You can add the SVGImageElement to your markup to display arbitrary SVG in your shapes. Just convert the SVG file content to dataURL and set the xlink:href attribute.

var shape = new joint.shapes.basic.Image({
  // markup: '<g class="rotatable"><g class="scalable"><image/></g><text/></g>',      
  attrs: {
    image: {
      'xlink:href': 'data:image/svg+xml;utf8,' + encodeURLComponent(svgFileContent)
    } 
  }
});

http://jsfiddle.net/kumilingus/eqen3pdf/

In order to create a shape showing an SVG image and yet having ports you can e.g. use devs.Model shape and replace the only SVGRectElement in its markup with an SVGImageElement.

new joint.shapes.devs.Model({
  markup: '<g class="rotatable"><g class="scalable"><image class="body"/></g><text class="label"/><g class="inPorts"/><g class="outPorts"/></g>',
  attrs: {
  '.body': {
    'xlink:href': 'data:image/svg+xml;utf8,' + encodeURLComponent(svgFileContent)
  },
  inPorts: ['in'],
  outPorts: ['out']
});

http://jsfiddle.net/kumilingus/tm2ctvxq/

Note, that it's possible to insert the SVG file content directly into your markup (without <?xml version="1.0" standalone="no"?>). I would not recommended it for more complex SVG though.

For instance when SVG contains an SVGClipPathElement with an id. Creating 2 instances of your shape breaks condition that all the IDs in the SVG must be unique.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!