Skip to content

Commit e64ba69

Browse files
committed
doc
1 parent b6777f0 commit e64ba69

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

docs/API/canvas.mdx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,70 @@ It will also create the following scene internals:
6767

6868
In recent versions of threejs, `THREE.ColorManagement.enabled` will be set to `true` to enable automatic conversion of colors according to the renderer's configured color space. R3F will handle texture color space conversion. For more on this topic, see [https://threejs.org/docs/#manual/en/introduction/Color-management](https://threejs.org/docs/#manual/en/introduction/Color-management).
6969

70+
### Size
71+
72+
By default, the Canvas will be responsive (it uses [`useMeasure`](https://www.npmjs.com/package/react-use-measure) internally).
73+
74+
Eg: inside a 1280x800 container, on a dpr=2 device:
75+
76+
```html
77+
<div style="width:1280px; height:800px;">
78+
79+
<!-- <Canvas> -->
80+
<div style="width:100%; height:100%;">
81+
<div style="width:100%; height:100%;"> <!-- container-div -->
82+
<canvas width="2560" height="1600" style="display:block; width:1280px; height:800px;"></canvas>
83+
</div>
84+
</div>
85+
86+
</div>
87+
```
88+
89+
It:
90+
0. gets the computed size of the container-div
91+
1. sets the `<canvas>` width/height attributes accordingly to 0. and the dpr
92+
2. sets the `<canvas>` CSS width/height accordingly to 0.
93+
94+
> [!NOTE]
95+
> You can also override outer-div's `style` directly:
96+
> ```html
97+
> <!-- <Canvas style={{width:1280, height:800}}> -->
98+
> <div style="width:1280px; height:800px;"> <!-- outer-div -->
99+
> <div style="width:100%; height:100%;">
100+
> <canvas width="2560" height="1600" style="display:block; width:1280px; height:800px;"></canvas>
101+
> </div>
102+
> </div>
103+
> ```
104+
105+
<details>
106+
<summary>Fixed size</summary>
107+
108+
It is also possible to fix `<canvas>`'s width/height attributes, independently from the container-div's size:
109+
110+
```html
111+
<!-- <Canvas width={800} height={600} dpr={1}> -->
112+
<div style="position:relative; width:100%; height:100%; overflow:hidden; pointer-events:auto;">
113+
<div style="width:100%; height:100%;">
114+
<canvas width="800" height="600" style="display:block; width:800px; height:600px;"></canvas>
115+
</div>
116+
</div>
117+
```
118+
119+
> [!TIP]
120+
> Using tailwindcss, you can override nested structure styles:
121+
> ```tsx
122+
> <Canvas
123+
> width={800} height={600}
124+
>. className={cn(
125+
> "!w-full !h-dvh" // outer-div
126+
>. "[&>*]:!w-full [&>*]:!h-full" // container-div
127+
> "[&>*>*]:!max-w-full [&>*>*]:!max-h-full [&>*>*]:object-contain" // <canvas>
128+
> )}
129+
> />
130+
> ```
131+
132+
</details>
133+
70134
## Errors and fallbacks
71135
72136
On some systems WebGL may not be supported, you can provide a fallback component that will be rendered instead of the canvas:

0 commit comments

Comments
 (0)