前端前端
fabric
算法
jsBlock
styleBlock
svgBlock
工具其他
Vue、React相关
webgl
GitHub
fabric
算法
jsBlock
styleBlock
svgBlock
工具其他
Vue、React相关
webgl
GitHub
  • 元素操作

    • 元素分组
    • 元素定位
    • 元素层级
    • 元素禁止事件
    • 元素选中样式
  • 变换

    • 变换
  • 图片

    • 图片
    • 背景图
  • 基础图形

    • 三角形
    • 圆形
    • 多边形
    • 折线
    • 椭圆
    • 直线
    • 矩形
    • 路径
  • 序列化和反序列化

    • 反序列化
    • 输出JSON
    • 输出png
    • 输出svg
  • 文本

    • 可编辑文本
    • 多行文本
    • 文本
    • 文本省略
    • 文本边框
  • 源码

    • fabric.Canvas
    • fabric.Circle
    • fabric类的继承体系
  • 画布操作

    • 画布拖动和缩放
    • 画布框选样式
  • 示例

    • fabric-echarts-demo
    • 拓扑图一
    • 拓扑图二

文本

创建

使用 new fabric.Text(string, object) 方法创建文本

单行文本、不换行、不可编辑

show code

const text = new fabric.Text('你好啊', {
  left: 50,
  top: 50,
  fill: '#f00',
  stroke: 'blue',
  strokeWidth: 2,
})
canvas.add(text)

文字居中旋转

居中旋转 centeredRotation 默认为true,旋转时以元素的中心点旋转

为false时会以left、top为中心点旋转

试试旋转下面的示例和第一个示例

show code

const text = new fabric.Text('你好啊', {
  left: 50,
  top: 50,
  fill: '#f00',
  stroke: 'blue',
  strokeWidth: 2,
  centeredRotation: false
})
canvas.add(text)

文字居中

以 left, top 为中心点, originX、originY 居中

比如 canvas宽300,高150,那么中心点是150、75

show code

const text = new fabric.Text('你好啊', {
  fill: '#f00',
  stroke: 'blue',
  strokeWidth: 2,
  left: 150,
  top: 75,
  originX: 'center',
  originY: 'center',
})
canvas.add(text)

基础样式

{
  top: 40,
  left: 40,
  fontSize: 120,
  fontWeight: 'bold',

  overline: true, // 上划线
  underline: true, // 下划线
  linethrough: true, // 删除线
  width: 200,
  lineHeight: 1,
  textAlign: 'center',

  backgroundColor: 'green', // 背景色:绿色
  fill: 'orange', // 填充色:橙色
  stroke: '#f6416c', // 边框颜色:粉色
  strokeWidth: 3, // 边框粗细:3px
  strokeDashArray: [20, 5, 14], // 边框虚线规则:填充20px 空5px 填充14px 空20px 填充5px ……
  shadow: '10px 20px 6px rgba(10, 20, 30, 0.4)', // 投影:向右偏移10px,向下偏移20px,羽化6px,投影颜色及透明度
  transparentCorners: false, // 选中时,角是被填充了。true 空心;false 实心
  borderColor: '#16f1fc', // 选中时,边框颜色:天蓝
  borderScaleFactor: 5, // 选中时,边的粗细:5px
  borderDashArray: [20, 5, 10, 7], // 选中时,虚线边的规则
  cornerColor: "#a1de93", // 选中时,角的颜色是 青色
  cornerStrokeColor: 'pink', // 选中时,角的边框的颜色是 粉色
  cornerStyle: 'circle', // 选中时,角的属性。默认rect 矩形;circle 圆形
  cornerSize: 20, // 选中时,角的大小为20
  cornerDashArray: [10, 2, 6], // 选中时,虚线角的规则
  selectionBackgroundColor: '#7f1300', // 选中时,选框的背景色:朱红
  padding: 40, // 选中时,选择框离元素的内边距:40px
  borderOpacityWhenMoving: 0.6, // 当对象活动和移动时,对象控制边界的不透明度  
}
Edit this page
最近更新:: 2025/4/24 10:47
Contributors: wu.hui
Prev
多行文本
Next
文本省略