创建图形

创建图形 - 图1

标准创建

创建一个有背景色的矩形。

ts

  1. import { Leafer, Rect } from 'leafer-ui'
  2. const leafer = new Leafer({ view: window })
  3. const rect = new Rect({
  4. x: 100,
  5. y: 100,
  6. width: 200,
  7. height: 200,
  8. fill: '#32cd79' // 背景色
  9. })
  10. leafer.add(rect)

简洁创建

ts

  1. import { Leafer, Rect } from 'leafer-ui'
  2. const leafer = new Leafer({ view: window })
  3. // one( style, x?, y?, width?, height?)
  4. const rect = Rect.one({ fill: '#32cd79' }, 100, 100, 200, 200)
  5. leafer.add(rect)

使用 tag

ts

  1. import { Leafer, UI } from 'leafer-ui'
  2. const leafer = new Leafer({ view: window })
  3. const rect = UI.one({
  4. tag: 'Rect',
  5. x: 100,
  6. y: 100,
  7. width: 200,
  8. height: 200,
  9. fill: '#32cd79'
  10. })
  11. leafer.add(rect)

使用 JSON

ts

  1. import { Group, Leafer } from 'leafer-ui'
  2. const leafer = new Leafer({ view: window })
  3. const json = { "x": 20, "y": 20, "children": [{ "tag": "Rect", "x": 100, "y": 100, "width": 200, "height": 200, "fill": "#32cd79", "draggable": true }] }
  4. const group = new Group(json)
  5. leafer.add(group)

了解 JSON 数据 导入导出