gpt4 book ai didi

fabricjs - 未选择时在 Fabric 文本框上绘制边框

转载 作者:行者123 更新时间:2023-12-02 10:13:31 29 4
gpt4 key购买 nike

In this jsfiddle我有一个 Fabric(版本 1.x)文本框,在选择它时有红色边框,在文本可编辑时有蓝色边框。我需要的是未选择文本框时的边框。如何实现这一目标?

HTML

<canvas id="c" width="300" height="300"></canvas>

Javascript

var canvas = new fabric.Canvas('c');

var text = new fabric.Textbox("Some Text", {
left: 50,
top: 50,
width: 100,
fontSize: 12,
fontFamily: 'Arial',
backgroundColor: 'yellow',
borderColor: 'red',
editingBorderColor: 'blue',
padding: 2
});

canvas.add(text);

最佳答案

您可以重写文本框对象的渲染方法。并为文本对象绘制边框。

演示

var canvas = new fabric.Canvas('c');

var originalRender = fabric.Textbox.prototype._render;
fabric.Textbox.prototype._render = function(ctx) {
originalRender.call(this, ctx);
//Don't draw border if it is active(selected/ editing mode)
if (this.active) return;
if(this.showTextBoxBorder){
var w = this.width,
h = this.height,
x = -this.width / 2,
y = -this.height / 2;
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();
var stroke = ctx.strokeStyle;
ctx.strokeStyle = this.textboxBorderColor;
ctx.stroke();
ctx.strokeStyle = stroke;
}
}
fabric.Textbox.prototype.cacheProperties = fabric.Textbox.prototype.cacheProperties.concat('active');

var text = new fabric.Textbox("Some Text\n some more text", {
left: 50,
top: 50,
width: 100,
fontSize: 12,
fontFamily: 'Arial',
backgroundColor: 'yellow',
borderColor: 'red',
editingBorderColor: 'blue',
padding: 2,
showTextBoxBorder: true,
textboxBorderColor: 'green'
});
canvas.add(text);
canvas{
border : 2px solid #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.7/fabric.js"></script>
<canvas id="c" width="300" height="300"></canvas>

关于fabricjs - 未选择时在 Fabric 文本框上绘制边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51233082/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com