q5 2.14.3 → 2.14.5

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,398 +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
- function arc(x, y, w, h, lo, hi, mode) {
59
- if ($._angleMode) {
60
- lo = $.radians(lo);
61
- hi = $.radians(hi);
62
- }
63
- let full = $.TAU;
64
- lo %= full;
65
- hi %= full;
66
- if (lo < 0) lo += full;
67
- if (hi < 0) hi += full;
68
- if (lo > hi) hi += full;
69
- if (lo == hi) return $.ellipse(x, y, w, h);
70
-
71
- w /= 2;
72
- h /= 2;
73
-
74
- if (!$._doFill && mode == $.PIE_OPEN) mode = $.CHORD_OPEN;
75
-
76
- $.ctx.beginPath();
77
- $.ctx.ellipse(x, y, w, h, 0, lo, hi);
78
- if (mode == $.PIE || mode == $.PIE_OPEN) $.ctx.lineTo(x, y);
79
- if ($._doFill) $.ctx.fill();
80
-
81
- if ($._doStroke) {
82
- if (mode == $.PIE || mode == $.CHORD) $.ctx.closePath();
83
- if (mode != $.PIE_OPEN) return $.ctx.stroke();
84
-
85
- $.ctx.beginPath();
86
- $.ctx.ellipse(x, y, w, h, 0, lo, hi);
87
- $.ctx.stroke();
88
- }
89
- }
90
-
91
- $.arc = (x, y, w, h, start, stop, mode) => {
92
- if (start == stop) return $.ellipse(x, y, w, h);
93
-
94
- if ($._da) {
95
- x *= $._da;
96
- y *= $._da;
97
- w *= $._da;
98
- h *= $._da;
99
- }
100
- mode ??= $.PIE_OPEN;
101
-
102
- if ($._ellipseMode == $.CENTER) {
103
- arc(x, y, w, h, start, stop, mode);
104
- } else if ($._ellipseMode == $.RADIUS) {
105
- arc(x, y, w * 2, h * 2, start, stop, mode);
106
- } else if ($._ellipseMode == $.CORNER) {
107
- arc(x + w / 2, y + h / 2, w, h, start, stop, mode);
108
- } else if ($._ellipseMode == $.CORNERS) {
109
- arc((x + w) / 2, (y + h) / 2, w - x, h - y, start, stop, mode);
110
- }
111
- };
112
-
113
- function ellipse(x, y, w, h) {
114
- $.ctx.beginPath();
115
- $.ctx.ellipse(x, y, w / 2, h / 2, 0, 0, $.TAU);
116
- ink();
117
- }
118
-
119
- $.ellipse = (x, y, w, h) => {
120
- h ??= w;
121
- if ($._da) {
122
- x *= $._da;
123
- y *= $._da;
124
- w *= $._da;
125
- h *= $._da;
126
- }
127
- if ($._ellipseMode == $.CENTER) {
128
- ellipse(x, y, w, h);
129
- } else if ($._ellipseMode == $.RADIUS) {
130
- ellipse(x, y, w * 2, h * 2);
131
- } else if ($._ellipseMode == $.CORNER) {
132
- ellipse(x + w / 2, y + h / 2, w, h);
133
- } else if ($._ellipseMode == $.CORNERS) {
134
- ellipse((x + w) / 2, (y + h) / 2, w - x, h - y);
135
- }
136
- };
137
-
138
- $.circle = (x, y, d) => {
139
- if ($._ellipseMode == $.CENTER) {
140
- if ($._da) {
141
- x *= $._da;
142
- y *= $._da;
143
- d *= $._da;
144
- }
145
- $.ctx.beginPath();
146
- $.ctx.arc(x, y, d / 2, 0, $.TAU);
147
- ink();
148
- } else $.ellipse(x, y, d, d);
149
- };
150
-
151
- $.point = (x, y) => {
152
- if ($._doStroke) {
153
- if (x.x) {
154
- y = x.y;
155
- x = x.x;
156
- }
157
- if ($._da) {
158
- x *= $._da;
159
- y *= $._da;
160
- }
161
- $.ctx.beginPath();
162
- $.ctx.moveTo(x, y);
163
- $.ctx.lineTo(x, y);
164
- $.ctx.stroke();
165
- }
166
- };
167
-
168
- function rect(x, y, w, h) {
169
- if ($._da) {
170
- x *= $._da;
171
- y *= $._da;
172
- w *= $._da;
173
- h *= $._da;
174
- }
175
- $.ctx.beginPath();
176
- $.ctx.rect(x, y, w, h);
177
- ink();
178
- }
179
-
180
- function roundedRect(x, y, w, h, tl, tr, br, bl) {
181
- if (tl === undefined) {
182
- return rect(x, y, w, h);
183
- }
184
- if (tr === undefined) {
185
- return roundedRect(x, y, w, h, tl, tl, tl, tl);
186
- }
187
- if ($._da) {
188
- x *= $._da;
189
- y *= $._da;
190
- w *= $._da;
191
- h *= $._da;
192
- tl *= $._da;
193
- tr *= $._da;
194
- bl *= $._da;
195
- br *= $._da;
196
- }
197
- $.ctx.roundRect(x, y, w, h, [tl, tr, br, bl]);
198
- ink();
199
- }
200
-
201
- $.rect = (x, y, w, h = w, tl, tr, br, bl) => {
202
- if ($._rectMode == $.CENTER) {
203
- roundedRect(x - w / 2, y - h / 2, w, h, tl, tr, br, bl);
204
- } else if ($._rectMode == $.RADIUS) {
205
- roundedRect(x - w, y - h, w * 2, h * 2, tl, tr, br, bl);
206
- } else if ($._rectMode == $.CORNER) {
207
- roundedRect(x, y, w, h, tl, tr, br, bl);
208
- } else if ($._rectMode == $.CORNERS) {
209
- roundedRect(x, y, w - x, h - y, tl, tr, br, bl);
210
- }
211
- };
212
-
213
- $.square = (x, y, s, tl, tr, br, bl) => {
214
- return $.rect(x, y, s, s, tl, tr, br, bl);
215
- };
216
-
217
- $.beginShape = () => {
218
- curveBuff = [];
219
- $.ctx.beginPath();
220
- firstVertex = true;
221
- };
222
-
223
- $.beginContour = () => {
224
- $.ctx.closePath();
225
- curveBuff = [];
226
- firstVertex = true;
227
- };
228
-
229
- $.endContour = () => {
230
- curveBuff = [];
231
- firstVertex = true;
232
- };
233
-
234
- $.vertex = (x, y) => {
235
- if ($._da) {
236
- x *= $._da;
237
- y *= $._da;
238
- }
239
- curveBuff = [];
240
- if (firstVertex) {
241
- $.ctx.moveTo(x, y);
242
- } else {
243
- $.ctx.lineTo(x, y);
244
- }
245
- firstVertex = false;
246
- };
247
-
248
- $.bezierVertex = (cp1x, cp1y, cp2x, cp2y, x, y) => {
249
- if ($._da) {
250
- cp1x *= $._da;
251
- cp1y *= $._da;
252
- cp2x *= $._da;
253
- cp2y *= $._da;
254
- x *= $._da;
255
- y *= $._da;
256
- }
257
- curveBuff = [];
258
- $.ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
259
- };
260
-
261
- $.quadraticVertex = (cp1x, cp1y, x, y) => {
262
- if ($._da) {
263
- cp1x *= $._da;
264
- cp1y *= $._da;
265
- x *= $._da;
266
- y *= $._da;
267
- }
268
- curveBuff = [];
269
- $.ctx.quadraticCurveTo(cp1x, cp1y, x, y);
270
- };
271
-
272
- $.bezier = (x1, y1, x2, y2, x3, y3, x4, y4) => {
273
- $.beginShape();
274
- $.vertex(x1, y1);
275
- $.bezierVertex(x2, y2, x3, y3, x4, y4);
276
- $.endShape();
277
- };
278
-
279
- $.triangle = (x1, y1, x2, y2, x3, y3) => {
280
- $.beginShape();
281
- $.vertex(x1, y1);
282
- $.vertex(x2, y2);
283
- $.vertex(x3, y3);
284
- $.endShape($.CLOSE);
285
- };
286
-
287
- $.quad = (x1, y1, x2, y2, x3, y3, x4, y4) => {
288
- $.beginShape();
289
- $.vertex(x1, y1);
290
- $.vertex(x2, y2);
291
- $.vertex(x3, y3);
292
- $.vertex(x4, y4);
293
- $.endShape($.CLOSE);
294
- };
295
-
296
- $.endShape = (close) => {
297
- curveBuff = [];
298
- if (close) $.ctx.closePath();
299
- ink();
300
- };
301
-
302
- $.curveVertex = (x, y) => {
303
- if ($._da) {
304
- x *= $._da;
305
- y *= $._da;
306
- }
307
- curveBuff.push([x, y]);
308
- if (curveBuff.length < 4) return;
309
-
310
- let p0 = curveBuff.at(-4),
311
- p1 = curveBuff.at(-3),
312
- p2 = curveBuff.at(-2),
313
- p3 = curveBuff.at(-1);
314
-
315
- let cp1x = p1[0] + (p2[0] - p0[0]) / 6,
316
- cp1y = p1[1] + (p2[1] - p0[1]) / 6,
317
- cp2x = p2[0] - (p3[0] - p1[0]) / 6,
318
- cp2y = p2[1] - (p3[1] - p1[1]) / 6;
319
-
320
- if (firstVertex) {
321
- $.ctx.moveTo(p1[0], p1[1]);
322
- firstVertex = false;
323
- }
324
- $.ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, p2[0], p2[1]);
325
- };
326
-
327
- $.curve = (x1, y1, x2, y2, x3, y3, x4, y4) => {
328
- $.beginShape();
329
- $.curveVertex(x1, y1);
330
- $.curveVertex(x2, y2);
331
- $.curveVertex(x3, y3);
332
- $.curveVertex(x4, y4);
333
- $.endShape();
334
- };
335
-
336
- $.curvePoint = (a, b, c, d, t) => {
337
- const t3 = t * t * t,
338
- t2 = t * t,
339
- f1 = -0.5 * t3 + t2 - 0.5 * t,
340
- f2 = 1.5 * t3 - 2.5 * t2 + 1.0,
341
- f3 = -1.5 * t3 + 2.0 * t2 + 0.5 * t,
342
- f4 = 0.5 * t3 - 0.5 * t2;
343
- return a * f1 + b * f2 + c * f3 + d * f4;
344
- };
345
-
346
- $.bezierPoint = (a, b, c, d, t) => {
347
- const adjustedT = 1 - t;
348
- return (
349
- Math.pow(adjustedT, 3) * a +
350
- 3 * Math.pow(adjustedT, 2) * t * b +
351
- 3 * adjustedT * Math.pow(t, 2) * c +
352
- Math.pow(t, 3) * d
353
- );
354
- };
355
-
356
- $.curveTangent = (a, b, c, d, t) => {
357
- const t2 = t * t,
358
- f1 = (-3 * t2) / 2 + 2 * t - 0.5,
359
- f2 = (9 * t2) / 2 - 5 * t,
360
- f3 = (-9 * t2) / 2 + 4 * t + 0.5,
361
- f4 = (3 * t2) / 2 - t;
362
- return a * f1 + b * f2 + c * f3 + d * f4;
363
- };
364
-
365
- $.bezierTangent = (a, b, c, d, t) => {
366
- const adjustedT = 1 - t;
367
- return (
368
- 3 * d * Math.pow(t, 2) -
369
- 3 * c * Math.pow(t, 2) +
370
- 6 * c * adjustedT * t -
371
- 6 * b * adjustedT * t +
372
- 3 * b * Math.pow(adjustedT, 2) -
373
- 3 * a * Math.pow(adjustedT, 2)
374
- );
375
- };
376
-
377
- $.erase = function (fillAlpha = 255, strokeAlpha = 255) {
378
- $.ctx.save();
379
- $.ctx.globalCompositeOperation = 'destination-out';
380
- $.ctx.fillStyle = `rgba(0, 0, 0, ${fillAlpha / 255})`;
381
- $.ctx.strokeStyle = `rgba(0, 0, 0, ${strokeAlpha / 255})`;
382
- };
383
-
384
- $.noErase = function () {
385
- $.ctx.globalCompositeOperation = 'source-over';
386
- $.ctx.restore();
387
- };
388
-
389
- $.inFill = (x, y) => {
390
- const pd = $._pixelDensity;
391
- return $.ctx.isPointInPath(x * pd, y * pd);
392
- };
393
-
394
- $.inStroke = (x, y) => {
395
- const pd = $._pixelDensity;
396
- return $.ctx.isPointInStroke(x * pd, y * pd);
397
- };
398
- };