fabric Textbox 给每个文字设置颜色

const iText = new fabric.Textbox ('hello\nworld', {
  styles: { // 设置样式
    0: { // 第1行
      1: { // 该行索引是1的字符
        fill: '#f00' // 文字颜色
      }
    },
    1: { // 第2行
      2: { // 该行索引是2的字符
        fill: 'hotpink'
      }
    }
  }
})

动态设置:

const styles = {
    "0": { // 第1行
        "0": { // 该行索引是0的字符
            "fill": "transparent"
        },
        "1": {
            "fill": "transparent"
        },
        "2": {
            "fill": "transparent"
        },
        "3": {
            "fill": "transparent"
        },
        "4": {
            "fill": "transparent"
        },
        "5": {
            "fill": "transparent"
        },
        "6": {
            "fill": "transparent"
        },
        "7": {
            "fill": "transparent"
        },
        "8": {
            "fill": "transparent"
        },
        "9": {
            "fill": "transparent"
        },
        "10": {
            "fill": "transparent"
        },
        "11": {
            "fill": "transparent"
        },
        "12": {
            "fill": "transparent"
        }
    }
}

element.set({
    styles: styles
})
canvasFab.renderAll();