xpict 0.0.3 → 0.1.1

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.
Files changed (96) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +35 -1
  3. package/dist/index.d.ts +15 -0
  4. package/dist/index.js +33 -0
  5. package/dist/layers/circle-layer.d.ts +15 -0
  6. package/dist/layers/circle-layer.js +37 -0
  7. package/dist/layers/group-layer.d.ts +14 -0
  8. package/dist/layers/group-layer.js +29 -0
  9. package/dist/layers/image-layer.d.ts +16 -0
  10. package/dist/layers/image-layer.js +28 -0
  11. package/dist/layers/index.d.ts +8 -0
  12. package/dist/layers/index.js +24 -0
  13. package/dist/layers/layer.d.ts +7 -0
  14. package/dist/layers/layer.js +12 -0
  15. package/dist/layers/line-layer.d.ts +21 -0
  16. package/dist/layers/line-layer.js +40 -0
  17. package/dist/layers/rectangle-layer.d.ts +16 -0
  18. package/dist/layers/rectangle-layer.js +53 -0
  19. package/dist/layers/repeat-layer.d.ts +15 -0
  20. package/dist/layers/repeat-layer.js +32 -0
  21. package/dist/layers/text-layer.d.ts +30 -0
  22. package/dist/layers/text-layer.js +68 -0
  23. package/dist/render-context.d.ts +6 -0
  24. package/dist/render-context.js +2 -0
  25. package/dist/template.d.ts +21 -0
  26. package/dist/template.js +48 -0
  27. package/dist/utils/commit-frame.d.ts +2 -0
  28. package/dist/utils/commit-frame.js +18 -0
  29. package/dist/utils/font-config.d.ts +11 -0
  30. package/dist/utils/font-config.js +11 -0
  31. package/dist/utils/resolve-axis.d.ts +3 -0
  32. package/dist/utils/resolve-axis.js +9 -0
  33. package/package.json +30 -72
  34. package/dev-scripts/generate-exports.cjs +0 -57
  35. package/dev-scripts/index.ts +0 -149
  36. package/dev-scripts/prepare-package-json.js +0 -31
  37. package/dist/cjs/actions.js +0 -232
  38. package/dist/cjs/constants.js +0 -26
  39. package/dist/cjs/index.js +0 -77
  40. package/dist/cjs/package.json +0 -1
  41. package/dist/cjs/utils/clone-image.util.ts.js +0 -12
  42. package/dist/cjs/utils/color.util.js +0 -21
  43. package/dist/cjs/utils/create-image.util.js +0 -18
  44. package/dist/cjs/utils/index.js +0 -14
  45. package/dist/cjs/utils/open-image.util.js +0 -10
  46. package/dist/esm/actions.js +0 -214
  47. package/dist/esm/constants.js +0 -20
  48. package/dist/esm/index.js +0 -71
  49. package/dist/esm/package.json +0 -1
  50. package/dist/esm/utils/clone-image.util.ts.js +0 -7
  51. package/dist/esm/utils/color.util.js +0 -17
  52. package/dist/esm/utils/create-image.util.js +0 -13
  53. package/dist/esm/utils/index.js +0 -4
  54. package/dist/esm/utils/open-image.util.js +0 -5
  55. package/dist/tsconfig.cjs.tsbuildinfo +0 -1
  56. package/dist/tsconfig.esm.tsbuildinfo +0 -1
  57. package/dist/tsconfig.types.tsbuildinfo +0 -1
  58. package/dist/types/actions.d.ts +0 -77
  59. package/dist/types/constants.d.ts +0 -19
  60. package/dist/types/index.d.ts +0 -31
  61. package/dist/types/utils/clone-image.util.ts.d.ts +0 -4
  62. package/dist/types/utils/color.util.d.ts +0 -4
  63. package/dist/types/utils/create-image.util.d.ts +0 -12
  64. package/dist/types/utils/index.d.ts +0 -4
  65. package/dist/types/utils/open-image.util.d.ts +0 -3
  66. package/fonts/Curse Casual.ttf +0 -0
  67. package/fonts/Poppins-Black.ttf +0 -0
  68. package/fonts/Poppins-BlackItalic.ttf +0 -0
  69. package/fonts/Poppins-Bold.ttf +0 -0
  70. package/fonts/Poppins-BoldItalic.ttf +0 -0
  71. package/fonts/Poppins-ExtraBold.ttf +0 -0
  72. package/fonts/Poppins-ExtraBoldItalic.ttf +0 -0
  73. package/fonts/Poppins-ExtraLight.ttf +0 -0
  74. package/fonts/Poppins-ExtraLightItalic.ttf +0 -0
  75. package/fonts/Poppins-Italic.ttf +0 -0
  76. package/fonts/Poppins-Light.ttf +0 -0
  77. package/fonts/Poppins-LightItalic.ttf +0 -0
  78. package/fonts/Poppins-Medium.ttf +0 -0
  79. package/fonts/Poppins-MediumItalic.ttf +0 -0
  80. package/fonts/Poppins-Regular.ttf +0 -0
  81. package/fonts/Poppins-SemiBold.ttf +0 -0
  82. package/fonts/Poppins-SemiBoldItalic.ttf +0 -0
  83. package/fonts/Poppins-Thin.ttf +0 -0
  84. package/fonts/Poppins-ThinItalic.ttf +0 -0
  85. package/src/actions.ts +0 -390
  86. package/src/constants.ts +0 -30
  87. package/src/index.ts +0 -124
  88. package/src/utils/clone-image.util.ts.ts +0 -11
  89. package/src/utils/color.util.ts +0 -25
  90. package/src/utils/create-image.util.ts +0 -34
  91. package/src/utils/index.ts +0 -11
  92. package/src/utils/open-image.util.ts +0 -9
  93. package/tsconfig.cjs.json +0 -7
  94. package/tsconfig.esm.json +0 -7
  95. package/tsconfig.json +0 -15
  96. package/tsconfig.types.json +0 -8
@@ -1,232 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toGrayScale = toGrayScale;
4
- exports.cropImage = cropImage;
5
- exports.resizeImage = resizeImage;
6
- exports.rotateImage = rotateImage;
7
- exports.blurImage = blurImage;
8
- exports.modulateSaturation = modulateSaturation;
9
- exports.modulateBrightness = modulateBrightness;
10
- exports.invertColors = invertColors;
11
- exports.addBorder = addBorder;
12
- exports.adjustContrast = adjustContrast;
13
- exports.modulateOpacity = modulateOpacity;
14
- exports.flipImage = flipImage;
15
- exports.flopImage = flopImage;
16
- exports.insertText = insertText;
17
- exports.insertCircle = insertCircle;
18
- exports.insertRectangle = insertRectangle;
19
- exports.insertLine = insertLine;
20
- const canvas_1 = require("canvas");
21
- const constants_1 = require("./constants");
22
- function toGrayScale(grayscale = true) {
23
- return (image) => image.grayscale(grayscale);
24
- }
25
- function cropImage(options) {
26
- return (image) => image.extract(options);
27
- }
28
- function resizeImage({ width, height, fit = "inside" }) {
29
- return (image) => image.resize(width, height, { fit: fit });
30
- }
31
- function rotateImage(angle) {
32
- return (image) => image.rotate(angle);
33
- }
34
- function blurImage(sigma = 1) {
35
- return (image) => image.blur(sigma);
36
- }
37
- function modulateSaturation(saturation) {
38
- return (image) => image.modulate({ saturation });
39
- }
40
- function modulateBrightness(brightness) {
41
- return (image) => image.modulate({ brightness });
42
- }
43
- function invertColors() {
44
- return (image) => image.negate();
45
- }
46
- function addBorder({ size, color = constants_1.colors.black }) {
47
- return (image) => image.extend({
48
- top: size,
49
- bottom: size,
50
- left: size,
51
- right: size,
52
- background: color
53
- });
54
- }
55
- function adjustContrast(contrast) {
56
- return (image) => {
57
- const factor = (259 * (contrast + 255)) / (255 * (259 - contrast));
58
- return image.linear(factor, -(128 * factor) + 128);
59
- };
60
- }
61
- function modulateOpacity(opacity) {
62
- return (image) => image.flatten({ background: { alpha: opacity } });
63
- }
64
- function flipImage() {
65
- return (image) => image.flip();
66
- }
67
- function flopImage() {
68
- return (image) => image.flop();
69
- }
70
- function insertText({ text, font, x, y, backgroundColor = constants_1.insertTextDefaultOptions.backgroundColor, anchor = constants_1.insertTextDefaultOptions.anchor, stroke, rotation = constants_1.insertTextDefaultOptions.rotation }) {
71
- return async (image) => {
72
- const imageMetadata = await image.metadata();
73
- const width = imageMetadata.width;
74
- const height = imageMetadata.height;
75
- const canvas = (0, canvas_1.createCanvas)(width, height);
76
- const context = canvas.getContext("2d");
77
- if (backgroundColor !== "transparent") {
78
- context.fillStyle = backgroundColor;
79
- context.fillRect(0, 0, width, height);
80
- }
81
- if (font.filePath) {
82
- (0, canvas_1.registerFont)(font.filePath, { family: font.name });
83
- }
84
- context.font = `${font.size}px ${font.name ?? constants_1.insertTextDefaultOptions.font.name}`;
85
- context.fillStyle = font.color ?? constants_1.colors.black;
86
- const textMetrics = context.measureText(text);
87
- const textWidth = textMetrics.width;
88
- const textHeight = font.size;
89
- const anchorOffsets = {
90
- "top-left": {
91
- x: 0,
92
- y: 0
93
- },
94
- "top-center": {
95
- x: -textWidth / 2,
96
- y: 0
97
- },
98
- "top-right": {
99
- x: -textWidth,
100
- y: 0
101
- },
102
- "middle-left": {
103
- x: 0,
104
- y: -textHeight / 2
105
- },
106
- "middle-center": {
107
- x: -textWidth / 2,
108
- y: -textHeight / 2
109
- },
110
- "middle-right": {
111
- x: -textWidth,
112
- y: -textHeight / 2
113
- },
114
- "bottom-left": {
115
- x: 0,
116
- y: -textHeight
117
- },
118
- "bottom-center": {
119
- x: -textWidth / 2,
120
- y: -textHeight
121
- },
122
- "bottom-right": {
123
- x: -textWidth,
124
- y: -textHeight
125
- }
126
- };
127
- const { x: offsetX, y: offsetY } = anchorOffsets[anchor] || { x: 0, y: 0 };
128
- const adjustedX = x + offsetX;
129
- const adjustedY = y + offsetY;
130
- const adjustedRotation = rotation ?? 0;
131
- context.save();
132
- context.translate(adjustedX, adjustedY);
133
- context.rotate((adjustedRotation * Math.PI) / 180);
134
- if (stroke) {
135
- context.strokeStyle = stroke.fill;
136
- context.lineWidth = stroke.width;
137
- context.lineJoin = "round";
138
- context.strokeText(text, 0, 0);
139
- }
140
- context.fillStyle = font.color ?? constants_1.colors.black;
141
- context.fillText(text, 0, 0);
142
- context.restore();
143
- const textBuffer = canvas.toBuffer();
144
- return image.composite([
145
- {
146
- input: textBuffer,
147
- top: 0,
148
- left: 0
149
- }
150
- ]);
151
- };
152
- }
153
- function insertCircle({ x, y, radius, fill }) {
154
- return async (image) => {
155
- const imageMetadata = await image.metadata();
156
- const width = imageMetadata.width;
157
- const height = imageMetadata.height;
158
- const canvas = (0, canvas_1.createCanvas)(width, height);
159
- const context = canvas.getContext("2d");
160
- context.beginPath();
161
- context.arc(x, y, radius, 0, Math.PI * 2, true);
162
- context.closePath();
163
- context.fillStyle = fill;
164
- context.fill();
165
- const circleBuffer = canvas.toBuffer();
166
- return image.composite([
167
- {
168
- input: circleBuffer,
169
- top: 0,
170
- left: 0
171
- }
172
- ]);
173
- };
174
- }
175
- function insertRectangle({ x, y, width, height, fill, borderRadius }) {
176
- return async (image) => {
177
- const imageMetadata = await image.metadata();
178
- const canvasWidth = imageMetadata.width;
179
- const canvasHeight = imageMetadata.height;
180
- const canvas = (0, canvas_1.createCanvas)(canvasWidth, canvasHeight);
181
- const context = canvas.getContext("2d");
182
- context.fillStyle = fill;
183
- if (borderRadius && borderRadius > 0) {
184
- context.beginPath();
185
- context.moveTo(x + borderRadius, y);
186
- context.lineTo(x + width - borderRadius, y);
187
- context.arcTo(x + width, y, x + width, y + height, borderRadius);
188
- context.lineTo(x + width, y + height - borderRadius);
189
- context.arcTo(x + width, y + height, x, y + height, borderRadius);
190
- context.lineTo(x + borderRadius, y + height);
191
- context.arcTo(x, y + height, x, y, borderRadius);
192
- context.lineTo(x, y + borderRadius);
193
- context.arcTo(x, y, x + width, y, borderRadius);
194
- context.closePath();
195
- context.fill();
196
- }
197
- else {
198
- context.fillRect(x, y, width, height);
199
- }
200
- const rectangleBuffer = canvas.toBuffer();
201
- return image.composite([
202
- {
203
- input: rectangleBuffer,
204
- top: 0,
205
- left: 0
206
- }
207
- ]);
208
- };
209
- }
210
- function insertLine({ x1, y1, x2, y2, color, width }) {
211
- return async (image) => {
212
- const imageMetadata = await image.metadata();
213
- const canvasWidth = imageMetadata.width;
214
- const canvasHeight = imageMetadata.height;
215
- const canvas = (0, canvas_1.createCanvas)(canvasWidth, canvasHeight);
216
- const context = canvas.getContext("2d");
217
- context.strokeStyle = color;
218
- context.lineWidth = width;
219
- context.beginPath();
220
- context.moveTo(x1, y1);
221
- context.lineTo(x2, y2);
222
- context.stroke();
223
- const lineBuffer = canvas.toBuffer();
224
- return image.composite([
225
- {
226
- input: lineBuffer,
227
- top: 0,
228
- left: 0
229
- }
230
- ]);
231
- };
232
- }
@@ -1,26 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.insertTextDefaultOptions = exports.createImageDefaultOptions = exports.defautltTemplateAcitonType = exports.colors = void 0;
4
- const color_util_1 = require("./utils/color.util");
5
- const colors = {
6
- black: "#000000",
7
- white: "#FFFFFF"
8
- };
9
- exports.colors = colors;
10
- const defautltTemplateAcitonType = "beforeLayersProccess";
11
- exports.defautltTemplateAcitonType = defautltTemplateAcitonType;
12
- const createImageDefaultOptions = {
13
- chnannels: 4,
14
- fill: (0, color_util_1.rgba)(0, 0, 0, 0),
15
- format: "png"
16
- };
17
- exports.createImageDefaultOptions = createImageDefaultOptions;
18
- const insertTextDefaultOptions = {
19
- font: {
20
- name: "sans-serif",
21
- },
22
- anchor: "top-left",
23
- backgroundColor: "transparent",
24
- rotation: 0
25
- };
26
- exports.insertTextDefaultOptions = insertTextDefaultOptions;
package/dist/cjs/index.js DELETED
@@ -1,77 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.processImageTemplate = processImageTemplate;
7
- exports.extendImageTemplate = extendImageTemplate;
8
- const constants_1 = require("./constants");
9
- const clone_image_util_ts_1 = __importDefault(require("./utils/clone-image.util.ts"));
10
- function extractActions(actions) {
11
- const actionsLayersProccess = [];
12
- const actionsAfterLayersProccess = [];
13
- if (!actions) {
14
- actions = [];
15
- }
16
- for (const action of actions) {
17
- const actionType = typeof action === "function" ? constants_1.defautltTemplateAcitonType : action.type;
18
- const actionFunc = typeof action === "function" ? action : action.func;
19
- if (actionType === "beforeLayersProccess") {
20
- actionsLayersProccess.push(actionFunc);
21
- }
22
- else {
23
- actionsAfterLayersProccess.push(actionFunc);
24
- }
25
- }
26
- return {
27
- before: actionsLayersProccess,
28
- after: actionsAfterLayersProccess
29
- };
30
- }
31
- async function processImageTemplate(template) {
32
- const actions = extractActions(template.actions);
33
- let image = template.image;
34
- for (const action of actions.before) {
35
- const imageResult = await action(image);
36
- image = await (0, clone_image_util_ts_1.default)(imageResult);
37
- }
38
- if (template.layers) {
39
- const layerImages = [];
40
- for (const layer of template.layers) {
41
- const currentImage = await processImageTemplate(layer.template);
42
- const currentImageBuffer = await currentImage.png().toBuffer();
43
- layerImages.push({
44
- input: currentImageBuffer,
45
- top: layer.y,
46
- left: layer.x
47
- });
48
- }
49
- image = image.composite(layerImages);
50
- }
51
- for (const action of actions.after) {
52
- const imageResult = await action(image);
53
- image = await (0, clone_image_util_ts_1.default)(imageResult);
54
- }
55
- return image;
56
- }
57
- function extendImageTemplate(template, extendTemplate) {
58
- const actions = [];
59
- const layers = [];
60
- if (template.layers) {
61
- layers.push(...template.layers);
62
- }
63
- if (extendTemplate.layers) {
64
- layers.push(...extendTemplate.layers);
65
- }
66
- if (template.actions) {
67
- actions.push(...template.actions);
68
- }
69
- if (extendTemplate.actions) {
70
- actions.push(...extendTemplate.actions);
71
- }
72
- return {
73
- image: template.image,
74
- actions: actions,
75
- layers: layers
76
- };
77
- }
@@ -1 +0,0 @@
1
- {"type": "commonjs"}
@@ -1,12 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const sharp_1 = __importDefault(require("sharp"));
7
- async function cloneImage(image) {
8
- const imageBuffer = await image.toBuffer();
9
- const clonedImage = (0, sharp_1.default)(imageBuffer);
10
- return clonedImage;
11
- }
12
- exports.default = cloneImage;
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.rgb = rgb;
4
- exports.rgba = rgba;
5
- exports.hex = hex;
6
- function componentToHex(component) {
7
- const hex = component.toString(16);
8
- return hex.padStart(2, "0");
9
- }
10
- function rgb(red, green, blue) {
11
- return `#${componentToHex(red)}${componentToHex(green)}${componentToHex(blue)}`;
12
- }
13
- function rgba(red, green, blue, alpha) {
14
- const alphaHex = componentToHex(Math.round(alpha * 255));
15
- return `#${componentToHex(red)}${componentToHex(green)}${componentToHex(blue)}${alphaHex}`;
16
- }
17
- function hex(value) {
18
- const hexadecimalValue = value.toString(16);
19
- const fromatedHexadecimal = `#${hexadecimalValue.padStart(6, "0")}`;
20
- return fromatedHexadecimal;
21
- }
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const sharp_1 = __importDefault(require("sharp"));
7
- const constants_1 = require("../constants");
8
- function createImage({ width, height, fill = constants_1.createImageDefaultOptions.fill, channels = constants_1.createImageDefaultOptions.chnannels, format = constants_1.createImageDefaultOptions.format, }) {
9
- return (0, sharp_1.default)({
10
- create: {
11
- width: width,
12
- height: height,
13
- channels: channels,
14
- background: fill,
15
- },
16
- })[format]();
17
- }
18
- exports.default = createImage;
@@ -1,14 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.hex = exports.rgb = exports.rgba = exports.openImage = exports.createImage = void 0;
7
- const create_image_util_1 = __importDefault(require("./create-image.util"));
8
- exports.createImage = create_image_util_1.default;
9
- const color_util_1 = require("./color.util");
10
- Object.defineProperty(exports, "rgb", { enumerable: true, get: function () { return color_util_1.rgb; } });
11
- Object.defineProperty(exports, "rgba", { enumerable: true, get: function () { return color_util_1.rgba; } });
12
- Object.defineProperty(exports, "hex", { enumerable: true, get: function () { return color_util_1.hex; } });
13
- const open_image_util_1 = __importDefault(require("./open-image.util"));
14
- exports.openImage = open_image_util_1.default;
@@ -1,10 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const sharp_1 = __importDefault(require("sharp"));
7
- function openImage(filePath) {
8
- return (0, sharp_1.default)(filePath);
9
- }
10
- exports.default = openImage;
@@ -1,214 +0,0 @@
1
- import { createCanvas, registerFont } from "canvas";
2
- import { colors, insertTextDefaultOptions } from "./constants";
3
- function toGrayScale(grayscale = true) {
4
- return (image) => image.grayscale(grayscale);
5
- }
6
- function cropImage(options) {
7
- return (image) => image.extract(options);
8
- }
9
- function resizeImage({ width, height, fit = "inside" }) {
10
- return (image) => image.resize(width, height, { fit: fit });
11
- }
12
- function rotateImage(angle) {
13
- return (image) => image.rotate(angle);
14
- }
15
- function blurImage(sigma = 1) {
16
- return (image) => image.blur(sigma);
17
- }
18
- function modulateSaturation(saturation) {
19
- return (image) => image.modulate({ saturation });
20
- }
21
- function modulateBrightness(brightness) {
22
- return (image) => image.modulate({ brightness });
23
- }
24
- function invertColors() {
25
- return (image) => image.negate();
26
- }
27
- function addBorder({ size, color = colors.black }) {
28
- return (image) => image.extend({
29
- top: size,
30
- bottom: size,
31
- left: size,
32
- right: size,
33
- background: color
34
- });
35
- }
36
- function adjustContrast(contrast) {
37
- return (image) => {
38
- const factor = (259 * (contrast + 255)) / (255 * (259 - contrast));
39
- return image.linear(factor, -(128 * factor) + 128);
40
- };
41
- }
42
- function modulateOpacity(opacity) {
43
- return (image) => image.flatten({ background: { alpha: opacity } });
44
- }
45
- function flipImage() {
46
- return (image) => image.flip();
47
- }
48
- function flopImage() {
49
- return (image) => image.flop();
50
- }
51
- function insertText({ text, font, x, y, backgroundColor = insertTextDefaultOptions.backgroundColor, anchor = insertTextDefaultOptions.anchor, stroke, rotation = insertTextDefaultOptions.rotation }) {
52
- return async (image) => {
53
- const imageMetadata = await image.metadata();
54
- const width = imageMetadata.width;
55
- const height = imageMetadata.height;
56
- const canvas = createCanvas(width, height);
57
- const context = canvas.getContext("2d");
58
- if (backgroundColor !== "transparent") {
59
- context.fillStyle = backgroundColor;
60
- context.fillRect(0, 0, width, height);
61
- }
62
- if (font.filePath) {
63
- registerFont(font.filePath, { family: font.name });
64
- }
65
- context.font = `${font.size}px ${font.name ?? insertTextDefaultOptions.font.name}`;
66
- context.fillStyle = font.color ?? colors.black;
67
- const textMetrics = context.measureText(text);
68
- const textWidth = textMetrics.width;
69
- const textHeight = font.size;
70
- const anchorOffsets = {
71
- "top-left": {
72
- x: 0,
73
- y: 0
74
- },
75
- "top-center": {
76
- x: -textWidth / 2,
77
- y: 0
78
- },
79
- "top-right": {
80
- x: -textWidth,
81
- y: 0
82
- },
83
- "middle-left": {
84
- x: 0,
85
- y: -textHeight / 2
86
- },
87
- "middle-center": {
88
- x: -textWidth / 2,
89
- y: -textHeight / 2
90
- },
91
- "middle-right": {
92
- x: -textWidth,
93
- y: -textHeight / 2
94
- },
95
- "bottom-left": {
96
- x: 0,
97
- y: -textHeight
98
- },
99
- "bottom-center": {
100
- x: -textWidth / 2,
101
- y: -textHeight
102
- },
103
- "bottom-right": {
104
- x: -textWidth,
105
- y: -textHeight
106
- }
107
- };
108
- const { x: offsetX, y: offsetY } = anchorOffsets[anchor] || { x: 0, y: 0 };
109
- const adjustedX = x + offsetX;
110
- const adjustedY = y + offsetY;
111
- const adjustedRotation = rotation ?? 0;
112
- context.save();
113
- context.translate(adjustedX, adjustedY);
114
- context.rotate((adjustedRotation * Math.PI) / 180);
115
- if (stroke) {
116
- context.strokeStyle = stroke.fill;
117
- context.lineWidth = stroke.width;
118
- context.lineJoin = "round";
119
- context.strokeText(text, 0, 0);
120
- }
121
- context.fillStyle = font.color ?? colors.black;
122
- context.fillText(text, 0, 0);
123
- context.restore();
124
- const textBuffer = canvas.toBuffer();
125
- return image.composite([
126
- {
127
- input: textBuffer,
128
- top: 0,
129
- left: 0
130
- }
131
- ]);
132
- };
133
- }
134
- function insertCircle({ x, y, radius, fill }) {
135
- return async (image) => {
136
- const imageMetadata = await image.metadata();
137
- const width = imageMetadata.width;
138
- const height = imageMetadata.height;
139
- const canvas = createCanvas(width, height);
140
- const context = canvas.getContext("2d");
141
- context.beginPath();
142
- context.arc(x, y, radius, 0, Math.PI * 2, true);
143
- context.closePath();
144
- context.fillStyle = fill;
145
- context.fill();
146
- const circleBuffer = canvas.toBuffer();
147
- return image.composite([
148
- {
149
- input: circleBuffer,
150
- top: 0,
151
- left: 0
152
- }
153
- ]);
154
- };
155
- }
156
- function insertRectangle({ x, y, width, height, fill, borderRadius }) {
157
- return async (image) => {
158
- const imageMetadata = await image.metadata();
159
- const canvasWidth = imageMetadata.width;
160
- const canvasHeight = imageMetadata.height;
161
- const canvas = createCanvas(canvasWidth, canvasHeight);
162
- const context = canvas.getContext("2d");
163
- context.fillStyle = fill;
164
- if (borderRadius && borderRadius > 0) {
165
- context.beginPath();
166
- context.moveTo(x + borderRadius, y);
167
- context.lineTo(x + width - borderRadius, y);
168
- context.arcTo(x + width, y, x + width, y + height, borderRadius);
169
- context.lineTo(x + width, y + height - borderRadius);
170
- context.arcTo(x + width, y + height, x, y + height, borderRadius);
171
- context.lineTo(x + borderRadius, y + height);
172
- context.arcTo(x, y + height, x, y, borderRadius);
173
- context.lineTo(x, y + borderRadius);
174
- context.arcTo(x, y, x + width, y, borderRadius);
175
- context.closePath();
176
- context.fill();
177
- }
178
- else {
179
- context.fillRect(x, y, width, height);
180
- }
181
- const rectangleBuffer = canvas.toBuffer();
182
- return image.composite([
183
- {
184
- input: rectangleBuffer,
185
- top: 0,
186
- left: 0
187
- }
188
- ]);
189
- };
190
- }
191
- function insertLine({ x1, y1, x2, y2, color, width }) {
192
- return async (image) => {
193
- const imageMetadata = await image.metadata();
194
- const canvasWidth = imageMetadata.width;
195
- const canvasHeight = imageMetadata.height;
196
- const canvas = createCanvas(canvasWidth, canvasHeight);
197
- const context = canvas.getContext("2d");
198
- context.strokeStyle = color;
199
- context.lineWidth = width;
200
- context.beginPath();
201
- context.moveTo(x1, y1);
202
- context.lineTo(x2, y2);
203
- context.stroke();
204
- const lineBuffer = canvas.toBuffer();
205
- return image.composite([
206
- {
207
- input: lineBuffer,
208
- top: 0,
209
- left: 0
210
- }
211
- ]);
212
- };
213
- }
214
- export { toGrayScale, cropImage, resizeImage, rotateImage, blurImage, modulateSaturation, modulateBrightness, invertColors, addBorder, adjustContrast, modulateOpacity, flipImage, flopImage, insertText, insertCircle, insertRectangle, insertLine };
@@ -1,20 +0,0 @@
1
- import { rgba } from "./utils/color.util";
2
- const colors = {
3
- black: "#000000",
4
- white: "#FFFFFF"
5
- };
6
- const defautltTemplateAcitonType = "beforeLayersProccess";
7
- const createImageDefaultOptions = {
8
- chnannels: 4,
9
- fill: rgba(0, 0, 0, 0),
10
- format: "png"
11
- };
12
- const insertTextDefaultOptions = {
13
- font: {
14
- name: "sans-serif",
15
- },
16
- anchor: "top-left",
17
- backgroundColor: "transparent",
18
- rotation: 0
19
- };
20
- export { colors, defautltTemplateAcitonType, createImageDefaultOptions, insertTextDefaultOptions };