UNPKG

anon-canvas

Version:

**anon-zone** is a reusable React component for drawing and managing mask zones over images. It is designed for applications where anonymizing part of an image is required, such as hiding personal information on medical scans (e.g., ultrasounds).

23 lines (22 loc) 9.81 kB
"use strict";var oe=Object.defineProperty;var ce=(a,s,f)=>s in a?oe(a,s,{enumerable:!0,configurable:!0,writable:!0,value:f}):a[s]=f;var M=(a,s,f)=>ce(a,typeof s!="symbol"?s+"":s,f);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const _=require("react");var X={exports:{}},$={};/** * @license React * react-jsx-runtime.production.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */var K;function se(){if(K)return $;K=1;var a=Symbol.for("react.transitional.element"),s=Symbol.for("react.fragment");function f(d,i,h){var u=null;if(h!==void 0&&(u=""+h),i.key!==void 0&&(u=""+i.key),"key"in i){h={};for(var x in i)x!=="key"&&(h[x]=i[x])}else h=i;return i=h.ref,{$$typeof:a,type:d,key:u,ref:i!==void 0?i:null,props:h}}return $.Fragment=s,$.jsx=f,$.jsxs=f,$}var L={};/** * @license React * react-jsx-runtime.development.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */var ee;function ae(){return ee||(ee=1,process.env.NODE_ENV!=="production"&&function(){function a(e){if(e==null)return null;if(typeof e=="function")return e.$$typeof===B?null:e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case A:return"Fragment";case F:return"Profiler";case U:return"StrictMode";case J:return"Suspense";case p:return"SuspenseList";case V:return"Activity"}if(typeof e=="object")switch(typeof e.tag=="number"&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),e.$$typeof){case b:return"Portal";case N:return(e.displayName||"Context")+".Provider";case q:return(e._context.displayName||"Context")+".Consumer";case w:var n=e.render;return e=e.displayName,e||(e=n.displayName||n.name||"",e=e!==""?"ForwardRef("+e+")":"ForwardRef"),e;case D:return n=e.displayName||null,n!==null?n:a(e.type)||"Memo";case W:n=e._payload,e=e._init;try{return a(e(n))}catch{}}return null}function s(e){return""+e}function f(e){try{s(e);var n=!1}catch{n=!0}if(n){n=console;var l=n.error,E=typeof Symbol=="function"&&Symbol.toStringTag&&e[Symbol.toStringTag]||e.constructor.name||"Object";return l.call(n,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",E),s(e)}}function d(e){if(e===A)return"<>";if(typeof e=="object"&&e!==null&&e.$$typeof===W)return"<...>";try{var n=a(e);return n?"<"+n+">":"<...>"}catch{return"<...>"}}function i(){var e=Y.A;return e===null?null:e.getOwner()}function h(){return Error("react-stack-top-frame")}function u(e){if(Z.call(e,"key")){var n=Object.getOwnPropertyDescriptor(e,"key").get;if(n&&n.isReactWarning)return!1}return e.key!==void 0}function x(e,n){function l(){r||(r=!0,console.error("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",n))}l.isReactWarning=!0,Object.defineProperty(e,"key",{get:l,configurable:!0})}function m(){var e=a(this.type);return R[e]||(R[e]=!0,console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")),e=this.props.ref,e!==void 0?e:null}function O(e,n,l,E,k,g,G,H){return l=g.ref,e={$$typeof:S,type:e,key:n,props:g,_owner:k},(l!==void 0?l:null)!==null?Object.defineProperty(e,"ref",{enumerable:!1,get:m}):Object.defineProperty(e,"ref",{enumerable:!1,value:null}),e._store={},Object.defineProperty(e._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(e,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(e,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:G}),Object.defineProperty(e,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:H}),Object.freeze&&(Object.freeze(e.props),Object.freeze(e)),e}function o(e,n,l,E,k,g,G,H){var v=n.children;if(v!==void 0)if(E)if(t(v)){for(E=0;E<v.length;E++)j(v[E]);Object.freeze&&Object.freeze(v)}else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else j(v);if(Z.call(n,"key")){v=a(e);var P=Object.keys(n).filter(function(ne){return ne!=="key"});E=0<P.length?"{key: someKey, "+P.join(": ..., ")+": ...}":"{key: someKey}",C[v+E]||(P=0<P.length?"{"+P.join(": ..., ")+": ...}":"{}",console.error(`A props object containing a "key" prop is being spread into JSX: let props = %s; <%s {...props} /> React keys must be passed directly to JSX without using spread: let props = %s; <%s key={someKey} {...props} />`,E,v,P,v),C[v+E]=!0)}if(v=null,l!==void 0&&(f(l),v=""+l),u(n)&&(f(n.key),v=""+n.key),"key"in n){l={};for(var Q in n)Q!=="key"&&(l[Q]=n[Q])}else l=n;return v&&x(l,typeof e=="function"?e.displayName||e.name||"Unknown":e),O(e,v,g,k,i(),l,G,H)}function j(e){typeof e=="object"&&e!==null&&e.$$typeof===S&&e._store&&(e._store.validated=1)}var T=_,S=Symbol.for("react.transitional.element"),b=Symbol.for("react.portal"),A=Symbol.for("react.fragment"),U=Symbol.for("react.strict_mode"),F=Symbol.for("react.profiler"),q=Symbol.for("react.consumer"),N=Symbol.for("react.context"),w=Symbol.for("react.forward_ref"),J=Symbol.for("react.suspense"),p=Symbol.for("react.suspense_list"),D=Symbol.for("react.memo"),W=Symbol.for("react.lazy"),V=Symbol.for("react.activity"),B=Symbol.for("react.client.reference"),Y=T.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,Z=Object.prototype.hasOwnProperty,t=Array.isArray,c=console.createTask?console.createTask:function(){return null};T={"react-stack-bottom-frame":function(e){return e()}};var r,R={},y=T["react-stack-bottom-frame"].bind(T,h)(),I=c(d(h)),C={};L.Fragment=A,L.jsx=function(e,n,l,E,k){var g=1e4>Y.recentlyCreatedOwnerStacks++;return o(e,n,l,!1,E,k,g?Error("react-stack-top-frame"):y,g?c(d(e)):I)},L.jsxs=function(e,n,l,E,k){var g=1e4>Y.recentlyCreatedOwnerStacks++;return o(e,n,l,!0,E,k,g?Error("react-stack-top-frame"):y,g?c(d(e)):I)}}()),L}var te;function ue(){return te||(te=1,process.env.NODE_ENV==="production"?X.exports=se():X.exports=ae()),X.exports}var ie=ue();class z{constructor(s){M(this,"id");M(this,"x");M(this,"y");M(this,"width");M(this,"height");this.id=crypto.randomUUID(),this.x=s.x,this.y=s.y,this.width=s.width,this.height=s.height}isInside(s){const f=this.x,d=this.x+this.width,i=this.y,h=this.y+this.height;return s.x>Math.min(f,d)&&s.x<Math.max(f,d)&&s.y>Math.min(i,h)&&s.y<Math.max(i,h)}}const le=(a,s,f)=>{const d=a.getContext("2d");if(!d)return;const i=window.devicePixelRatio||1;d.scale(i,i),d.imageSmoothingEnabled=!0,d.imageSmoothingQuality="high";const h=s??a.clientWidth,u=f??a.clientHeight;a.width=h*i,a.style.width=`${h}px`,a.height=u*i,a.style.height=`${u}px`,a.style.cursor="crosshair"},re=(a,s)=>{const f=s.getContext("2d");if(!f)return;const d=s.width,i=s.height,h=d/i,u=a.width/a.height;let x=d,m=i;u>h?(x=d,m=d/u):(m=i,x=i*u);const O=(d-x)/2,o=(i-m)/2;f.drawImage(a,O,o,x,m)},fe=_.forwardRef(({imageSrc:a,zones:s,zoneColor:f="black",className:d,width:i,height:h,...u},x)=>{const m=_.useRef(null),O=_.useRef(null),[o,j]=_.useState(null),[T,S]=_.useState(s?s.map(t=>new z(t)):[]),[b,A]=_.useState(null);_.useEffect(()=>{if(!m.current)return;const t=m.current;if(!(t==null?void 0:t.getContext("2d"))||!t)return;const r=new Image;r.crossOrigin="anonymous",r.src=a,r.alt="Anon Image",r.onload=()=>{le(t,i,h),re(r,t),O.current=r}},[m.current,i,h,a]);const U=()=>{var r;const t=(r=m.current)==null?void 0:r.getContext("2d"),c=O.current;!t||!c||(t.clearRect(0,0,t.canvas.width,t.canvas.height),re(c,m.current),t.fillStyle=f,T.forEach(R=>{t.beginPath(),t.fillRect(R.x,R.y,R.width,R.height)}))};_.useEffect(()=>{U()},[T]);const F=t=>{S(c=>[...c,t])},q=t=>{S(c=>c.filter(r=>r.id!==t))},N=t=>T.find(c=>c.isInside(t)),w=t=>{const c=m.current;if(!c)return{x:0,y:0};const r=c.getBoundingClientRect(),R=c.width/r.width,y=c.height/r.height;return{x:(t.clientX-r.left)*R,y:(t.clientY-r.top)*y}},J=t=>{var r;const c=N(w(t));c?A(c):j(w(t)),(r=u.onMouseDown)==null||r.call(u,t)},p=t=>{var c;if(o){const r=w(t);F(new z({x:(o==null?void 0:o.x)||0,y:(o==null?void 0:o.y)||0,width:r.x-((o==null?void 0:o.x)||0),height:r.y-((o==null?void 0:o.y)||0)})),j(null)}else b&&A(null);(c=u.onMouseUp)==null||c.call(u,t)},D=t=>{var c,r,R;if(o){const y={x:t.clientX-(((c=m.current)==null?void 0:c.offsetLeft)||0),y:t.clientY-(((r=m.current)==null?void 0:r.offsetTop)||0)};F(new z({x:(o==null?void 0:o.x)||0,y:(o==null?void 0:o.y)||0,width:y.x-((o==null?void 0:o.x)||0),height:y.y-((o==null?void 0:o.y)||0)}))}else b&&A(null);j(null),(R=u.onMouseLeave)==null||R.call(u,t)},W=t=>{var y,I;const c=(y=m.current)==null?void 0:y.getContext("2d"),r=w(t);if(!c)return;if(N(w(t))||b?m.current.style.cursor="pointer":m.current.style.cursor="crosshair",o){const C=r.x-o.x,e=r.y-o.y;U(),c.fillStyle=f,c.beginPath(),c.fillRect(o.x,o.y,C,e)}else b&&S(C=>C.map(e=>(e.id===(b==null?void 0:b.id)&&(e.x=r.x-b.width/2,e.y=r.y-b.height/2),e)));(I=u.onMouseMove)==null||I.call(u,t)},V=t=>{var R;const c=w(t),r=N(c);r&&q(r.id),(R=u.onDoubleClick)==null||R.call(u,t)},B=()=>{S([])},Y=async()=>{const t=m.current;return t?new Promise(c=>{t.toBlob(r=>{c(r)})}):null},Z=()=>{const t=m.current;return t?t.toDataURL("image/png",1):null};return _.useImperativeHandle(x,()=>({reset:B,toBlob:Y,toBase64:Z})),ie.jsx("canvas",{ref:m,className:d,...u,onMouseDown:J,onMouseUp:p,onMouseMove:W,onMouseLeave:D,onDoubleClick:V})});exports.AnonCanvas=fe;