• <legend id='1HFL2'><style id='1HFL2'><dir id='1HFL2'><q id='1HFL2'></q></dir></style></legend>
  • <tfoot id='1HFL2'></tfoot>
    <i id='1HFL2'><tr id='1HFL2'><dt id='1HFL2'><q id='1HFL2'><span id='1HFL2'><b id='1HFL2'><form id='1HFL2'><ins id='1HFL2'></ins><ul id='1HFL2'></ul><sub id='1HFL2'></sub></form><legend id='1HFL2'></legend><bdo id='1HFL2'><pre id='1HFL2'><center id='1HFL2'></center></pre></bdo></b><th id='1HFL2'></th></span></q></dt></tr></i><div id='1HFL2'><tfoot id='1HFL2'></tfoot><dl id='1HFL2'><fieldset id='1HFL2'></fieldset></dl></div>

      <bdo id='1HFL2'></bdo><ul id='1HFL2'></ul>

    1. <small id='1HFL2'></small><noframes id='1HFL2'>

      1. 在 Fabric.js 中按宽度/高度在另一个画布对象内居中和缩放画布对象

        时间:2023-08-02

        <i id='7TDEt'><tr id='7TDEt'><dt id='7TDEt'><q id='7TDEt'><span id='7TDEt'><b id='7TDEt'><form id='7TDEt'><ins id='7TDEt'></ins><ul id='7TDEt'></ul><sub id='7TDEt'></sub></form><legend id='7TDEt'></legend><bdo id='7TDEt'><pre id='7TDEt'><center id='7TDEt'></center></pre></bdo></b><th id='7TDEt'></th></span></q></dt></tr></i><div id='7TDEt'><tfoot id='7TDEt'></tfoot><dl id='7TDEt'><fieldset id='7TDEt'></fieldset></dl></div>

        <small id='7TDEt'></small><noframes id='7TDEt'>

          <tbody id='7TDEt'></tbody>
        <legend id='7TDEt'><style id='7TDEt'><dir id='7TDEt'><q id='7TDEt'></q></dir></style></legend>
          • <tfoot id='7TDEt'></tfoot>

                • <bdo id='7TDEt'></bdo><ul id='7TDEt'></ul>
                  本文介绍了在 Fabric.js 中按宽度/高度在另一个画布对象内居中和缩放画布对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  目标:通过 Fabric.js 或通过 Javascript 保持原始对象的纵横比相同,但又不超过父对象的宽度/高度比例?

                  父对象(矩形或组)不会以 canvas 元素为中心.

                  这是我到目前为止的代码:

                  父矩形width: 300 and height: 50:

                  所需的解决方案:

                  父矩形width: 300 and height: 200:

                  父矩形width: 300 and height: 50:

                  有人可以帮忙吗?

                  解决方案

                  想通了.. 这里是 StackBlitz:https://stackblitz.com/edit/angular-pg4fis

                  诀窍是首先将矩形添加到 Fabric 组中,如下所示:

                  var rect = new fabric.Rect({左:100,最高:50,宽度:450,身高:200,填充:'#e3e3e3',});var rectGroup = new fabric.Group([rect], {名称:'矩形',});this.canvas.add(rectGroup);

                  然后,我能够建立父(组)元素边界并使用可变比例因子通过 Math 功能设置图像:

                  fabric.Image.fromURL('https://angular.io/assets/images/logos/angular/logo-nav@2x.png', (img) => {让边界 = rectGroup.getBoundingRect();常量 scaleFactor = Math.min(Math.min(1, bounds.width/img.width),Math.min(1, bounds.height/img.height));img.scale(比例因子);img.set({顶部:bounds.top + Math.max(bounds.height - img.height * scaleFactor, 0)/2,左:bounds.left + Math.max(bounds.width - img.width * scaleFactor, 0)/2,});rectGroup.addWithUpdate(img);this.canvas.renderAll();});

                  Goal: To center an object (horizontally and vertically) inside another object (rectangle or group) on canvas via Fabric.js or via Javascript which keeps the original object aspect ratio the same, but also not exceed the parent object width/height proportions?

                  The parent object (rectangle or group) won't be centered on the canvas element.

                  Here's the code I have so far: https://stackblitz.com/edit/angular-my8hky

                  My app.component.ts so far:

                  canvas: fabric.Canvas;
                  
                  ngOnInit() {
                    this.canvas = new fabric.Canvas('canvas');
                    var rect = new fabric.Rect({
                      left: 100,
                      top: 50,
                      width: 300,
                      height: 200,
                      fill: '#eee'
                    });
                    this.canvas.add(rect);
                  
                    fabric.Image.fromURL('https://angular.io/assets/images/logos/angular/logo-nav@2x.png', (img) => {
                      let bounds = rect.getBoundingRect();
                      let oImg = img.set({
                        left: bounds.left,
                        top: bounds.top,
                        width: rect.width
                      }).scale(1);
                      this.canvas.add(oImg).renderAll();
                    });
                  }
                  

                  Not only is the new object not centered vertically, but also not centered horizontally if the rectangle object height is decreased (for example to 50px in height).

                  I realize I'm only declaring the inner image object width to be the same as the parent rectangle boundary width.

                  Current solution:

                  Parent rectangle width: 300 and height: 200:

                  Parent rectangle width: 300 and height: 50:

                  Desired solution:

                  Parent rectangle width: 300 and height: 200:

                  Parent rectangle width: 300 and height: 50:

                  Can anyone assist?

                  解决方案

                  Figured it out.. Here's the StackBlitz: https://stackblitz.com/edit/angular-pg4fis

                  The trick was to first add the rectangle to a Fabric group, like so:

                  var rect = new fabric.Rect({
                    left: 100,
                    top: 50,
                    width: 450,
                    height: 200,
                    fill: '#e3e3e3',
                  });
                  
                  var rectGroup = new fabric.Group([rect], {
                    name: 'Rectangle',
                  });
                  
                  this.canvas.add(rectGroup);
                  

                  Then, I was able to establish the parent (group) element boundaries and use a variable scaling factor to set the image via Math functionality:

                  fabric.Image.fromURL('https://angular.io/assets/images/logos/angular/logo-nav@2x.png', (img) => {
                    let bounds = rectGroup.getBoundingRect();
                  
                    const scaleFactor = Math.min(
                      Math.min(1, bounds.width / img.width),
                      Math.min(1, bounds.height / img.height)
                    );
                    img.scale(scaleFactor);
                  
                    img.set({
                      top: bounds.top + Math.max(bounds.height - img.height * scaleFactor, 0)/2,
                      left: bounds.left + Math.max(bounds.width - img.width * scaleFactor, 0)/2,
                    });
                  
                    rectGroup.addWithUpdate(img);
                    this.canvas.renderAll();
                  });
                  

                  这篇关于在 Fabric.js 中按宽度/高度在另一个画布对象内居中和缩放画布对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:KineticJS 撤消图层:撤消时图层不会消失? 下一篇:我如何“写作"上下文画布中的图标?

                  相关文章

                    <small id='AnBka'></small><noframes id='AnBka'>

                  1. <tfoot id='AnBka'></tfoot>

                    1. <legend id='AnBka'><style id='AnBka'><dir id='AnBka'><q id='AnBka'></q></dir></style></legend>

                        <bdo id='AnBka'></bdo><ul id='AnBka'></ul>
                      <i id='AnBka'><tr id='AnBka'><dt id='AnBka'><q id='AnBka'><span id='AnBka'><b id='AnBka'><form id='AnBka'><ins id='AnBka'></ins><ul id='AnBka'></ul><sub id='AnBka'></sub></form><legend id='AnBka'></legend><bdo id='AnBka'><pre id='AnBka'><center id='AnBka'></center></pre></bdo></b><th id='AnBka'></th></span></q></dt></tr></i><div id='AnBka'><tfoot id='AnBka'></tfoot><dl id='AnBka'><fieldset id='AnBka'></fieldset></dl></div>