Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

78
Visualizações
Fabric 4.6 Cómo hacer que la selección se ajuste a la cuadrícula

Ok, antes que nada, estoy usando esta función para redondear:

 var round = function (num, precision) { // function used for round numbers num = parseFloat(num); if (!precision) return num; return (Math.round(num / precision) * precision); };

Al dibujar una línea, se ajusta a la cuadrícula sin ningún tipo de problema:

 function startDrawingLine(o) { if(mouseDown===true) { let pointer = canvas.getPointer(oe); pointer.x = round(pointer.x, 10); // these two lines will cause the pointer to snap every 10 lines even. pointer.y = round(pointer.y, 10); line.set({ x2: pointer.x, y2: pointer.y }); canvas.requestRenderAll(); // Will actually render the line drawn } }

Pero al mover la línea, simplemente... no se ajusta a la cuadrícula y no estoy muy seguro de lo que hice mal. Al leer y demás, pensé que el código sería algo como esto ... pero parece que no puedo encontrar la respuesta correcta o la implementación de esto:

 canvas.on('object:moving', function(options) { options.target.set({ left: Math.round(options.target.left / grid) * grid, top: Math.round(options.target.top / grid) * grid });

Aquí es donde creo que la línea que se actualiza debe tener la capacidad de ajustarse a la cuadrícula, pero tal vez me equivoque... (No escribí este código, estaba siguiendo un tutorial y he estado haciendo mis propias modificaciones).

 canvas.on({ // this is so that when you move the line, the dblclick circles on the ends move with it. 'object:moved': updateNewLineCoordinates, 'selection:created': updateNewLineCoordinates, 'selection:updated': updateNewLineCoordinates, 'mouse:dblclick': addingControlPoints }); let newLineCoords = {}; function updateNewLineCoordinates(o) { newLineCoords = {}; let obj = o.target; if(obj.id==='added-line') { let centerX = obj.getCenterPoint().x; // Gets the X coordinate of the center of the line drawn let centerY = obj.getCenterPoint().y; // Gets the Y coordinate of the center of the line drawn let x1offset = obj.calcLinePoints().x1; // Now calculate the end beginng and ending of each line to place the red circle for editing the line (so you can drag and make the line bigger or shorter) let y1offset = obj.calcLinePoints().y1; // The offset is just the number of pixels from the center of the line to the end of the line. let x2offset = obj.calcLinePoints().x2; let y2offset = obj.calcLinePoints().y2; newLineCoords = { // places circle at the end of each line x1, y1 being the beginning of the line x1: centerX+x1offset, y1: centerY+y1offset, x2: centerX+x2offset, y2: centerY+y2offset } obj.set({ x1: centerX+x1offset, y1: centerY+y1offset, x2: centerX+x2offset, y2: centerY+y2offset }); obj.setCoords(); } }
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda