gpt4 book ai didi

javascript - 我如何在 Fabric.js 版本中获取 itext 样式2?

转载 作者:行者123 更新时间:2023-12-03 02:01:33 34 4
gpt4 key购买 nike

在以前版本的fabric.js 中,我按照在getStyle 函数中使用的方式使用getSelectionStyles,并取得了巨大成功。

在这种情况下:当我选择一些文本时,fontsize-slider 将更改为第一个选定字符的当前值。

但是升级到版本 2+ 后它就不再起作用了。 getStyle 函数返回undefined

canvas.on('text:selection:changed', onSelectionChanged);

function onSelectionChanged() {
var obj = canvas.getActiveObject();
if (obj.selectionStart>-1) {
$('#fontSize').val(getStyle(obj,'fontSize'));
}
}

function getStyle(object, styleName) {
return (object.getSelectionStyles && object.isEditing)
? object.getSelectionStyles()[styleName] : object[styleName];
}

这是一个使用 Fabric.js 版本 1.7.22 的工作示例。如果将其更改为当前版本,则会失败。如何在版本 2 中使用 getSelectionStyles?发生了什么变化?

var canvas = new fabric.Canvas('c');
canvas.add(new fabric.IText('Fontsize test'))
canvas.on('text:selection:changed', onSelectionChanged);

$( "#fontSize" ).on("input", function() {
var obj = canvas.getActiveObject();
if (obj) {
setStyle(obj, 'fontSize', $( "#fontSize" ).val());
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 onSelectionChanged() {
var obj = canvas.getActiveObject();
if (obj.selectionStart>-1) {
$('#fontSize').val(getStyle(obj,'fontSize'));
}
}

function getStyle(object, styleName) {
return (object.getSelectionStyles && object.isEditing)
? object.getSelectionStyles()[styleName] : object[styleName];
}
canvas{
border:2px solid #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.min.js"></script>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.2.3/fabric.min.js"></script>-->
<canvas id='c' width=400 height=120></canvas>
<label for="fontSize" hint="Font size"><a style="font-size:12px;color:#999">T</a><a style="font-size:19px;color:#ccc">T</a></label>
<input type="range" value="" min="1" max="120" step="1" id="fontSize">

最佳答案

getSelectionStyles返回当前选择的样式对象数组。

因此,object.getSelectionStyles()[styleName] 这将始终是未定义

演示

var canvas = new fabric.Canvas('c');
canvas.add(new fabric.IText('Hello'))
canvas.on('text:selection:changed', onSelectionChanged);

function onSelectionChanged() {
var obj = canvas.getActiveObject();
if (obj.selectionStart>-1) {
console.log(getStyle(obj,'fontSize'));
}
}

function getStyle(object, styleName) {
console.log(object.getSelectionStyles())
return object[styleName];
}
canvas{
border:2px solid #000;
}
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<canvas id='c' width=400 height=400></canvas>

您需要使用getStyleAtPosition(position)来获取该特定位置的样式。

var canvas = new fabric.Canvas('c');
canvas.add(new fabric.IText('Fontsize test'))
canvas.on('text:selection:changed', onSelectionChanged);

$("#fontSize").on("input", function() {
var obj = canvas.getActiveObject();
if (obj) {
setStyle(obj, 'fontSize', $("#fontSize").val());
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 onSelectionChanged() {
var obj = canvas.getActiveObject();
if (obj.selectionStart > -1) {
console.log(obj.getSelectionStyles())
$('#fontSize').val(getStyle(obj, 'fontSize'));
}
}

function getStyle(object, styleName) {
return (object.getStyleAtPosition && object.isEditing) ?
object.getStyleAtPosition(object.selectionStart)[styleName] : object[styleName];
}
canvas{
border:2px solid #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.2.3/fabric.min.js"></script>-->
<canvas id='c' width=400 height=120></canvas>
<label for="fontSize" hint="Font size"><a style="font-size:12px;color:#999">T</a><a style="font-size:19px;color:#ccc">T</a></label>
<input type="range" value="" min="1" max="120" step="1" id="fontSize">

关于javascript - 我如何在 Fabric.js 版本中获取 itext 样式2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49995602/

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