q5 2.14.4 → 2.15.0

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.
@@ -1,202 +0,0 @@
1
- Q5.renderers.q2d = {};
2
-
3
- Q5.renderers.q2d.canvas = ($, q) => {
4
- let c = $.canvas;
5
-
6
- if ($.colorMode) {
7
- $.colorMode(Q5.canvasOptions.colorSpace != 'srgb' ? 'rgb' : 'srgb', 255);
8
- }
9
-
10
- $._createCanvas = function (w, h, options) {
11
- if (!c) {
12
- console.error('q5 canvas could not be created. skia-canvas and jsdom packages not found.');
13
- return;
14
- }
15
- q.ctx = q.drawingContext = c.getContext('2d', options);
16
-
17
- if ($._scope != 'image') {
18
- // default styles
19
- $.ctx.fillStyle = $._fill = 'white';
20
- $.ctx.strokeStyle = $._stroke = 'black';
21
- $.ctx.lineCap = 'round';
22
- $.ctx.lineJoin = 'miter';
23
- $.ctx.textAlign = 'left';
24
- $._strokeWeight = 1;
25
- }
26
- $.ctx.scale($._pixelDensity, $._pixelDensity);
27
- $.ctx.save();
28
- return c;
29
- };
30
-
31
- $.clear = () => {
32
- $.ctx.save();
33
- $.ctx.resetTransform();
34
- $.ctx.clearRect(0, 0, $.canvas.width, $.canvas.height);
35
- $.ctx.restore();
36
- };
37
-
38
- if ($._scope == 'image') return;
39
-
40
- $._resizeCanvas = (w, h) => {
41
- let t = {};
42
- for (let prop in $.ctx) {
43
- if (typeof $.ctx[prop] != 'function') t[prop] = $.ctx[prop];
44
- }
45
- delete t.canvas;
46
-
47
- let o;
48
- if ($.frameCount > 1) {
49
- o = new $._OffscreenCanvas(c.width, c.height);
50
- o.w = c.w;
51
- o.h = c.h;
52
- let oCtx = o.getContext('2d');
53
- oCtx.drawImage(c, 0, 0);
54
- }
55
-
56
- $._setCanvasSize(w, h);
57
-
58
- for (let prop in t) $.ctx[prop] = t[prop];
59
- $.scale($._pixelDensity);
60
-
61
- if (o) $.ctx.drawImage(o, 0, 0, o.w, o.h);
62
- };
63
-
64
- $.fill = function (c) {
65
- $._doFill = $._fillSet = true;
66
- if (Q5.Color) {
67
- if (!c._q5Color) {
68
- if (typeof c != 'string') c = $.color(...arguments);
69
- else if ($._namedColors[c]) c = $.color(...$._namedColors[c]);
70
- }
71
- if (c.a <= 0) return ($._doFill = false);
72
- }
73
- $.ctx.fillStyle = $._fill = c.toString();
74
- };
75
-
76
- $.stroke = function (c) {
77
- $._doStroke = $._strokeSet = true;
78
- if (Q5.Color) {
79
- if (!c._q5Color) {
80
- if (typeof c != 'string') c = $.color(...arguments);
81
- else if ($._namedColors[c]) c = $.color(...$._namedColors[c]);
82
- }
83
- if (c.a <= 0) return ($._doStroke = false);
84
- }
85
- $.ctx.strokeStyle = $._stroke = c.toString();
86
- };
87
-
88
- $.strokeWeight = (n) => {
89
- if (!n) $._doStroke = false;
90
- if ($._da) n *= $._da;
91
- $.ctx.lineWidth = $._strokeWeight = n || 0.0001;
92
- };
93
-
94
- $.noFill = () => ($._doFill = false);
95
- $.noStroke = () => ($._doStroke = false);
96
- $.opacity = (a) => ($.ctx.globalAlpha = a);
97
-
98
- $._doShadow = false;
99
- $._shadowOffsetX = $._shadowOffsetY = $._shadowBlur = 10;
100
-
101
- $.shadow = function (c) {
102
- if (Q5.Color) {
103
- if (!c._q5Color) {
104
- if (typeof c != 'string') c = $.color(...arguments);
105
- else if ($._namedColors[c]) c = $.color(...$._namedColors[c]);
106
- }
107
- }
108
- $.ctx.shadowColor = $._shadow = c.toString();
109
- $._doShadow = true;
110
-
111
- $.ctx.shadowOffsetX ||= $._shadowOffsetX;
112
- $.ctx.shadowOffsetY ||= $._shadowOffsetY;
113
- $.ctx.shadowBlur ||= $._shadowBlur;
114
- };
115
-
116
- $.shadowBox = (offsetX, offsetY, blur) => {
117
- $.ctx.shadowOffsetX = $._shadowOffsetX = offsetX;
118
- $.ctx.shadowOffsetY = $._shadowOffsetY = offsetY || offsetX;
119
- $.ctx.shadowBlur = $._shadowBlur = blur || 0;
120
- };
121
-
122
- $.noShadow = () => {
123
- $._doShadow = false;
124
- $.ctx.shadowOffsetX = $.ctx.shadowOffsetY = $.ctx.shadowBlur = 0;
125
- };
126
-
127
- // DRAWING MATRIX
128
-
129
- $.translate = (x, y) => {
130
- if ($._da) {
131
- x *= $._da;
132
- y *= $._da;
133
- }
134
- $.ctx.translate(x, y);
135
- };
136
-
137
- $.rotate = (r) => {
138
- if ($._angleMode) r = $.radians(r);
139
- $.ctx.rotate(r);
140
- };
141
-
142
- $.scale = (x, y) => {
143
- if (x.x) {
144
- y = x.y;
145
- x = x.x;
146
- }
147
- y ??= x;
148
- $.ctx.scale(x, y);
149
- };
150
-
151
- $.applyMatrix = (a, b, c, d, e, f) => $.ctx.transform(a, b, c, d, e, f);
152
- $.shearX = (ang) => $.ctx.transform(1, 0, $.tan(ang), 1, 0, 0);
153
- $.shearY = (ang) => $.ctx.transform(1, $.tan(ang), 0, 1, 0, 0);
154
-
155
- $.resetMatrix = () => {
156
- if ($.ctx) {
157
- $.ctx.resetTransform();
158
- $.scale($._pixelDensity);
159
- if ($._webgpuFallback) $.translate($.canvas.hw, $.canvas.hh);
160
- }
161
- };
162
-
163
- $.pushMatrix = () => $.ctx.save();
164
- $.popMatrix = () => $.ctx.restore();
165
-
166
- let _popStyles = $.popStyles;
167
-
168
- $.popStyles = () => {
169
- _popStyles();
170
-
171
- $.ctx.fillStyle = $._fill;
172
- $.ctx.strokeStyle = $._stroke;
173
- $.ctx.lineWidth = $._strokeWeight;
174
- $.ctx.shadowColor = $._shadow;
175
- $.ctx.shadowOffsetX = $._doShadow ? $._shadowOffsetX : 0;
176
- $.ctx.shadowOffsetY = $._doShadow ? $._shadowOffsetY : 0;
177
- $.ctx.shadowBlur = $._doShadow ? $._shadowBlur : 0;
178
- };
179
-
180
- $.push = () => {
181
- $.ctx.save();
182
- $.pushStyles();
183
- };
184
- $.pop = () => {
185
- $.ctx.restore();
186
- _popStyles();
187
- };
188
-
189
- $.createCapture = (x) => {
190
- var vid = document.createElement('video');
191
- vid.playsinline = 'playsinline';
192
- vid.autoplay = 'autoplay';
193
- navigator.mediaDevices.getUserMedia(x).then((stream) => {
194
- vid.srcObject = stream;
195
- });
196
- vid.style.position = 'absolute';
197
- vid.style.opacity = 0.00001;
198
- vid.style.zIndex = -1000;
199
- document.body.append(vid);
200
- return vid;
201
- };
202
- };
@@ -1,399 +0,0 @@
1
- Q5.renderers.q2d.drawing = ($) => {
2
- $._doStroke = true;
3
- $._doFill = true;
4
- $._strokeSet = false;
5
- $._fillSet = false;
6
- $._ellipseMode = Q5.CENTER;
7
- $._rectMode = Q5.CORNER;
8
- $._curveDetail = 20;
9
- $._curveAlpha = 0.0;
10
-
11
- let firstVertex = true;
12
- let curveBuff = [];
13
-
14
- function ink() {
15
- if ($._doFill) $.ctx.fill();
16
- if ($._doStroke) $.ctx.stroke();
17
- }
18
-
19
- // DRAWING SETTINGS
20
-
21
- $.blendMode = (x) => ($.ctx.globalCompositeOperation = x);
22
- $.strokeCap = (x) => ($.ctx.lineCap = x);
23
- $.strokeJoin = (x) => ($.ctx.lineJoin = x);
24
- $.ellipseMode = (x) => ($._ellipseMode = x);
25
- $.rectMode = (x) => ($._rectMode = x);
26
- $.curveDetail = (x) => ($._curveDetail = x);
27
- $.curveAlpha = (x) => ($._curveAlpha = x);
28
- $.curveTightness = (x) => ($._curveAlpha = x);
29
-
30
- // DRAWING
31
-
32
- $.background = function (c) {
33
- $.ctx.save();
34
- $.ctx.resetTransform();
35
- $.ctx.globalAlpha = 1;
36
- if (c.canvas) $.image(c, 0, 0, $.canvas.width, $.canvas.height);
37
- else {
38
- if (Q5.Color && !c._q5Color) {
39
- if (typeof c != 'string') c = $.color(...arguments);
40
- else if ($._namedColors[c]) c = $.color(...$._namedColors[c]);
41
- }
42
- $.ctx.fillStyle = c.toString();
43
- $.ctx.fillRect(0, 0, $.canvas.width, $.canvas.height);
44
- }
45
- $.ctx.restore();
46
- };
47
-
48
- $.line = (x0, y0, x1, y1) => {
49
- if ($._doStroke) {
50
- $._da && ((x0 *= $._da), (y0 *= $._da), (x1 *= $._da), (y1 *= $._da));
51
- $.ctx.beginPath();
52
- $.ctx.moveTo(x0, y0);
53
- $.ctx.lineTo(x1, y1);
54
- $.ctx.stroke();
55
- }
56
- };
57
-
58
- const TAU = Math.PI * 2;
59
-
60
- function arc(x, y, w, h, lo, hi, mode) {
61
- if ($._angleMode) {
62
- lo = $.radians(lo);
63
- hi = $.radians(hi);
64
- }
65
- lo %= TAU;
66
- hi %= TAU;
67
- if (lo < 0) lo += TAU;
68
- if (hi < 0) hi += TAU;
69
- if (lo > hi) hi += TAU;
70
- if (lo == hi) return $.ellipse(x, y, w, h);
71
-
72
- w /= 2;
73
- h /= 2;
74
-
75
- if (!$._doFill && mode == $.PIE_OPEN) mode = $.CHORD_OPEN;
76
-
77
- $.ctx.beginPath();
78
- $.ctx.ellipse(x, y, w, h, 0, lo, hi);
79
- if (mode == $.PIE || mode == $.PIE_OPEN) $.ctx.lineTo(x, y);
80
- if ($._doFill) $.ctx.fill();
81
-
82
- if ($._doStroke) {
83
- if (mode == $.PIE || mode == $.CHORD) $.ctx.closePath();
84
- if (mode != $.PIE_OPEN) return $.ctx.stroke();
85
-
86
- $.ctx.beginPath();
87
- $.ctx.ellipse(x, y, w, h, 0, lo, hi);
88
- $.ctx.stroke();
89
- }
90
- }
91
-
92
- $.arc = (x, y, w, h, start, stop, mode) => {
93
- if (start == stop) return $.ellipse(x, y, w, h);
94
-
95
- if ($._da) {
96
- x *= $._da;
97
- y *= $._da;
98
- w *= $._da;
99
- h *= $._da;
100
- }
101
- mode ??= $.PIE_OPEN;
102
-
103
- if ($._ellipseMode == $.CENTER) {
104
- arc(x, y, w, h, start, stop, mode);
105
- } else if ($._ellipseMode == $.RADIUS) {
106
- arc(x, y, w * 2, h * 2, start, stop, mode);
107
- } else if ($._ellipseMode == $.CORNER) {
108
- arc(x + w / 2, y + h / 2, w, h, start, stop, mode);
109
- } else if ($._ellipseMode == $.CORNERS) {
110
- arc((x + w) / 2, (y + h) / 2, w - x, h - y, start, stop, mode);
111
- }
112
- };
113
-
114
- function ellipse(x, y, w, h) {
115
- $.ctx.beginPath();
116
- $.ctx.ellipse(x, y, w / 2, h / 2, 0, 0, TAU);
117
- ink();
118
- }
119
-
120
- $.ellipse = (x, y, w, h) => {
121
- h ??= w;
122
- if ($._da) {
123
- x *= $._da;
124
- y *= $._da;
125
- w *= $._da;
126
- h *= $._da;
127
- }
128
- if ($._ellipseMode == $.CENTER) {
129
- ellipse(x, y, w, h);
130
- } else if ($._ellipseMode == $.RADIUS) {
131
- ellipse(x, y, w * 2, h * 2);
132
- } else if ($._ellipseMode == $.CORNER) {
133
- ellipse(x + w / 2, y + h / 2, w, h);
134
- } else if ($._ellipseMode == $.CORNERS) {
135
- ellipse((x + w) / 2, (y + h) / 2, w - x, h - y);
136
- }
137
- };
138
-
139
- $.circle = (x, y, d) => {
140
- if ($._ellipseMode == $.CENTER) {
141
- if ($._da) {
142
- x *= $._da;
143
- y *= $._da;
144
- d *= $._da;
145
- }
146
- $.ctx.beginPath();
147
- $.ctx.arc(x, y, d / 2, 0, TAU);
148
- ink();
149
- } else $.ellipse(x, y, d, d);
150
- };
151
-
152
- $.point = (x, y) => {
153
- if ($._doStroke) {
154
- if (x.x) {
155
- y = x.y;
156
- x = x.x;
157
- }
158
- if ($._da) {
159
- x *= $._da;
160
- y *= $._da;
161
- }
162
- $.ctx.beginPath();
163
- $.ctx.moveTo(x, y);
164
- $.ctx.lineTo(x, y);
165
- $.ctx.stroke();
166
- }
167
- };
168
-
169
- function rect(x, y, w, h) {
170
- if ($._da) {
171
- x *= $._da;
172
- y *= $._da;
173
- w *= $._da;
174
- h *= $._da;
175
- }
176
- $.ctx.beginPath();
177
- $.ctx.rect(x, y, w, h);
178
- ink();
179
- }
180
-
181
- function roundedRect(x, y, w, h, tl, tr, br, bl) {
182
- if (tl === undefined) {
183
- return rect(x, y, w, h);
184
- }
185
- if (tr === undefined) {
186
- return roundedRect(x, y, w, h, tl, tl, tl, tl);
187
- }
188
- if ($._da) {
189
- x *= $._da;
190
- y *= $._da;
191
- w *= $._da;
192
- h *= $._da;
193
- tl *= $._da;
194
- tr *= $._da;
195
- bl *= $._da;
196
- br *= $._da;
197
- }
198
- $.ctx.roundRect(x, y, w, h, [tl, tr, br, bl]);
199
- ink();
200
- }
201
-
202
- $.rect = (x, y, w, h = w, tl, tr, br, bl) => {
203
- if ($._rectMode == $.CENTER) {
204
- roundedRect(x - w / 2, y - h / 2, w, h, tl, tr, br, bl);
205
- } else if ($._rectMode == $.RADIUS) {
206
- roundedRect(x - w, y - h, w * 2, h * 2, tl, tr, br, bl);
207
- } else if ($._rectMode == $.CORNER) {
208
- roundedRect(x, y, w, h, tl, tr, br, bl);
209
- } else if ($._rectMode == $.CORNERS) {
210
- roundedRect(x, y, w - x, h - y, tl, tr, br, bl);
211
- }
212
- };
213
-
214
- $.square = (x, y, s, tl, tr, br, bl) => {
215
- return $.rect(x, y, s, s, tl, tr, br, bl);
216
- };
217
-
218
- $.beginShape = () => {
219
- curveBuff = [];
220
- $.ctx.beginPath();
221
- firstVertex = true;
222
- };
223
-
224
- $.beginContour = () => {
225
- $.ctx.closePath();
226
- curveBuff = [];
227
- firstVertex = true;
228
- };
229
-
230
- $.endContour = () => {
231
- curveBuff = [];
232
- firstVertex = true;
233
- };
234
-
235
- $.vertex = (x, y) => {
236
- if ($._da) {
237
- x *= $._da;
238
- y *= $._da;
239
- }
240
- curveBuff = [];
241
- if (firstVertex) {
242
- $.ctx.moveTo(x, y);
243
- } else {
244
- $.ctx.lineTo(x, y);
245
- }
246
- firstVertex = false;
247
- };
248
-
249
- $.bezierVertex = (cp1x, cp1y, cp2x, cp2y, x, y) => {
250
- if ($._da) {
251
- cp1x *= $._da;
252
- cp1y *= $._da;
253
- cp2x *= $._da;
254
- cp2y *= $._da;
255
- x *= $._da;
256
- y *= $._da;
257
- }
258
- curveBuff = [];
259
- $.ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
260
- };
261
-
262
- $.quadraticVertex = (cp1x, cp1y, x, y) => {
263
- if ($._da) {
264
- cp1x *= $._da;
265
- cp1y *= $._da;
266
- x *= $._da;
267
- y *= $._da;
268
- }
269
- curveBuff = [];
270
- $.ctx.quadraticCurveTo(cp1x, cp1y, x, y);
271
- };
272
-
273
- $.bezier = (x1, y1, x2, y2, x3, y3, x4, y4) => {
274
- $.beginShape();
275
- $.vertex(x1, y1);
276
- $.bezierVertex(x2, y2, x3, y3, x4, y4);
277
- $.endShape();
278
- };
279
-
280
- $.triangle = (x1, y1, x2, y2, x3, y3) => {
281
- $.beginShape();
282
- $.vertex(x1, y1);
283
- $.vertex(x2, y2);
284
- $.vertex(x3, y3);
285
- $.endShape($.CLOSE);
286
- };
287
-
288
- $.quad = (x1, y1, x2, y2, x3, y3, x4, y4) => {
289
- $.beginShape();
290
- $.vertex(x1, y1);
291
- $.vertex(x2, y2);
292
- $.vertex(x3, y3);
293
- $.vertex(x4, y4);
294
- $.endShape($.CLOSE);
295
- };
296
-
297
- $.endShape = (close) => {
298
- curveBuff = [];
299
- if (close) $.ctx.closePath();
300
- ink();
301
- };
302
-
303
- $.curveVertex = (x, y) => {
304
- if ($._da) {
305
- x *= $._da;
306
- y *= $._da;
307
- }
308
- curveBuff.push([x, y]);
309
- if (curveBuff.length < 4) return;
310
-
311
- let p0 = curveBuff.at(-4),
312
- p1 = curveBuff.at(-3),
313
- p2 = curveBuff.at(-2),
314
- p3 = curveBuff.at(-1);
315
-
316
- let cp1x = p1[0] + (p2[0] - p0[0]) / 6,
317
- cp1y = p1[1] + (p2[1] - p0[1]) / 6,
318
- cp2x = p2[0] - (p3[0] - p1[0]) / 6,
319
- cp2y = p2[1] - (p3[1] - p1[1]) / 6;
320
-
321
- if (firstVertex) {
322
- $.ctx.moveTo(p1[0], p1[1]);
323
- firstVertex = false;
324
- }
325
- $.ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, p2[0], p2[1]);
326
- };
327
-
328
- $.curve = (x1, y1, x2, y2, x3, y3, x4, y4) => {
329
- $.beginShape();
330
- $.curveVertex(x1, y1);
331
- $.curveVertex(x2, y2);
332
- $.curveVertex(x3, y3);
333
- $.curveVertex(x4, y4);
334
- $.endShape();
335
- };
336
-
337
- $.curvePoint = (a, b, c, d, t) => {
338
- const t3 = t * t * t,
339
- t2 = t * t,
340
- f1 = -0.5 * t3 + t2 - 0.5 * t,
341
- f2 = 1.5 * t3 - 2.5 * t2 + 1.0,
342
- f3 = -1.5 * t3 + 2.0 * t2 + 0.5 * t,
343
- f4 = 0.5 * t3 - 0.5 * t2;
344
- return a * f1 + b * f2 + c * f3 + d * f4;
345
- };
346
-
347
- $.bezierPoint = (a, b, c, d, t) => {
348
- const adjustedT = 1 - t;
349
- return (
350
- Math.pow(adjustedT, 3) * a +
351
- 3 * Math.pow(adjustedT, 2) * t * b +
352
- 3 * adjustedT * Math.pow(t, 2) * c +
353
- Math.pow(t, 3) * d
354
- );
355
- };
356
-
357
- $.curveTangent = (a, b, c, d, t) => {
358
- const t2 = t * t,
359
- f1 = (-3 * t2) / 2 + 2 * t - 0.5,
360
- f2 = (9 * t2) / 2 - 5 * t,
361
- f3 = (-9 * t2) / 2 + 4 * t + 0.5,
362
- f4 = (3 * t2) / 2 - t;
363
- return a * f1 + b * f2 + c * f3 + d * f4;
364
- };
365
-
366
- $.bezierTangent = (a, b, c, d, t) => {
367
- const adjustedT = 1 - t;
368
- return (
369
- 3 * d * Math.pow(t, 2) -
370
- 3 * c * Math.pow(t, 2) +
371
- 6 * c * adjustedT * t -
372
- 6 * b * adjustedT * t +
373
- 3 * b * Math.pow(adjustedT, 2) -
374
- 3 * a * Math.pow(adjustedT, 2)
375
- );
376
- };
377
-
378
- $.erase = function (fillAlpha = 255, strokeAlpha = 255) {
379
- $.ctx.save();
380
- $.ctx.globalCompositeOperation = 'destination-out';
381
- $.ctx.fillStyle = `rgba(0, 0, 0, ${fillAlpha / 255})`;
382
- $.ctx.strokeStyle = `rgba(0, 0, 0, ${strokeAlpha / 255})`;
383
- };
384
-
385
- $.noErase = function () {
386
- $.ctx.globalCompositeOperation = 'source-over';
387
- $.ctx.restore();
388
- };
389
-
390
- $.inFill = (x, y) => {
391
- const pd = $._pixelDensity;
392
- return $.ctx.isPointInPath(x * pd, y * pd);
393
- };
394
-
395
- $.inStroke = (x, y) => {
396
- const pd = $._pixelDensity;
397
- return $.ctx.isPointInStroke(x * pd, y * pd);
398
- };
399
- };