# 04. Textures from a canvas, explained

**Goal:** put images on surfaces, using a texture drawn with the 2D canvas API so the example needs no image files. Learn color space, wrapping, repeating and filtering.

## Walkthrough

```js
function makeCheckerCanvas(size = 256, squares = 8) { ... }
```
Creates an off-screen `<canvas>` and draws a checkerboard plus text with the ordinary 2D API. Anything you can draw on a canvas (charts, text labels, generated noise) can become a texture. Keep texture sizes at powers of two (128, 256, 512, 1024...) where you can: WebGL 2 no longer requires it, but mipmapping and memory use are most predictable that way.

```js
const texture = new THREE.CanvasTexture(makeCheckerCanvas());
texture.colorSpace = THREE.SRGBColorSpace;
```
`CanvasTexture` wraps the canvas and uploads it to the GPU. **Color textures (albedo/`map`, `emissiveMap`) must be marked sRGB**; data textures (normal, roughness, metalness, AO maps) stay linear (the default). Forgetting this makes colors look washed out. `TextureLoader` images need the same line. Textures loaded by `GLTFLoader` are configured for you.

```js
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
```
Anisotropic filtering keeps textures sharp when viewed at steep angles (floors, roads). The maximum is hardware-dependent, commonly 16.

```js
new THREE.MeshStandardMaterial({ map: texture })
```
`map` is the base color texture. Other slots: `normalMap`, `roughnessMap`, `metalnessMap`, `aoMap`, `emissiveMap`, `alphaMap`, `displacementMap`.

```js
floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping;
floorTexture.repeat.set(10, 10);
```
UV coordinates normally run 0 to 1 across a face. `repeat` multiplies them; `RepeatWrapping` tiles the image instead of stretching the edge pixels (`ClampToEdgeWrapping`, the default). `MirroredRepeatWrapping` flips every other tile.

```js
floorTexture.magFilter = THREE.NearestFilter;
```
`magFilter` controls how a texture is enlarged: `LinearFilter` (default) blends pixels; `NearestFilter` keeps hard edges for pixel art. `minFilter` controls shrinking and uses mipmaps by default.

```js
floorTexture.offset.x = (time / 20000) % 1;
```
Changing `offset`, `repeat` or `rotation` animates a texture without re-uploading it, handy for conveyor belts, water or scrolling backgrounds.

## Loading image files

```js
const loader = new THREE.TextureLoader();
const photo = await loader.loadAsync('textures/wood.jpg');
photo.colorSpace = THREE.SRGBColorSpace;
material.map = photo;
material.needsUpdate = true; // only needed when adding a map to a material that was already rendered without one
```
Image files must be served over HTTP (not opened with `file://`) and from the same origin or with CORS headers.

## Texture formats compared

| Format | Pros | Cons | Use for |
| --- | --- | --- | --- |
| JPEG | Small, universal | No transparency, lossy | Photos, color maps |
| PNG | Lossless, transparency | Larger | UI, decals, masks |
| WebP / AVIF | Smaller than JPEG/PNG | Encode step needed | Web delivery of color maps |
| KTX2 (Basis Universal) | Stays compressed on the GPU, much less VRAM | Needs `KTX2Loader` and transcoder files | Large scenes, mobile |
| HDR / EXR | High dynamic range | Large files | Environment maps (lighting) |

## Try this

1. Change `repeat.set(10, 10)` to `(2, 2)` and switch `magFilter` back to `LinearFilter`.
2. Draw the current time on the canvas every second and set `texture.needsUpdate = true` to make a live display.
3. Remove the `colorSpace` line and compare the colors.
