sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-path2d`

Chiusa

#1272 aperta il 13 mag 2021

 (2 commenti) (1 reazione) (0 assegnatari)JavaScript (468 fork)user submission
help wantednew rule

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 4h 30m) (26 PR mergiate in 30 g)

Descrizione

Before Path2D was added, when doing animation with lots of CanvasRenderingContext2D draw methods, we have to put in a function and call it again and again. But Path2D can be reused, it should be recommanded.

Fail

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

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

requestionAnimationFrame(step)

Pass

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

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

requestionAnimationFrame(step)

Not sure if it's doable in ESLint, maybe we can check a function called many CanvasRenderingContext2D draw method without variables, and suggest Path2D.

Guida contributor