noctcore-three/renderer-dpr-cap
noctcore-three/renderer-dpr-cap
- Package
- three v0.1.0
- Preset
- recommended
- Severity
- error
- Autofix
- No
- Suggestions
- No
- Options
- Optional
- Type info
- Not needed
A renderer’s pixel ratio is capped, never taken straight from devicePixelRatio.
The cost of a fragment shader grows with the square of the pixel ratio. A phone at
devicePixelRatio 3 shades nine pixels for every CSS pixel, 2.25 times the work of a ratio of 2,
for a difference few people can see. three.js runs at whatever ratio it is given, so the raw
ratio puts the heaviest load on the devices with the least GPU per pixel:
import { WebGLRenderer } from 'three';
const renderer = new WebGLRenderer({ antialias: true });renderer.setPixelRatio(window.devicePixelRatio);import { WebGLRenderer } from 'three';
const renderer = new WebGLRenderer({ antialias: true });renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));What it flags
Section titled “What it flags”setPixelRatio(devicePixelRatio)on any receiver (window.,globalThis.andself.too), so renderers and post-processing composers are both covered.Math.max(devicePixelRatio, n), which raises the floor and caps nothing.setPixelRatio(n)andMath.min(devicePixelRatio, n)with a literalnabovemaxDpr.- The same values in React Three Fiber’s
<Canvas dpr={...}>(theCanvasimported from@react-three/fiber, under any local name), including the max of a[min, max]pair.
import { Canvas } from '@react-three/fiber';
export const Hero = () => <Canvas dpr={window.devicePixelRatio} />;export const Product = () => <Canvas dpr={[1, 3]} />;import { Canvas } from '@react-three/fiber';
export const Hero = () => <Canvas dpr={[1, 2]} />;export const Product = () => <Canvas dpr={[1, 2]} />;What it does not flag
Section titled “What it does not flag”- Files that import neither
three(or athree/*subpath),three-stdlib, an@react-three/*package nor one ofextraModules: the rule only runs in three.js code, so another engine’s API with the same names (a Phaser or Babylon camera) is never read as three.js. Only staticimportdeclarations count: a file that loads three.js only throughimport()orrequire()is skipped, which can hide a report but never adds one. - A
<Canvas>with nodprprop: R3F already defaults to[1, 2]. - A value the rule cannot read: a variable, a store value, a function call such as R3F’s
PerformanceMonitorresult. Those are runtime decisions. - A
dprprop on any component that is not R3F’sCanvas, and a plainsetPixelRatio()function that is not a method.
import { Canvas } from '@react-three/fiber';
export const Hero = () => <Canvas />;export const Product = ({ dpr }: { dpr: number }) => <Canvas dpr={dpr} />;Capping by total pixel count (a 4K monitor at ratio 2 is still 33 million pixels) is better than capping the ratio, but it needs the canvas size at runtime, which a static rule does not have.
Options
Section titled “Options”| Option | Type | Default | Meaning |
|---|---|---|---|
maxDpr |
number |
2 |
The highest pixel ratio a renderer may run at. |
allowIn |
string[] (globs) |
[] |
Files the rule skips, matched against the absolute path and the path relative to the working directory or workspace root. |
extraModules |
string[] |
[] |
More module specifiers that mark a file as three.js code (a project module that re-exports three.js, such as @acme/engine); subpaths match too, and their imports count as three.js classes and R3F hooks. |
'noctcore-three/renderer-dpr-cap': ['error', { maxDpr: 1.5, allowIn: ['**/tools/screenshot/**'] }]When not to use it
Section titled “When not to use it”An offline renderer or a screenshot tool that renders at full device resolution on purpose: put
it in allowIn instead of turning the rule off.