p5.html 0.0.1 → 0.0.2

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/dist/p5.html.js CHANGED
@@ -41,6 +41,40 @@
41
41
  }
42
42
  };
43
43
 
44
+ // src/elements/capture.ts
45
+ var P5Capture = class extends P5Element {
46
+ video;
47
+ replaceVideo = false;
48
+ flipped = Boolean(this.getAttribute("flipped"));
49
+ static observedAttributes = ["flipped"];
50
+ setup(p) {
51
+ this.initializeVideo(p);
52
+ P5Element.setupChildren(this, p);
53
+ }
54
+ draw(p) {
55
+ if (this.replaceVideo) {
56
+ this.initializeVideo(p);
57
+ this.replaceVideo = false;
58
+ }
59
+ P5Element.drawChildren(this, p);
60
+ }
61
+ attributeChangedCallback(name, oldValue, newValue) {
62
+ if (name === "flipped" && oldValue !== newValue) {
63
+ this.flipped = newValue?.toLowerCase() === "true";
64
+ this.replaceVideo = true;
65
+ }
66
+ }
67
+ initializeVideo(p) {
68
+ this.video?.remove();
69
+ this.video = p.createCapture("video", { flipped: this.flipped });
70
+ if (p.p5Root) {
71
+ this.video.size(p.p5Root.width, p.p5Root.height);
72
+ }
73
+ this.video.hide();
74
+ }
75
+ };
76
+ customElements.define("p5-capture", P5Capture);
77
+
44
78
  // src/elements/circle.ts
45
79
  var P5Circle = class extends P5Element {
46
80
  observedAttributes = ["d"];
@@ -86,6 +120,57 @@
86
120
  };
87
121
  customElements.define("p5-ellipse", P5Ellipse);
88
122
 
123
+ // src/elements/image.ts
124
+ var P5Image = class extends P5Element {
125
+ src = "";
126
+ fromCapture = "";
127
+ dirty = false;
128
+ capture = null;
129
+ image = null;
130
+ static observedAttributes = ["src", "from-capture"];
131
+ attributeChangedCallback(name, oldVal, newVal) {
132
+ if (oldVal !== newVal) {
133
+ this.dirty = true;
134
+ if (name === "from-capture") {
135
+ this.fromCapture = newVal;
136
+ } else if (name === "src") {
137
+ this.src = newVal;
138
+ }
139
+ }
140
+ }
141
+ setup(p) {
142
+ this.updateProvider(p);
143
+ }
144
+ draw(p) {
145
+ if (this.dirty) {
146
+ this.updateProvider(p);
147
+ this.dirty = false;
148
+ }
149
+ if (this.capture && this.capture.video) {
150
+ let video = this.capture.video;
151
+ this.cssContext(p, () => {
152
+ p.image(video, 0, 0);
153
+ });
154
+ } else if (this.image) {
155
+ let image = this.image;
156
+ this.cssContext(p, () => {
157
+ p.image(image, 0, 0);
158
+ });
159
+ }
160
+ }
161
+ async updateProvider(p) {
162
+ let capture = document.getElementById(this.fromCapture);
163
+ this.capture = null;
164
+ this.image = null;
165
+ if (capture instanceof P5Capture) {
166
+ this.capture = capture;
167
+ } else if (this.src !== "") {
168
+ this.image = await p.loadImage(this.src);
169
+ }
170
+ }
171
+ };
172
+ customElements.define("p5-image", P5Image);
173
+
89
174
  // src/elements/rect.ts
90
175
  var P5Rect = class extends P5Element {
91
176
  observedAttributes = ["w", "h"];
@@ -193,7 +278,9 @@
193
278
  width = Number(this.getAttribute("width")) ?? 0;
194
279
  height = Number(this.getAttribute("height")) ?? 0;
195
280
  dimensionsChanged = false;
196
- static observedAttributes = ["width", "height"];
281
+ paused = this.getAttribute("paused")?.toLowerCase() === "true";
282
+ ranFrameOne = false;
283
+ static observedAttributes = ["width", "height", "paused"];
197
284
  connectedCallback() {
198
285
  const globalMode = window.setup && typeof window.setup === "function" || window.draw && typeof window.draw === "function";
199
286
  if (!("p5" in window) || globalMode) {
@@ -217,8 +304,15 @@
217
304
  if (name === "width" || name === "height") {
218
305
  this.dimensionsChanged = true;
219
306
  }
307
+ if (name === "paused") {
308
+ this.paused = newValue?.toLowerCase() === "true";
309
+ if (this.p5 && !this.paused) {
310
+ this.p5.loop();
311
+ }
312
+ }
220
313
  }
221
314
  presetup(p) {
315
+ this.p5 = p;
222
316
  p.createCanvas(this.width, this.height);
223
317
  this.drawBackground(p);
224
318
  }
@@ -226,6 +320,12 @@
226
320
  P5Element.setupChildren(this, p);
227
321
  }
228
322
  predraw(p) {
323
+ if (this.paused && this.ranFrameOne) {
324
+ p.noLoop();
325
+ } else {
326
+ p.loop();
327
+ this.ranFrameOne = true;
328
+ }
229
329
  this.drawBackground(p);
230
330
  }
231
331
  draw(p) {
@@ -0,0 +1 @@
1
+ (()=>{var s=class r extends HTMLElement{setup(t){r.setupChildren(this,t)}draw(t){r.drawChildren(this,t)}cssContext(t,i){t.stroke(getComputedStyle(this).getPropertyValue("color")),t.fill(getComputedStyle(this).getPropertyValue("background"));let e=Number(getComputedStyle(this).getPropertyValue("opacity"));t.tint(255,e*255),i(t),t.noStroke(),t.noFill(),t.noTint()}static setupChildren(t,i){Array.from(t.children).forEach(e=>{e instanceof r?e.setup(i):e.children&&r.setupChildren(e,i)})}static drawChildren(t,i){Array.from(t.children).forEach(e=>{e instanceof r?e.draw(i):e.children&&r.drawChildren(e,i)})}};var o=class extends s{video;replaceVideo=!1;flipped=!!this.getAttribute("flipped");static observedAttributes=["flipped"];setup(t){this.initializeVideo(t),s.setupChildren(this,t)}draw(t){this.replaceVideo&&(this.initializeVideo(t),this.replaceVideo=!1),s.drawChildren(this,t)}attributeChangedCallback(t,i,e){t==="flipped"&&i!==e&&(this.flipped=e?.toLowerCase()==="true",this.replaceVideo=!0)}initializeVideo(t){this.video?.remove(),this.video=t.createCapture("video",{flipped:this.flipped}),t.p5Root&&this.video.size(t.p5Root.width,t.p5Root.height),this.video.hide()}};customElements.define("p5-capture",o);var a=class extends s{observedAttributes=["d"];diameter=0;connectedCallback(){this.diameter=Number(this.getAttribute("d"))||0}attributeChangedCallback(t,i,e){t==="d"&&(this.diameter=Number(e)||0)}draw(t){this.cssContext(t,()=>{t.circle(0,0,this.diameter)})}};customElements.define("p5-circle",a);var d=class extends s{observedAttributes=["w","h"];width=0;height=0;connectedCallback(){this.width=Number(this.getAttribute("w"))||0,this.height=Number(this.getAttribute("h"))||0}attributeChangedCallback(t,i,e){t==="w"&&(this.width=Number(e)||0),t==="h"&&(this.height=Number(e)||0)}draw(t){this.cssContext(t,()=>{t.ellipse(0,0,this.width,this.height)})}};customElements.define("p5-ellipse",d);var h=class extends s{src="";fromCapture="";dirty=!1;capture=null;image=null;static observedAttributes=["src","from-capture"];attributeChangedCallback(t,i,e){i!==e&&(this.dirty=!0,t==="from-capture"?this.fromCapture=e:t==="src"&&(this.src=e))}setup(t){this.updateProvider(t)}draw(t){if(this.dirty&&(this.updateProvider(t),this.dirty=!1),this.capture&&this.capture.video){let i=this.capture.video;this.cssContext(t,()=>{t.image(i,0,0)})}else if(this.image){let i=this.image;this.cssContext(t,()=>{t.image(i,0,0)})}}async updateProvider(t){let i=document.getElementById(this.fromCapture);this.capture=null,this.image=null,i instanceof o?this.capture=i:this.src!==""&&(this.image=await t.loadImage(this.src))}};customElements.define("p5-image",h);var p=class extends s{observedAttributes=["w","h"];width=0;height=0;connectedCallback(){this.width=Number(this.getAttribute("w"))||0,this.height=Number(this.getAttribute("h"))||0}attributeChangedCallback(t,i,e){t==="w"&&(this.width=Number(e)||0),t==="h"&&(this.height=Number(e)||0)}draw(t){this.cssContext(t,()=>{t.rect(0,0,this.width,this.height)})}};customElements.define("p5-rect",p);var l=class extends s{angle=Number(this.getAttribute("angle")??0);static observedAttributes=["angle"];draw(t){t.push(),t.rotate(this.angle),s.drawChildren(this,t),t.pop()}attributeChangedCallback(t,i,e){t==="angle"&&(this.angle=Number(e)??0)}};customElements.define("p5-rotate",l);var u=class extends s{x=Number(this.getAttribute("x")??0);y=Number(this.getAttribute("y")??0);static observedAttributes=["x","y"];draw(t){t.push(),t.scale(this.x,this.y),s.drawChildren(this,t),t.pop()}attributeChangedCallback(t,i,e){t==="x"&&(this.x=Number(e)??0),t==="y"&&(this.y=Number(e)??0)}};customElements.define("p5-scale",u);var m=class extends s{x=Number(this.getAttribute("x")??0);y=Number(this.getAttribute("y")??0);static observedAttributes=["x","y"];draw(t){t.push(),t.translate(this.x,this.y),s.drawChildren(this,t),t.pop()}attributeChangedCallback(t,i,e){t==="x"&&(this.x=Number(e)??0),t==="y"&&(this.y=Number(e)??0)}};customElements.define("p5-translate",m);var f=class extends s{content=this.innerText;connectedCallback(){this.style.display="none"}draw(t){this.cssContext(t,()=>{t.text(this.content,0,0)})}};customElements.define("p5-text",f);var n=class extends s{p5=null;width=Number(this.getAttribute("width"))??0;height=Number(this.getAttribute("height"))??0;dimensionsChanged=!1;paused=this.getAttribute("paused")?.toLowerCase()==="true";ranFrameOne=!1;static observedAttributes=["width","height","paused"];connectedCallback(){let t=window.setup&&typeof window.setup=="function"||window.draw&&typeof window.draw=="function";if(!("p5"in window)||t)return;let i=new window.p5(e=>{e.setup=()=>{},e.draw=()=>{}},this);i.p5Root=this}attributeChangedCallback(t,i,e){t==="width"&&(this.width=Number(e)??0),t==="height"&&(this.height=Number(e)??0),(t==="width"||t==="height")&&(this.dimensionsChanged=!0),t==="paused"&&(this.paused=e?.toLowerCase()==="true",this.p5&&!this.paused&&this.p5.loop())}presetup(t){this.p5=t,t.createCanvas(this.width,this.height),this.drawBackground(t)}setup(t){s.setupChildren(this,t)}predraw(t){this.paused&&this.ranFrameOne?t.noLoop():(t.loop(),this.ranFrameOne=!0),this.drawBackground(t)}draw(t){this.dimensionsChanged&&(t.resizeCanvas(this.width,this.height),this.dimensionsChanged=!1),s.drawChildren(this,t)}drawBackground(t){let i=getComputedStyle(this).background;i&&i!=="none"&&i!=="transparent"&&t.background(i)}};window.p5&&window.p5.registerAddon((r,t,i)=>{t.p5Root=null,i.presetup=function(){if(this.p5Root===null){let e=document.getElementsByTagName("p5-canvas");e.length>0&&(this.p5Root=e[0])}this.p5Root!==null&&this.p5Root.presetup(this)},i.postsetup=function(){this.p5Root!==null&&this.p5Root.setup(this)},i.predraw=function(){this.p5Root!==null&&this.p5Root.predraw(this)},i.postdraw=function(){this.p5Root!==null&&this.p5Root.draw(this)},i.remove=function(){this.p5Root!==null&&(this.p5Root.remove(),this.p5Root=null)}});customElements.define("p5-canvas",n);})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "p5.html",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Processing for HTML!",
5
5
  "author": "a@ndr.ooo",
6
6
  "license": "LGPL-3.0-only",
@@ -11,7 +11,7 @@
11
11
  "scripts": {
12
12
  "test": "echo \"Error: no test specified\" && exit 1",
13
13
  "dev": "esbuild --bundle src/index.ts --outfile=www/p5.html.js --watch --servedir=www --sourcemap",
14
- "build": "esbuild --bundle src/index.ts --outfile=dist/p5.html.js"
14
+ "build": "node build.esbuild.mjs"
15
15
  },
16
16
  "devDependencies": {
17
17
  "@types/p5": "1.7.7",