Skip to content

Commit f91b22a

Browse files
committed
updates
1 parent 57b5696 commit f91b22a

9 files changed

+24
-28
lines changed

2-ui/1-document/11-coordinates/article.md

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@ To move elements around we should be familiar with coordinates.
55
Most JavaScript methods deal with one of two coordinate systems:
66

77
1. **Relative to the window** - similar to `position:fixed`, calculated from the window top/left edge.
8-
- we'll denote these coordinates as `clientX/clientY`, the reason for such name will become clear after next chapters ,
8+
- we'll denote these coordinates as `clientX/clientY`, the reasoning for such name will become clear later, when we study event properties.
99
2. **Relative to the document** - similar to `position:absolute` in the document root, calculated from the document top/left edge.
10-
- we'll denote them `pageX/pageY`
10+
- we'll denote them `pageX/pageY`.
1111

12-
When the page is scrolled to the top, so that the window top/left corner is exactly the document top/left corner, these coordinates equal each other. But after the document shifts, window-relative coordinates of elements change, as elements move across the window.
12+
When the page is scrolled to the top, so that the top/left corner of the window is exactly the document top/left corner, these coordinates equal each other. But after the document shifts, window-relative coordinates of elements change, as elements move across the window, while document-relative coordinates remain the same.
1313

14-
Here's the picture, the left part is before the scroll, and the right part - after scroll up:
14+
Here's the picture, the left part is before the scroll, and the right part - after scrolling the page up:
1515

1616
![](document-and-window-coordinates-scrolled.png)
1717

1818
As the document moved up:
19-
- `pageY` - document-relative coordinate of the same point is still the same, it's counted from the document top (scrolled out).
20-
- `clientY` - window-relative coordinate did change (the arrow became shorter), as the same point now became closer to window top.
19+
- `pageY` - document-relative coordinate of the same point stayed the same, it's counted from the document top (now scrolled out).
20+
- `clientY` - window-relative coordinate did change (the arrow became shorter), as the same point became closer to window top.
2121

2222
We'll see more examples of window and document coordinates through this chapter.
2323

2424
## Element coordinates: getBoundingClientRect
2525

2626
The method `elem.getBoundingClientRect()` returns window coordinates for a minimal rectangle that encloses `elem` as an object of built-in [DOMRect](https://www.w3.org/TR/geometry-1/#domrect) class.
2727

28-
Main `DomRect` properties:
28+
Main `DOMRect` properties:
2929

3030
- `x/y` -- X/Y-coordinates of the rectangle origin relative to window,
3131
- `width/height` -- width/height of the rectangle (can be negative).
@@ -36,7 +36,7 @@ Additionally, there are derived properties:
3636
- `left/right` -- X-coordinate for the left/right edge.
3737

3838
```online
39-
Click the button to see its window coordinates:
39+
For instance click this button to see its window coordinates:
4040
4141
<p><input id="brTest" type="button" value="Get coordinates using button.getBoundingClientRect() for this button" onclick='showRect(this)'/></p>
4242
@@ -55,42 +55,38 @@ right:${r.right}
5555
}
5656
</script>
5757
58-
Please, scroll the page and repeat. You will notice that as window-relative button position changes, its window coordinates (`y/top/bottom` if you scroll vertically) change as well.
58+
If you scroll the page and repeat, you'll notice that as window-relative button position changes, its window coordinates (`y/top/bottom` if you scroll vertically) change as well.
5959
```
6060

61-
Here's the picture:
61+
Here's the picture of `elem.getBoundingClientRect()` output:
6262

6363
![](coordinates.png)
6464

65-
As you can see, derived properties can be easily calculated:
66-
- `top = y`
65+
As you can see, `x/y` and `width/height` fully describe the rectangle. Derived properties can be easily calculated:
6766
- `left = x`
68-
- `bottom = y + height`
67+
- `top = y`
6968
- `right = x + width`
69+
- `bottom = y + height`
70+
71+
Also:
72+
73+
- Coordinates may be decimal fractions, such as `10.5`. That's normal, internally browser uses fractions in calculations. We don't have to round them when setting to `style.position.left/top`.
74+
- Coordinates may be negative. For instance, if the page is scrolled down and the top `elem` is now above the window. Then, `elem.getBoundingClientRect().top` is negative.
7075

71-
**Why derived properties are needed? Why does `top/left` exist if there's `x/y`?**
76+
```smart header="Why derived properties are needed? Why does `top/left` exist if there's `x/y`?"
7277

7378
The reason is that technically it's possible for `width/height` to be negative. A rectangle starts at `(x,y)` and `(width,height)` is actually the direction vector where it goes.
7479

75-
That may be useful e.g. for mouse selections, to properly mark selection start and end.
80+
Negative `width/height` may be useful for directed rectangles, e.g. to represent mouse selections with properly marked start and end.
7681

77-
Here's a vectorized example with negative `width` and `height`:
82+
Here's a rectangle with negative `width` and `height` (e.g. `width=-200`, `height=-100`):
7883

7984
![](coordinates-negative.png)
8085

8186
The rectangle above starts at its bottom-right corner and then spans left/up.
8287

83-
As you can see, `left/top` are not `x/y` any more. The formula can be adjusted to cover negative `width/height`. That's simple enough to do, but rarely needed.
84-
85-
**All element coordinates are returned with positive width/height.**
86-
87-
The main reason why negative rectangles are covered here is to explain the need for derived propeties. Otherwise it would be odd to see `top/left` duplicating `x/y`.
88-
89-
Also:
90-
91-
- Coordinates may be decimal fractions, such as `10.5`. That's normal, internally browser uses fractions in calculations. We don't have to round them when setting to `style.position.left/top`.
92-
- Coordinates may be negative. For instance, if the page is scrolled down and the top `elem` is now above the window. Then, `elem.getBoundingClientRect().top` is negative.
93-
88+
As you can see, `left/top` are not `x/y` here. So these are actually not duplicates. The formula can be adjusted to cover negative `width/height`. That's simple enough to do, but rarely needed, as the result of `elem.getBoundingClientRect()` always has positive width/height.
89+
```
9490
9591
```warn header="Internet Explorer and Edge: no support for `x/y`"
9692
Internet Explorer and Edge don't support `x/y` properties for historical reasons.
Loading
Loading
-21 Bytes
Loading
-27 Bytes
Loading
Loading
Loading

2-ui/3-event-details/4-mouse-drag-and-drop/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,4 @@ We can lay a lot on this foundation.
294294
- We can use event delegation for `mousedown/up`. A large-area event handler that checks `event.target` can manage Drag'n'Drop for hundreds of elements.
295295
- And so on.
296296

297-
There are frameworks that build architecture over it: `DragZone`, `Droppable`, `Draggable` and other classes. Most of them do the similar stuff to described above, so it should be easy to understand them now. Or roll our own, because you already know how to handle the process, and it may be more flexible than to adapt something else.
297+
There are frameworks that build architecture over it: `DragZone`, `Droppable`, `Draggable` and other classes. Most of them do the similar stuff to described above, so it should be easy to understand them now. Or roll our own, as you can see that's easy enough to do, maybe easier than adapting something else.

figures.sketch

804 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)