# color wheel

[browser](/web/browser.md) ⟩ [canvas](/web/browser/canvas.md) ⟩ [graphics state](/web/browser/canvas/state.md) ⟩ [fill/stroke styles](/web/browser/canvas/state/styles.md) ⟩ [colors](/web/browser/canvas/state/styles/colors.md) ⟩ color wheel

{% tabs %}
{% tab title="💾 程式" %}
![color wheel (12 colors)](/files/QobOqGaL5Tqbq7hx2tyl)

:floppy\_disk: replit：[color wheel](https://replit.com/@pegasusroe/canvas-color-wheel#script.js)

⬆️ require： [drawOnCanvas2D()](/web/browser/canvas/+ext/drawoncanvas2d.md)

```javascript
function deg(x) { return Math.PI * x / 180 }   // degrees

// color wheel
function colorWheel(n) {
    // draw on canvas
    drawOnCanvas2D('#playground', (ctx) => {

        const dt = deg(360) / n;
        const dh = 360 / n;
        const r = 100;
        const dx = 20;
        const center = [r + dx, r + dx];

        for (let i = 0; i < n; i++) {
            const angle = deg(-90) + dt * i;
            const range = [angle - dt / 2, angle + dt / 2];
            const hue = dh * i;
            ctx.beginPath();
            ctx.moveTo(...center);
            ctx.arc(...center, r, ...range);
            ctx.fillStyle = `hsl(${hue} 100% 50%)`;
            ctx.fill();
        }

    });
}

// 12-color color wheel
colorWheel(12);
```

{% endtab %}

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

* [ ] MDN ⟩ [Canvas tutorial](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial) ⟩ [Applying styles and colors](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Applying_styles_and_colors)
  {% 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/canvas/state/styles/colors/color-wheel.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.
