Skip to content

Instantly share code, notes, and snippets.

@steveruizok
Last active June 24, 2021 21:35
Show Gist options
  • Save steveruizok/8dee9fd046192d97a75b14a55c76421d to your computer and use it in GitHub Desktop.
Save steveruizok/8dee9fd046192d97a75b14a55c76421d to your computer and use it in GitHub Desktop.
Fill a tldraw shape with lines.
function fillRectangleWithLines(
shape: Rectangle,
gap: number,
messiness = 0.5
) {
const { size } = shape
const rng = Utils.rng(shape.shape.id)
const gapX = size[0] / (Math.floor((size[0] - gap) / gap) / 2)
const gapY = size[1] / (Math.floor((size[1] - gap) / gap) / 2)
let left = [-gap / 2, -gap / 2]
let right = [-gap / 2, -gap / 2]
const points: number[][] = []
while (true) {
points.push(
...Utils.getPointsBetween(
Vec.add(right, [rng() * gap * messiness, rng() * gap * messiness]),
left,
{
steps: 3,
ease: (t) => t * t * t,
}
)
)
if (left[0] >= size[0]) {
left[1] += gapY
} else {
left[0] += gapX
}
points.push(
...Utils.getPointsBetween(
Vec.add(left, [rng() * gap * messiness, rng() * gap * messiness]),
right,
{
steps: 3,
ease: (t) => t * t * t,
}
)
)
if (right[1] >= size[1]) {
right[0] += gapX
} else {
right[1] += gapY
}
if (right[0] >= size[0] - gap || left[1] >= size[1] - gap) {
break
}
}
return new Draw({
point: shape.point,
points,
style: {
size: SizeStyle.Large,
color: ColorStyle.Blue,
},
childIndex: shape.childIndex - 1,
})
}
@steveruizok
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment