# prefer-path2d

Rule `unicorn/prefer-path2d`. Prefer `Path2D` for repeatedly drawn canvas paths.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 9.9M weekly downloads of presets that enable it
- Trend: +28.3% last quarter
- Rank: #987 by reach, #1242 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-path2d` | no | no | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-path2d.md) |

## Code examples

### Incorrect: the rule reports this

```js
function draw() {
  context.moveTo(220, 60);
  context.arc(170, 60, 50, 0, 2 * Math.PI);
  context.stroke();
}

function step() {
  draw();
  requestAnimationFrame(step);
}

requestAnimationFrame(step);
```

### Correct: the rule accepts this

```js
const path = new Path2D();
path.moveTo(220, 60);
path.arc(170, 60, 50, 0, 2 * Math.PI);

function step() {
  context.stroke(path);
  requestAnimationFrame(step);
}

requestAnimationFrame(step);
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-path2d": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [Unicorn unopinionated](https://lintradar.dev/presets/unicorn-unopinionated.md) | ESLint | `eslint-plugin-unicorn` | 236 | 9.5M | +30.3% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-object-iterable-methods](https://lintradar.dev/rules/unicorn/prefer-object-iterable-methods.md): Prefer the most specific `Object` iterable method.
- [unicorn/prefer-promise-static-methods](https://lintradar.dev/rules/unicorn/prefer-promise-static-methods.md): Prefer `Promise.resolve()` and `Promise.reject()` over trivial `new Promise()` calls.
- [unicorn/prefer-object-define-properties](https://lintradar.dev/rules/unicorn/prefer-object-define-properties.md): Prefer `Object.defineProperties()` over multiple `Object.defineProperty()` calls.
- [unicorn/prefer-promise-with-resolvers](https://lintradar.dev/rules/unicorn/prefer-promise-with-resolvers.md): Prefer `Promise.withResolvers()` when extracting resolver functions from `new Promise()`.
- [unicorn/prefer-minimal-ternary](https://lintradar.dev/rules/unicorn/prefer-minimal-ternary.md): Prefer moving ternaries into the minimal varying part of an expression.
- [unicorn/prefer-queue-microtask](https://lintradar.dev/rules/unicorn/prefer-queue-microtask.md): Prefer `queueMicrotask()` over `process.nextTick()`, `setImmediate()`, and `setTimeout(…, 0)`.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-path2d. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
