I'm currently using fabricjs version 1.7.21 and I have a button that allows me to add text to the canvas. I am wondering how I can add a group of objects, such as multiple editable IText fields (as shown on the right side), to the canvas just like I can with a single IText object.
In simpler terms, I want to be able to add elements similar to those on the right side by clicking a button and then moving them around as a group.
var canvas = new fabric.Canvas("c");
canvas.setHeight(500);
canvas.setWidth(500);
// Add Text
function Addtext() {
var text = new fabric.IText("Type...", {
fontSize: 30,
top: 10,
left: 10,
width: 200,
height: 200,
textAlign: "center"
});
canvas.add(text);
canvas.setActiveObject(text);
text.enterEditing();
text.selectAll();
text.renderCursorOrSelection(); // or canvas.renderAll();
canvas.isDrawingMode = false;
}
// Right Side Objects
// Header Constants
const toppers = {
fontSize: 27,
hasBorders: true,
hasControls: false,
left: 380,
lockMovementX: true,
lockMovementY: true,
lockRotation: true,
originX: "center",
originY: "center",
selectable: true,
align: "mid",
centeredScaling: true,
};
const headingOne = {
lockMovementX: true,
lockMovementY: true,
originX: "center",
originY: "center",
selectable: true,
align: "mid",
fontSize: 14,
fontStyle: "italic",
hasBorders: true,
hasControls: false,
hasRotatingPoint: false,
left: 380,
};
const headingTwo = {
align: "mid",
centeredScaling: true,
fontSize: 20,
hasBorders: true,
hasControls: false,
hasRotatingPoint: false,
lockMovementX: true,
lockMovementY: true,
lockRotation: true,
originX: "center",
originY: "center",
selectable: true,
left: 380,
};
canvas.add(new fabric.IText("Some Text", {
...toppers,
top: 25,
}));
canvas.add(new fabric.IText("Some Text", {
...toppers,
fontWeight: "bold",
top: 60,
}));
canvas.add(new fabric.IText("Some Text", {
...headingOne,
top: 90,
}));
canvas.add(new fabric.IText("Some Text", {
...headingTwo,
top: 110,
}));
canvas {
border: 1px solid #dddddd;
margin-top: 10px;
border-radius: 3px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.21/fabric.min.js"></script>
<button onclick="Addtext()">Add Text</button>
<canvas id="c"></canvas>