# draw dots

[browser](/web/browser.md) ⟩ [event](/web/browser/event.md) ⟩ [type](/web/browser/event/type.md) ⟩ [mouse](/web/browser/event/type/mouse.md) ⟩ [click](/web/browser/event/type/mouse/click.md) ⟩ example ⟩ draw dots

{% hint style="success" %}
Every time the document is [**clicked**](/web/browser/event/type/mouse/click.md), it adds a dot.
{% endhint %}

{% tabs %}
{% tab title="💈範例" %}

* replit ⟩ [draw dots](https://replit.com/@pegasusroe/draw-dots#index.js) ,  require ⟩ [Node extension](/web/browser/dom/type/node/+ext.md)

```javascript
const { log } = console

// ⭐️ import
import { $, $all, elem } from './js/ext/Node_ext.js'; // Node extension
// --------------------------------------------------------------------

const DOT_SIZE = 16;    // in px

// ⭐️ window.onclick -> draw dot
window.onclick = (event) => {
    document.body.appendChild(elem('div', div => {
        // (pageX, pageY): relative to document (top-left corner)
        const {pageX: x,pageY: y} = event;
        div.className = 'dot';
        div.style.left = (x - DOT_SIZE/2) + "px";
        div.style.top = (y - DOT_SIZE/2) + "px";
        log(`(pageX, pageY) = (${x}, ${y})`);
    }));
};

// ⭐️ button.onclock -> clear all dots
$('button').onclick = (e) => {
    $all('.dot').forEach(node => node.remove());
    e.stopPropagation();    // prevent from drawing new dot.
    log(`dots all cleared ...`);
};
```

{% endtab %}

{% tab title="🗺️ 圖表" %}
{% embed url="<https://replit.com/@pegasusroe/draw-dots#index.js>" %}
{% endtab %}

{% tab title="👥 相關" %}

* [stop propagation](/web/browser/event/propagation/stop.md)
  {% endtab %}

{% tab title="📗 參考" %}

* [ ] Eloquent JS ⟩ [Ch. 15: Event Handling](https://eloquentjavascript.net/15_event.html) ⟩ [Mouse Clicks](https://eloquentjavascript.net/15_event.html#i_D5iwImkmyt)
  {% endtab %}

{% tab title="📘 手冊" %}

* [Event](https://developer.mozilla.org/en-US/docs/Web/API/Event) ⟩ [UIEvent](https://developer.mozilla.org/en-US/docs/Web/API/UIEvent) ⟩ [MouseEvent](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent) ⟩&#x20;
  * [click event](https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event)
    {% endtab %}
    {% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/browser/event/type/mouse/click/draw-dots.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
