@gjsify/canvas2d-core 0.3.16 → 0.3.17
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/lib/esm/cairo-utils.js +1 -191
- package/lib/esm/canvas-gradient.js +1 -28
- package/lib/esm/canvas-path.js +1 -161
- package/lib/esm/canvas-pattern.js +1 -63
- package/lib/esm/canvas-rendering-context-2d.js +1 -975
- package/lib/esm/canvas-state.js +1 -40
- package/lib/esm/color.js +1 -272
- package/lib/esm/image-data.js +1 -25
- package/lib/esm/index.js +1 -8
- package/package.json +11 -11
- package/tmp/.tsbuildinfo +1 -1
package/lib/esm/cairo-utils.js
CHANGED
|
@@ -1,191 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Convert quadratic Bezier control point to cubic Bezier control points.
|
|
4
|
-
* Canvas 2D has quadraticCurveTo but Cairo only has cubic curveTo.
|
|
5
|
-
*
|
|
6
|
-
* Given current point (cx, cy), quadratic control point (cpx, cpy), and end (x, y):
|
|
7
|
-
* cp1 = current + 2/3 * (cp - current)
|
|
8
|
-
* cp2 = end + 2/3 * (cp - end)
|
|
9
|
-
*/
|
|
10
|
-
function quadraticToCubic(cx, cy, cpx, cpy, x, y) {
|
|
11
|
-
return {
|
|
12
|
-
cp1x: cx + 2 / 3 * (cpx - cx),
|
|
13
|
-
cp1y: cy + 2 / 3 * (cpy - cy),
|
|
14
|
-
cp2x: x + 2 / 3 * (cpx - x),
|
|
15
|
-
cp2y: y + 2 / 3 * (cpy - y)
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Compute arcTo parameters.
|
|
20
|
-
* Canvas arcTo(x1,y1,x2,y2,radius) draws a line from current point to the tangent point,
|
|
21
|
-
* then an arc of the given radius tangent to both lines (current→p1 and p1→p2).
|
|
22
|
-
*
|
|
23
|
-
* Returns the two tangent points and arc center, or null if degenerate (collinear points).
|
|
24
|
-
*/
|
|
25
|
-
function computeArcTo(x0, y0, x1, y1, x2, y2, radius) {
|
|
26
|
-
const dx0 = x0 - x1;
|
|
27
|
-
const dy0 = y0 - y1;
|
|
28
|
-
const dx1 = x2 - x1;
|
|
29
|
-
const dy1 = y2 - y1;
|
|
30
|
-
const len0 = Math.sqrt(dx0 * dx0 + dy0 * dy0);
|
|
31
|
-
const len1 = Math.sqrt(dx1 * dx1 + dy1 * dy1);
|
|
32
|
-
if (len0 === 0 || len1 === 0) return null;
|
|
33
|
-
const ux0 = dx0 / len0;
|
|
34
|
-
const uy0 = dy0 / len0;
|
|
35
|
-
const ux1 = dx1 / len1;
|
|
36
|
-
const uy1 = dy1 / len1;
|
|
37
|
-
const cross = ux0 * uy1 - uy0 * ux1;
|
|
38
|
-
if (Math.abs(cross) < 1e-10) return null;
|
|
39
|
-
const dot = ux0 * ux1 + uy0 * uy1;
|
|
40
|
-
const halfAngle = Math.acos(Math.max(-1, Math.min(1, dot))) / 2;
|
|
41
|
-
const tanDist = radius / Math.tan(halfAngle);
|
|
42
|
-
const tx0 = x1 + ux0 * tanDist;
|
|
43
|
-
const ty0 = y1 + uy0 * tanDist;
|
|
44
|
-
const tx1 = x1 + ux1 * tanDist;
|
|
45
|
-
const ty1 = y1 + uy1 * tanDist;
|
|
46
|
-
const centerDist = radius / Math.sin(halfAngle);
|
|
47
|
-
const bisectX = (ux0 + ux1) / 2;
|
|
48
|
-
const bisectY = (uy0 + uy1) / 2;
|
|
49
|
-
const bisectLen = Math.sqrt(bisectX * bisectX + bisectY * bisectY);
|
|
50
|
-
const cx = x1 + bisectX / bisectLen * centerDist;
|
|
51
|
-
const cy = y1 + bisectY / bisectLen * centerDist;
|
|
52
|
-
const startAngle = Math.atan2(ty0 - cy, tx0 - cx);
|
|
53
|
-
const endAngle = Math.atan2(ty1 - cy, tx1 - cx);
|
|
54
|
-
const counterclockwise = cross > 0;
|
|
55
|
-
return {
|
|
56
|
-
tx0,
|
|
57
|
-
ty0,
|
|
58
|
-
tx1,
|
|
59
|
-
ty1,
|
|
60
|
-
cx,
|
|
61
|
-
cy,
|
|
62
|
-
startAngle,
|
|
63
|
-
endAngle,
|
|
64
|
-
counterclockwise
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Apply an arcTo operation to a Cairo context.
|
|
69
|
-
*/
|
|
70
|
-
function cairoArcTo(ctx, x0, y0, x1, y1, x2, y2, radius) {
|
|
71
|
-
const result = computeArcTo(x0, y0, x1, y1, x2, y2, radius);
|
|
72
|
-
if (!result) {
|
|
73
|
-
ctx.lineTo(x1, y1);
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
const { tx0, ty0, cx, cy, startAngle, endAngle, counterclockwise } = result;
|
|
77
|
-
ctx.lineTo(tx0, ty0);
|
|
78
|
-
if (counterclockwise) {
|
|
79
|
-
ctx.arcNegative(cx, cy, radius, startAngle, endAngle);
|
|
80
|
-
} else {
|
|
81
|
-
ctx.arc(cx, cy, radius, startAngle, endAngle);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Draw an ellipse on a Cairo context.
|
|
86
|
-
* Canvas ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise)
|
|
87
|
-
* is implemented via save/translate/rotate/scale/arc/restore.
|
|
88
|
-
*/
|
|
89
|
-
function cairoEllipse(ctx, x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) {
|
|
90
|
-
ctx.save();
|
|
91
|
-
ctx.translate(x, y);
|
|
92
|
-
ctx.rotate(rotation);
|
|
93
|
-
ctx.scale(radiusX, radiusY);
|
|
94
|
-
if (counterclockwise) {
|
|
95
|
-
ctx.arcNegative(0, 0, 1, startAngle, endAngle);
|
|
96
|
-
} else {
|
|
97
|
-
ctx.arc(0, 0, 1, startAngle, endAngle);
|
|
98
|
-
}
|
|
99
|
-
ctx.restore();
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Draw a rounded rectangle path on a Cairo context.
|
|
103
|
-
* Implements the Canvas roundRect(x, y, w, h, radii) method.
|
|
104
|
-
*/
|
|
105
|
-
function cairoRoundRect(ctx, x, y, w, h, radii) {
|
|
106
|
-
let tl, tr, br, bl;
|
|
107
|
-
if (typeof radii === "number") {
|
|
108
|
-
tl = tr = br = bl = radii;
|
|
109
|
-
} else if (radii.length === 1) {
|
|
110
|
-
tl = tr = br = bl = radii[0];
|
|
111
|
-
} else if (radii.length === 2) {
|
|
112
|
-
tl = br = radii[0];
|
|
113
|
-
tr = bl = radii[1];
|
|
114
|
-
} else if (radii.length === 3) {
|
|
115
|
-
tl = radii[0];
|
|
116
|
-
tr = bl = radii[1];
|
|
117
|
-
br = radii[2];
|
|
118
|
-
} else {
|
|
119
|
-
tl = radii[0];
|
|
120
|
-
tr = radii[1];
|
|
121
|
-
br = radii[2];
|
|
122
|
-
bl = radii[3];
|
|
123
|
-
}
|
|
124
|
-
const maxR = Math.min(w / 2, h / 2);
|
|
125
|
-
tl = Math.min(tl, maxR);
|
|
126
|
-
tr = Math.min(tr, maxR);
|
|
127
|
-
br = Math.min(br, maxR);
|
|
128
|
-
bl = Math.min(bl, maxR);
|
|
129
|
-
const PI_2 = Math.PI / 2;
|
|
130
|
-
ctx.newSubPath();
|
|
131
|
-
ctx.arc(x + tl, y + tl, tl, Math.PI, Math.PI + PI_2);
|
|
132
|
-
ctx.arc(x + w - tr, y + tr, tr, -PI_2, 0);
|
|
133
|
-
ctx.arc(x + w - br, y + h - br, br, 0, PI_2);
|
|
134
|
-
ctx.arc(x + bl, y + h - bl, bl, PI_2, Math.PI);
|
|
135
|
-
ctx.closePath();
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Map Canvas globalCompositeOperation to Cairo.Operator values.
|
|
139
|
-
*
|
|
140
|
-
* Cairo.Operator enum (verified runtime in GJS 1.86):
|
|
141
|
-
* CLEAR=0, SOURCE=1, OVER=2, IN=3, OUT=4, ATOP=5,
|
|
142
|
-
* DEST=6, DEST_OVER=7, DEST_IN=8, DEST_OUT=9, DEST_ATOP=10,
|
|
143
|
-
* XOR=11, ADD=12, SATURATE=13,
|
|
144
|
-
* MULTIPLY=14, SCREEN=15, OVERLAY=16, DARKEN=17, LIGHTEN=18,
|
|
145
|
-
* COLOR_DODGE=19, COLOR_BURN=20, HARD_LIGHT=21, SOFT_LIGHT=22,
|
|
146
|
-
* DIFFERENCE=23, EXCLUSION=24, HSL_HUE=25, HSL_SATURATION=26,
|
|
147
|
-
* HSL_COLOR=27, HSL_LUMINOSITY=28
|
|
148
|
-
*/
|
|
149
|
-
const COMPOSITE_OP_MAP = {
|
|
150
|
-
"source-over": 2,
|
|
151
|
-
"source-in": 3,
|
|
152
|
-
"source-out": 4,
|
|
153
|
-
"source-atop": 5,
|
|
154
|
-
"destination-over": 7,
|
|
155
|
-
"destination-in": 8,
|
|
156
|
-
"destination-out": 9,
|
|
157
|
-
"destination-atop": 10,
|
|
158
|
-
"lighter": 12,
|
|
159
|
-
"copy": 1,
|
|
160
|
-
"xor": 11,
|
|
161
|
-
"multiply": 14,
|
|
162
|
-
"screen": 15,
|
|
163
|
-
"overlay": 16,
|
|
164
|
-
"darken": 17,
|
|
165
|
-
"lighten": 18,
|
|
166
|
-
"color-dodge": 19,
|
|
167
|
-
"color-burn": 20,
|
|
168
|
-
"hard-light": 21,
|
|
169
|
-
"soft-light": 22,
|
|
170
|
-
"difference": 23,
|
|
171
|
-
"exclusion": 24,
|
|
172
|
-
"hue": 25,
|
|
173
|
-
"saturation": 26,
|
|
174
|
-
"color": 27,
|
|
175
|
-
"luminosity": 28
|
|
176
|
-
};
|
|
177
|
-
/** Map Canvas lineCap to Cairo.LineCap values */
|
|
178
|
-
const LINE_CAP_MAP = {
|
|
179
|
-
"butt": 0,
|
|
180
|
-
"round": 1,
|
|
181
|
-
"square": 2
|
|
182
|
-
};
|
|
183
|
-
/** Map Canvas lineJoin to Cairo.LineJoin values */
|
|
184
|
-
const LINE_JOIN_MAP = {
|
|
185
|
-
"miter": 0,
|
|
186
|
-
"round": 1,
|
|
187
|
-
"bevel": 2
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
//#endregion
|
|
191
|
-
export { COMPOSITE_OP_MAP, LINE_CAP_MAP, LINE_JOIN_MAP, cairoArcTo, cairoEllipse, cairoRoundRect, computeArcTo, quadraticToCubic };
|
|
1
|
+
function e(e,t,n,r,i,a){return{cp1x:e+2/3*(n-e),cp1y:t+2/3*(r-t),cp2x:i+2/3*(n-i),cp2y:a+2/3*(r-a)}}function t(e,t,n,r,i,a,o){let s=e-n,c=t-r,l=i-n,u=a-r,d=Math.sqrt(s*s+c*c),f=Math.sqrt(l*l+u*u);if(d===0||f===0)return null;let p=s/d,m=c/d,h=l/f,g=u/f,_=p*g-m*h;if(Math.abs(_)<1e-10)return null;let v=p*h+m*g,y=Math.acos(Math.max(-1,Math.min(1,v)))/2,b=o/Math.tan(y),x=n+p*b,S=r+m*b,C=n+h*b,w=r+g*b,T=o/Math.sin(y),E=(p+h)/2,D=(m+g)/2,O=Math.sqrt(E*E+D*D),k=n+E/O*T,A=r+D/O*T;return{tx0:x,ty0:S,tx1:C,ty1:w,cx:k,cy:A,startAngle:Math.atan2(S-A,x-k),endAngle:Math.atan2(w-A,C-k),counterclockwise:_>0}}function n(e,n,r,i,a,o,s,c){let l=t(n,r,i,a,o,s,c);if(!l){e.lineTo(i,a);return}let{tx0:u,ty0:d,cx:f,cy:p,startAngle:m,endAngle:h,counterclockwise:g}=l;e.lineTo(u,d),g?e.arcNegative(f,p,c,m,h):e.arc(f,p,c,m,h)}function r(e,t,n,r,i,a,o,s,c){e.save(),e.translate(t,n),e.rotate(a),e.scale(r,i),c?e.arcNegative(0,0,1,o,s):e.arc(0,0,1,o,s),e.restore()}function i(e,t,n,r,i,a){let o,s,c,l;typeof a==`number`?o=s=c=l=a:a.length===1?o=s=c=l=a[0]:a.length===2?(o=c=a[0],s=l=a[1]):a.length===3?(o=a[0],s=l=a[1],c=a[2]):(o=a[0],s=a[1],c=a[2],l=a[3]);let u=Math.min(r/2,i/2);o=Math.min(o,u),s=Math.min(s,u),c=Math.min(c,u),l=Math.min(l,u);let d=Math.PI/2;e.newSubPath(),e.arc(t+o,n+o,o,Math.PI,Math.PI+d),e.arc(t+r-s,n+s,s,-d,0),e.arc(t+r-c,n+i-c,c,0,d),e.arc(t+l,n+i-l,l,d,Math.PI),e.closePath()}const a={"source-over":2,"source-in":3,"source-out":4,"source-atop":5,"destination-over":7,"destination-in":8,"destination-out":9,"destination-atop":10,lighter:12,copy:1,xor:11,multiply:14,screen:15,overlay:16,darken:17,lighten:18,"color-dodge":19,"color-burn":20,"hard-light":21,"soft-light":22,difference:23,exclusion:24,hue:25,saturation:26,color:27,luminosity:28},o={butt:0,round:1,square:2},s={miter:0,round:1,bevel:2};export{a as COMPOSITE_OP_MAP,o as LINE_CAP_MAP,s as LINE_JOIN_MAP,n as cairoArcTo,r as cairoEllipse,i as cairoRoundRect,t as computeArcTo,e as quadraticToCubic};
|
|
@@ -1,28 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import Cairo from "cairo";
|
|
3
|
-
|
|
4
|
-
//#region src/canvas-gradient.ts
|
|
5
|
-
/**
|
|
6
|
-
* CanvasGradient wrapping a Cairo LinearGradient or RadialGradient.
|
|
7
|
-
*/
|
|
8
|
-
var CanvasGradient = class {
|
|
9
|
-
constructor(type, x0, y0, x1, y1, r0, r1) {
|
|
10
|
-
if (type === "radial") {
|
|
11
|
-
this._pattern = new Cairo.RadialGradient(x0, y0, r0, x1, y1, r1);
|
|
12
|
-
} else {
|
|
13
|
-
this._pattern = new Cairo.LinearGradient(x0, y0, x1, y1);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
addColorStop(offset, color) {
|
|
17
|
-
const parsed = parseColor(color);
|
|
18
|
-
if (!parsed) return;
|
|
19
|
-
this._pattern.addColorStopRGBA(offset, parsed.r, parsed.g, parsed.b, parsed.a);
|
|
20
|
-
}
|
|
21
|
-
/** @internal Get the underlying Cairo pattern for rendering. */
|
|
22
|
-
_getCairoPattern() {
|
|
23
|
-
return this._pattern;
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
//#endregion
|
|
28
|
-
export { CanvasGradient };
|
|
1
|
+
import{parseColor as e}from"./color.js";import t from"cairo";var n=class{constructor(e,n,r,i,a,o,s){e===`radial`?this._pattern=new t.RadialGradient(n,r,o,i,a,s):this._pattern=new t.LinearGradient(n,r,i,a)}addColorStop(t,n){let r=e(n);r&&this._pattern.addColorStopRGBA(t,r.r,r.g,r.b,r.a)}_getCairoPattern(){return this._pattern}};export{n as CanvasGradient};
|
package/lib/esm/canvas-path.js
CHANGED
|
@@ -1,161 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
//#region src/canvas-path.ts
|
|
4
|
-
/**
|
|
5
|
-
* Path2D records path operations for later replay on a CanvasRenderingContext2D.
|
|
6
|
-
*/
|
|
7
|
-
var Path2D = class Path2D {
|
|
8
|
-
constructor(pathOrSvg) {
|
|
9
|
-
this._ops = [];
|
|
10
|
-
if (pathOrSvg instanceof Path2D) {
|
|
11
|
-
this._ops = [...pathOrSvg._ops];
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
addPath(path) {
|
|
15
|
-
this._ops.push(...path._ops);
|
|
16
|
-
}
|
|
17
|
-
moveTo(x, y) {
|
|
18
|
-
this._ops.push({
|
|
19
|
-
type: "moveTo",
|
|
20
|
-
x,
|
|
21
|
-
y
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
lineTo(x, y) {
|
|
25
|
-
this._ops.push({
|
|
26
|
-
type: "lineTo",
|
|
27
|
-
x,
|
|
28
|
-
y
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
closePath() {
|
|
32
|
-
this._ops.push({ type: "closePath" });
|
|
33
|
-
}
|
|
34
|
-
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
|
|
35
|
-
this._ops.push({
|
|
36
|
-
type: "bezierCurveTo",
|
|
37
|
-
cp1x,
|
|
38
|
-
cp1y,
|
|
39
|
-
cp2x,
|
|
40
|
-
cp2y,
|
|
41
|
-
x,
|
|
42
|
-
y
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
quadraticCurveTo(cpx, cpy, x, y) {
|
|
46
|
-
this._ops.push({
|
|
47
|
-
type: "quadraticCurveTo",
|
|
48
|
-
cpx,
|
|
49
|
-
cpy,
|
|
50
|
-
x,
|
|
51
|
-
y
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
arc(x, y, radius, startAngle, endAngle, counterclockwise = false) {
|
|
55
|
-
this._ops.push({
|
|
56
|
-
type: "arc",
|
|
57
|
-
x,
|
|
58
|
-
y,
|
|
59
|
-
radius,
|
|
60
|
-
startAngle,
|
|
61
|
-
endAngle,
|
|
62
|
-
ccw: counterclockwise
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise = false) {
|
|
66
|
-
if (radiusX < 0 || radiusY < 0) throw new RangeError("The radii provided are negative");
|
|
67
|
-
this._ops.push({
|
|
68
|
-
type: "ellipse",
|
|
69
|
-
x,
|
|
70
|
-
y,
|
|
71
|
-
rx: radiusX,
|
|
72
|
-
ry: radiusY,
|
|
73
|
-
rotation,
|
|
74
|
-
startAngle,
|
|
75
|
-
endAngle,
|
|
76
|
-
ccw: counterclockwise
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
rect(x, y, w, h) {
|
|
80
|
-
this._ops.push({
|
|
81
|
-
type: "rect",
|
|
82
|
-
x,
|
|
83
|
-
y,
|
|
84
|
-
w,
|
|
85
|
-
h
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
roundRect(x, y, w, h, radii = 0) {
|
|
89
|
-
this._ops.push({
|
|
90
|
-
type: "roundRect",
|
|
91
|
-
x,
|
|
92
|
-
y,
|
|
93
|
-
w,
|
|
94
|
-
h,
|
|
95
|
-
radii
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* @internal Replay all recorded path operations onto a Cairo context.
|
|
100
|
-
*/
|
|
101
|
-
_replayOnCairo(ctx) {
|
|
102
|
-
let lastX = 0, lastY = 0;
|
|
103
|
-
for (const op of this._ops) {
|
|
104
|
-
switch (op.type) {
|
|
105
|
-
case "moveTo":
|
|
106
|
-
ctx.moveTo(op.x, op.y);
|
|
107
|
-
lastX = op.x;
|
|
108
|
-
lastY = op.y;
|
|
109
|
-
break;
|
|
110
|
-
case "lineTo":
|
|
111
|
-
ctx.lineTo(op.x, op.y);
|
|
112
|
-
lastX = op.x;
|
|
113
|
-
lastY = op.y;
|
|
114
|
-
break;
|
|
115
|
-
case "closePath":
|
|
116
|
-
ctx.closePath();
|
|
117
|
-
break;
|
|
118
|
-
case "bezierCurveTo":
|
|
119
|
-
ctx.curveTo(op.cp1x, op.cp1y, op.cp2x, op.cp2y, op.x, op.y);
|
|
120
|
-
lastX = op.x;
|
|
121
|
-
lastY = op.y;
|
|
122
|
-
break;
|
|
123
|
-
case "quadraticCurveTo": {
|
|
124
|
-
const { cp1x, cp1y, cp2x, cp2y } = quadraticToCubic(lastX, lastY, op.cpx, op.cpy, op.x, op.y);
|
|
125
|
-
ctx.curveTo(cp1x, cp1y, cp2x, cp2y, op.x, op.y);
|
|
126
|
-
lastX = op.x;
|
|
127
|
-
lastY = op.y;
|
|
128
|
-
break;
|
|
129
|
-
}
|
|
130
|
-
case "arc":
|
|
131
|
-
if (op.ccw) {
|
|
132
|
-
ctx.arcNegative(op.x, op.y, op.radius, op.startAngle, op.endAngle);
|
|
133
|
-
} else {
|
|
134
|
-
ctx.arc(op.x, op.y, op.radius, op.startAngle, op.endAngle);
|
|
135
|
-
}
|
|
136
|
-
break;
|
|
137
|
-
case "ellipse":
|
|
138
|
-
ctx.save();
|
|
139
|
-
ctx.translate(op.x, op.y);
|
|
140
|
-
ctx.rotate(op.rotation);
|
|
141
|
-
ctx.scale(op.rx, op.ry);
|
|
142
|
-
if (op.ccw) {
|
|
143
|
-
ctx.arcNegative(0, 0, 1, op.startAngle, op.endAngle);
|
|
144
|
-
} else {
|
|
145
|
-
ctx.arc(0, 0, 1, op.startAngle, op.endAngle);
|
|
146
|
-
}
|
|
147
|
-
ctx.restore();
|
|
148
|
-
break;
|
|
149
|
-
case "rect":
|
|
150
|
-
ctx.rectangle(op.x, op.y, op.w, op.h);
|
|
151
|
-
break;
|
|
152
|
-
case "roundRect":
|
|
153
|
-
cairoRoundRect(ctx, op.x, op.y, op.w, op.h, op.radii);
|
|
154
|
-
break;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
//#endregion
|
|
161
|
-
export { Path2D };
|
|
1
|
+
import{cairoRoundRect as e,quadraticToCubic as t}from"./cairo-utils.js";var n=class n{constructor(e){this._ops=[],e instanceof n&&(this._ops=[...e._ops])}addPath(e){this._ops.push(...e._ops)}moveTo(e,t){this._ops.push({type:`moveTo`,x:e,y:t})}lineTo(e,t){this._ops.push({type:`lineTo`,x:e,y:t})}closePath(){this._ops.push({type:`closePath`})}bezierCurveTo(e,t,n,r,i,a){this._ops.push({type:`bezierCurveTo`,cp1x:e,cp1y:t,cp2x:n,cp2y:r,x:i,y:a})}quadraticCurveTo(e,t,n,r){this._ops.push({type:`quadraticCurveTo`,cpx:e,cpy:t,x:n,y:r})}arc(e,t,n,r,i,a=!1){this._ops.push({type:`arc`,x:e,y:t,radius:n,startAngle:r,endAngle:i,ccw:a})}ellipse(e,t,n,r,i,a,o,s=!1){if(n<0||r<0)throw RangeError(`The radii provided are negative`);this._ops.push({type:`ellipse`,x:e,y:t,rx:n,ry:r,rotation:i,startAngle:a,endAngle:o,ccw:s})}rect(e,t,n,r){this._ops.push({type:`rect`,x:e,y:t,w:n,h:r})}roundRect(e,t,n,r,i=0){this._ops.push({type:`roundRect`,x:e,y:t,w:n,h:r,radii:i})}_replayOnCairo(n){let r=0,i=0;for(let a of this._ops)switch(a.type){case`moveTo`:n.moveTo(a.x,a.y),r=a.x,i=a.y;break;case`lineTo`:n.lineTo(a.x,a.y),r=a.x,i=a.y;break;case`closePath`:n.closePath();break;case`bezierCurveTo`:n.curveTo(a.cp1x,a.cp1y,a.cp2x,a.cp2y,a.x,a.y),r=a.x,i=a.y;break;case`quadraticCurveTo`:{let{cp1x:e,cp1y:o,cp2x:s,cp2y:c}=t(r,i,a.cpx,a.cpy,a.x,a.y);n.curveTo(e,o,s,c,a.x,a.y),r=a.x,i=a.y;break}case`arc`:a.ccw?n.arcNegative(a.x,a.y,a.radius,a.startAngle,a.endAngle):n.arc(a.x,a.y,a.radius,a.startAngle,a.endAngle);break;case`ellipse`:n.save(),n.translate(a.x,a.y),n.rotate(a.rotation),n.scale(a.rx,a.ry),a.ccw?n.arcNegative(0,0,1,a.startAngle,a.endAngle):n.arc(0,0,1,a.startAngle,a.endAngle),n.restore();break;case`rect`:n.rectangle(a.x,a.y,a.w,a.h);break;case`roundRect`:e(n,a.x,a.y,a.w,a.h,a.radii);break}}};export{n as Path2D};
|
|
@@ -1,63 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import Gdk from "gi://Gdk?version=4.0";
|
|
3
|
-
|
|
4
|
-
//#region src/canvas-pattern.ts
|
|
5
|
-
/**
|
|
6
|
-
* CanvasPattern wrapping a Cairo SurfacePattern.
|
|
7
|
-
*/
|
|
8
|
-
var CanvasPattern = class CanvasPattern {
|
|
9
|
-
constructor(surface, repetition) {
|
|
10
|
-
this._pattern = new Cairo.SurfacePattern(surface);
|
|
11
|
-
const pat = this._pattern;
|
|
12
|
-
switch (repetition) {
|
|
13
|
-
case "repeat":
|
|
14
|
-
case "":
|
|
15
|
-
case null:
|
|
16
|
-
pat.setExtend(Cairo.Extend.REPEAT);
|
|
17
|
-
break;
|
|
18
|
-
case "repeat-x":
|
|
19
|
-
case "repeat-y":
|
|
20
|
-
pat.setExtend(Cairo.Extend.REPEAT);
|
|
21
|
-
break;
|
|
22
|
-
case "no-repeat":
|
|
23
|
-
pat.setExtend(Cairo.Extend.NONE);
|
|
24
|
-
break;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
/** Create a CanvasPattern from a supported image source. Returns null if unsupported. */
|
|
28
|
-
static create(image, repetition) {
|
|
29
|
-
if ("isPixbuf" in image && typeof image.isPixbuf === "function" && image.isPixbuf()) {
|
|
30
|
-
const pixbuf = image._pixbuf;
|
|
31
|
-
const w = pixbuf.get_width();
|
|
32
|
-
const h = pixbuf.get_height();
|
|
33
|
-
const surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, w, h);
|
|
34
|
-
const ctx = new Cairo.Context(surface);
|
|
35
|
-
Gdk.cairo_set_source_pixbuf(ctx, pixbuf, 0, 0);
|
|
36
|
-
ctx.paint();
|
|
37
|
-
ctx.$dispose();
|
|
38
|
-
return new CanvasPattern(surface, repetition);
|
|
39
|
-
}
|
|
40
|
-
if (typeof image?.getContext === "function") {
|
|
41
|
-
const ctx2d = image.getContext("2d");
|
|
42
|
-
if (ctx2d && typeof ctx2d._getSurface === "function") {
|
|
43
|
-
const sourceSurface = ctx2d._getSurface();
|
|
44
|
-
const w = sourceSurface.getWidth();
|
|
45
|
-
const h = sourceSurface.getHeight();
|
|
46
|
-
const surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, w, h);
|
|
47
|
-
const ctx = new Cairo.Context(surface);
|
|
48
|
-
ctx.setSourceSurface(sourceSurface, 0, 0);
|
|
49
|
-
ctx.paint();
|
|
50
|
-
ctx.$dispose();
|
|
51
|
-
return new CanvasPattern(surface, repetition);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return null;
|
|
55
|
-
}
|
|
56
|
-
/** @internal Get the underlying Cairo pattern for rendering. */
|
|
57
|
-
_getCairoPattern() {
|
|
58
|
-
return this._pattern;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
//#endregion
|
|
63
|
-
export { CanvasPattern };
|
|
1
|
+
import e from"cairo";import t from"gi://Gdk?version=4.0";var n=class n{constructor(t,n){this._pattern=new e.SurfacePattern(t);let r=this._pattern;switch(n){case`repeat`:case``:case null:r.setExtend(e.Extend.REPEAT);break;case`repeat-x`:case`repeat-y`:r.setExtend(e.Extend.REPEAT);break;case`no-repeat`:r.setExtend(e.Extend.NONE);break}}static create(r,i){if(`isPixbuf`in r&&typeof r.isPixbuf==`function`&&r.isPixbuf()){let a=r._pixbuf,o=a.get_width(),s=a.get_height(),c=new e.ImageSurface(e.Format.ARGB32,o,s),l=new e.Context(c);return t.cairo_set_source_pixbuf(l,a,0,0),l.paint(),l.$dispose(),new n(c,i)}if(typeof r?.getContext==`function`){let t=r.getContext(`2d`);if(t&&typeof t._getSurface==`function`){let r=t._getSurface(),a=r.getWidth(),o=r.getHeight(),s=new e.ImageSurface(e.Format.ARGB32,a,o),c=new e.Context(s);return c.setSourceSurface(r,0,0),c.paint(),c.$dispose(),new n(s,i)}}return null}_getCairoPattern(){return this._pattern}};export{n as CanvasPattern};
|