Skip to content

Commit 4519906

Browse files
committed
Prepare chapter 19 for editing
1 parent f090498 commit 4519906

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

19_paint.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Updates to the state are represented as objects, which we'll call _((action))s_.
5656

5757
{{index [DOM, components]}}
5858

59-
We're taking the messy task of running a ((user interface)) and applying some ((structure)) to it. Though the DOM-related pieces are still full of ((side effect))s, they are held up by a conceptually simple backbone: the state update cycle. The state determines what the DOM looks like, and the only way DOM events can change the state is by dispatching actions to the state.
59+
We're taking the messy task of running a ((user interface)) and applying ((structure)) to it. Though the DOM-related pieces are still full of ((side effect))s, they are held up by a conceptually simple backbone: the state update cycle. The state determines what the DOM looks like, and the only way DOM events can change the state is by dispatching actions to the state.
6060

6161
{{index "data flow"}}
6262

@@ -440,14 +440,16 @@ const around = [{dx: -1, dy: 0}, {dx: 1, dy: 0},
440440
function fill({x, y}, state, dispatch) {
441441
let targetColor = state.picture.pixel(x, y);
442442
let drawn = [{x, y, color: state.color}];
443+
let visited = new Set();
443444
for (let done = 0; done < drawn.length; done++) {
444445
for (let {dx, dy} of around) {
445446
let x = drawn[done].x + dx, y = drawn[done].y + dy;
446447
if (x >= 0 && x < state.picture.width &&
447448
y >= 0 && y < state.picture.height &&
448-
state.picture.pixel(x, y) == targetColor &&
449-
!drawn.some(p => p.x == x && p.y == y)) {
449+
!visited.has(x + "," + y) &&
450+
state.picture.pixel(x, y) == targetColor) {
450451
drawn.push({x, y, color: state.color});
452+
visited.add(x + "," + y);
451453
}
452454
}
453455
}
@@ -532,9 +534,7 @@ The `toDataURL` method on a canvas element creates a URL that starts with `data:
532534

533535
{{index "a (HTML tag)", "download attribute"}}
534536

535-
To actually get the browser to download the picture, we then create a ((link)) element that points at this URL and has a `download` attribute. Such links, when clicked, make the browser show a file save dialog. We add that link to the document, simulate a click on it, and remove it again.
536-
537-
You can do a lot with ((browser)) technology, but sometimes the way to do it is rather odd.
537+
To actually get the browser to download the picture, we then create a ((link)) element that points at this URL and has a `download` attribute. Such links, when clicked, make the browser show a file save dialog. We add that link to the document, simulate a click on it, and remove it again. You can do a lot with ((browser)) technology, but sometimes the way to do it is rather odd.
538538

539539
{{index "LoadButton class", control, [file, image]}}
540540

@@ -927,7 +927,7 @@ if}}
927927

928928
You can take some inspiration from the `rectangle` tool. Like that tool, you'll want to keep drawing on the _starting_ picture, rather than the current picture, when the pointer moves.
929929

930-
To figure out which pixels to color, you can use the ((Pythagorean theorem)). First figure out the distance between the current pointer position and the start position by taking the square root (`Math.sqrt`) of the sum of the square (`Math.pow(x, 2)`) of the difference in x-coordinates and the square of the difference in y-coordinates. Then loop over a square of pixels around the start position, whose sides are at least twice the ((radius)), and color those that are within the circle's radius, again using the Pythagorean formula to figure out their ((distance)) from the center.
930+
To figure out which pixels to color, you can use the ((Pythagorean theorem)). First figure out the distance between the current pointer position and the start position by taking the square root (`Math.sqrt`) of the sum of the square (`x ** 2`) of the difference in x-coordinates and the square of the difference in y-coordinates. Then loop over a square of pixels around the start position, whose sides are at least twice the ((radius)), and color those that are within the circle's radius, again using the Pythagorean formula to figure out their ((distance)) from the center.
931931

932932
Make sure you don't try to color pixels that are outside of the picture's boundaries.
933933

code/solutions/19_3_circles.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
<script>
88
function circle(pos, state, dispatch) {
99
function drawCircle(to) {
10-
let radius = Math.sqrt(Math.pow(to.x - pos.x, 2) +
11-
Math.pow(to.y - pos.y, 2));
10+
let radius = Math.sqrt((to.x - pos.x) ** 2 +
11+
(to.y - pos.y) ** 2);
1212
let radiusC = Math.ceil(radius);
1313
let drawn = [];
1414
for (let dy = -radiusC; dy <= radiusC; dy++) {
1515
for (let dx = -radiusC; dx <= radiusC; dx++) {
16-
let dist = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
16+
let dist = Math.sqrt(dx ** 2 + dy ** 2);
1717
if (dist > radius) continue;
1818
let y = pos.y + dy, x = pos.x + dx;
1919
if (y < 0 || y >= state.picture.height ||

0 commit comments

Comments
 (0)