p5.Vector.random2D() 是如何工作的?

How does p5.Vector.random2D() work under the hood?

如何在纯 vanilla JS 中编写 p5.js 函数 p5.Vector.random2D()?我试图在不使用 p5.js

的情况下以常规 canvas 重新创建它

代码来自 github/p5.js

Vector.random2D = function random2D() {
  let angle = Math.random() * constants.TWO_PI;
  let length = 1;
  return {x: length * Math.cos(angle), y: length * Math.sin(angle)};
};