# 文本边框

# 自定义边框

# show code

import { fabric } from 'fabric';

export const FabricText = fabric.util.createClass(fabric.Text, {
    type: 'border-text', // 添加一个 type 属性
    // 初始化
    initialize(text, options) {
        this.callSuper('initialize', text, options); // 继承
        return this;
    },
    _render(ctx) {
        fabric.Textbox.prototype._render.call(this, ctx);
        if (this.active) return;
        const padding = this.padding || 2;
        if (this.showTextBoxBorder) {
            const w = this.width + 2 * padding;
            const h = this.height + 2 * padding;
            const x = -this.width / 2 - padding;
            const y = -this.height / 2 - padding;
            ctx.beginPath();
            ctx.moveTo(x, y);
            ctx.lineTo(x + w, y);
            ctx.lineTo(x + w, y + h);
            ctx.lineTo(x, y + h);
            ctx.lineTo(x, y);
            ctx.closePath();
            const stroke = ctx.strokeStyle;
            ctx.strokeStyle = this.textboxBorderColor;
            ctx.stroke();
            ctx.strokeStyle = stroke;
        }
    },
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
最后更新时间: 12/8/2023, 3:40:46 PM