Ratatoskr/src/app/components/message/pose/silhouette.ts
Void 636cad9655
Some checks failed
Build pull request / Build pull request (pull_request) Failing after 2m59s
phone layout: text wraps the traced portrait outline; status lines no longer sit under the composer
shape-outside: url() sizes the image at its natural size and anchors it at the margin box, which the
phone layout's -82px top margin moves down by the overhang, so the exclusion was the wrong size and
in the wrong place and long-post words ran under the art. PoseAvatar now traces the displayed art's
alpha (0.35, as before) into a polygon in border-box pixels, retraced on load and resize.

The pose status lines sat in a shrinkable flex box (12px high, three 18px lines) and overflowed into
the composer; the box is shrink="No".

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-07 23:27:53 +00:00

34 lines
1.5 KiB
TypeScript

const SOLID = 0.35 * 255;
const ROW = 4;
// The edge of the portrait that the text wraps against, as a polygon in border-box pixels.
// `shape-outside: url()` cannot do this: the browser sizes that image at its natural size and
// anchors it at the margin box, which the phone layout's negative top margin shifts down by the
// overhang, so the exclusion neither matches the art's size nor its place.
export const silhouette = (img: HTMLImageElement, textOnRight: boolean): string | undefined => {
const w = Math.round(img.clientWidth);
const h = Math.round(img.clientHeight);
const ctx = w > 0 && h > 0 ? document.createElement('canvas').getContext('2d') : null;
if (!ctx) return undefined;
ctx.canvas.width = w;
ctx.canvas.height = h;
ctx.drawImage(img, 0, 0, w, h);
const { data } = ctx.getImageData(0, 0, w, h);
const outer = textOnRight ? 0 : w;
const edge: string[] = [];
for (let y = 0; y <= h; y += ROW) {
let x = outer;
// A band's edge is the extreme solid pixel over its rows, so no row pokes out of the polygon.
for (let r = Math.max(0, y - ROW); r < Math.min(h, y + 1); r += 1) {
for (let i = 0; i < w; i += 1) {
const px = textOnRight ? w - 1 - i : i;
if (data[(r * w + px) * 4 + 3] > SOLID) {
x = textOnRight ? Math.max(x, px + 1) : Math.min(x, px);
break;
}
}
}
edge.push(`${x}px ${y}px`);
}
return `polygon(${outer}px 0, ${edge.join(', ')}, ${outer}px ${h}px) border-box`;
};