【发布时间】:2020-08-21 10:07:10
【问题描述】:
我需要一个自定义文本框,您可以在不拉伸字体的情况下调整边界框的大小。
基于 fabric.Textbox 类创建了一个自定义结构类。有一些在自定义类中调用父类方法并覆盖它们的经验。
fabric['CustomText'] = fabric.util.createClass(fabric.Textbox, {
type: 'custom-text',
initialize: function(element, options) {
this.callSuper('initialize', element, options)
options && this.set('id', options.id) && this.set('clipTo', options.clipTo)
},
insertChars: function(chars) {
if (this.maxWidth) {
const textWidthUnderCursor = this._getLineWidth(this.ctx, this.get2DCursorLocation().lineIndex)
if (textWidthUnderCursor + this.ctx.measureText(chars).width > this.maxWidth) {
chars = '\n' + chars
}
}
if (this.maxLines) {
const newLinesLength = this._wrapText(this.ctx, this.text + chars).length
if (newLinesLength > this.maxLines) {
return
}
}
// Call parent class method
this.callSuper('insertChars', chars)
},
toObject: function() {
return fabric.util.object.extend(this.callSuper('toObject'), { id: this.id, clipTo: this.clipTo })
},
})
我需要能够
- 在不改变字体比例的情况下拉伸 TextBox 的边界框。
- 能够在文本框中的特定字符索引处拖放文本。
【问题讨论】:
-
这里有一个例子:fabricjs.com/kitchensink。转到“简单”选项卡,您有“添加文本框”
-
谢谢你,但试着把它拉下来。它拉伸字体。另外——如果你对角拉伸,字体会拉伸。固定字体大小似乎不起作用。也许超级边界框类?
标签: fabricjs