@alpha-video-kit/svg 0.1.0 → 0.2.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.
package/dist/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
- "use strict";var K=Object.defineProperty;var P=s=>{throw TypeError(s)};var N=(s,i,e)=>i in s?K(s,i,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[i]=e;var j=(s,i,e)=>N(s,typeof i!="symbol"?i+"":i,e),q=(s,i,e)=>i.has(s)||P("Cannot "+e);var t=(s,i,e)=>(q(s,i,"read from private field"),e?e.call(s):i.get(s)),v=(s,i,e)=>i.has(s)?P("Cannot add the same private member more than once"):i instanceof WeakSet?i.add(s):i.set(s,e),o=(s,i,e,d)=>(q(s,i,"write to private field"),d?d.call(s,e):i.set(s,e),e),h=(s,i,e)=>(q(s,i,"access private method"),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function T(s){const{canvas:i}=s,e=i.getContext("2d"),d=document.createElement("canvas"),y=d.getContext("2d");let c=!1;function L(w,a,n,p){return`<svg xmlns="http://www.w3.org/2000/svg"
1
+ "use strict";var G=Object.defineProperty;var U=i=>{throw TypeError(i)};var z=(i,s,t)=>s in i?G(i,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[s]=t;var B=(i,s,t)=>z(i,typeof s!="symbol"?s+"":s,t),E=(i,s,t)=>s.has(i)||U("Cannot "+t);var e=(i,s,t)=>(E(i,s,"read from private field"),t?t.call(i):s.get(i)),y=(i,s,t)=>s.has(i)?U("Cannot add the same private member more than once"):s instanceof WeakSet?s.add(i):s.set(i,t),h=(i,s,t,a)=>(E(i,s,"write to private field"),a?a.call(i,t):s.set(i,t),t),u=(i,s,t)=>(E(i,s,"access private method"),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function j(i){const{canvas:s}=i,t=s.getContext("2d"),a=document.createElement("canvas"),d=a.getContext("2d");let p=!1;function x(v,n,l,f){return`<svg xmlns="http://www.w3.org/2000/svg"
2
2
  xmlns:xlink="http://www.w3.org/1999/xlink"
3
- width="${a}" height="${n}"
4
- viewBox="0 0 ${a} ${n}">
3
+ width="${n}" height="${l}"
4
+ viewBox="0 0 ${n} ${l}">
5
5
  <defs>
6
6
  <filter id="r2a" color-interpolation-filters="sRGB">
7
7
  <feColorMatrix type="matrix"
@@ -11,41 +11,24 @@
11
11
  1 0 0 0 0"/>
12
12
  </filter>
13
13
  <mask id="alphaMask">
14
- <svg viewBox="0 ${n} ${a} ${n}"
15
- width="${a}" height="${n}"
14
+ <svg viewBox="0 ${l} ${n} ${l}"
15
+ width="${n}" height="${l}"
16
16
  preserveAspectRatio="none">
17
- <image href="${w}"
18
- width="${a}" height="${p}"
17
+ <image href="${v}"
18
+ width="${n}" height="${f}"
19
19
  filter="url(#r2a)"/>
20
20
  </svg>
21
21
  </mask>
22
22
  </defs>
23
- <svg viewBox="0 0 ${a} ${n}"
24
- width="${a}" height="${n}"
23
+ <svg viewBox="0 0 ${n} ${l}"
24
+ width="${n}" height="${l}"
25
25
  preserveAspectRatio="none"
26
26
  mask="url(#alphaMask)">
27
- <image href="${w}"
28
- width="${a}" height="${p}"/>
27
+ <image href="${v}"
28
+ width="${n}" height="${f}"/>
29
29
  </svg>
30
- </svg>`}return{drawFrame(w){if(c)return;const a=w.videoWidth,n=w.videoHeight,p=Math.floor(n/2);(i.width!==a||i.height!==p)&&(i.width=a,i.height=p),(d.width!==a||d.height!==n)&&(d.width=a,d.height=n),y.drawImage(w,0,0);const x=d.toDataURL("image/png"),S=L(x,a,p,n),E=new Blob([S],{type:"image/svg+xml"}),u=URL.createObjectURL(E),F=new Image;F.onload=()=>{if(c){URL.revokeObjectURL(u);return}e.clearRect(0,0,a,p),e.drawImage(F,0,0),URL.revokeObjectURL(u)},F.src=u},setPremultipliedAlpha(w){},destroy(){c||(c=!0)},get isDestroyed(){return c}}}function _(s){const{canvas:i,premultipliedAlpha:e=!1}=s,d=i.getContext("2d",{willReadFrequently:!0}),y=document.createElement("canvas"),c=y.getContext("2d",{willReadFrequently:!0});let L=!1,w=e;return{drawFrame(a){if(L)return;const n=a.videoWidth,p=a.videoHeight,x=Math.floor(p/2);(i.width!==n||i.height!==x)&&(i.width=n,i.height=x),(y.width!==n||y.height!==p)&&(y.width=n,y.height=p),c.drawImage(a,0,0);const S=c.getImageData(0,0,n,x),E=c.getImageData(0,x,n,x),u=S.data,F=E.data;for(let m=0;m<u.length;m+=4){const I=F[m];if(w)u[m+3]=I;else{const O=I/255;u[m]=Math.round(u[m]*O),u[m+1]=Math.round(u[m+1]*O),u[m+2]=Math.round(u[m+2]*O),u[m+3]=I}}d.putImageData(S,0,0)},setPremultipliedAlpha(a){w=a},destroy(){L||(L=!0)},get isDestroyed(){return L}}}function G(s){const{mode:i="canvas",...e}=s;return i==="svg-filter"?T(e):_(e)}var M,g,l,b,R,A,k,C,r,$,H,Q,D,f,z,U,V,J,B;class W extends HTMLElement{constructor(){super();v(this,r);v(this,M);v(this,g,null);v(this,l,null);v(this,b,null);v(this,R,null);v(this,A,null);v(this,k,!1);v(this,C,!1);v(this,f,()=>{h(this,r,$).call(this)});const e=this.attachShadow({mode:"open"});e.innerHTML=`
31
- <style>
32
- :host {
33
- display: inline-block;
34
- position: relative;
35
- }
36
- canvas {
37
- display: block;
38
- width: 100%;
39
- height: 100%;
40
- }
41
- ::slotted(video) {
42
- position: absolute;
43
- opacity: 0;
44
- pointer-events: none;
45
- width: 0;
46
- height: 0;
47
- }
48
- </style>
49
- <canvas></canvas>
50
- <slot></slot>
51
- `,o(this,M,e.querySelector("canvas"))}get premultipliedAlpha(){return this.hasAttribute("premultipliedalpha")}set premultipliedAlpha(e){e?this.setAttribute("premultipliedalpha",""):this.removeAttribute("premultipliedalpha")}attributeChangedCallback(e,d,y){var c;e==="premultipliedalpha"&&((c=t(this,g))==null||c.setPremultipliedAlpha(this.premultipliedAlpha)),e==="mode"&&(h(this,r,U).call(this),h(this,r,$).call(this))}connectedCallback(){o(this,R,new IntersectionObserver(([e])=>{o(this,k,e.isIntersecting),h(this,r,$).call(this)},{threshold:0})),t(this,R).observe(this),o(this,A,new MutationObserver(()=>h(this,r,$).call(this))),t(this,A).observe(this,{childList:!0}),h(this,r,$).call(this)}disconnectedCallback(){var e,d;(e=t(this,R))==null||e.disconnect(),o(this,R,null),(d=t(this,A))==null||d.disconnect(),o(this,A,null),h(this,r,D).call(this),h(this,r,U).call(this)}}M=new WeakMap,g=new WeakMap,l=new WeakMap,b=new WeakMap,R=new WeakMap,A=new WeakMap,k=new WeakMap,C=new WeakMap,r=new WeakSet,$=function(){t(this,C)||(o(this,C,!0),queueMicrotask(()=>{o(this,C,!1),h(this,r,H).call(this)}))},H=function(){const e=this.querySelector("video");e!==t(this,l)&&(h(this,r,D).call(this),o(this,l,e),e&&h(this,r,Q).call(this,e)),t(this,k)&&t(this,l)?(h(this,r,z).call(this),h(this,r,V).call(this),h(this,r,J).call(this)):h(this,r,B).call(this)},Q=function(e){e.addEventListener("play",t(this,f)),e.addEventListener("pause",t(this,f)),e.addEventListener("seeked",t(this,f)),e.addEventListener("loadeddata",t(this,f))},D=function(){t(this,l)&&(t(this,l).removeEventListener("play",t(this,f)),t(this,l).removeEventListener("pause",t(this,f)),t(this,l).removeEventListener("seeked",t(this,f)),t(this,l).removeEventListener("loadeddata",t(this,f)),o(this,l,null),h(this,r,B).call(this))},f=new WeakMap,z=function(){if(t(this,g)&&!t(this,g).isDestroyed)return;const e=this.getAttribute("mode")??"canvas";o(this,g,G({canvas:t(this,M),premultipliedAlpha:this.premultipliedAlpha,mode:e}))},U=function(){var e;(e=t(this,g))==null||e.destroy(),o(this,g,null)},V=function(){!t(this,g)||!t(this,l)||t(this,l).readyState<2||t(this,g).drawFrame(t(this,l))},J=function(){if(t(this,b)!==null||!t(this,l)||t(this,l).paused)return;const e=()=>{h(this,r,V).call(this),t(this,l)&&!t(this,l).paused&&t(this,k)?o(this,b,requestAnimationFrame(e)):o(this,b,null)};o(this,b,requestAnimationFrame(e))},B=function(){t(this,b)!==null&&(cancelAnimationFrame(t(this,b)),o(this,b,null))},j(W,"observedAttributes",["premultipliedalpha","mode"]);exports.StackedAlphaVideoSVG=W;exports.createCanvasFilterRenderer=_;exports.createRenderer=G;exports.createSvgFilterRenderer=T;
30
+ </svg>`}return{drawFrame(v){if(p)return;const n=v.videoWidth,l=v.videoHeight,f=Math.floor(l/2);(s.width!==n||s.height!==f)&&(s.width=n,s.height=f),(a.width!==n||a.height!==l)&&(a.width=n,a.height=l),d.drawImage(v,0,0);const A=a.toDataURL("image/png"),F=x(A,n,f,l),L=new Blob([F],{type:"image/svg+xml"}),g=URL.createObjectURL(L),C=new Image;C.onload=()=>{if(p){URL.revokeObjectURL(g);return}t.clearRect(0,0,n,f),t.drawImage(C,0,0),URL.revokeObjectURL(g)},C.src=g},setPremultipliedAlpha(v){},destroy(){p||(p=!0)},get isDestroyed(){return p}}}function W(i){const{canvas:s,premultipliedAlpha:t=!1}=i,a=s.getContext("2d",{willReadFrequently:!0}),d=document.createElement("canvas"),p=d.getContext("2d",{willReadFrequently:!0});let x=!1,v=t;return{drawFrame(n){if(x)return;const l=n.videoWidth,f=n.videoHeight,A=Math.floor(f/2);(s.width!==l||s.height!==A)&&(s.width=l,s.height=A),(d.width!==l||d.height!==f)&&(d.width=l,d.height=f),p.drawImage(n,0,0);const F=p.getImageData(0,0,l,A),L=p.getImageData(0,A,l,A),g=F.data,C=L.data;for(let m=0;m<g.length;m+=4){const M=C[m];if(v)g[m+3]=M;else{const T=M/255;g[m]=Math.round(g[m]*T),g[m+1]=Math.round(g[m+1]*T),g[m+2]=Math.round(g[m+2]*T),g[m+3]=M}}a.putImageData(F,0,0)},setPremultipliedAlpha(n){v=n},destroy(){x||(x=!0)},get isDestroyed(){return x}}}function H(i){const{mode:s="canvas",...t}=i;return s==="svg-filter"?j(t):W(t)}const _=["src","crossorigin","preload","autoplay","loop","muted","playsinline","poster","width","height","mode"],K=new Set(["autoplay","loop","muted","playsinline"]),J=["loadstart","progress","suspend","abort","error","emptied","stalled","loadedmetadata","loadeddata","canplay","canplaythrough","playing","waiting","seeking","seeked","ended","durationchange","timeupdate","play","pause","ratechange","resize","volumechange"];var r,$,c,R,S,b,w,k,o,I,q,D,P,O,V;class N extends HTMLElement{constructor(){super();y(this,o);y(this,r);y(this,$);y(this,c,null);y(this,R,null);y(this,S,null);y(this,b,!1);y(this,w,0);y(this,k,0);const t=this.attachShadow({mode:"open"});t.innerHTML=`<style>
31
+ :host{display:inline-block;overflow:hidden}
32
+ video{position:absolute;opacity:0;pointer-events:none;width:0;height:0}
33
+ canvas{display:block;width:100%;height:100%}
34
+ </style><video></video><canvas></canvas>`,h(this,r,t.querySelector("video")),h(this,$,t.querySelector("canvas"));for(const a of J)e(this,r).addEventListener(a,d=>{this.dispatchEvent(new Event(d.type,{bubbles:!1,cancelable:!1}))})}connectedCallback(){for(const t of _)t!=="mode"&&this.hasAttribute(t)&&u(this,o,I).call(this,t,this.getAttribute(t));u(this,o,q).call(this),h(this,S,new MutationObserver(()=>u(this,o,q).call(this))),e(this,S).observe(this,{childList:!0}),h(this,R,new IntersectionObserver(([t])=>{h(this,b,t.isIntersecting),e(this,b)?(u(this,o,D).call(this),u(this,o,P).call(this)):u(this,o,O).call(this)},{threshold:0})),e(this,R).observe(this)}disconnectedCallback(){var t,a,d;u(this,o,O).call(this),(t=e(this,R))==null||t.disconnect(),h(this,R,null),(a=e(this,S))==null||a.disconnect(),h(this,S,null),(d=e(this,c))==null||d.destroy(),h(this,c,null)}attributeChangedCallback(t,a,d){var p;if(t==="mode"){(p=e(this,c))==null||p.destroy(),h(this,c,null),e(this,b)&&(u(this,o,D).call(this),u(this,o,P).call(this));return}u(this,o,I).call(this,t,d)}get src(){return e(this,r).src}set src(t){this.setAttribute("src",t)}get currentSrc(){return e(this,r).currentSrc}get currentTime(){return e(this,r).currentTime}set currentTime(t){e(this,r).currentTime=t}get duration(){return e(this,r).duration}get paused(){return e(this,r).paused}get ended(){return e(this,r).ended}get readyState(){return e(this,r).readyState}get videoWidth(){return e(this,r).videoWidth}get videoHeight(){return Math.floor(e(this,r).videoHeight/2)}get volume(){return e(this,r).volume}set volume(t){e(this,r).volume=t}get playbackRate(){return e(this,r).playbackRate}set playbackRate(t){e(this,r).playbackRate=t}get defaultPlaybackRate(){return e(this,r).defaultPlaybackRate}set defaultPlaybackRate(t){e(this,r).defaultPlaybackRate=t}get muted(){return e(this,r).muted}set muted(t){e(this,r).muted=t,this.toggleAttribute("muted",t)}get loop(){return e(this,r).loop}set loop(t){e(this,r).loop=t,this.toggleAttribute("loop",t)}get autoplay(){return e(this,r).autoplay}set autoplay(t){e(this,r).autoplay=t,this.toggleAttribute("autoplay",t)}get preload(){return e(this,r).preload}set preload(t){this.setAttribute("preload",t)}get crossOrigin(){return e(this,r).crossOrigin}set crossOrigin(t){t===null?this.removeAttribute("crossorigin"):this.setAttribute("crossorigin",t)}get buffered(){return e(this,r).buffered}get seekable(){return e(this,r).seekable}get played(){return e(this,r).played}get networkState(){return e(this,r).networkState}get error(){return e(this,r).error}get seeking(){return e(this,r).seeking}play(){return e(this,r).play()}pause(){e(this,r).pause()}load(){e(this,r).load()}canPlayType(t){return e(this,r).canPlayType(t)}}r=new WeakMap,$=new WeakMap,c=new WeakMap,R=new WeakMap,S=new WeakMap,b=new WeakMap,w=new WeakMap,k=new WeakMap,o=new WeakSet,I=function(t,a){K.has(t)?e(this,r)[t]=a!==null:a===null?e(this,r).removeAttribute(t):e(this,r).setAttribute(t,a)},q=function(){e(this,r).querySelectorAll("source").forEach(t=>t.remove()),this.querySelectorAll("source").forEach(t=>{e(this,r).appendChild(t.cloneNode(!0))}),e(this,r).querySelectorAll("source").length>0&&!e(this,r).src&&e(this,r).load()},D=function(){if(!(e(this,c)&&!e(this,c).isDestroyed))try{const t=this.getAttribute("mode")||"canvas";h(this,c,H({canvas:e(this,$),mode:t}))}catch{}},P=function(){if(!(!e(this,b)||!e(this,c)))if(u(this,o,O).call(this),"requestVideoFrameCallback"in e(this,r)){const t=()=>{!e(this,b)||!e(this,c)||(u(this,o,V).call(this),h(this,k,e(this,r).requestVideoFrameCallback(t)))};h(this,k,e(this,r).requestVideoFrameCallback(t))}else{const t=()=>{!e(this,b)||!e(this,c)||(u(this,o,V).call(this),h(this,w,requestAnimationFrame(t)))};h(this,w,requestAnimationFrame(t))}},O=function(){var t,a;e(this,k)&&((a=(t=e(this,r)).cancelVideoFrameCallback)==null||a.call(t,e(this,k)),h(this,k,0)),e(this,w)&&(cancelAnimationFrame(e(this,w)),h(this,w,0))},V=function(){e(this,r).readyState<2||!e(this,c)||e(this,c).drawFrame(e(this,r))},B(N,"observedAttributes",[..._]);exports.AlphaVideoKitSVG=N;exports.createCanvasFilterRenderer=W;exports.createRenderer=H;exports.createSvgFilterRenderer=j;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,49 @@
1
+ export declare class AlphaVideoKitSVG extends HTMLElement {
2
+ #private;
3
+ static observedAttributes: ("mode" | "src" | "crossorigin" | "preload" | "autoplay" | "loop" | "muted" | "playsinline" | "poster" | "width" | "height")[];
4
+ constructor();
5
+ connectedCallback(): void;
6
+ disconnectedCallback(): void;
7
+ attributeChangedCallback(name: string, _old: string | null, value: string | null): void;
8
+ get src(): string;
9
+ set src(v: string);
10
+ get currentSrc(): string;
11
+ get currentTime(): number;
12
+ set currentTime(v: number);
13
+ get duration(): number;
14
+ get paused(): boolean;
15
+ get ended(): boolean;
16
+ get readyState(): number;
17
+ get videoWidth(): number;
18
+ get videoHeight(): number;
19
+ get volume(): number;
20
+ set volume(v: number);
21
+ get playbackRate(): number;
22
+ set playbackRate(v: number);
23
+ get defaultPlaybackRate(): number;
24
+ set defaultPlaybackRate(v: number);
25
+ get muted(): boolean;
26
+ set muted(v: boolean);
27
+ get loop(): boolean;
28
+ set loop(v: boolean);
29
+ get autoplay(): boolean;
30
+ set autoplay(v: boolean);
31
+ get preload(): string;
32
+ set preload(v: string);
33
+ get crossOrigin(): string | null;
34
+ set crossOrigin(v: string | null);
35
+ get buffered(): TimeRanges;
36
+ get seekable(): TimeRanges;
37
+ get played(): TimeRanges;
38
+ get networkState(): number;
39
+ get error(): MediaError | null;
40
+ get seeking(): boolean;
41
+ play(): Promise<void>;
42
+ pause(): void;
43
+ load(): void;
44
+ canPlayType(type: string): CanPlayTypeResult;
45
+ }
46
+
1
47
  /**
2
48
  * Canvas 2D renderer with pixel manipulation.
3
49
  * Draws the video to an offscreen canvas, reads pixels from top and bottom halves,
@@ -27,17 +73,6 @@ export declare interface StackedAlphaRendererOptions {
27
73
  premultipliedAlpha?: boolean;
28
74
  }
29
75
 
30
- export declare class StackedAlphaVideoSVG extends HTMLElement {
31
- #private;
32
- static observedAttributes: string[];
33
- constructor();
34
- get premultipliedAlpha(): boolean;
35
- set premultipliedAlpha(value: boolean);
36
- attributeChangedCallback(name: string, _oldValue: string | null, _newValue: string | null): void;
37
- connectedCallback(): void;
38
- disconnectedCallback(): void;
39
- }
40
-
41
76
  export declare type SvgRendererMode = 'svg-filter' | 'canvas';
42
77
 
43
78
  export declare interface SvgRendererOptions extends StackedAlphaRendererOptions {
package/dist/index.js CHANGED
@@ -1,18 +1,18 @@
1
- var H = Object.defineProperty;
2
- var P = (s) => {
3
- throw TypeError(s);
1
+ var W = Object.defineProperty;
2
+ var V = (r) => {
3
+ throw TypeError(r);
4
4
  };
5
- var Q = (s, i, t) => i in s ? H(s, i, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[i] = t;
6
- var j = (s, i, t) => Q(s, typeof i != "symbol" ? i + "" : i, t), O = (s, i, t) => i.has(s) || P("Cannot " + t);
7
- var e = (s, i, t) => (O(s, i, "read from private field"), t ? t.call(s) : i.get(s)), v = (s, i, t) => i.has(s) ? P("Cannot add the same private member more than once") : i instanceof WeakSet ? i.add(s) : i.set(s, t), o = (s, i, t, d) => (O(s, i, "write to private field"), d ? d.call(s, t) : i.set(s, t), t), l = (s, i, t) => (O(s, i, "access private method"), t);
8
- function z(s) {
9
- const { canvas: i } = s, t = i.getContext("2d"), d = document.createElement("canvas"), y = d.getContext("2d");
10
- let c = !1;
11
- function $(w, a, n, p) {
5
+ var j = (r, i, t) => i in r ? W(r, i, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[i] = t;
6
+ var B = (r, i, t) => j(r, typeof i != "symbol" ? i + "" : i, t), M = (r, i, t) => i.has(r) || V("Cannot " + t);
7
+ var e = (r, i, t) => (M(r, i, "read from private field"), t ? t.call(r) : i.get(r)), y = (r, i, t) => i.has(r) ? V("Cannot add the same private member more than once") : i instanceof WeakSet ? i.add(r) : i.set(r, t), l = (r, i, t, a) => (M(r, i, "write to private field"), a ? a.call(r, t) : i.set(r, t), t), d = (r, i, t) => (M(r, i, "access private method"), t);
8
+ function H(r) {
9
+ const { canvas: i } = r, t = i.getContext("2d"), a = document.createElement("canvas"), u = a.getContext("2d");
10
+ let p = !1;
11
+ function S(v, n, h, f) {
12
12
  return `<svg xmlns="http://www.w3.org/2000/svg"
13
13
  xmlns:xlink="http://www.w3.org/1999/xlink"
14
- width="${a}" height="${n}"
15
- viewBox="0 0 ${a} ${n}">
14
+ width="${n}" height="${h}"
15
+ viewBox="0 0 ${n} ${h}">
16
16
  <defs>
17
17
  <filter id="r2a" color-interpolation-filters="sRGB">
18
18
  <feColorMatrix type="matrix"
@@ -22,182 +22,315 @@ function z(s) {
22
22
  1 0 0 0 0"/>
23
23
  </filter>
24
24
  <mask id="alphaMask">
25
- <svg viewBox="0 ${n} ${a} ${n}"
26
- width="${a}" height="${n}"
25
+ <svg viewBox="0 ${h} ${n} ${h}"
26
+ width="${n}" height="${h}"
27
27
  preserveAspectRatio="none">
28
- <image href="${w}"
29
- width="${a}" height="${p}"
28
+ <image href="${v}"
29
+ width="${n}" height="${f}"
30
30
  filter="url(#r2a)"/>
31
31
  </svg>
32
32
  </mask>
33
33
  </defs>
34
- <svg viewBox="0 0 ${a} ${n}"
35
- width="${a}" height="${n}"
34
+ <svg viewBox="0 0 ${n} ${h}"
35
+ width="${n}" height="${h}"
36
36
  preserveAspectRatio="none"
37
37
  mask="url(#alphaMask)">
38
- <image href="${w}"
39
- width="${a}" height="${p}"/>
38
+ <image href="${v}"
39
+ width="${n}" height="${f}"/>
40
40
  </svg>
41
41
  </svg>`;
42
42
  }
43
43
  return {
44
- drawFrame(w) {
45
- if (c) return;
46
- const a = w.videoWidth, n = w.videoHeight, p = Math.floor(n / 2);
47
- (i.width !== a || i.height !== p) && (i.width = a, i.height = p), (d.width !== a || d.height !== n) && (d.width = a, d.height = n), y.drawImage(w, 0, 0);
48
- const x = d.toDataURL("image/png"), F = $(x, a, p, n), I = new Blob([F], { type: "image/svg+xml" }), u = URL.createObjectURL(I), M = new Image();
49
- M.onload = () => {
50
- if (c) {
51
- URL.revokeObjectURL(u);
44
+ drawFrame(v) {
45
+ if (p) return;
46
+ const n = v.videoWidth, h = v.videoHeight, f = Math.floor(h / 2);
47
+ (i.width !== n || i.height !== f) && (i.width = n, i.height = f), (a.width !== n || a.height !== h) && (a.width = n, a.height = h), u.drawImage(v, 0, 0);
48
+ const A = a.toDataURL("image/png"), F = S(A, n, f, h), O = new Blob([F], { type: "image/svg+xml" }), g = URL.createObjectURL(O), C = new Image();
49
+ C.onload = () => {
50
+ if (p) {
51
+ URL.revokeObjectURL(g);
52
52
  return;
53
53
  }
54
- t.clearRect(0, 0, a, p), t.drawImage(M, 0, 0), URL.revokeObjectURL(u);
55
- }, M.src = u;
54
+ t.clearRect(0, 0, n, f), t.drawImage(C, 0, 0), URL.revokeObjectURL(g);
55
+ }, C.src = g;
56
56
  },
57
- setPremultipliedAlpha(w) {
57
+ setPremultipliedAlpha(v) {
58
58
  },
59
59
  destroy() {
60
- c || (c = !0);
60
+ p || (p = !0);
61
61
  },
62
62
  get isDestroyed() {
63
- return c;
63
+ return p;
64
64
  }
65
65
  };
66
66
  }
67
- function J(s) {
68
- const { canvas: i, premultipliedAlpha: t = !1 } = s, d = i.getContext("2d", { willReadFrequently: !0 }), y = document.createElement("canvas"), c = y.getContext("2d", { willReadFrequently: !0 });
69
- let $ = !1, w = t;
67
+ function N(r) {
68
+ const { canvas: i, premultipliedAlpha: t = !1 } = r, a = i.getContext("2d", { willReadFrequently: !0 }), u = document.createElement("canvas"), p = u.getContext("2d", { willReadFrequently: !0 });
69
+ let S = !1, v = t;
70
70
  return {
71
- drawFrame(a) {
72
- if ($) return;
73
- const n = a.videoWidth, p = a.videoHeight, x = Math.floor(p / 2);
74
- (i.width !== n || i.height !== x) && (i.width = n, i.height = x), (y.width !== n || y.height !== p) && (y.width = n, y.height = p), c.drawImage(a, 0, 0);
75
- const F = c.getImageData(0, 0, n, x), I = c.getImageData(0, x, n, x), u = F.data, M = I.data;
76
- for (let m = 0; m < u.length; m += 4) {
77
- const q = M[m];
78
- if (w)
79
- u[m + 3] = q;
71
+ drawFrame(n) {
72
+ if (S) return;
73
+ const h = n.videoWidth, f = n.videoHeight, A = Math.floor(f / 2);
74
+ (i.width !== h || i.height !== A) && (i.width = h, i.height = A), (u.width !== h || u.height !== f) && (u.width = h, u.height = f), p.drawImage(n, 0, 0);
75
+ const F = p.getImageData(0, 0, h, A), O = p.getImageData(0, A, h, A), g = F.data, C = O.data;
76
+ for (let m = 0; m < g.length; m += 4) {
77
+ const E = C[m];
78
+ if (v)
79
+ g[m + 3] = E;
80
80
  else {
81
- const D = q / 255;
82
- u[m] = Math.round(u[m] * D), u[m + 1] = Math.round(u[m + 1] * D), u[m + 2] = Math.round(u[m + 2] * D), u[m + 3] = q;
81
+ const I = E / 255;
82
+ g[m] = Math.round(g[m] * I), g[m + 1] = Math.round(g[m + 1] * I), g[m + 2] = Math.round(g[m + 2] * I), g[m + 3] = E;
83
83
  }
84
84
  }
85
- d.putImageData(F, 0, 0);
85
+ a.putImageData(F, 0, 0);
86
86
  },
87
- setPremultipliedAlpha(a) {
88
- w = a;
87
+ setPremultipliedAlpha(n) {
88
+ v = n;
89
89
  },
90
90
  destroy() {
91
- $ || ($ = !0);
91
+ S || (S = !0);
92
92
  },
93
93
  get isDestroyed() {
94
- return $;
94
+ return S;
95
95
  }
96
96
  };
97
97
  }
98
- function K(s) {
99
- const { mode: i = "canvas", ...t } = s;
100
- return i === "svg-filter" ? z(t) : J(t);
98
+ function z(r) {
99
+ const { mode: i = "canvas", ...t } = r;
100
+ return i === "svg-filter" ? H(t) : N(t);
101
101
  }
102
- var E, g, h, b, L, A, k, C, r, R, _, T, S, f, W, U, V, G, B;
103
- class N extends HTMLElement {
102
+ const _ = [
103
+ "src",
104
+ "crossorigin",
105
+ "preload",
106
+ "autoplay",
107
+ "loop",
108
+ "muted",
109
+ "playsinline",
110
+ "poster",
111
+ "width",
112
+ "height",
113
+ "mode"
114
+ ], G = /* @__PURE__ */ new Set(["autoplay", "loop", "muted", "playsinline"]), J = [
115
+ "loadstart",
116
+ "progress",
117
+ "suspend",
118
+ "abort",
119
+ "error",
120
+ "emptied",
121
+ "stalled",
122
+ "loadedmetadata",
123
+ "loadeddata",
124
+ "canplay",
125
+ "canplaythrough",
126
+ "playing",
127
+ "waiting",
128
+ "seeking",
129
+ "seeked",
130
+ "ended",
131
+ "durationchange",
132
+ "timeupdate",
133
+ "play",
134
+ "pause",
135
+ "ratechange",
136
+ "resize",
137
+ "volumechange"
138
+ ];
139
+ var s, $, c, R, x, b, w, k, o, T, q, D, P, L, U;
140
+ class K extends HTMLElement {
104
141
  constructor() {
105
142
  super();
106
- v(this, r);
107
- v(this, E);
108
- v(this, g, null);
109
- v(this, h, null);
110
- v(this, b, null);
111
- v(this, L, null);
112
- v(this, A, null);
113
- v(this, k, !1);
114
- v(this, C, !1);
115
- v(this, f, () => {
116
- l(this, r, R).call(this);
117
- });
143
+ y(this, o);
144
+ y(this, s);
145
+ y(this, $);
146
+ y(this, c, null);
147
+ y(this, R, null);
148
+ y(this, x, null);
149
+ y(this, b, !1);
150
+ y(this, w, 0);
151
+ y(this, k, 0);
118
152
  const t = this.attachShadow({ mode: "open" });
119
- t.innerHTML = `
120
- <style>
121
- :host {
122
- display: inline-block;
123
- position: relative;
124
- }
125
- canvas {
126
- display: block;
127
- width: 100%;
128
- height: 100%;
129
- }
130
- ::slotted(video) {
131
- position: absolute;
132
- opacity: 0;
133
- pointer-events: none;
134
- width: 0;
135
- height: 0;
136
- }
137
- </style>
138
- <canvas></canvas>
139
- <slot></slot>
140
- `, o(this, E, t.querySelector("canvas"));
141
- }
142
- get premultipliedAlpha() {
143
- return this.hasAttribute("premultipliedalpha");
144
- }
145
- set premultipliedAlpha(t) {
146
- t ? this.setAttribute("premultipliedalpha", "") : this.removeAttribute("premultipliedalpha");
147
- }
148
- attributeChangedCallback(t, d, y) {
149
- var c;
150
- t === "premultipliedalpha" && ((c = e(this, g)) == null || c.setPremultipliedAlpha(this.premultipliedAlpha)), t === "mode" && (l(this, r, U).call(this), l(this, r, R).call(this));
153
+ t.innerHTML = `<style>
154
+ :host{display:inline-block;overflow:hidden}
155
+ video{position:absolute;opacity:0;pointer-events:none;width:0;height:0}
156
+ canvas{display:block;width:100%;height:100%}
157
+ </style><video></video><canvas></canvas>`, l(this, s, t.querySelector("video")), l(this, $, t.querySelector("canvas"));
158
+ for (const a of J)
159
+ e(this, s).addEventListener(a, (u) => {
160
+ this.dispatchEvent(new Event(u.type, { bubbles: !1, cancelable: !1 }));
161
+ });
151
162
  }
152
163
  connectedCallback() {
153
- o(this, L, new IntersectionObserver(
164
+ for (const t of _)
165
+ t !== "mode" && this.hasAttribute(t) && d(this, o, T).call(this, t, this.getAttribute(t));
166
+ d(this, o, q).call(this), l(this, x, new MutationObserver(() => d(this, o, q).call(this))), e(this, x).observe(this, { childList: !0 }), l(this, R, new IntersectionObserver(
154
167
  ([t]) => {
155
- o(this, k, t.isIntersecting), l(this, r, R).call(this);
168
+ l(this, b, t.isIntersecting), e(this, b) ? (d(this, o, D).call(this), d(this, o, P).call(this)) : d(this, o, L).call(this);
156
169
  },
157
170
  { threshold: 0 }
158
- )), e(this, L).observe(this), o(this, A, new MutationObserver(() => l(this, r, R).call(this))), e(this, A).observe(this, { childList: !0 }), l(this, r, R).call(this);
171
+ )), e(this, R).observe(this);
159
172
  }
160
173
  disconnectedCallback() {
161
- var t, d;
162
- (t = e(this, L)) == null || t.disconnect(), o(this, L, null), (d = e(this, A)) == null || d.disconnect(), o(this, A, null), l(this, r, S).call(this), l(this, r, U).call(this);
174
+ var t, a, u;
175
+ d(this, o, L).call(this), (t = e(this, R)) == null || t.disconnect(), l(this, R, null), (a = e(this, x)) == null || a.disconnect(), l(this, x, null), (u = e(this, c)) == null || u.destroy(), l(this, c, null);
176
+ }
177
+ attributeChangedCallback(t, a, u) {
178
+ var p;
179
+ if (t === "mode") {
180
+ (p = e(this, c)) == null || p.destroy(), l(this, c, null), e(this, b) && (d(this, o, D).call(this), d(this, o, P).call(this));
181
+ return;
182
+ }
183
+ d(this, o, T).call(this, t, u);
184
+ }
185
+ // --- Proxied properties ---
186
+ get src() {
187
+ return e(this, s).src;
188
+ }
189
+ set src(t) {
190
+ this.setAttribute("src", t);
191
+ }
192
+ get currentSrc() {
193
+ return e(this, s).currentSrc;
194
+ }
195
+ get currentTime() {
196
+ return e(this, s).currentTime;
197
+ }
198
+ set currentTime(t) {
199
+ e(this, s).currentTime = t;
200
+ }
201
+ get duration() {
202
+ return e(this, s).duration;
203
+ }
204
+ get paused() {
205
+ return e(this, s).paused;
206
+ }
207
+ get ended() {
208
+ return e(this, s).ended;
209
+ }
210
+ get readyState() {
211
+ return e(this, s).readyState;
212
+ }
213
+ get videoWidth() {
214
+ return e(this, s).videoWidth;
215
+ }
216
+ get videoHeight() {
217
+ return Math.floor(e(this, s).videoHeight / 2);
218
+ }
219
+ get volume() {
220
+ return e(this, s).volume;
221
+ }
222
+ set volume(t) {
223
+ e(this, s).volume = t;
224
+ }
225
+ get playbackRate() {
226
+ return e(this, s).playbackRate;
227
+ }
228
+ set playbackRate(t) {
229
+ e(this, s).playbackRate = t;
230
+ }
231
+ get defaultPlaybackRate() {
232
+ return e(this, s).defaultPlaybackRate;
233
+ }
234
+ set defaultPlaybackRate(t) {
235
+ e(this, s).defaultPlaybackRate = t;
236
+ }
237
+ get muted() {
238
+ return e(this, s).muted;
239
+ }
240
+ set muted(t) {
241
+ e(this, s).muted = t, this.toggleAttribute("muted", t);
242
+ }
243
+ get loop() {
244
+ return e(this, s).loop;
245
+ }
246
+ set loop(t) {
247
+ e(this, s).loop = t, this.toggleAttribute("loop", t);
248
+ }
249
+ get autoplay() {
250
+ return e(this, s).autoplay;
251
+ }
252
+ set autoplay(t) {
253
+ e(this, s).autoplay = t, this.toggleAttribute("autoplay", t);
254
+ }
255
+ get preload() {
256
+ return e(this, s).preload;
257
+ }
258
+ set preload(t) {
259
+ this.setAttribute("preload", t);
260
+ }
261
+ get crossOrigin() {
262
+ return e(this, s).crossOrigin;
263
+ }
264
+ set crossOrigin(t) {
265
+ t === null ? this.removeAttribute("crossorigin") : this.setAttribute("crossorigin", t);
266
+ }
267
+ get buffered() {
268
+ return e(this, s).buffered;
269
+ }
270
+ get seekable() {
271
+ return e(this, s).seekable;
272
+ }
273
+ get played() {
274
+ return e(this, s).played;
275
+ }
276
+ get networkState() {
277
+ return e(this, s).networkState;
278
+ }
279
+ get error() {
280
+ return e(this, s).error;
281
+ }
282
+ get seeking() {
283
+ return e(this, s).seeking;
284
+ }
285
+ // --- Proxied methods ---
286
+ play() {
287
+ return e(this, s).play();
288
+ }
289
+ pause() {
290
+ e(this, s).pause();
291
+ }
292
+ load() {
293
+ e(this, s).load();
294
+ }
295
+ canPlayType(t) {
296
+ return e(this, s).canPlayType(t);
163
297
  }
164
298
  }
165
- E = new WeakMap(), g = new WeakMap(), h = new WeakMap(), b = new WeakMap(), L = new WeakMap(), A = new WeakMap(), k = new WeakMap(), C = new WeakMap(), r = new WeakSet(), R = function() {
166
- e(this, C) || (o(this, C, !0), queueMicrotask(() => {
167
- o(this, C, !1), l(this, r, _).call(this);
168
- }));
169
- }, _ = function() {
170
- const t = this.querySelector("video");
171
- t !== e(this, h) && (l(this, r, S).call(this), o(this, h, t), t && l(this, r, T).call(this, t)), e(this, k) && e(this, h) ? (l(this, r, W).call(this), l(this, r, V).call(this), l(this, r, G).call(this)) : l(this, r, B).call(this);
172
- }, T = function(t) {
173
- t.addEventListener("play", e(this, f)), t.addEventListener("pause", e(this, f)), t.addEventListener("seeked", e(this, f)), t.addEventListener("loadeddata", e(this, f));
174
- }, S = function() {
175
- e(this, h) && (e(this, h).removeEventListener("play", e(this, f)), e(this, h).removeEventListener("pause", e(this, f)), e(this, h).removeEventListener("seeked", e(this, f)), e(this, h).removeEventListener("loadeddata", e(this, f)), o(this, h, null), l(this, r, B).call(this));
176
- }, f = new WeakMap(), W = function() {
177
- if (e(this, g) && !e(this, g).isDestroyed) return;
178
- const t = this.getAttribute("mode") ?? "canvas";
179
- o(this, g, K({
180
- canvas: e(this, E),
181
- premultipliedAlpha: this.premultipliedAlpha,
182
- mode: t
183
- }));
299
+ s = new WeakMap(), $ = new WeakMap(), c = new WeakMap(), R = new WeakMap(), x = new WeakMap(), b = new WeakMap(), w = new WeakMap(), k = new WeakMap(), o = new WeakSet(), T = function(t, a) {
300
+ G.has(t) ? e(this, s)[t] = a !== null : a === null ? e(this, s).removeAttribute(t) : e(this, s).setAttribute(t, a);
301
+ }, q = function() {
302
+ e(this, s).querySelectorAll("source").forEach((t) => t.remove()), this.querySelectorAll("source").forEach((t) => {
303
+ e(this, s).appendChild(t.cloneNode(!0));
304
+ }), e(this, s).querySelectorAll("source").length > 0 && !e(this, s).src && e(this, s).load();
305
+ }, D = function() {
306
+ if (!(e(this, c) && !e(this, c).isDestroyed))
307
+ try {
308
+ const t = this.getAttribute("mode") || "canvas";
309
+ l(this, c, z({ canvas: e(this, $), mode: t }));
310
+ } catch {
311
+ }
312
+ }, P = function() {
313
+ if (!(!e(this, b) || !e(this, c)))
314
+ if (d(this, o, L).call(this), "requestVideoFrameCallback" in e(this, s)) {
315
+ const t = () => {
316
+ !e(this, b) || !e(this, c) || (d(this, o, U).call(this), l(this, k, e(this, s).requestVideoFrameCallback(t)));
317
+ };
318
+ l(this, k, e(this, s).requestVideoFrameCallback(t));
319
+ } else {
320
+ const t = () => {
321
+ !e(this, b) || !e(this, c) || (d(this, o, U).call(this), l(this, w, requestAnimationFrame(t)));
322
+ };
323
+ l(this, w, requestAnimationFrame(t));
324
+ }
325
+ }, L = function() {
326
+ var t, a;
327
+ e(this, k) && ((a = (t = e(this, s)).cancelVideoFrameCallback) == null || a.call(t, e(this, k)), l(this, k, 0)), e(this, w) && (cancelAnimationFrame(e(this, w)), l(this, w, 0));
184
328
  }, U = function() {
185
- var t;
186
- (t = e(this, g)) == null || t.destroy(), o(this, g, null);
187
- }, V = function() {
188
- !e(this, g) || !e(this, h) || e(this, h).readyState < 2 || e(this, g).drawFrame(e(this, h));
189
- }, G = function() {
190
- if (e(this, b) !== null || !e(this, h) || e(this, h).paused) return;
191
- const t = () => {
192
- l(this, r, V).call(this), e(this, h) && !e(this, h).paused && e(this, k) ? o(this, b, requestAnimationFrame(t)) : o(this, b, null);
193
- };
194
- o(this, b, requestAnimationFrame(t));
195
- }, B = function() {
196
- e(this, b) !== null && (cancelAnimationFrame(e(this, b)), o(this, b, null));
197
- }, j(N, "observedAttributes", ["premultipliedalpha", "mode"]);
329
+ e(this, s).readyState < 2 || !e(this, c) || e(this, c).drawFrame(e(this, s));
330
+ }, B(K, "observedAttributes", [..._]);
198
331
  export {
199
- N as StackedAlphaVideoSVG,
200
- J as createCanvasFilterRenderer,
201
- K as createRenderer,
202
- z as createSvgFilterRenderer
332
+ K as AlphaVideoKitSVG,
333
+ N as createCanvasFilterRenderer,
334
+ z as createRenderer,
335
+ H as createSvgFilterRenderer
203
336
  };
package/dist/register.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";const e=require("./index.cjs");customElements.get("stacked-alpha-video-svg")||customElements.define("stacked-alpha-video-svg",e.StackedAlphaVideoSVG);
1
+ "use strict";const e=require("./index.cjs");customElements.get("alpha-video-kit-svg")||customElements.define("alpha-video-kit-svg",e.AlphaVideoKitSVG);
package/dist/register.js CHANGED
@@ -1,2 +1,2 @@
1
- import { StackedAlphaVideoSVG as e } from "./index.js";
2
- customElements.get("stacked-alpha-video-svg") || customElements.define("stacked-alpha-video-svg", e);
1
+ import { AlphaVideoKitSVG as e } from "./index.js";
2
+ customElements.get("alpha-video-kit-svg") || customElements.define("alpha-video-kit-svg", e);
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@alpha-video-kit/svg",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "SVG filter-based renderer for stacked alpha videos",
5
5
  "type": "module",
6
- "files": ["dist"],
6
+ "files": ["dist", "react.d.ts"],
7
7
  "main": "./dist/index.cjs",
8
8
  "module": "./dist/index.js",
9
9
  "types": "./dist/index.d.ts",
@@ -16,6 +16,9 @@
16
16
  "./register": {
17
17
  "import": "./dist/register.js",
18
18
  "require": "./dist/register.cjs"
19
+ },
20
+ "./react": {
21
+ "types": "./react.d.ts"
19
22
  }
20
23
  },
21
24
  "scripts": {
package/react.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ import type { AlphaVideoKitSVG } from './dist/index.js';
2
+
3
+ interface AlphaVideoKitSVGAttributes extends React.HTMLAttributes<AlphaVideoKitSVG> {
4
+ src?: string;
5
+ crossOrigin?: string;
6
+ preload?: string;
7
+ autoPlay?: boolean;
8
+ loop?: boolean;
9
+ muted?: boolean;
10
+ playsInline?: boolean;
11
+ poster?: string;
12
+ width?: number | string;
13
+ height?: number | string;
14
+ mode?: 'canvas' | 'svg-filter';
15
+ }
16
+
17
+ declare global {
18
+ namespace JSX {
19
+ interface IntrinsicElements {
20
+ 'alpha-video-kit-svg': AlphaVideoKitSVGAttributes;
21
+ }
22
+ }
23
+ }