设置样式

设置样式 - 图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. stroke: 'black',
  10. strokeWidth: 2
  11. })
  12. leafer.add(rect)

修改样式

元素只能检测到第一层级属性的变化,如:修改 rect.fill.url = url 是不会渲染更新的。

ts

  1. rect.stroke = 'blue'
  2. rect.strokeWidth = 4

简洁修改

ts

  1. rect.set({
  2. stroke: 'blue',
  3. strokeWidth: 4
  4. })

重置样式

ts

  1. rect.reset() // 完全重置
  2. rect.reset({ // 重置为新样式
  3. stroke: 'blue',
  4. strokeWidth: 4
  5. })

使用 JSON

ts

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