如何为 IText Fabricjs 中的每个项目设置绝对位置
How to set absolute position for each item in IText Fabricjs
请帮助我,如何在 IText Fabricjs 中设置每个文本项的绝对位置。在此示例中,我希望文本 "KÍNH MỜI" 与下方文本水平居中对齐。
预期结果:
实际结果:
这是我的代码:
var canvasObject = document.getElementById("editorCanvas");
// set canvas equal size with div
$(canvasObject).width($("#canvasContainer").width());
$(canvasObject).height($("#canvasContainer").height());
var canvas = new fabric.Canvas('editorCanvas', {
backgroundColor: 'white',
selectionLineWidth: 2,
width: $("#canvasContainer").width(),
height: $("#canvasContainer").height()
});
var textbox1 = new fabric.IText("KÍNH MỜI\n \n.......................................................................................", {
left: 162,
top: 50,
width: 216.125889,
fontSize: 11.3735,
fontFamily: 'Times New Roman',
fontWeight: 'normal',
fill: "#404041",
editable: false,
styles: {
0: {
0: { fontFamily: 'Times New Roman', fontSize: '11.3735', fill: '#404041' }
},
1: {
0: { fontFamily: 'Times New Roman', fontSize: '11.3735', fill: '#404041' }
},
2: {
0: { fontFamily: 'Times New Roman', fontSize: '7.4898', fill: '#404041' }
}
}
});
canvas.add(textbox1);
canvas.renderAll();
#canvasContainer {
width: 100%;
height: 100vh;
background-color: gray;
}
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
<div id="canvasContainer">
<canvas id="editorCanvas"></canvas>
</div>
你需要textAlign: center
。我还将 lineHeight
更改为 1(默认值为 1.16)以使其看起来更像您的预期结果。
var canvasObject = document.getElementById("editorCanvas");
// set canvas equal size with div
$(canvasObject).width($("#canvasContainer").width());
$(canvasObject).height($("#canvasContainer").height());
var canvas = new fabric.Canvas('editorCanvas', {
backgroundColor: 'white',
selectionLineWidth: 2,
width: $("#canvasContainer").width(),
height: $("#canvasContainer").height()
});
var textbox1 = new fabric.IText("KÍNH MỜI\n \n.......................................................................................", {
left: 162,
top: 50,
width: 216.125889,
fontSize: 11.3735,
fontFamily: 'Times New Roman',
fontWeight: 'normal',
textAlign: 'center',
lineHeight: 1,
fill: "#404041",
editable: false,
styles: {
0: {
0: { fontFamily: 'Times New Roman', fontSize: '11.3735', fill: '#404041' }
},
1: {
0: { fontFamily: 'Times New Roman', fontSize: '11.3735', fill: '#404041' }
},
2: {
0: { fontFamily: 'Times New Roman', fontSize: '7.4898', fill: '#404041' }
}
}
});
canvas.add(textbox1);
canvas.renderAll();
#canvasContainer {
width: 100%;
height: 100vh;
background-color: gray;
}
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
<div id="canvasContainer">
<canvas id="editorCanvas"></canvas>
</div>
请帮助我,如何在 IText Fabricjs 中设置每个文本项的绝对位置。在此示例中,我希望文本 "KÍNH MỜI" 与下方文本水平居中对齐。
预期结果:
实际结果:
这是我的代码:
var canvasObject = document.getElementById("editorCanvas");
// set canvas equal size with div
$(canvasObject).width($("#canvasContainer").width());
$(canvasObject).height($("#canvasContainer").height());
var canvas = new fabric.Canvas('editorCanvas', {
backgroundColor: 'white',
selectionLineWidth: 2,
width: $("#canvasContainer").width(),
height: $("#canvasContainer").height()
});
var textbox1 = new fabric.IText("KÍNH MỜI\n \n.......................................................................................", {
left: 162,
top: 50,
width: 216.125889,
fontSize: 11.3735,
fontFamily: 'Times New Roman',
fontWeight: 'normal',
fill: "#404041",
editable: false,
styles: {
0: {
0: { fontFamily: 'Times New Roman', fontSize: '11.3735', fill: '#404041' }
},
1: {
0: { fontFamily: 'Times New Roman', fontSize: '11.3735', fill: '#404041' }
},
2: {
0: { fontFamily: 'Times New Roman', fontSize: '7.4898', fill: '#404041' }
}
}
});
canvas.add(textbox1);
canvas.renderAll();
#canvasContainer {
width: 100%;
height: 100vh;
background-color: gray;
}
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
<div id="canvasContainer">
<canvas id="editorCanvas"></canvas>
</div>
你需要textAlign: center
。我还将 lineHeight
更改为 1(默认值为 1.16)以使其看起来更像您的预期结果。
var canvasObject = document.getElementById("editorCanvas");
// set canvas equal size with div
$(canvasObject).width($("#canvasContainer").width());
$(canvasObject).height($("#canvasContainer").height());
var canvas = new fabric.Canvas('editorCanvas', {
backgroundColor: 'white',
selectionLineWidth: 2,
width: $("#canvasContainer").width(),
height: $("#canvasContainer").height()
});
var textbox1 = new fabric.IText("KÍNH MỜI\n \n.......................................................................................", {
left: 162,
top: 50,
width: 216.125889,
fontSize: 11.3735,
fontFamily: 'Times New Roman',
fontWeight: 'normal',
textAlign: 'center',
lineHeight: 1,
fill: "#404041",
editable: false,
styles: {
0: {
0: { fontFamily: 'Times New Roman', fontSize: '11.3735', fill: '#404041' }
},
1: {
0: { fontFamily: 'Times New Roman', fontSize: '11.3735', fill: '#404041' }
},
2: {
0: { fontFamily: 'Times New Roman', fontSize: '7.4898', fill: '#404041' }
}
}
});
canvas.add(textbox1);
canvas.renderAll();
#canvasContainer {
width: 100%;
height: 100vh;
background-color: gray;
}
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
<div id="canvasContainer">
<canvas id="editorCanvas"></canvas>
</div>