customized-fabric 2.0.1 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,7 +29,7 @@ export declare const toCurvedTextObject: (object: CurvedText) => {
29
29
  prefix: any;
30
30
  infix: any;
31
31
  suffix: any;
32
- curvedWidth: any;
32
+ curvedLengthRatio: any;
33
33
  pathSide: any;
34
34
  pathStartOffset: any;
35
35
  isAllCapital: any;
@@ -52,8 +52,7 @@ export default class CurvedText extends fabric.Group {
52
52
  setSuffix?: (text: string) => void;
53
53
  setMaxFontSize?: (fontSize: string) => void;
54
54
  setFontFamily?: (fontName: string, fontUrl?: string) => void;
55
- setTextAttributes?: (options: fabric.ITextOptions) => void;
56
- setTextWidth?: (width: number) => void;
55
+ setTextAttributes?: (options: any) => void;
57
56
  getTextAttribute?: (attribute: string) => any;
58
57
  getSettings?: (attribute: string) => any;
59
58
  setSizes?: (options: {
@@ -18,7 +18,8 @@ const CurvedTextClass = fabric_1.fabric.util.createClass(fabric_1.fabric.Group,
18
18
  objectCaching: false,
19
19
  ...constants_1.CURVED_TEXT_OBJECT_ATTRIBUTES.stroke,
20
20
  });
21
- const { prefix = "", infix = "", suffix = "" } = text ?? {};
21
+ const { prefix = "", infix = "", suffix = "", isAllCapital } = text ?? {};
22
+ const fullText = prefix + infix + suffix;
22
23
  this.textObject = new fabric_1.fabric.IText("", {
23
24
  originX: "center",
24
25
  originY: "center",
@@ -26,8 +27,9 @@ const CurvedTextClass = fabric_1.fabric.util.createClass(fabric_1.fabric.Group,
26
27
  objectCaching: false,
27
28
  fontFamily: "",
28
29
  paintFirst: "stroke",
30
+ curvedLengthRatio: 0.3,
29
31
  ...text,
30
- text: prefix + infix + suffix,
32
+ text: isAllCapital ? fullText.toUpperCase() : fullText,
31
33
  });
32
34
  const group = new fabric_1.fabric.Group([this.rectObject, this.textObject]);
33
35
  this.set({
@@ -66,11 +68,13 @@ const CurvedTextClass = fabric_1.fabric.util.createClass(fabric_1.fabric.Group,
66
68
  }
67
69
  this.fire("scaling");
68
70
  },
69
- autoChangeFontSize: function (changeSpeed) {
70
- let { curvedWidth, fontSize = 0, maxFontSize } = this.textObject;
71
+ autoChangeFontSize: function (changeSpeed = 0.1) {
72
+ let { curvedLengthRatio, fontSize = 0, maxFontSize } = this.textObject;
73
+ const circumference = (0, svg_util_1.getEllipseCircumference)(this.width / 2, this.height / 2);
74
+ const curvedLength = circumference * curvedLengthRatio;
71
75
  let maxLineWidth = Math.max(...this.textObject.__lineWidths);
72
76
  fontSize = Number(fontSize);
73
- while (curvedWidth <= maxLineWidth) {
77
+ while (curvedLength <= maxLineWidth) {
74
78
  fontSize -= changeSpeed;
75
79
  this.textObject.set({
76
80
  fontSize,
@@ -78,7 +82,7 @@ const CurvedTextClass = fabric_1.fabric.util.createClass(fabric_1.fabric.Group,
78
82
  this.canvas?.renderAll?.();
79
83
  maxLineWidth = Math.max(...this.textObject.__lineWidths);
80
84
  }
81
- while (maxFontSize > fontSize.toFixed(1) && curvedWidth > maxLineWidth) {
85
+ while (maxFontSize > fontSize.toFixed(1) && curvedLength > maxLineWidth) {
82
86
  fontSize += changeSpeed;
83
87
  this.textObject.set({
84
88
  fontSize,
@@ -144,10 +148,6 @@ const CurvedTextClass = fabric_1.fabric.util.createClass(fabric_1.fabric.Group,
144
148
  this.textObject.set(options);
145
149
  this.fire("scaling");
146
150
  },
147
- setTextWidth: function (width) {
148
- this.textObject.set({ curvedWidth: width });
149
- this.fire("scaling");
150
- },
151
151
  getTextAttribute: function (attribute) {
152
152
  return this.textObject.get(attribute);
153
153
  },
@@ -211,7 +211,7 @@ const toCurvedTextObject = (object) => {
211
211
  prefix: textObject?.prefix,
212
212
  infix: textObject?.infix,
213
213
  suffix: textObject?.suffix,
214
- curvedWidth: textObject?.curvedWidth,
214
+ curvedLengthRatio: textObject?.curvedLengthRatio,
215
215
  pathSide: textObject?.pathSide,
216
216
  pathStartOffset: textObject?.pathStartOffset,
217
217
  isAllCapital: textObject?.isAllCapital,
@@ -18,7 +18,7 @@ export interface ICurvedTextOptions extends fabric.IGroupOptions {
18
18
  prefix?: string;
19
19
  infix?: string;
20
20
  suffix?: string;
21
- curvedWidth?: number;
21
+ curvedLengthRatio?: number;
22
22
  isAllCapital?: boolean;
23
23
  };
24
24
  fontUrl?: string;
@@ -1,3 +1,4 @@
1
1
  import { fabric } from "fabric";
2
2
  export declare const getEllipsePath: (rx: number, ry: number, side?: "left" | "right") => string;
3
3
  export declare const getPathLength: (path: fabric.Path) => any;
4
+ export declare const getEllipseCircumference: (a: number, b: number) => number;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPathLength = exports.getEllipsePath = void 0;
3
+ exports.getEllipseCircumference = exports.getPathLength = exports.getEllipsePath = void 0;
4
4
  const fabric_1 = require("fabric");
5
5
  const getEllipsePath = (rx, ry, side = "left") => {
6
6
  const sideFactor = side == "left" ? -1 : 1;
@@ -17,3 +17,7 @@ const getPathLength = (path) => {
17
17
  return pathLength;
18
18
  };
19
19
  exports.getPathLength = getPathLength;
20
+ const getEllipseCircumference = (a, b) => {
21
+ return Math.PI * (3 * (a + b) - Math.sqrt((3 * a + b) * (a + 3 * b)));
22
+ };
23
+ exports.getEllipseCircumference = getEllipseCircumference;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "customized-fabric",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "Customized fabric",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",