p5.zine 0.0.1

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/src/custom.js ADDED
@@ -0,0 +1,430 @@
1
+ // customized functions
2
+ export function customAddon(p5, fn) {
3
+ const applyMethod = (name, method) => {
4
+ fn[name] = method;
5
+ if (p5?.Graphics?.prototype) {
6
+ p5.Graphics.prototype[name] = method;
7
+ }
8
+ };
9
+
10
+ applyMethod("Background", function (r, g, b) {
11
+ if (typeof r === "string") {
12
+ if (r.indexOf("rgb") > -1) {
13
+ let colorArr = r.slice(r.indexOf("(") + 1, r.indexOf(")")).split(",");
14
+ this.background(
15
+ colorArr.map((str) => {
16
+ return Number(str);
17
+ })
18
+ );
19
+ } else {
20
+ this.background(r);
21
+ }
22
+ }
23
+ this.background(r, g, b);
24
+ });
25
+
26
+ applyMethod("selfieBackground", function (r, g, b) {
27
+ this.push();
28
+ this.translate(this.width, 0);
29
+ this.scale(-1, 1);
30
+
31
+ if (r != undefined && g != undefined && b != undefined) {
32
+ this.tint(r, g, b);
33
+ }
34
+
35
+ if (selfie.height > selfie.width) {
36
+ this.image(
37
+ selfie,
38
+ 0,
39
+ 0,
40
+ this.width,
41
+ (this.height * selfie.height) / selfie.width
42
+ );
43
+ } else {
44
+ this.image(
45
+ selfie,
46
+ -50,
47
+ 0,
48
+ (this.width * selfie.width) / selfie.height,
49
+ this.height
50
+ );
51
+ }
52
+ this.pop();
53
+ });
54
+
55
+ applyMethod("rightCamera", function () {
56
+ image(
57
+ capture,
58
+ 0,
59
+ 0,
60
+ this.width,
61
+ (this.width * capture.height) / capture.width
62
+ );
63
+ });
64
+
65
+ applyMethod("randomLayout", function (
66
+ word = ["word"],
67
+ size = 7,
68
+ num = 5,
69
+ repeat = 20,
70
+ fun = false
71
+ ) {
72
+ const sizer = size*10;
73
+ this.push();
74
+
75
+ this.translate(this.width / 2, this.height / 2);
76
+ for (let i = 0; i < repeat; i++) {
77
+ for (let j = 0; j < word.length; j++) {
78
+ if (fun) {
79
+ this.textSize(random(20, 100));
80
+ }
81
+ if (typeof word[j] === "string") {
82
+ this.text(
83
+ word[j],
84
+ random(-(this.width / 10) * num, (this.width / 10) * num),
85
+ random(-(this.height / 10) * num, (this.height / 10) * num)
86
+ );
87
+ } else if (typeof word[j] === "object") {
88
+ let ar = word[j].width / word[j].height;
89
+ this.image(
90
+ word[j],
91
+ random(-(this.width / 10) * num, (this.width / 10) * num),
92
+ random(-(this.height / 10) * num, (this.height / 10) * num),
93
+ sizer * ar * 2,
94
+ sizer * 2
95
+ );
96
+ }
97
+ }
98
+ }
99
+ this.pop();
100
+ });
101
+
102
+ applyMethod("gridLayout", function (
103
+ word = ["word"],
104
+ size = 7,
105
+ columnNum = 5,
106
+ repeat = 15,
107
+ fun = false
108
+ ) {
109
+ this.push();
110
+ this.translate(10, 30);
111
+ let row = 0,
112
+ column = 0;
113
+ let num = 0;
114
+ let sizer = size*10;
115
+
116
+ for (let i = 0; i < repeat; i++) {
117
+ for (let j = 0; j < word.length; j++) {
118
+ if (fun) {
119
+ const rowNum = int(random(5, 10));
120
+ row = floor(num / rowNum);
121
+ column = num % columnNum;
122
+ } else {
123
+ row = floor(num / columnNum);
124
+ column = num % columnNum;
125
+ }
126
+
127
+ if (typeof word[j] === "string") {
128
+ this.text(word[j], column * sizer * 2.5, row * sizer * 2.5);
129
+ } else if (typeof word[j] === "object") {
130
+ let ar = word[j].width / word[j].height;
131
+ this.image(
132
+ word[j],
133
+ column * sizer * 2.5 - sizer / 2,
134
+ row * sizer * 2.5 - sizer,
135
+ sizer * ar * 2,
136
+ sizer * 2
137
+ );
138
+ }
139
+ num++;
140
+ }
141
+ }
142
+ this.pop();
143
+ });
144
+
145
+ applyMethod("glitchLayout", function (
146
+ word = ["word"],
147
+ sizer = 7,
148
+ numMax = 5,
149
+ repeat = 20,
150
+ fun = false
151
+ ) {
152
+
153
+ let wave;
154
+ if (fun) {
155
+ wave = random(100);
156
+ } else {
157
+ wave = random(10, 20);
158
+ }
159
+
160
+ for (let i = 0; i < repeat; i++) {
161
+ const num = random(1, numMax)
162
+
163
+ for (let j = 0; j < word.length; j++) {
164
+ const w = random(this.width);
165
+ const h = random(this.height);
166
+
167
+ for (let n = 0; n < num; n++) {
168
+ if (typeof word[j] === "string") {
169
+ this.text(word[j], w + n * 20, h + sin(n * wave) * 30);
170
+ } else if (typeof word[j] === "object") {
171
+ let ar = word[j].width / word[j].height;
172
+ this.image(word[j], w + n * 30, h + sin(n * wave) * 40, 10 * ar * 2 * sizer+ n * 5, 10 * 2 *sizer);
173
+ }
174
+ }
175
+ }
176
+ }
177
+ });
178
+
179
+ applyMethod("leftBackground", function (r, g, b) {
180
+ this.push();
181
+ this.noStroke();
182
+ if (typeof r === "string") {
183
+ if (r.indexOf("rgb") > -1) {
184
+ let colorArr = r.slice(r.indexOf("(") + 1, r.indexOf(")")).split(",");
185
+ this.fill(
186
+ colorArr.map((str) => {
187
+ return Number(str);
188
+ })
189
+ );
190
+ } else {
191
+ this.fill(r);
192
+ }
193
+ } else {
194
+ this.fill(r, g, b);
195
+ }
196
+ this.rect(0, 0, this.width / 2, this.height);
197
+ this.pop();
198
+ });
199
+
200
+ applyMethod("rightBackground", function (r, g, b) {
201
+ this.push();
202
+ this.noStroke();
203
+ if (typeof r === "string") {
204
+ if (r.indexOf("rgb") > -1) {
205
+ let colorArr = r.slice(r.indexOf("(") + 1, r.indexOf(")")).split(",");
206
+ this.fill(
207
+ colorArr.map((str) => {
208
+ return Number(str);
209
+ })
210
+ );
211
+ } else {
212
+ this.fill(r);
213
+ }
214
+ } else {
215
+ this.fill(r, g, b);
216
+ }
217
+ this.rect(this.width / 2, 0, this.width / 2, this.height);
218
+ this.pop();
219
+ });
220
+
221
+ applyMethod("fullPage", function (material) {
222
+ let size;
223
+
224
+ if (typeof material === "object") {
225
+ if (material.width > material.height) {
226
+ size = this.height;
227
+ let ar = material.width / material.height;
228
+ this.image(material, 0, 0, size * ar, size);
229
+ } else {
230
+ size = this.width;
231
+ let ar = material.height / material.width;
232
+ this.image(material, 0, 0, size, size * ar);
233
+ }
234
+ } else {
235
+ this.text(material, 10, 10, this.width - 20);
236
+ }
237
+ });
238
+
239
+ applyMethod("leftPage", function (material, offsetX, offsetY) {
240
+ this.push();
241
+ this.translate(offsetX, offsetY);
242
+ if (typeof material === "object") {
243
+ this.push();
244
+ if (material.width > material.height) {
245
+ size = this.height;
246
+ let ar = material.width / material.height;
247
+ this.image(material, 0, 0, size * ar, size);
248
+ } else {
249
+ size = this.width / 2;
250
+ let ar = material.height / material.width;
251
+ this.image(material, 0, 0, size, size * ar);
252
+ }
253
+ this.pop();
254
+ } else {
255
+ this.push();
256
+
257
+ if (typeof material === "string") {
258
+ this.text(material, 10, 10, this.width / 2 - 20);
259
+ }
260
+ this.pop();
261
+ }
262
+ this.pop();
263
+ });
264
+
265
+ applyMethod("rightPage", function (material, offsetX, offsetY) {
266
+ this.push();
267
+ this.translate(offsetX, offsetY);
268
+ if (typeof material === "object") {
269
+ this.push();
270
+ this.translate(this.width / 2, 0);
271
+
272
+ if (typeof material === "object") {
273
+ if (material.width > material.height) {
274
+ size = this.height;
275
+ let ar = material.width / material.height;
276
+ this.image(material, 0, 0, size * ar, size);
277
+ } else {
278
+ size = this.width / 2;
279
+ let ar = material.height / material.width;
280
+ this.image(material, 0, 0, size, size * ar);
281
+ }
282
+ }
283
+ this.pop();
284
+ } else {
285
+ this.push();
286
+ this.translate(this.width / 2, 0);
287
+
288
+ if (typeof material === "string") {
289
+ this.text(material, 10, 10, this.width / 2 - 20);
290
+ }
291
+ this.pop();
292
+ }
293
+ this.pop();
294
+ });
295
+
296
+ applyMethod("textSet", function (
297
+ theFont = "Averia Libre",
298
+ theSize = 20,
299
+ theAlign = LEFT,
300
+ theLead = theSize,
301
+ theColor
302
+ ) {
303
+ if(typeof theColor !== "undefined" && typeof theColor !== null){
304
+ this.fill(theColor)
305
+ }
306
+ this.textFont(theFont);
307
+ this.textSize(theSize);
308
+ this.textAlign(theAlign);
309
+ this.textLeading(theLead);
310
+ });
311
+
312
+ applyMethod("textBox", function (
313
+ inputText,
314
+ startX = 0,
315
+ startY = 0,
316
+ boxWidth = this.width,
317
+ boxHeight = this.height,
318
+ gapX = 50,
319
+ showBox = false
320
+ ) {
321
+ let paragraphs = inputText.split("\n");
322
+ let lineHeight = this.textAscent() + this.textDescent();
323
+ let currentY = startY;
324
+ let currentX = startX;
325
+ let textAlignMode = this.textAlign().horizontal;
326
+ let textVerticalAlignMode = this.textAlign().vertical;
327
+
328
+ let linesInColumn = [];
329
+ let currentColumn = 0;
330
+ let linesCount = 0;
331
+
332
+ // Calculate the total number of lines in each column
333
+ for (const paragraph of paragraphs) {
334
+ let words = paragraph.split(" ");
335
+ let currentLine = words[0];
336
+
337
+ for (let i = 1; i < words.length + 1; i++) {
338
+ let word = words[i];
339
+ let nextLine = currentLine + " " + word;
340
+
341
+ if (i < words.length && ceil(this.textWidth(nextLine)) < boxWidth) {
342
+ currentLine = nextLine;
343
+ } else {
344
+ linesCount++;
345
+ currentY += lineHeight;
346
+
347
+ if (currentY + lineHeight > startY + boxHeight) {
348
+ linesInColumn[currentColumn] = linesCount;
349
+ linesCount = 0;
350
+ currentY = startY;
351
+ currentX += boxWidth + gapX;
352
+ currentColumn++;
353
+ }
354
+
355
+ currentLine = word;
356
+ }
357
+ }
358
+ }
359
+
360
+ linesInColumn[currentColumn] = linesCount;
361
+
362
+ // Draw the text with the correct alignment
363
+ currentY = startY;
364
+ currentX = startX;
365
+ currentColumn = 0;
366
+ linesCount = 0;
367
+
368
+ for (const paragraph of paragraphs) {
369
+ let words = paragraph.split(" ");
370
+ let currentLine = words[0];
371
+
372
+ for (let i = 1; i < words.length + 1; i++) {
373
+ let word = words[i];
374
+ let nextLine = currentLine + " " + word;
375
+
376
+ if (i < words.length && ceil(this.textWidth(nextLine)) < boxWidth) {
377
+ currentLine = nextLine;
378
+ } else {
379
+ let offsetX = 0;
380
+ let offsetY = 0;
381
+
382
+ // Calculate horizontal offset based on textAlign
383
+ if (textAlignMode === this.CENTER) {
384
+ offsetX = boxWidth / 2;
385
+ } else if (textAlignMode === this.RIGHT) {
386
+ offsetX = boxWidth;
387
+ }
388
+
389
+ // Calculate vertical offset based on textBaseline
390
+ if (textVerticalAlignMode === this.CENTER) {
391
+ offsetY = ceil(
392
+ (boxHeight - (linesInColumn[currentColumn] - 1) * lineHeight) / 2
393
+ );
394
+ } else if (textVerticalAlignMode === this.BOTTOM) {
395
+ offsetY = ceil(
396
+ boxHeight - (linesInColumn[currentColumn] - 1) * lineHeight
397
+ );
398
+ }
399
+
400
+ this.text(currentLine, currentX + offsetX, currentY + offsetY);
401
+
402
+ currentY += lineHeight;
403
+
404
+ if (currentY + lineHeight > startY + boxHeight) {
405
+ currentY = startY;
406
+ currentX += boxWidth + gapX;
407
+ currentColumn++;
408
+ }
409
+
410
+ currentLine = word;
411
+ }
412
+ }
413
+ }
414
+
415
+ // console.log(linesInColumn);
416
+
417
+ // Draw outlines for text boxes
418
+ if (showBox === true) {
419
+ this.push();
420
+ this.stroke(0, 0, 255);
421
+ this.strokeWeight(1);
422
+ this.noFill();
423
+ let numColumns = Math.ceil((currentX - startX) / (boxWidth + gapX)) + 1;
424
+ for (let i = 0; i < numColumns; i++) {
425
+ this.rect(startX + i * (boxWidth + gapX), startY, boxWidth, boxHeight);
426
+ }
427
+ this.pop();
428
+ }
429
+ });
430
+ }
package/src/html.js ADDED
@@ -0,0 +1,6 @@
1
+ import html from "./ui.html";
2
+
3
+ // HTML-----------------------------------------
4
+ document.addEventListener("DOMContentLoaded", function () {
5
+ document.body.insertAdjacentHTML("beforeend", html);
6
+ });
package/src/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import "./html.js";
2
+ import "./style.js";
3
+ import "./adapters/p5-2.js";
@@ -0,0 +1,3 @@
1
+ import "./html.js";
2
+ import "./style.js";
3
+ import "./adapters/p5-1.js";
@@ -0,0 +1,3 @@
1
+ import "./html.js";
2
+ import "./style.js";
3
+ import "./adapters/p5-2.js";
@@ -0,0 +1,143 @@
1
+ const UNIT_TO_INCH = {
2
+ in: 1,
3
+ inch: 1,
4
+ inches: 1,
5
+ cm: 1 / 2.54,
6
+ mm: 1 / 25.4,
7
+ pt: 1 / 72,
8
+ px: null,
9
+ };
10
+
11
+ function parseWithUnit(value) {
12
+ const trimmed = String(value).trim().toLowerCase();
13
+ const match = trimmed.match(/^([0-9]*\.?[0-9]+)\s*([a-z%]*)$/);
14
+ if (!match) {
15
+ return null;
16
+ }
17
+ return {
18
+ number: Number(match[1]),
19
+ unit: match[2] || null,
20
+ };
21
+ }
22
+
23
+ export function parseDimension(value, options = {}) {
24
+ if (value === null || typeof value === "undefined") {
25
+ return null;
26
+ }
27
+
28
+ const dpi = typeof options.dpi === "number" ? options.dpi : 96;
29
+ const defaultUnit = options.defaultUnit || null;
30
+
31
+ if (typeof value === "number") {
32
+ if (defaultUnit && UNIT_TO_INCH[defaultUnit]) {
33
+ return value * UNIT_TO_INCH[defaultUnit] * dpi;
34
+ }
35
+ return value;
36
+ }
37
+
38
+ const parsed = parseWithUnit(value);
39
+ if (!parsed) {
40
+ return null;
41
+ }
42
+
43
+ const unit = parsed.unit || defaultUnit || "px";
44
+ if (unit === "px") {
45
+ return parsed.number;
46
+ }
47
+
48
+ const inchFactor = UNIT_TO_INCH[unit];
49
+ if (!inchFactor) {
50
+ return null;
51
+ }
52
+
53
+ return parsed.number * inchFactor * dpi;
54
+ }
55
+
56
+ export function resolvePageDimensions(config = {}, defaults = {}) {
57
+ const dpi =
58
+ typeof config.pageDPI === "number"
59
+ ? config.pageDPI
60
+ : typeof config.paperDPI === "number"
61
+ ? config.paperDPI
62
+ : typeof config.dpi === "number"
63
+ ? config.dpi
64
+ : 96;
65
+ const defaultUnit =
66
+ typeof config.pageUnit === "string"
67
+ ? config.pageUnit
68
+ : typeof config.paperUnit === "string"
69
+ ? config.paperUnit
70
+ : typeof config.unit === "string"
71
+ ? config.unit
72
+ : null;
73
+
74
+ let width = null;
75
+ let height = null;
76
+
77
+ if (config.pageSize) {
78
+ if (Array.isArray(config.pageSize)) {
79
+ width = config.pageSize[0];
80
+ height = config.pageSize[1];
81
+ } else if (typeof config.pageSize === "object") {
82
+ width = config.pageSize.width;
83
+ height = config.pageSize.height;
84
+ }
85
+ }
86
+
87
+ if (typeof config.pageWidth !== "undefined") {
88
+ width = config.pageWidth;
89
+ }
90
+ if (typeof config.pageHeight !== "undefined") {
91
+ height = config.pageHeight;
92
+ }
93
+
94
+ if (typeof width === "undefined" || width === null || typeof height === "undefined" || height === null) {
95
+ let paperWidth = null;
96
+ let paperHeight = null;
97
+
98
+ if (config.paperSize) {
99
+ if (Array.isArray(config.paperSize)) {
100
+ paperWidth = config.paperSize[0];
101
+ paperHeight = config.paperSize[1];
102
+ } else if (typeof config.paperSize === "object") {
103
+ paperWidth = config.paperSize.width;
104
+ paperHeight = config.paperSize.height;
105
+ }
106
+ }
107
+
108
+ if (typeof config.paperWidth !== "undefined") {
109
+ paperWidth = config.paperWidth;
110
+ }
111
+ if (typeof config.paperHeight !== "undefined") {
112
+ paperHeight = config.paperHeight;
113
+ }
114
+
115
+ const parsedPaperWidth = parseDimension(paperWidth, { dpi, defaultUnit });
116
+ const parsedPaperHeight = parseDimension(paperHeight, { dpi, defaultUnit });
117
+
118
+ if (typeof parsedPaperWidth === "number" && typeof parsedPaperHeight === "number") {
119
+ width = parsedPaperHeight / 4;
120
+ height = parsedPaperWidth / 2;
121
+ }
122
+ }
123
+
124
+ const parsedWidth = parseDimension(width, { dpi, defaultUnit });
125
+ const parsedHeight = parseDimension(height, { dpi, defaultUnit });
126
+
127
+ let finalWidth = typeof parsedWidth === "number" ? parsedWidth : defaults.width;
128
+ let finalHeight = typeof parsedHeight === "number" ? parsedHeight : defaults.height;
129
+
130
+ if (typeof parsedWidth === "number" && typeof parsedHeight !== "number" && defaults.ratio) {
131
+ finalHeight = parsedWidth / defaults.ratio;
132
+ }
133
+
134
+ if (typeof parsedHeight === "number" && typeof parsedWidth !== "number" && defaults.ratio) {
135
+ finalWidth = parsedHeight * defaults.ratio;
136
+ }
137
+
138
+ return {
139
+ width: finalWidth,
140
+ height: finalHeight,
141
+ dpi,
142
+ };
143
+ }
package/src/style.js ADDED
@@ -0,0 +1,28 @@
1
+ import styles from "./ui.css";
2
+
3
+ // css------------------------------------------
4
+ const styleSheet = document.createElement("style");
5
+ styleSheet.innerText = styles;
6
+ document.head.appendChild(styleSheet);
7
+
8
+ // dark mode
9
+ if (
10
+ window.matchMedia &&
11
+ window.matchMedia("(prefers-color-scheme: dark)").matches
12
+ ) {
13
+ document.documentElement.setAttribute("data-theme", "dark");
14
+ } else {
15
+ document.documentElement.setAttribute("data-theme", "light");
16
+ }
17
+
18
+ window
19
+ .matchMedia("(prefers-color-scheme: dark)")
20
+ .addEventListener("change", (event) => {
21
+ const colorScheme = event.matches ? "dark" : "light";
22
+
23
+ if (colorScheme === "dark") {
24
+ document.documentElement.setAttribute("data-theme", "dark");
25
+ } else {
26
+ document.documentElement.setAttribute("data-theme", "light");
27
+ }
28
+ });