gpt4 book ai didi

javascript - 仅当我以另一种方式更改对象时,下划线才会起作用

转载 作者:行者123 更新时间:2023-12-03 02:52:17 24 4
gpt4 key购买 nike

我使用以下代码来操作fabric.js 中的IText 对象。它们都可以工作,但是直到我选择了要应用于对象的另一种样式之后,下划线才会显示自己。

换句话说,我会

  1. 为元素添加下划线(但不显示下划线)
  2. 例如,选择“粗体”,则会显示下划线以及粗体

这是我的代码:

// Text Styles
function addHandler(id, fn, eventName) {
document.getElementById(id)[eventName || 'onclick'] = function() {
var el = this;
if (obj = canvas.getActiveObject()) {
fn.call(el, obj);
canvas.renderAll();
}
};
}
function setStyle(object, styleName, value) {
if (object.setSelectionStyles && object.isEditing) {
var style = { };
style[styleName] = value;
object.setSelectionStyles(style);
}
else {
object[styleName] = value;
}
}
function getStyle(object, styleName) {
return (object.getSelectionStyles && object.isEditing)
? object.getSelectionStyles()[styleName]
: object[styleName];
}
// bold
addHandler('bold', function(obj) {
var isBold = (getStyle(obj, 'fontWeight') || '').indexOf('bold') > -1;
setStyle(obj, 'fontWeight', isBold ? '' : 'bold');
});
// italic
addHandler('italic', function(obj) {
var isItalic = (getStyle(obj, 'fontStyle') || '').indexOf('italic') > -1;
setStyle(obj, 'fontStyle', isItalic ? '' : 'italic');
});
// underline
addHandler('underline', function(obj) {
var isUnderline = (getStyle(obj, 'textDecoration') || '').indexOf('underline') > -1;
setStyle(obj, 'textDecoration', isUnderline ? '' : 'underline');
});

我用来切换样式的按钮是:

<a class="dropdown-item" id="bold" style="font-weight: bold;">Bold</a>
<a class="dropdown-item" id="italic" style="text-decoration: italic;"><em>Italic</em></a>
<a class="dropdown-item" id="underline" style="text-decoration: underline;">Underline</a>

最佳答案

在下划线处理程序中设置obj.dirty = true;

演示

// Text Styles
function addHandler(id, fn, eventName) {
document.getElementById(id)[eventName || 'onclick'] = function() {
var el = this;
if (obj = canvas.getActiveObject()) {
fn.call(el, obj);
canvas.renderAll();
}
};
}

function setStyle(object, styleName, value) {
if (object.setSelectionStyles && object.isEditing) {
var style = {};
style[styleName] = value;
object.setSelectionStyles(style);
} else {
object[styleName] = value;
}
}

function getStyle(object, styleName) {
return (object.getSelectionStyles && object.isEditing) ?
object.getSelectionStyles()[styleName] :
object[styleName];
}
// bold
addHandler('bold', function(obj) {
var isBold = (getStyle(obj, 'fontWeight') || '').indexOf('bold') > -1;
setStyle(obj, 'fontWeight', isBold ? '' : 'bold');
});
// italic
addHandler('italic', function(obj) {
var isItalic = (getStyle(obj, 'fontStyle') || '').indexOf('italic') > -1;
setStyle(obj, 'fontStyle', isItalic ? '' : 'italic');
});
// underline
addHandler('underline', function(obj) {
var isUnderline = (getStyle(obj, 'textDecoration') || '').indexOf('underline') > -1;
setStyle(obj, 'textDecoration', isUnderline ? '' : 'underline');
obj.dirty = true;
});

var canvas = this.__canvas = new fabric.Canvas('c');
var text = new fabric.IText('Sample Text', {
left: 50,
top: 100,
fontFamily: 'arial',
fill: '#333',
fontSize: 50
});
canvas.add(text);
canvas.setActiveObject(text);
canvas{
border-width: 1pz;
border-style: solid;
border-color: #000;
}
<a class="dropdown-item" id="bold" style="font-weight: bold;">Bold</a>
<a class="dropdown-item" id="italic" style="text-decoration: italic;"><em>Italic</em></a>
<a class="dropdown-item" id="underline" style="text-decoration: underline;">Underline</a>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.20/fabric.js"></script>
<div id="canvases">
<canvas id="c" width="600" height="600"></canvas>
</div>

关于javascript - 仅当我以另一种方式更改对象时,下划线才会起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47795183/

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