image-in-browser
Version:
Package for encoding / decoding images, transforming images, applying filters, drawing primitives on images on the client side (no need for server Node.js)
122 lines • 4.27 kB
JavaScript
import { Point } from '../common/point.js';
export class ImageUtils {
static circleTest(p, center, rad2, antialias = true) {
let total = 0;
const dx1 = p.x - center.x;
const dy1 = p.y - center.y;
const d1 = dx1 * dx1 + dy1 * dy1;
const r1 = d1 <= rad2 ? 1 : 0;
total += r1;
const dx2 = p.x + 1 - center.x;
const dy2 = p.y - center.y;
const d2 = dx2 * dx2 + dy2 * dy2;
const r2 = d2 <= rad2 ? 1 : 0;
total += r2;
const dx3 = p.x + 1 - center.x;
const dy3 = p.y + 1 - center.y;
const d3 = dx3 * dx3 + dy3 * dy3;
const r3 = d3 <= rad2 ? 1 : 0;
total += r3;
const dx4 = p.x - center.x;
const dy4 = p.y + 1 - center.y;
const d4 = dx4 * dx4 + dy4 * dy4;
const r4 = d4 <= rad2 ? 1 : 0;
total += r4;
const dx5 = p.x + 0.5 - center.x;
const dy5 = p.y - center.y;
const d5 = dx5 * dx5 + dy5 * dy5;
const r5 = d5 <= rad2 ? 1 : 0;
total += r5;
const dx6 = p.x + 0.5 - center.x;
const dy6 = p.y + 1 - center.y;
const d6 = dx6 * dx6 + dy6 * dy6;
const r6 = d6 <= rad2 ? 1 : 0;
total += r6;
const dx7 = p.x - center.x;
const dy7 = p.y + 0.5 - center.y;
const d7 = dx7 * dx7 + dy7 * dy7;
const r7 = d7 <= rad2 ? 1 : 0;
total += r7;
const dx8 = p.x + 1 - center.x;
const dy8 = p.y + 0.5 - center.y;
const d8 = dx8 * dx8 + dy8 * dy8;
const r8 = d8 <= rad2 ? 1 : 0;
total += r8;
const dx9 = p.x + 0.5 - center.x;
const dy9 = p.y + 0.5 - center.y;
const d9 = dx9 * dx9 + dy9 * dy9;
const r9 = d9 <= rad2 ? 1 : 0;
total += r9;
return antialias ? total / 9 : total > 0 ? 1 : 0;
}
static clipLine(rect, line) {
const xmin = rect.left;
const ymin = rect.top;
const xmax = rect.right;
const ymax = rect.bottom;
const inside = 0;
const left = 1;
const right = 2;
const bottom = 4;
const top = 8;
const computeOutCode = (p) => {
let code = inside;
if (p.x < xmin) {
code |= left;
}
else if (p.x > xmax) {
code |= right;
}
if (p.y < ymin) {
code |= bottom;
}
else if (p.y > ymax) {
code |= top;
}
return code;
};
let outcode1 = computeOutCode(new Point(line.x1, line.y1));
let outcode2 = computeOutCode(new Point(line.x2, line.y2));
let accept = false;
while (true) {
if ((outcode1 | outcode2) === 0) {
accept = true;
break;
}
else if ((outcode1 & outcode2) !== 0) {
break;
}
else {
const outcodeOut = outcode1 !== 0 ? outcode1 : outcode2;
let x = 0;
let y = 0;
if ((outcodeOut & top) !== 0) {
x = line.x1 + Math.trunc((line.dx * (ymax - line.y1)) / line.dy);
y = ymax;
}
else if ((outcodeOut & bottom) !== 0) {
x = line.x1 + Math.trunc((line.dx * (ymin - line.y1)) / line.dy);
y = ymin;
}
else if ((outcodeOut & right) !== 0) {
y = line.y1 + Math.trunc((line.dy * (xmax - line.x1)) / line.dx);
x = xmax;
}
else if ((outcodeOut & left) !== 0) {
y = line.y1 + Math.trunc((line.dy * (xmin - line.x1)) / line.dx);
x = xmin;
}
if (outcodeOut === outcode1) {
line.movePoint1(x, y);
outcode1 = computeOutCode(new Point(line.x1, line.y1));
}
else {
line.movePoint2(x, y);
outcode2 = computeOutCode(new Point(line.x2, line.y2));
}
}
}
return accept;
}
}
//# sourceMappingURL=image-utils.js.map