Estoy trabajando en Fabric Grid usando Fabric.js y quiero agregar las dimensiones de la forma que se agrega en el lienzo. El nuevo ancho y alto están definidos, pero necesitaré no solo mostrar las dimensiones en la pantalla, sino también actualizarlo cuando cambie el tamaño de la forma.
A continuación se muestra la función JS, donde se definen newWidth
y newHeight
:
function snapToGrid() { let checkBox = document.getElementById("myCheck"); if (checkBox.checked == true) { //snap only to the grid const gridSize = 50; c.on("object:moving", function (options) { options.target.set({ left: Math.round(options.target.left / gridSize) * gridSize, top: Math.round(options.target.top / gridSize) * gridSize, }); }); c.on("object:modified", function (options) { var newWidth = Math.round(options.target.getWidth() / gridSize) * gridSize; var newHeight = Math.round(options.target.getHeight() / gridSize) * gridSize; options.target.set({ width: newWidth, height: newHeight, scaleX: 1, scaleY: 1, }); }); } else { c.off("object:modified"); c.off("object:moving"); } }
Soy nuevo en Fabric.js y agradecería que alguien me ayudara con esto. Avíseme si tengo que compartir más detalles del código.
De acuerdo, respondí mi propia pregunta, así que si alguien lo necesita, aquí está el código a continuación que ayudaría a mostrar dinámicamente el ancho y la altura de su forma mientras está alterada.
function onObjectScaled(e){ var scaledObject = e.target; var text = document.getElementById("text"); text.value = 'Width = '+scaledObject.getWidth() + ' Height = '+scaledObject.getHeight(); }