-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworklet.js
144 lines (131 loc) · 6.08 KB
/
worklet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Represents the read-only values of the output bitmap's width and height.
* @typedef {Object} PaintSize
* @property {number} width - width
* @property {number} height - height
*/
/**
* Represents the read-only values of the output bitmap's width and height.
* @typedef {Object} PaintRenderingContext2DSettings
* @property {boolean} alpha - Defines if alpha transparency is allowed. Alpha is set to true by default. If set to false, all colors used on the canvas will be fully opaque.
*/
registerPaint(
"my_paint",
class {
/**
* Gets the context options for the paint.
* @static
* @returns {PaintRenderingContext2DSettings} The context options.
*/
static get contextOptions() {
return { alpha: true };
}
/**
* Gets the input properties for the paint.
* @static
* @returns {Array.<string>} The input properties.
* @description
* Retrieves a list of supported CSS properties and custom properties required for rendering the custom paint.
* If custom properties are not included in this list, they will not be accessible inside the `paint()` function.
*
* @example
* // Example 1: Only native CSS properties
* // Returns: ["color", "font-size", "background-color", "border-width"]
*
* // Example 2: Only custom CSS properties
* // Returns: ["--custom-background-color", "--custom-text-color", "--custom-border-radius"]
*
* // Example 3: Mix of native and custom CSS properties
* // Returns: ["color", "--custom-background-color", "font-size", "--custom-text-color", "border-width", "--custom-border-radius"]
*
*/
static get inputProperties() {
return ["--signspaces-items", "--highlight-color", "--words-color"];
}
/**
* Gets the input arguments for the paint.
* @static
* @returns {Array.<string>} The input arguments.
* @description
* Retrieves a list of additional arguments or inputs required for rendering the custom paint function. These arguments are not necessarily related to CSS properties and can include dynamic data or configuration options.
* @example
* // Example: Additional arguments for rendering a custom pattern
* // Returns: ["patternType", "patternSize"]
*/
static get inputArguments() {
return ["patternType", "patternSize"]; // Not working. In theory we can access to this values as 4th param of pant(_, _, _, args). So, args[0] === patternType, args[1] === patternSize
}
/**
* Paints the sign spaces on the canvas.
* @param {CanvasRenderingContext2D} ctx - ctx is the 2D drawing context a subset of the HTML Canvas API
* @param {PaintSize} size - paintSize: width and height
* @param {StylePropertyMapReadOnly} styleMap - properties: get() method
* @param {Array} args - Additional arguments passed to the paint function.
*/
paint(ctx, size, styleMap, args) {
const highlightColor = styleMap.get("--highlight-color").toString().trim();
// const signspacesItems = styleMap.get("--signspaces-items");
const words_color = styleMap.get("--words-color");
/** @type {[number, number, number, number][] | undefined} */
let items;
/** @type {[string, string][] | undefined} */
let colors_word;
try {
// items = JSON.parse(signspacesItems);
} catch (error) {
}
try {
colors_word = JSON.parse(words_color);
} catch (error) {
}
// if (!items) return;
if (!highlightColor) return;
if (!Object.keys(colors_word).length) return
// console.log('colors_word', colors_word);
Object.entries(colors_word).map(([color, list_of_coordinates_outer]) => {
for (const list_of_coordinates_inner of list_of_coordinates_outer) {
for (const item of list_of_coordinates_inner) {
let [item_x, item_y, item_width, item_height] = item;
ctx.fillStyle = color;
this.roundRect(ctx, item_x, item_y, item_width, item_height, 5);
}
}
})
// for (let item of items) {
// let [item_x, item_y, item_width, item_height, item_color] = item;
// ctx.fillStyle = highlightColor;
// this.roundRect(ctx, item_x, item_y, item_width, item_height, 5);
// }
// for (let item of items) {
// let [item_x, item_y, item_width, item_height, item_color] = item;
// ctx.fillStyle = highlightColor;
// this.roundRect(ctx, item_x, item_y, item_width, item_height, 5);
// }
}
/**
* Draws a rounded rectangle on the canvas context.
* @param {CanvasRenderingContext2D} ctx - The canvas rendering context.
* @param {number} x - The x-coordinate of the rectangle's starting point.
* @param {number} y - The y-coordinate of the rectangle's starting point.
* @param {number} width - The width of the rectangle.
* @param {number} height - The height of the rectangle.
* @param {number} radius - The corner radius of the rectangle.
*/
roundRect(ctx, x, y, width, height, radius) {
if (width < 2 * radius) {
radius = width / 2;
}
if (height < 2 * radius) {
radius = height / 2;
}
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.arcTo(x + width, y, x + width, y + height, radius);
ctx.arcTo(x + width, y + height, x, y + height, radius);
ctx.arcTo(x, y + height, x, y, radius);
ctx.arcTo(x, y, x + width, y, radius);
ctx.closePath();
ctx.fill();
}
}
);