简体   繁体   English

Fabric.js 3.4 - 动态调整组或多边形上的 SVG strokeWidth

[英]Fabric.js 3.4 - Dynamically adjusting SVG strokeWidth on Group or Polygon

I have an app (based on fabric.js 3.4) that allows the user to adjust the strokeWith of an SVG ( Group or Polygon ) in real-time via a slider.我有一个应用程序(基于fabric.js 3.4),它允许用户通过slider 实时调整SVG(组或多边形)的strokeWith。 However, this a seems to break the image.然而,这似乎打破了形象。 So, for example if I have an SVG (Group) of a smiley face with 2 circles (strokes) for eyes, a stroked mouth and outer circle -- updating the slider to increase stroke width with deforms the image such that eyes, etc start overlapping/shifting (ie do not scale together as a unit/image) or, the image "flashes" - disappearing/reappearing as the slider is moved.因此,例如,如果我有一个笑脸的 SVG(组),其中有 2 个圆圈(笔划)用于眼睛、一个笔划嘴巴和外圈——更新 slider 以增加笔划宽度,使图像变形,使眼睛等开始重叠/移动(即不作为一个单元/图像一起缩放)或图像“闪烁” - 随着 slider 的移动而消失/重新出现。

The code being used to update is:用于更新的代码是:

 var value = //get value from slider widget
 var obj = canvas.getActiveObject();
   if (obj && obj.type == "group") {          
        for (i = 0; i < obj._objects.length; i++) {
            if (obj._objects[i]) {
                obj._objects[i].set({ strokeWidth: value });
            }
        }      
    } else if (obj && obj.type == "polygon") {
        obj.set({ strokeWidth: value });
    }
    canvas.renderAll();

Is this the wrong way to go about it?难道这是把go弄错了方式吗? Any help would be greatly appreciated.任何帮助将不胜感激。

the right way of scaling the elements inside the group is to change the scale value of the group itself:缩放组内元素的正确方法是更改组本身的scale值:

group.set({ scaleX: value, scaleY: value })

If however you need to change some values of the objects inside the group, then assure to call addWithUpdate near you call to canvas.renderAll() to update all the necessary values of the group.但是,如果您需要更改组内对象的某些值,请确保在调用canvas.renderAll canvas.renderAll()附近调用 addWithUpdate 以更新组的所有必要值。 Please refer to my recent reply fabric.js 3.6 - Bounding box for group doesn't update when grouped objects are changed for some additional details on that.请参阅我最近的回复fabric.js 3.6 - 当分组对象更改时,组的边界框不会更新,以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM