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).
448 lines (447 loc) • 14.3 kB
JavaScript
var se = Object.defineProperty;
var ae = (a, s, f) => s in a ? se(a, s, { enumerable: !0, configurable: !0, writable: !0, value: f }) : a[s] = f;
var P = (a, s, f) => ae(a, typeof s != "symbol" ? s + "" : s, f);
import ue, { forwardRef as ie, useRef as K, useState as Q, useEffect as ee, useImperativeHandle as le } from "react";
var Z = { exports: {} }, I = {};
/**
* @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 te;
function fe() {
if (te) return I;
te = 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 v in i)
v !== "key" && (h[v] = i[v]);
} else h = i;
return i = h.ref, {
$$typeof: a,
type: d,
key: u,
ref: i !== void 0 ? i : null,
props: h
};
}
return I.Fragment = s, I.jsx = f, I.jsxs = f, I;
}
var $ = {};
/**
* @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 re;
function de() {
return re || (re = 1, process.env.NODE_ENV !== "production" && function() {
function a(e) {
if (e == null) return null;
if (typeof e == "function")
return e.$$typeof === V ? null : e.displayName || e.name || null;
if (typeof e == "string") return e;
switch (e) {
case A:
return "Fragment";
case U:
return "Profiler";
case L:
return "StrictMode";
case p:
return "Suspense";
case q:
return "SuspenseList";
case D:
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 M:
return (e.displayName || "Context") + ".Provider";
case X:
return (e._context.displayName || "Context") + ".Consumer";
case T:
var n = e.render;
return e = e.displayName, e || (e = n.displayName || n.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e;
case J:
return n = e.displayName || null, n !== null ? n : a(e.type) || "Memo";
case F:
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, x = 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.",
x
), s(e);
}
}
function d(e) {
if (e === A) return "<>";
if (typeof e == "object" && e !== null && e.$$typeof === F)
return "<...>";
try {
var n = a(e);
return n ? "<" + n + ">" : "<...>";
} catch {
return "<...>";
}
}
function i() {
var e = N.A;
return e === null ? null : e.getOwner();
}
function h() {
return Error("react-stack-top-frame");
}
function u(e) {
if (W.call(e, "key")) {
var n = Object.getOwnPropertyDescriptor(e, "key").get;
if (n && n.isReactWarning) return !1;
}
return e.key !== void 0;
}
function v(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 S(e, n, l, x, w, _, B, G) {
return l = _.ref, e = {
$$typeof: k,
type: e,
key: n,
props: _,
_owner: w
}, (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: B
}), Object.defineProperty(e, "_debugTask", {
configurable: !1,
enumerable: !1,
writable: !0,
value: G
}), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e;
}
function o(e, n, l, x, w, _, B, G) {
var E = n.children;
if (E !== void 0)
if (x)
if (t(E)) {
for (x = 0; x < E.length; x++)
O(E[x]);
Object.freeze && Object.freeze(E);
} 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 O(E);
if (W.call(n, "key")) {
E = a(e);
var C = Object.keys(n).filter(function(ce) {
return ce !== "key";
});
x = 0 < C.length ? "{key: someKey, " + C.join(": ..., ") + ": ...}" : "{key: someKey}", j[E + x] || (C = 0 < C.length ? "{" + C.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} />`,
x,
E,
C,
E
), j[E + x] = !0);
}
if (E = null, l !== void 0 && (f(l), E = "" + l), u(n) && (f(n.key), E = "" + n.key), "key" in n) {
l = {};
for (var H in n)
H !== "key" && (l[H] = n[H]);
} else l = n;
return E && v(
l,
typeof e == "function" ? e.displayName || e.name || "Unknown" : e
), S(
e,
E,
_,
w,
i(),
l,
B,
G
);
}
function O(e) {
typeof e == "object" && e !== null && e.$$typeof === k && e._store && (e._store.validated = 1);
}
var g = ue, k = Symbol.for("react.transitional.element"), b = Symbol.for("react.portal"), A = Symbol.for("react.fragment"), L = Symbol.for("react.strict_mode"), U = Symbol.for("react.profiler"), X = Symbol.for("react.consumer"), M = Symbol.for("react.context"), T = Symbol.for("react.forward_ref"), p = Symbol.for("react.suspense"), q = Symbol.for("react.suspense_list"), J = Symbol.for("react.memo"), F = Symbol.for("react.lazy"), D = Symbol.for("react.activity"), V = Symbol.for("react.client.reference"), N = g.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, W = Object.prototype.hasOwnProperty, t = Array.isArray, c = console.createTask ? console.createTask : function() {
return null;
};
g = {
"react-stack-bottom-frame": function(e) {
return e();
}
};
var r, R = {}, y = g["react-stack-bottom-frame"].bind(
g,
h
)(), Y = c(d(h)), j = {};
$.Fragment = A, $.jsx = function(e, n, l, x, w) {
var _ = 1e4 > N.recentlyCreatedOwnerStacks++;
return o(
e,
n,
l,
!1,
x,
w,
_ ? Error("react-stack-top-frame") : y,
_ ? c(d(e)) : Y
);
}, $.jsxs = function(e, n, l, x, w) {
var _ = 1e4 > N.recentlyCreatedOwnerStacks++;
return o(
e,
n,
l,
!0,
x,
w,
_ ? Error("react-stack-top-frame") : y,
_ ? c(d(e)) : Y
);
};
}()), $;
}
var ne;
function he() {
return ne || (ne = 1, process.env.NODE_ENV === "production" ? Z.exports = fe() : Z.exports = de()), Z.exports;
}
var me = he();
class z {
constructor(s) {
P(this, "id");
P(this, "x");
P(this, "y");
P(this, "width");
P(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 Re = (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";
}, oe = (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 v = d, m = i;
u > h ? (v = d, m = d / u) : (m = i, v = i * u);
const S = (d - v) / 2, o = (i - m) / 2;
f.drawImage(a, S, o, v, m);
}, ve = ie(
({
imageSrc: a,
zones: s,
zoneColor: f = "black",
className: d,
width: i,
height: h,
...u
}, v) => {
const m = K(null), S = K(null), [o, O] = Q(null), [g, k] = Q(
s ? s.map((t) => new z(t)) : []
), [b, A] = Q(null);
ee(() => {
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 = () => {
Re(t, i, h), oe(r, t), S.current = r;
};
}, [m.current, i, h, a]);
const L = () => {
var r;
const t = (r = m.current) == null ? void 0 : r.getContext("2d"), c = S.current;
!t || !c || (t.clearRect(0, 0, t.canvas.width, t.canvas.height), oe(c, m.current), t.fillStyle = f, g.forEach((R) => {
t.beginPath(), t.fillRect(R.x, R.y, R.width, R.height);
}));
};
ee(() => {
L();
}, [g]);
const U = (t) => {
k((c) => [...c, t]);
}, X = (t) => {
k((c) => c.filter((r) => r.id !== t));
}, M = (t) => g.find((c) => c.isInside(t)), T = (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
};
}, p = (t) => {
var r;
const c = M(T(t));
c ? A(c) : O(T(t)), (r = u.onMouseDown) == null || r.call(u, t);
}, q = (t) => {
var c;
if (o) {
const r = T(t);
U(
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)
})
), O(null);
} else b && A(null);
(c = u.onMouseUp) == null || c.call(u, t);
}, J = (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)
};
U(
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);
O(null), (R = u.onMouseLeave) == null || R.call(u, t);
}, F = (t) => {
var y, Y;
const c = (y = m.current) == null ? void 0 : y.getContext("2d"), r = T(t);
if (!c) return;
if (M(T(t)) || b ? m.current.style.cursor = "pointer" : m.current.style.cursor = "crosshair", o) {
const j = r.x - o.x, e = r.y - o.y;
L(), c.fillStyle = f, c.beginPath(), c.fillRect(
o.x,
o.y,
j,
e
);
} else b && k(
(j) => j.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))
);
(Y = u.onMouseMove) == null || Y.call(u, t);
}, D = (t) => {
var R;
const c = T(t), r = M(c);
r && X(r.id), (R = u.onDoubleClick) == null || R.call(u, t);
}, V = () => {
k([]);
}, N = async () => {
const t = m.current;
return t ? new Promise((c) => {
t.toBlob((r) => {
c(r);
});
}) : null;
}, W = () => {
const t = m.current;
return t ? t.toDataURL("image/png", 1) : null;
};
return le(v, () => ({
reset: V,
toBlob: N,
toBase64: W
})), /* @__PURE__ */ me.jsx(
"canvas",
{
ref: m,
className: d,
...u,
onMouseDown: p,
onMouseUp: q,
onMouseMove: F,
onMouseLeave: J,
onDoubleClick: D
}
);
}
);
export {
ve as AnonCanvas
};