@gjsify/canvas2d 0.4.0 → 0.4.4
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.
- package/package.json +53 -49
- package/src/cairo-utils.ts +0 -243
- package/src/canvas-gradient.ts +0 -2
- package/src/canvas-path.ts +0 -2
- package/src/canvas-pattern.ts +0 -2
- package/src/canvas-rendering-context-2d.ts +0 -2
- package/src/canvas-state.ts +0 -77
- package/src/canvas2d-bridge.ts +0 -196
- package/src/color.ts +0 -125
- package/src/image-data.ts +0 -3
- package/src/index.spec.ts +0 -888
- package/src/index.ts +0 -42
- package/src/test.mts +0 -6
- package/tmp/.tsbuildinfo +0 -1
- package/tsconfig.json +0 -47
package/package.json
CHANGED
|
@@ -1,51 +1,55 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
2
|
+
"name": "@gjsify/canvas2d",
|
|
3
|
+
"version": "0.4.4",
|
|
4
|
+
"description": "Canvas 2D rendering context for GJS, backed by Cairo",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"module": "lib/esm/index.js",
|
|
7
|
+
"types": "lib/types/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/types/index.d.ts",
|
|
11
|
+
"default": "./lib/esm/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"lib"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"clear": "rm -rf lib tmp tsconfig.tsbuildinfo test.gjs.mjs || exit 0",
|
|
19
|
+
"check": "tsc --noEmit",
|
|
20
|
+
"build": "gjsify run build:gjsify && gjsify run build:types",
|
|
21
|
+
"build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
|
|
22
|
+
"build:types": "tsc",
|
|
23
|
+
"build:test": "gjsify run build:test:gjs",
|
|
24
|
+
"build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
|
|
25
|
+
"test": "gjsify run build:gjsify && gjsify run build:test && gjsify run test:gjs",
|
|
26
|
+
"test:gjs": "gjsify run test.gjs.mjs"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"gjs",
|
|
30
|
+
"canvas",
|
|
31
|
+
"canvas2d",
|
|
32
|
+
"cairo"
|
|
33
|
+
],
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@girs/gdk-4.0": "4.0.0-4.0.0-rc.15",
|
|
36
|
+
"@girs/gdkpixbuf-2.0": "2.0.0-4.0.0-rc.15",
|
|
37
|
+
"@girs/gjs": "4.0.0-rc.15",
|
|
38
|
+
"@girs/glib-2.0": "2.88.0-4.0.0-rc.15",
|
|
39
|
+
"@girs/gobject-2.0": "2.88.0-4.0.0-rc.15",
|
|
40
|
+
"@girs/gtk-4.0": "4.23.0-4.0.0-rc.15",
|
|
41
|
+
"@girs/pango-1.0": "1.57.1-4.0.0-rc.15",
|
|
42
|
+
"@girs/pangocairo-1.0": "1.0.0-4.0.0-rc.15",
|
|
43
|
+
"@gjsify/canvas2d-core": "^0.4.4",
|
|
44
|
+
"@gjsify/dom-elements": "^0.4.4",
|
|
45
|
+
"@gjsify/dom-events": "^0.4.4",
|
|
46
|
+
"@gjsify/event-bridge": "^0.4.4",
|
|
47
|
+
"@gjsify/utils": "^0.4.4"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@gjsify/cli": "^0.4.4",
|
|
51
|
+
"@gjsify/unit": "^0.4.4",
|
|
52
|
+
"@types/node": "^25.6.2",
|
|
53
|
+
"typescript": "^6.0.3"
|
|
12
54
|
}
|
|
13
|
-
|
|
14
|
-
"scripts": {
|
|
15
|
-
"clear": "rm -rf lib tmp tsconfig.tsbuildinfo test.gjs.mjs || exit 0",
|
|
16
|
-
"check": "tsc --noEmit",
|
|
17
|
-
"build": "yarn build:gjsify && yarn build:types",
|
|
18
|
-
"build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
|
|
19
|
-
"build:types": "tsc",
|
|
20
|
-
"build:test": "yarn build:test:gjs",
|
|
21
|
-
"build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
|
|
22
|
-
"test": "yarn build:gjsify && yarn build:test && yarn test:gjs",
|
|
23
|
-
"test:gjs": "gjsify run test.gjs.mjs"
|
|
24
|
-
},
|
|
25
|
-
"keywords": [
|
|
26
|
-
"gjs",
|
|
27
|
-
"canvas",
|
|
28
|
-
"canvas2d",
|
|
29
|
-
"cairo"
|
|
30
|
-
],
|
|
31
|
-
"dependencies": {
|
|
32
|
-
"@girs/gdk-4.0": "4.0.0-4.0.0-rc.15",
|
|
33
|
-
"@girs/gdkpixbuf-2.0": "2.0.0-4.0.0-rc.15",
|
|
34
|
-
"@girs/gjs": "4.0.0-rc.15",
|
|
35
|
-
"@girs/glib-2.0": "2.88.0-4.0.0-rc.15",
|
|
36
|
-
"@girs/gobject-2.0": "2.88.0-4.0.0-rc.15",
|
|
37
|
-
"@girs/gtk-4.0": "4.23.0-4.0.0-rc.15",
|
|
38
|
-
"@girs/pango-1.0": "1.57.1-4.0.0-rc.15",
|
|
39
|
-
"@girs/pangocairo-1.0": "1.0.0-4.0.0-rc.15",
|
|
40
|
-
"@gjsify/canvas2d-core": "^0.4.0",
|
|
41
|
-
"@gjsify/dom-elements": "^0.4.0",
|
|
42
|
-
"@gjsify/event-bridge": "^0.4.0",
|
|
43
|
-
"@gjsify/utils": "^0.4.0"
|
|
44
|
-
},
|
|
45
|
-
"devDependencies": {
|
|
46
|
-
"@gjsify/cli": "^0.4.0",
|
|
47
|
-
"@gjsify/unit": "^0.4.0",
|
|
48
|
-
"@types/node": "^25.6.2",
|
|
49
|
-
"typescript": "^6.0.3"
|
|
50
|
-
}
|
|
51
|
-
}
|
|
55
|
+
}
|
package/src/cairo-utils.ts
DELETED
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
// Cairo utility helpers for Canvas 2D context
|
|
2
|
-
// Handles format conversions and path operations not directly available in Cairo.
|
|
3
|
-
|
|
4
|
-
import type Cairo from 'cairo';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Convert quadratic Bezier control point to cubic Bezier control points.
|
|
8
|
-
* Canvas 2D has quadraticCurveTo but Cairo only has cubic curveTo.
|
|
9
|
-
*
|
|
10
|
-
* Given current point (cx, cy), quadratic control point (cpx, cpy), and end (x, y):
|
|
11
|
-
* cp1 = current + 2/3 * (cp - current)
|
|
12
|
-
* cp2 = end + 2/3 * (cp - end)
|
|
13
|
-
*/
|
|
14
|
-
export function quadraticToCubic(
|
|
15
|
-
cx: number, cy: number,
|
|
16
|
-
cpx: number, cpy: number,
|
|
17
|
-
x: number, y: number,
|
|
18
|
-
): { cp1x: number; cp1y: number; cp2x: number; cp2y: number } {
|
|
19
|
-
return {
|
|
20
|
-
cp1x: cx + (2 / 3) * (cpx - cx),
|
|
21
|
-
cp1y: cy + (2 / 3) * (cpy - cy),
|
|
22
|
-
cp2x: x + (2 / 3) * (cpx - x),
|
|
23
|
-
cp2y: y + (2 / 3) * (cpy - y),
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Compute arcTo parameters.
|
|
29
|
-
* Canvas arcTo(x1,y1,x2,y2,radius) draws a line from current point to the tangent point,
|
|
30
|
-
* then an arc of the given radius tangent to both lines (current→p1 and p1→p2).
|
|
31
|
-
*
|
|
32
|
-
* Returns the two tangent points and arc center, or null if degenerate (collinear points).
|
|
33
|
-
*/
|
|
34
|
-
export function computeArcTo(
|
|
35
|
-
x0: number, y0: number,
|
|
36
|
-
x1: number, y1: number,
|
|
37
|
-
x2: number, y2: number,
|
|
38
|
-
radius: number,
|
|
39
|
-
): { tx0: number; ty0: number; tx1: number; ty1: number; cx: number; cy: number; startAngle: number; endAngle: number; counterclockwise: boolean } | null {
|
|
40
|
-
// Direction vectors
|
|
41
|
-
const dx0 = x0 - x1;
|
|
42
|
-
const dy0 = y0 - y1;
|
|
43
|
-
const dx1 = x2 - x1;
|
|
44
|
-
const dy1 = y2 - y1;
|
|
45
|
-
|
|
46
|
-
// Lengths
|
|
47
|
-
const len0 = Math.sqrt(dx0 * dx0 + dy0 * dy0);
|
|
48
|
-
const len1 = Math.sqrt(dx1 * dx1 + dy1 * dy1);
|
|
49
|
-
|
|
50
|
-
if (len0 === 0 || len1 === 0) return null;
|
|
51
|
-
|
|
52
|
-
// Normalize
|
|
53
|
-
const ux0 = dx0 / len0;
|
|
54
|
-
const uy0 = dy0 / len0;
|
|
55
|
-
const ux1 = dx1 / len1;
|
|
56
|
-
const uy1 = dy1 / len1;
|
|
57
|
-
|
|
58
|
-
// Cross product to determine direction
|
|
59
|
-
const cross = ux0 * uy1 - uy0 * ux1;
|
|
60
|
-
if (Math.abs(cross) < 1e-10) return null; // Collinear
|
|
61
|
-
|
|
62
|
-
// Half-angle between the two direction vectors
|
|
63
|
-
const dot = ux0 * ux1 + uy0 * uy1;
|
|
64
|
-
const halfAngle = Math.acos(Math.max(-1, Math.min(1, dot))) / 2;
|
|
65
|
-
|
|
66
|
-
// Distance from p1 to tangent point
|
|
67
|
-
const tanDist = radius / Math.tan(halfAngle);
|
|
68
|
-
|
|
69
|
-
// Tangent points
|
|
70
|
-
const tx0 = x1 + ux0 * tanDist;
|
|
71
|
-
const ty0 = y1 + uy0 * tanDist;
|
|
72
|
-
const tx1 = x1 + ux1 * tanDist;
|
|
73
|
-
const ty1 = y1 + uy1 * tanDist;
|
|
74
|
-
|
|
75
|
-
// Center of the arc circle: perpendicular to the bisector, distance = radius / sin(halfAngle)
|
|
76
|
-
const centerDist = radius / Math.sin(halfAngle);
|
|
77
|
-
const bisectX = (ux0 + ux1) / 2;
|
|
78
|
-
const bisectY = (uy0 + uy1) / 2;
|
|
79
|
-
const bisectLen = Math.sqrt(bisectX * bisectX + bisectY * bisectY);
|
|
80
|
-
const cx = x1 + (bisectX / bisectLen) * centerDist;
|
|
81
|
-
const cy = y1 + (bisectY / bisectLen) * centerDist;
|
|
82
|
-
|
|
83
|
-
// Start and end angles
|
|
84
|
-
const startAngle = Math.atan2(ty0 - cy, tx0 - cx);
|
|
85
|
-
const endAngle = Math.atan2(ty1 - cy, tx1 - cx);
|
|
86
|
-
|
|
87
|
-
// Counterclockwise if cross product is positive
|
|
88
|
-
const counterclockwise = cross > 0;
|
|
89
|
-
|
|
90
|
-
return { tx0, ty0, tx1, ty1, cx, cy, startAngle, endAngle, counterclockwise };
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Apply an arcTo operation to a Cairo context.
|
|
95
|
-
*/
|
|
96
|
-
export function cairoArcTo(
|
|
97
|
-
ctx: Cairo.Context,
|
|
98
|
-
x0: number, y0: number,
|
|
99
|
-
x1: number, y1: number,
|
|
100
|
-
x2: number, y2: number,
|
|
101
|
-
radius: number,
|
|
102
|
-
): void {
|
|
103
|
-
const result = computeArcTo(x0, y0, x1, y1, x2, y2, radius);
|
|
104
|
-
if (!result) {
|
|
105
|
-
// Degenerate: just draw a line to (x1, y1)
|
|
106
|
-
ctx.lineTo(x1, y1);
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const { tx0, ty0, cx, cy, startAngle, endAngle, counterclockwise } = result;
|
|
111
|
-
|
|
112
|
-
// Line from current point to first tangent point
|
|
113
|
-
ctx.lineTo(tx0, ty0);
|
|
114
|
-
|
|
115
|
-
// Arc
|
|
116
|
-
if (counterclockwise) {
|
|
117
|
-
ctx.arcNegative(cx, cy, radius, startAngle, endAngle);
|
|
118
|
-
} else {
|
|
119
|
-
ctx.arc(cx, cy, radius, startAngle, endAngle);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Draw an ellipse on a Cairo context.
|
|
125
|
-
* Canvas ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise)
|
|
126
|
-
* is implemented via save/translate/rotate/scale/arc/restore.
|
|
127
|
-
*/
|
|
128
|
-
export function cairoEllipse(
|
|
129
|
-
ctx: Cairo.Context,
|
|
130
|
-
x: number, y: number,
|
|
131
|
-
radiusX: number, radiusY: number,
|
|
132
|
-
rotation: number,
|
|
133
|
-
startAngle: number, endAngle: number,
|
|
134
|
-
counterclockwise: boolean,
|
|
135
|
-
): void {
|
|
136
|
-
ctx.save();
|
|
137
|
-
ctx.translate(x, y);
|
|
138
|
-
ctx.rotate(rotation);
|
|
139
|
-
ctx.scale(radiusX, radiusY);
|
|
140
|
-
|
|
141
|
-
if (counterclockwise) {
|
|
142
|
-
ctx.arcNegative(0, 0, 1, startAngle, endAngle);
|
|
143
|
-
} else {
|
|
144
|
-
ctx.arc(0, 0, 1, startAngle, endAngle);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
ctx.restore();
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Draw a rounded rectangle path on a Cairo context.
|
|
152
|
-
* Implements the Canvas roundRect(x, y, w, h, radii) method.
|
|
153
|
-
*/
|
|
154
|
-
export function cairoRoundRect(
|
|
155
|
-
ctx: Cairo.Context,
|
|
156
|
-
x: number, y: number,
|
|
157
|
-
w: number, h: number,
|
|
158
|
-
radii: number | number[],
|
|
159
|
-
): void {
|
|
160
|
-
// Normalize radii to [topLeft, topRight, bottomRight, bottomLeft]
|
|
161
|
-
let tl: number, tr: number, br: number, bl: number;
|
|
162
|
-
if (typeof radii === 'number') {
|
|
163
|
-
tl = tr = br = bl = radii;
|
|
164
|
-
} else if (radii.length === 1) {
|
|
165
|
-
tl = tr = br = bl = radii[0];
|
|
166
|
-
} else if (radii.length === 2) {
|
|
167
|
-
tl = br = radii[0];
|
|
168
|
-
tr = bl = radii[1];
|
|
169
|
-
} else if (radii.length === 3) {
|
|
170
|
-
tl = radii[0];
|
|
171
|
-
tr = bl = radii[1];
|
|
172
|
-
br = radii[2];
|
|
173
|
-
} else {
|
|
174
|
-
tl = radii[0];
|
|
175
|
-
tr = radii[1];
|
|
176
|
-
br = radii[2];
|
|
177
|
-
bl = radii[3];
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Clamp radii so they don't exceed half the width/height
|
|
181
|
-
const maxR = Math.min(w / 2, h / 2);
|
|
182
|
-
tl = Math.min(tl, maxR);
|
|
183
|
-
tr = Math.min(tr, maxR);
|
|
184
|
-
br = Math.min(br, maxR);
|
|
185
|
-
bl = Math.min(bl, maxR);
|
|
186
|
-
|
|
187
|
-
const PI_2 = Math.PI / 2;
|
|
188
|
-
|
|
189
|
-
ctx.newSubPath();
|
|
190
|
-
// Top-left corner
|
|
191
|
-
ctx.arc(x + tl, y + tl, tl, Math.PI, Math.PI + PI_2);
|
|
192
|
-
// Top-right corner
|
|
193
|
-
ctx.arc(x + w - tr, y + tr, tr, -PI_2, 0);
|
|
194
|
-
// Bottom-right corner
|
|
195
|
-
ctx.arc(x + w - br, y + h - br, br, 0, PI_2);
|
|
196
|
-
// Bottom-left corner
|
|
197
|
-
ctx.arc(x + bl, y + h - bl, bl, PI_2, Math.PI);
|
|
198
|
-
ctx.closePath();
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/** Map Canvas globalCompositeOperation to Cairo.Operator values */
|
|
202
|
-
export const COMPOSITE_OP_MAP: Record<string, number> = {
|
|
203
|
-
'source-over': 2, // OVER
|
|
204
|
-
'source-in': 5, // IN
|
|
205
|
-
'source-out': 6, // OUT
|
|
206
|
-
'source-atop': 7, // ATOP
|
|
207
|
-
'destination-over': 8, // DEST_OVER
|
|
208
|
-
'destination-in': 9, // DEST_IN
|
|
209
|
-
'destination-out': 10, // DEST_OUT
|
|
210
|
-
'destination-atop': 11,// DEST_ATOP
|
|
211
|
-
'lighter': 12, // ADD
|
|
212
|
-
'copy': 1, // SOURCE
|
|
213
|
-
'xor': 13, // XOR
|
|
214
|
-
'multiply': 14, // MULTIPLY
|
|
215
|
-
'screen': 15, // SCREEN
|
|
216
|
-
'overlay': 16, // OVERLAY
|
|
217
|
-
'darken': 17, // DARKEN
|
|
218
|
-
'lighten': 18, // LIGHTEN
|
|
219
|
-
'color-dodge': 19, // COLOR_DODGE
|
|
220
|
-
'color-burn': 20, // COLOR_BURN
|
|
221
|
-
'hard-light': 21, // HARD_LIGHT
|
|
222
|
-
'soft-light': 22, // SOFT_LIGHT
|
|
223
|
-
'difference': 23, // DIFFERENCE
|
|
224
|
-
'exclusion': 24, // EXCLUSION
|
|
225
|
-
'hue': 25, // HSL_HUE
|
|
226
|
-
'saturation': 26, // HSL_SATURATION
|
|
227
|
-
'color': 27, // HSL_COLOR
|
|
228
|
-
'luminosity': 28, // HSL_LUMINOSITY
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
/** Map Canvas lineCap to Cairo.LineCap values */
|
|
232
|
-
export const LINE_CAP_MAP: Record<string, number> = {
|
|
233
|
-
'butt': 0,
|
|
234
|
-
'round': 1,
|
|
235
|
-
'square': 2,
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
/** Map Canvas lineJoin to Cairo.LineJoin values */
|
|
239
|
-
export const LINE_JOIN_MAP: Record<string, number> = {
|
|
240
|
-
'miter': 0,
|
|
241
|
-
'round': 1,
|
|
242
|
-
'bevel': 2,
|
|
243
|
-
};
|
package/src/canvas-gradient.ts
DELETED
package/src/canvas-path.ts
DELETED
package/src/canvas-pattern.ts
DELETED
package/src/canvas-state.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
// Drawing state for Canvas 2D context save()/restore() stack.
|
|
2
|
-
// Each save() pushes a clone of the current state; restore() pops it.
|
|
3
|
-
|
|
4
|
-
import type { RGBA } from './color.js';
|
|
5
|
-
import { BLACK } from './color.js';
|
|
6
|
-
|
|
7
|
-
export interface CanvasState {
|
|
8
|
-
// Fill & stroke
|
|
9
|
-
fillStyle: string | CanvasGradient | CanvasPattern;
|
|
10
|
-
fillColor: RGBA;
|
|
11
|
-
strokeStyle: string | CanvasGradient | CanvasPattern;
|
|
12
|
-
strokeColor: RGBA;
|
|
13
|
-
|
|
14
|
-
// Line properties
|
|
15
|
-
lineWidth: number;
|
|
16
|
-
lineCap: CanvasLineCap;
|
|
17
|
-
lineJoin: CanvasLineJoin;
|
|
18
|
-
miterLimit: number;
|
|
19
|
-
lineDash: number[];
|
|
20
|
-
lineDashOffset: number;
|
|
21
|
-
|
|
22
|
-
// Compositing
|
|
23
|
-
globalAlpha: number;
|
|
24
|
-
globalCompositeOperation: GlobalCompositeOperation;
|
|
25
|
-
|
|
26
|
-
// Shadows (Phase 5 — tracked in state for save/restore correctness)
|
|
27
|
-
shadowColor: string;
|
|
28
|
-
shadowBlur: number;
|
|
29
|
-
shadowOffsetX: number;
|
|
30
|
-
shadowOffsetY: number;
|
|
31
|
-
|
|
32
|
-
// Text (Phase 4)
|
|
33
|
-
font: string;
|
|
34
|
-
textAlign: CanvasTextAlign;
|
|
35
|
-
textBaseline: CanvasTextBaseline;
|
|
36
|
-
direction: CanvasDirection;
|
|
37
|
-
|
|
38
|
-
// Image smoothing
|
|
39
|
-
imageSmoothingEnabled: boolean;
|
|
40
|
-
imageSmoothingQuality: ImageSmoothingQuality;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function createDefaultState(): CanvasState {
|
|
44
|
-
return {
|
|
45
|
-
fillStyle: '#000000',
|
|
46
|
-
fillColor: { ...BLACK },
|
|
47
|
-
strokeStyle: '#000000',
|
|
48
|
-
strokeColor: { ...BLACK },
|
|
49
|
-
lineWidth: 1,
|
|
50
|
-
lineCap: 'butt',
|
|
51
|
-
lineJoin: 'miter',
|
|
52
|
-
miterLimit: 10,
|
|
53
|
-
lineDash: [],
|
|
54
|
-
lineDashOffset: 0,
|
|
55
|
-
globalAlpha: 1,
|
|
56
|
-
globalCompositeOperation: 'source-over',
|
|
57
|
-
shadowColor: 'rgba(0, 0, 0, 0)',
|
|
58
|
-
shadowBlur: 0,
|
|
59
|
-
shadowOffsetX: 0,
|
|
60
|
-
shadowOffsetY: 0,
|
|
61
|
-
font: '10px sans-serif',
|
|
62
|
-
textAlign: 'start',
|
|
63
|
-
textBaseline: 'alphabetic',
|
|
64
|
-
direction: 'ltr',
|
|
65
|
-
imageSmoothingEnabled: true,
|
|
66
|
-
imageSmoothingQuality: 'low',
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function cloneState(state: CanvasState): CanvasState {
|
|
71
|
-
return {
|
|
72
|
-
...state,
|
|
73
|
-
fillColor: { ...state.fillColor },
|
|
74
|
-
strokeColor: { ...state.strokeColor },
|
|
75
|
-
lineDash: [...state.lineDash],
|
|
76
|
-
};
|
|
77
|
-
}
|
package/src/canvas2d-bridge.ts
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
// Canvas2DBridge — GTK container for HTMLCanvasElement (2D) backed by Cairo.
|
|
2
|
-
// Provides a Gtk.DrawingArea subclass that handles Canvas 2D bootstrapping.
|
|
3
|
-
// Pattern follows packages/dom/iframe/src/iframe-bridge.ts (IFrameBridge)
|
|
4
|
-
|
|
5
|
-
import GObject from 'gi://GObject';
|
|
6
|
-
import Gdk from 'gi://Gdk?version=4.0';
|
|
7
|
-
import GLib from 'gi://GLib?version=2.0';
|
|
8
|
-
import Gtk from 'gi://Gtk?version=4.0';
|
|
9
|
-
import { HTMLCanvasElement as GjsifyHTMLCanvasElement } from '@gjsify/dom-elements';
|
|
10
|
-
import { attachEventControllers } from '@gjsify/event-bridge';
|
|
11
|
-
import { CanvasRenderingContext2D } from '@gjsify/canvas2d-core';
|
|
12
|
-
import { Event } from '@gjsify/dom-events';
|
|
13
|
-
|
|
14
|
-
type Canvas2DReadyCallback = (canvas: globalThis.HTMLCanvasElement, ctx: CanvasRenderingContext2D) => void;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* A `Gtk.DrawingArea` subclass that handles Canvas 2D bootstrapping:
|
|
18
|
-
* - Creates an `HTMLCanvasElement` + `CanvasRenderingContext2D` on first draw
|
|
19
|
-
* - Blits the Canvas 2D Cairo.ImageSurface onto the DrawingArea each frame
|
|
20
|
-
* - Fires `onReady()` callbacks with (canvas, ctx) once the context is available
|
|
21
|
-
* - Provides `requestAnimationFrame()` backed by GTK frame clock (vsync)
|
|
22
|
-
* - `installGlobals()` sets `globalThis.requestAnimationFrame` and `globalThis.performance`
|
|
23
|
-
*
|
|
24
|
-
* Usage:
|
|
25
|
-
* ```ts
|
|
26
|
-
* const widget = new Canvas2DBridge();
|
|
27
|
-
* widget.installGlobals(); // sets globalThis.requestAnimationFrame
|
|
28
|
-
* widget.onReady((canvas, ctx) => {
|
|
29
|
-
* ctx.fillStyle = 'red';
|
|
30
|
-
* ctx.fillRect(0, 0, 100, 100);
|
|
31
|
-
* });
|
|
32
|
-
* window.set_child(widget);
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
// Gtk.DrawingArea inherits a 'resize' signal with signature
|
|
36
|
-
// (widget: Drawable, width: int, height: int) from its ancestors (Gtk.Widget).
|
|
37
|
-
// WebGLBridge uses Gtk.GLArea which has the same-shaped signal. We do
|
|
38
|
-
// NOT register a custom signal — we piggyback on the inherited one so
|
|
39
|
-
// consumers can use a single pattern on both widgets:
|
|
40
|
-
// widget.connect('resize', (w, width, height) => { ... })
|
|
41
|
-
// In addition, _onDraw fires onResize(cb) callbacks and dispatches a DOM
|
|
42
|
-
// 'resize' event on the canvas for browser-style listeners.
|
|
43
|
-
|
|
44
|
-
export const Canvas2DBridge = GObject.registerClass(
|
|
45
|
-
{ GTypeName: 'GjsifyCanvas2DBridge' },
|
|
46
|
-
class Canvas2DBridge extends Gtk.DrawingArea {
|
|
47
|
-
_canvas: GjsifyHTMLCanvasElement | null = null;
|
|
48
|
-
_ctx: CanvasRenderingContext2D | null = null;
|
|
49
|
-
_readyCallbacks: Canvas2DReadyCallback[] = [];
|
|
50
|
-
_resizeCallbacks: ((w: number, h: number) => void)[] = [];
|
|
51
|
-
_tickCallbackId: number | null = null;
|
|
52
|
-
_frameCallback: FrameRequestCallback | null = null;
|
|
53
|
-
// Time origin in microseconds (GLib monotonic clock).
|
|
54
|
-
// Both requestAnimationFrame timestamps and performance.now() are
|
|
55
|
-
// relative to this origin, matching the browser DOMHighResTimeStamp spec.
|
|
56
|
-
_timeOrigin: number = GLib.get_monotonic_time();
|
|
57
|
-
|
|
58
|
-
constructor(params?: Partial<Gtk.DrawingArea.ConstructorProps>) {
|
|
59
|
-
super(params);
|
|
60
|
-
this.set_draw_func(this._onDraw.bind(this));
|
|
61
|
-
|
|
62
|
-
// Bridge GTK events → DOM events on the canvas element
|
|
63
|
-
// captureKeys: true prevents GTK focus traversal consuming arrow/Enter keys
|
|
64
|
-
attachEventControllers(this, () => this._canvas, { captureKeys: true });
|
|
65
|
-
|
|
66
|
-
this.connect('unrealize', () => {
|
|
67
|
-
if (this._tickCallbackId !== null) {
|
|
68
|
-
this.remove_tick_callback(this._tickCallbackId);
|
|
69
|
-
this._tickCallbackId = null;
|
|
70
|
-
}
|
|
71
|
-
if (this._ctx) {
|
|
72
|
-
this._ctx._dispose();
|
|
73
|
-
}
|
|
74
|
-
this._canvas = null;
|
|
75
|
-
this._ctx = null;
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** @internal Draw function called by GTK. Blits the Cairo surface to screen. */
|
|
80
|
-
_onDraw(_area: Gtk.DrawingArea, cr: any, width: number, height: number): void {
|
|
81
|
-
// Lazy init: create canvas + 2D context on first draw.
|
|
82
|
-
// We do NOT sync dimensions after onReady fires: ready callbacks may set their
|
|
83
|
-
// own canvas dimensions (e.g. a static render at a fixed size). Syncing here
|
|
84
|
-
// would clear the surface they just drew into.
|
|
85
|
-
if (!this._canvas) {
|
|
86
|
-
this._canvas = new GjsifyHTMLCanvasElement();
|
|
87
|
-
this._canvas.width = width;
|
|
88
|
-
this._canvas.height = height;
|
|
89
|
-
// Import side-effect registers the '2d' factory, so getContext('2d') works
|
|
90
|
-
this._ctx = this._canvas.getContext('2d') as unknown as CanvasRenderingContext2D;
|
|
91
|
-
if (this._ctx) {
|
|
92
|
-
for (const cb of this._readyCallbacks) {
|
|
93
|
-
cb(this._canvas as unknown as globalThis.HTMLCanvasElement, this._ctx);
|
|
94
|
-
}
|
|
95
|
-
this._readyCallbacks = [];
|
|
96
|
-
}
|
|
97
|
-
} else if (this._canvas.width !== width || this._canvas.height !== height) {
|
|
98
|
-
// Subsequent draw: GTK widget was resized — sync canvas and notify listeners.
|
|
99
|
-
// NOTE: Gtk.DrawingArea itself emits the 'resize' GObject signal
|
|
100
|
-
// (inherited) when the allocation changes, so consumers can use
|
|
101
|
-
// widget.connect('resize', ...) without us emitting it here.
|
|
102
|
-
// We additionally dispatch a DOM 'resize' event on the canvas
|
|
103
|
-
// and fire onResize() callbacks for cross-widget API parity
|
|
104
|
-
// with WebGLBridge.
|
|
105
|
-
this._canvas.width = width;
|
|
106
|
-
this._canvas.height = height;
|
|
107
|
-
this._canvas.dispatchEvent(new Event('resize'));
|
|
108
|
-
for (const cb of this._resizeCallbacks) {
|
|
109
|
-
cb(width, height);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// Blit the Canvas 2D's Cairo.ImageSurface onto the DrawingArea
|
|
114
|
-
if (this._ctx) {
|
|
115
|
-
const surface = this._ctx._getSurface();
|
|
116
|
-
cr.setSourceSurface(surface, 0, 0);
|
|
117
|
-
cr.paint();
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** The HTMLCanvasElement backing this widget. Available after the first draw. */
|
|
122
|
-
get canvas(): globalThis.HTMLCanvasElement | null {
|
|
123
|
-
return this._canvas as unknown as globalThis.HTMLCanvasElement | null;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/** Get the 2D rendering context. Available after the first draw. */
|
|
127
|
-
getContext(_id: '2d'): CanvasRenderingContext2D | null {
|
|
128
|
-
return this._ctx;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Register a callback to be invoked once the Canvas 2D context is ready.
|
|
133
|
-
* If the context is already available, the callback fires synchronously.
|
|
134
|
-
*/
|
|
135
|
-
onReady(cb: Canvas2DReadyCallback): void {
|
|
136
|
-
if (this._canvas && this._ctx) {
|
|
137
|
-
cb(this._canvas as unknown as globalThis.HTMLCanvasElement, this._ctx);
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
this._readyCallbacks.push(cb);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Register a callback invoked whenever the GTK widget is resized.
|
|
145
|
-
* Canvas dimensions are already updated when the callback fires.
|
|
146
|
-
* Call `queue_draw()` after re-rendering to push the new surface to screen.
|
|
147
|
-
*/
|
|
148
|
-
onResize(cb: (width: number, height: number) => void): void {
|
|
149
|
-
this._resizeCallbacks.push(cb);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Schedules a single animation frame callback, matching the browser `requestAnimationFrame` API.
|
|
154
|
-
* Backed by GTK frame clock (vsync-synced, typically ~60 FPS).
|
|
155
|
-
* Returns 0 (handle — cancel not yet implemented).
|
|
156
|
-
*/
|
|
157
|
-
requestAnimationFrame(cb: FrameRequestCallback): number {
|
|
158
|
-
this._frameCallback = cb;
|
|
159
|
-
if (this._tickCallbackId === null) {
|
|
160
|
-
this._tickCallbackId = this.add_tick_callback((_widget: Gtk.Widget, frameClock: Gdk.FrameClock) => {
|
|
161
|
-
this._tickCallbackId = null;
|
|
162
|
-
// DOMHighResTimeStamp: ms since time origin, matching performance.now()
|
|
163
|
-
const time = (frameClock.get_frame_time() - this._timeOrigin) / 1000;
|
|
164
|
-
this._frameCallback?.(time);
|
|
165
|
-
this.queue_draw();
|
|
166
|
-
return GLib.SOURCE_REMOVE;
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
// Ensure GTK schedules a new frame so the tick callback fires.
|
|
170
|
-
// Without this, requestAnimationFrame called during the paint phase
|
|
171
|
-
// (e.g. from onReady) may not trigger the frame clock to tick again.
|
|
172
|
-
this.queue_draw();
|
|
173
|
-
return 0;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Sets browser globals (`requestAnimationFrame`, `performance`) so that
|
|
178
|
-
* browser-targeted code works unchanged on GJS.
|
|
179
|
-
*/
|
|
180
|
-
installGlobals(): void {
|
|
181
|
-
(globalThis as any).requestAnimationFrame = (cb: FrameRequestCallback) =>
|
|
182
|
-
this.requestAnimationFrame(cb);
|
|
183
|
-
// Install performance.now() on the same time origin as rAF timestamps.
|
|
184
|
-
// Always override to ensure consistency — native GJS performance.now()
|
|
185
|
-
// may use a different time origin than the frame clock.
|
|
186
|
-
const timeOrigin = this._timeOrigin;
|
|
187
|
-
(globalThis as any).performance = {
|
|
188
|
-
now: () => (GLib.get_monotonic_time() - timeOrigin) / 1000,
|
|
189
|
-
timeOrigin: Date.now(),
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
);
|
|
194
|
-
|
|
195
|
-
// Export the instance type so callers can type-annotate their Canvas2DBridge variables
|
|
196
|
-
export type Canvas2DBridge = InstanceType<typeof Canvas2DBridge>;
|