Skip to content

Commit d3b89fb

Browse files
authored
Merge pull request #219 from codedex-io/curr/pretext-review
curr(pretext): Review for the Pretext library PT
2 parents ee5dd92 + 23538ca commit d3b89fb

1 file changed

Lines changed: 15 additions & 15 deletions

File tree

projects/build-a-canvas-typography-layout-with-pretext/build-a-canvas-typography-layout-with-pretext.mdx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ The traditional way to find out is to literally render the text to the screen (e
3434
Pretext solves the problem of calculating the height of a paragraph of line-wrapped text without touching the DOM at all.
3535

3636

37-
View demos of pretext here: https://chenglou.me/pretext/
37+
View demos of pretext here: [Pretext](https://www.pretext.cool/)
3838

3939
## Introduction
4040

@@ -201,7 +201,7 @@ const TEXT_WIDTH = CANVAS_WIDTH - PADDING * 2;
201201
const IMAGE_GAP = 20;
202202
```
203203

204-
After the setup, create a `const` variable called `ARTICLE`, and in quotes, paste your article or text. If you want some sample text you can use text from this repo [here]().
204+
After the setup, create a `const` variable called `ARTICLE`, and in quotes, paste your article or text. If you want some sample text you can use text from this repo [here](https://raw.githubusercontent.com/codedex-io/projects/main/projects/build-a-canvas-typography-layout-with-pretext/sample-article.txt).
205205

206206
Now, let's connect the JavaScript to the HTML elements we have:
207207

@@ -258,11 +258,11 @@ let y = PADDING;
258258
const lines = [];
259259

260260
while (true) {
261-
let lineWidth = TEXT_WIDTH;
262-
let xOffset = PADDING;
261+
let leftCut = PADDING;
262+
let rightCut = CANVAS_WIDTH - PADDING;
263263
```
264264
265-
We'll then check if each line overlaps an image.
265+
We track the left and right edges of the available text band separately so multiple images on the same line can each tighten one side without overwriting each other. We'll then check if each line overlaps an image.
266266
267267
```js
268268
for (const obs of obstacles) {
@@ -274,21 +274,21 @@ We'll then check if each line overlaps an image.
274274
const canvasCenter = CANVAS_WIDTH / 2;
275275
```
276276
277-
For the following lines of code, we're asking to detect if the image is on the right to then shrink line width, and If image is on the left to push text to the right. Depending on your desired layout or behaviour you want to execute, this could change.
277+
For the following lines of code, we're asking to detect if the image is on the right to then tighten the right edge, and if the image is on the left to push the left edge inward. Depending on your desired layout or behaviour you want to execute, this could change.
278278
279279
```js
280280
if (obsCenter > canvasCenter) {
281-
// Image on right → shrink width
282-
const availableRight = obs.left - IMAGE_GAP;
283-
lineWidth = Math.max(50, availableRight - PADDING);
281+
// Image on right → tighten the right edge
282+
rightCut = Math.min(rightCut, obs.left - IMAGE_GAP);
284283
} else {
285-
// Image on left → push text right
286-
const imgRight = obs.right + IMAGE_GAP;
287-
xOffset = Math.max(PADDING, imgRight);
288-
lineWidth = Math.max(50, CANVAS_WIDTH - PADDING - xOffset);
284+
// Image on left → push the left edge inward
285+
leftCut = Math.max(leftCut, obs.right + IMAGE_GAP);
289286
}
290287
}
291288
}
289+
290+
const xOffset = leftCut;
291+
const lineWidth = Math.max(50, rightCut - leftCut);
292292
```
293293
294294
Now, we'll ask pretext for the next line that fits using `layoutNextLineRange()` and `materializeLineRange()` to then store it.
@@ -369,8 +369,8 @@ Huge shoutout to [Cheng Lou](https://chenglou.me/) for pioneering this engineeri
369369

370370
Feel free to take a look at these resources and some ideas to expand on this current pretext project!
371371

372-
- [pretext GitHub](https://github.com/chenglou/pretext#js-repo-pjax-container) from @_chenglou
373-
- [pretext demos](https://chenglou.me/pretext/)
372+
- [pretext GitHub](https://github.com/chenglou/pretext) from @_chenglou
373+
- [pretext demos](https://www.pretext.cool/)
374374
- [more! pretext demos](https://somnai-dreams.github.io/pretext-demos/)
375375
- [this](https://github.com/exrlla/pretext-scrollabe-project) project demo repo on GitHub
376376
- [x thread on pretext projects](https://x.com/_chenglou/status/2038497396033012131)

0 commit comments

Comments
 (0)