How to set absolute position for each item in IText Fabricjs - html5-canvas

Please help me, how to set absolute position of each text item in IText Fabricjs. In this example, I want text "KÍNH MỜI" is align horizontal center with below text.
Expected Result:
Actual Result:
Here is my code:
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>

You need textAlign: center. I also changed lineHeight to 1 (default is 1.16) to make it look more like your expected result.
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>

Related

Value does not update with clicking - Bootstrap Slider by Seiyria

when I click between the ticks, it doesn't change the "current value" output. How can I fix this?
Maybe someone is able to help me. I will attach a snippet below, so u can try and test.
Thank you very much!
var minSliderValue = $("#mySlider").data("slider-min");
var maxSliderValue = $("#mySlider").data("slider-max");
$('#mySlider').slider({ // if max 100, 150
ticks: [1, 2, 3, 5, 10, 25, 50, 100],
ticks_positions: [0, 10, 20, 35, 50, 65, 82.5, 100],
ticks_labels: ["1x", "2x", "3x", "5x", "10x", "25x", "50x", "100x"],
ticks_snap_bounds: 1, //0.5
ticks_tooltip: true,
step: 1,
max: 100, //JS script
value: 10,
formatter: function (value) {
return value + 'x';
},
});
$("#leverage").on("keyup", function () {
var val = Math.abs(parseInt(this.value, 10) || minSliderValue);
this.value = val > maxSliderValue ? maxSliderValue : val;
$('#mySlider').slider('setValue', val);
});
// Get Value Output //Bug
$("#mySlider").slider();
$("#mySlider").on("slide", function (slideEvt) {
$("#leverage").val(slideEvt.value);
});
body {
background-color: #0f121b!important;
color: #fff!important;
padding: 4rem 2rem;
}
#mySlider {
margin: 2rem;
width: 70%;
}
.slider-tick {
background-color: #ffffff !important;
background-image: none !important;
opacity: 1 !important;
height: 14px !important;
width: 14px !important;
margin: 0.2rem;
}
.slider-handle {
background-color: #ffffff !important;
background-image: none !important;
opacity: 1 !important;
}
.slider-track {
background-image: -webkit-gradient(linear, left top, right top, from(#129b6b), color-stop(#e5d02b), to(#b44b53))!important;
background-image: -o-linear-gradient(left, #129b6b, #e5d02b, #b44b53)!important;
background-image: linear-gradient(to right, #129b6b, #e5d02b, #b44b53)!important;
}
.slider-selection.tick-slider-selection {
background-color: transparent!important;
background-image: none!important;
}
.slider-track-high {
background-color: #2d343e!important;
background-image: none!important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/11.0.2/css/bootstrap-slider.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/11.0.2/bootstrap-slider.js"></script>
<div style="text-align:center">
<div>
<input id="mySlider" data-slider-id='mySliderSlider' type="text" data-slider-min="0" data-slider-max="100" data-slider-step="1" />
<input type="number" id="leverage" min="1" max="100" value="10"
step="1" size="10" inputmode=" decimal">x
<br />
</div>
Use "change" instead of "slide" in the code below, also change slideEvt.value to slideEvt.value.newValue
$("#mySlider").on("slide", function (slideEvt) {
$("#leverage").val(slideEvt.value.newValue);
});
var minSliderValue = $("#mySlider").data("slider-min");
var maxSliderValue = $("#mySlider").data("slider-max");
$('#mySlider').slider({ // if max 100, 150
ticks: [1, 2, 3, 5, 10, 25, 50, 100],
ticks_positions: [0, 10, 20, 35, 50, 65, 82.5, 100],
ticks_labels: ["1x", "2x", "3x", "5x", "10x", "25x", "50x", "100x"],
ticks_snap_bounds: 1, //0.5
ticks_tooltip: true,
step: 1,
max: 100, //JS script
value: 10,
formatter: function (value) {
return value + 'x';
},
});
$("#leverage").on("keyup", function () {
var val = Math.abs(parseInt(this.value, 10) || minSliderValue);
this.value = val > maxSliderValue ? maxSliderValue : val;
$('#mySlider').slider('setValue', val);
});
// Get Value Output //Bug
$("#mySlider").slider();
$("#mySlider").on("change", function (slideEvt) {
$("#leverage").val(slideEvt.value.newValue);
});
body {
background-color: #0f121b!important;
color: #fff!important;
padding: 4rem 2rem;
}
#mySlider {
margin: 2rem;
width: 70%;
}
.slider-tick {
background-color: #ffffff !important;
background-image: none !important;
opacity: 1 !important;
height: 14px !important;
width: 14px !important;
margin: 0.2rem;
}
.slider-handle {
background-color: #ffffff !important;
background-image: none !important;
opacity: 1 !important;
}
.slider-track {
background-image: -webkit-gradient(linear, left top, right top, from(#129b6b), color-stop(#e5d02b), to(#b44b53))!important;
background-image: -o-linear-gradient(left, #129b6b, #e5d02b, #b44b53)!important;
background-image: linear-gradient(to right, #129b6b, #e5d02b, #b44b53)!important;
}
.slider-selection.tick-slider-selection {
background-color: transparent!important;
background-image: none!important;
}
.slider-track-high {
background-color: #2d343e!important;
background-image: none!important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/11.0.2/css/bootstrap-slider.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/11.0.2/bootstrap-slider.js"></script>
<div style="text-align:center">
<div>
<input id="mySlider" data-slider-id='mySliderSlider' type="text" data-slider-min="0" data-slider-max="100" data-slider-step="1" />
<input type="number" id="leverage" min="1" max="100" value="10"
step="1" size="10" inputmode=" decimal">x
<br />
</div>

Center animation text

Hello there I am completely new in design and struggling since a few days to resolve this issue but may be I need some help with you guys. Here is the result of what I want to achieve:
https://tobiasahlin.com/moving-letters/#4
the titles must appears in the center of the row
and here is my result:
https://codepen.io/Amidamaro/pen/dyMZMYP
HTML:
<h1 class="ml4">
<span class="letters letters-1">Ready</span>
<span class="letters letters-2">Set</span>
<span class="letters letters-3">Go!</span>
</h1>
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/2.0.2/anime.min.js"></script>
CSS:
.ml4 {
width: 100%;
height: 100px;
position: relative;
display: flex;
}
.ml4 .letters {
position: absolute;
margin: auto;
left: 0;
top: 0.3em;
right: 0;
opacity: 0;
justify-content: center;
}
JS:
var ml4 = {};
ml4.opacityIn = [0, 1];
ml4.scaleIn = [0.2, 1];
ml4.scaleOut = 3;
ml4.durationIn = 800;
ml4.durationOut = 600;
ml4.delay = 500;
anime
.timeline({ loop: true })
.add({
targets: ".ml4 .letters-1",
opacity: ml4.opacityIn,
scale: ml4.scaleIn,
duration: ml4.durationIn
})
.add({
targets: ".ml4 .letters-1",
opacity: 0,
scale: ml4.scaleOut,
duration: ml4.durationOut,
easing: "easeInExpo",
delay: ml4.delay
})
.add({
targets: ".ml4 .letters-2",
opacity: ml4.opacityIn,
scale: ml4.scaleIn,
duration: ml4.durationIn
})
.add({
targets: ".ml4 .letters-2",
opacity: 0,
scale: ml4.scaleOut,
duration: ml4.durationOut,
easing: "easeInExpo",
delay: ml4.delay
})
.add({
targets: ".ml4 .letters-3",
opacity: ml4.opacityIn,
scale: ml4.scaleIn,
duration: ml4.durationIn
})
.add({
targets: ".ml4 .letters-3",
opacity: 0,
scale: ml4.scaleOut,
duration: ml4.durationOut,
easing: "easeInExpo",
delay: ml4.delay
})
.add({
targets: ".ml4",
opacity: 0,
duration: 500,
delay: 500
});
I tried to display: flex; the parent and from there on to set the children to the center but nothing changes. Will appreciate any help, thanks !
Yeah! I've managed to solve this.
CSS:
.ml4 {
width: 100%;
height: 100px;
position: relative;
display: flex;
overflow: hidden;
}
.ml4 .letters {
position: absolute;
margin: auto;
left: 0;
top: 0.3em;
right: 0;
opacity: 0;
text-align: center;
}

How to mapping "letter-spacing" value from text node of SVG to "charSpacing" property of fabric.TextBox in FabricJS?

I have problem with how to re-presentation of node in SVG with "letter-spacing" in SVG document to fabric.TextBox.
In file SVG text node is:
<text transform="matrix(1 0 0 1 51.5211 22.2889)" style="fill:#3C2415; font-size:11px; letter-spacing:3;">MINH TUẤN</text>
And my fabric.TextBox is:
var textbox_0_1 = new fabric.Textbox("MINH TUẤN", {
width: 200,
fontSize: 11,
fill: "#3C2415",
editable: true,
textAlign: "center",
charSpacing: 3,
});
Here is display on SVG & FabricJS:
How to find correct charSpacing which is respectively with letter-spacing attribute in SVG's Text node ?
Notes:
In fabric.TextBox document write:
charSpacing :Number
additional space between characters expressed in thousands of em unit
And in document for SVG write:
(if no unit identifier is provided) values in user space — for example, "15"
And Here is my code:
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 textbox_0_1 = new fabric.Textbox("MINH TUẤN", {
top: 20,
left: 20,
width: 200,
fontSize: 11,
fill: "#000000",
editable: true,
textAlign: "center",
charSpacing: 3,
});
canvas.add(textbox_0_1);
setObjectCoords();
canvas.renderAll();
function setObjectCoords() {
canvas.forEachObject(function(object) {
object.setCoords();
});
}
#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>
Thank you,
You need to parse from pixel to em.
DEMO
var canvas = new fabric.Canvas('editorCanvas', {
backgroundColor: 'white',
width: 300,
height: 100
});
var letterSpacing = 5, fontSize = 30;
var textbox = new fabric.Text("MINH TUẤN", {
top: 20,
fontSize: fontSize,
fill: "#000000",
fontFamily : "Verdana"
});
canvas.add(textbox);
var parsedSpacing = fabric.util.parseUnit(letterSpacing, fontSize) / fontSize * 1000;
textbox.set({charSpacing : parsedSpacing});
canvas.requestRenderAll();
console.log(parsedSpacing)
canvas{
border:1px solid;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
<svg width="300" height="100" viewBox="0 0 300 100">
<text font-size="30" letter-spacing="5" font-family="Verdana" y="40">MINH TUẤN</text>
</svg>
<canvas id="editorCanvas"></canvas>

How to create an Editable Text which is curved along a Path in Fabricjs?

I want to make Editable Text which is curved along a fabric.Path as bellow expected image.
Here is my code:
I created a curved fabric.Path & a fabric.TextBox, but I don't know how to curve text along that Path. Please help me resolve this problem.
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', {
selectionLineWidth: 2,
width: $("#canvasContainer").width(),
height: $("#canvasContainer").height()
});
var Path_0_1 = new fabric.Path('M127.91,99.29c32.16,10.73,53.41,7.33,67.81,0 c34.07-17.34,41.99-62.82,60.98-60.98c8.19,0.79,14.11,9.98,17.91,17.91', {
fill : null,
stroke: '#000000',
});
canvas.add(Path_0_1);
var curvedText = new fabric.IText("Anh Minh", {
left: 50,
top: 50,
width: 200,
fontSize: 24,
fontFamily: 'Times New Roman',
fontWeight: 'normal',
textAlign: 'center',
lineHeight: 1,
fill: "#404041",
});
canvas.add(curvedText);
#canvasContainer {
width: 100%;
height: 500px;
}
<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>
Addition information:
After few hours google search, I see an interesting Konva.TextPath, I wonder If can we make same Object like that Konva.TextPath in Fabric.js?
Thank you!

Why fabric.Path on FabricJS is display different from svg file?

I want to display an path in Fabric.JS, in svg file:
<g>
<path style="fill:none;stroke:#000000;stroke-miterlimit:10;"
d="M221.58-0.55 c17.5,31.22,4.77,57.16-8.14,88.46c-13.75,33.35,0.71,57.72,0.71,57.72"/>
</g>
And code in my Fabricjs
var Path_0_1 = new fabric.Path('M221.58-0.55 c17.5,31.22,4.77,57.16-8.14,88.46c-13.75,33.35,0.71,57.72,0.71,57.72', {
fill: 'none',
stroke: '#000000',
strokeMiterLimit: 10,
opacity: 1,
});
But result is not same:
Expected:
Path in FabricJS display same as in SVG file.
Here is my code:
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 Path_0_1 = new fabric.Path('M221.58-0.55 c17.5,31.22,4.77,57.16-8.14,88.46c-13.75,33.35,0.71,57.72,0.71,57.72', {
// fill : 'none',
stroke: '#000000',
strokeMiterLimit: 10,
opacity: 1,
});
canvas.add(Path_0_1);
canvas.moveTo(Path_0_1, 1);
setObjectCoords();
canvas.renderAll();
function setObjectCoords() {
canvas.forEachObject(function(object) {
object.setCoords();
});
}
#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>
And here is my svg file:
https://svgur.com/s/Bw6
Set fill value '' || null || 'transparent', so on ctx.fill() it wont fill anything to the object.
DEMO
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', {
selectionLineWidth: 2,
width: $("#canvasContainer").width(),
height: $("#canvasContainer").height()
});
var Path_0_1 = new fabric.Path('M221.58-0.55c17.5,31.22,4.77,57.16-8.14,88.46c-13.75,33.35,0.71,57.72,0.71,57.72', {
fill : null,
stroke: '#000000',
});
canvas.add(Path_0_1);
#canvasContainer {
width: 100%;
height: 100vh;
}
<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>

Resources