【问题标题】:JointJs: Define a custom shape with Node.JsJointJs:使用 Node.Js 定义自定义形状
【发布时间】:2021-06-14 12:13:38
【问题描述】:

我有点困惑,在我的应用程序中,使用 JointJs 和 NodeJs 组合来定义自定义形状似乎不起作用。我正在尝试以下代码:

const joint = require('jointjs')

const rectWidth = 130
const rectHeight = 65

joint.shapes.standard.Rectangle.define('examples.CustomRectangle', {
  attrs: {
    body: {
      refWidth: '100%',
      refHeight: '100%',
      strokeWidth: 5,
      stroke: '#000000',
      strokeDasharray: rectWidth + ',' + rectHeight,
      fill: 'white'
    },
    label: {
      textVerticalAnchor: 'middle',
      textAnchor: 'middle',
      refX: '50%',
      refY: '50%',
      fontSize: 14,
      fill: '#333333'
    }
  }
})

console.log(joint)

export class ElementFactory {
  createDatastore (x, y) {
    return new joint.shapes.examples.CustomRectangle({
      position: { x: x - rectWidth / 2, y: y - rectHeight / 2 },
      size: { width: rectWidth, height: rectHeight },
      attrs: {
        text: { textWrap: { width: rectWidth * 0.95, height: 20, ellipsis: true, text: 'Datastore' } }
      }
    })
  }
}

现在我的理解是,如果我在脚本开头需要 JointJs 模块,并在其下定义一个自定义形状,它将出现在模块中,如果以后需要(在工厂中)我仍然可以访问它在同一个变量上。但相反,我收到以下错误:

未捕获的类型错误:无法读取属性“CustomRectangle”的 未定义

我也在定义自定义形状后立即记录模块,但它仍然没有显示。

我是否遗漏了有关 NodeJ 或 JointJ 的内容?有谁知道问题是什么?任何帮助将不胜感激。

【问题讨论】:

    标签: javascript node.js jointjs


    【解决方案1】:

    我认为问题在于您如何将其附加到 joint

    试试这个:

    joint.shapes.examples.CustomRectangle = joint.shapes.standard.Rectangle.define('examples.CustomRectangle', {
      attrs: {
        body: {
          refWidth: '100%',
          refHeight: '100%',
          strokeWidth: 5,
          stroke: '#000000',
          strokeDasharray: rectWidth + ',' + rectHeight,
          fill: 'white'
        },
        label: {
          textVerticalAnchor: 'middle',
          textAnchor: 'middle',
          refX: '50%',
          refY: '50%',
          fontSize: 14,
          fill: '#333333'
        }
      }
    });
    
    

    尽管文档 (https://resources.jointjs.com/tutorial/custom-elements) 强烈暗示,define() 并未将其附加到关节。

    【讨论】:

    • 非常感谢,这样可以完美运行。对于将来遇到此问题的人,我唯一要添加的是,您需要事先添加“joint.shapes.examples = {}”之类的内容,以便命名空间可用。
    猜你喜欢
    • 2014-02-11
    • 2021-09-01
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 2021-10-04
    • 2017-03-05
    相关资源
    最近更新 更多