You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 19_paint.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -56,7 +56,7 @@ Updates to the state are represented as objects, which we'll call _((action))s_.
56
56
57
57
{{index [DOM, components]}}
58
58
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.
let x = drawn[done].x + dx, y = drawn[done].y + dy;
446
447
if (x >= 0 && x < state.picture.width &&
447
448
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) {
450
451
drawn.push({x, y, color: state.color});
452
+
visited.add(x + "," + y);
451
453
}
452
454
}
453
455
}
@@ -532,9 +534,7 @@ The `toDataURL` method on a canvas element creates a URL that starts with `data:
532
534
533
535
{{index "a (HTML tag)", "download attribute"}}
534
536
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.
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.
929
929
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.
931
931
932
932
Make sure you don't try to color pixels that are outside of the picture's boundaries.
0 commit comments