Skip to content

Commit

Permalink
WIP mask check
Browse files Browse the repository at this point in the history
  • Loading branch information
leomcelroy committed Oct 31, 2023
1 parent 9c1405d commit 4bfff18
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
53 changes: 34 additions & 19 deletions astro/src/components/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,46 @@ export default function Toolbar() {
const { turtles } = getStore();
const { isVisible } = createMask();

const newTurtle = new Turtle();
let lastVisible = false;
// const newTurtle = new Turtle();
// let lastVisible = false;

// turtles.forEach(turtle => {
// turtle
// .resample(0.1)
// .path.forEach(pl => {
// pl.forEach((pt, i) => {
// const [x, y] = pt;
// const visible = isVisible(x, y);

// if (lastVisible && i > 0 && visible) newTurtle.goTo([x, y]);
// else newTurtle.jump([x, y]);
// lastVisible = visible;
// })
// })
// })

// newTurtle.simplify(.01);
// newTurtle.style.fill ="none";
// newTurtle.style.stroke = "black";

// patchStore({
// turtles: [newTurtle]
// })

turtles.forEach(turtle => {
turtle
.resample(0.1)
.path.forEach(pl => {
pl.forEach((pt, i) => {
const [x, y] = pt;
const visible = isVisible(x, y);

if (lastVisible && i > 0 && visible) newTurtle.goTo([x, y]);
else newTurtle.jump([x, y]);
lastVisible = visible;
})
})
})
.resample(0.01)
.iteratePath(([x, y], t) => {
const visible = isVisible(x, y);

newTurtle.simplify(.01);
newTurtle.style.fill ="none";
newTurtle.style.stroke = "black";
if (!visible) return "BREAK";
})

patchStore({
turtles: [newTurtle]
// turtle.simplify(0.01);
turtle.style.fill = "none";
})

patchStore({ turtles });

}}>
cull hidden lines
Expand Down
32 changes: 12 additions & 20 deletions astro/src/lib/getBitmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ const SCALE = DPI/25.4;

export function getBitmap() {
const { turtles, docDimensions } = getStore()
const w = docDimensions.width*SCALE;
const h = docDimensions.height*SCALE;

// make min 0, 0

const canvas = document.createElement('canvas');
canvas.width = docDimensions.width*SCALE;
canvas.height = docDimensions.height*SCALE;
canvas.width = w;
canvas.height = h;

const ctx = canvas.getContext('2d');
// ctx.transform(1, 0, 0, -1, 0, canvas.height)
Expand Down Expand Up @@ -47,6 +49,8 @@ export function getBitmap() {
top: 0;
`

// document.body.appendChild(canvas);

// console.log(canvas);

// setTimeout(() => {
Expand All @@ -68,7 +72,7 @@ export function createMask() {
}

const avgGrey = (x, y) => {
const colors = ctx.getImageData(x*SCALE-1, y*SCALE-1, 3, 3);
const colors = ctx.getImageData(x*SCALE, y*SCALE, 1, 1);
const { data } = colors;

let totalGrey = 0;
Expand All @@ -94,25 +98,13 @@ export function createMask() {
}

const isVisible = (x, y) => {
const scaleX = x*SCALE;
const scaleY = y*SCALE;
const pixel0 = ctx.getImageData(scaleX, scaleY, 1, 1).data;
const isWhite0 = pixel0.every(v => v === 255);
const pixel1 = ctx.getImageData(scaleX-1, scaleY+1, 1, 1).data;
const isWhite1 = pixel1.every(v => v === 255);
const pixel2 = ctx.getImageData(scaleX+1, scaleY-1, 1, 1).data;
const isWhite2 = pixel2.every(v => v === 255);
const pixel3 = ctx.getImageData(scaleX-1, scaleY-1, 1, 1).data;
const isWhite3 = pixel3.every(v => v === 255);
const pixel4 = ctx.getImageData(scaleX+1, scaleY+1, 1, 1).data;
const isWhite4 = pixel4.every(v => v === 255);

// circle(scaleX, scaleY, isWhite ? "red" : "black", 4);

// const pixelAfter = ctx.getImageData(scaleX, scaleY, 1, 1).data;
const scaleX = Math.floor(x*SCALE);
const scaleY = Math.floor(y*SCALE);
const pixels = ctx.getImageData(scaleX-1, scaleY-1, 3, 3).data;

return !isWhite0 || !isWhite1 || !isWhite2 || !isWhite3 || !isWhite4;
const isWhite = pixels.every(v => v === 255);

return !isWhite;
}

function getPixelGrayValue(x, y) {
Expand Down

0 comments on commit 4bfff18

Please sign in to comment.