The shoelace formula sums up signed triangle areas formed with the origin. The same per-edge cross product also gives the area-weighted centroid, which generally is not the same as the average of the vertices. One vertex follows the mouse.
See also: Triangle signed area — shoelace is the same cross product applied edge by edge.
signedArea = 0
cx = 0
cy = 0
for each edge (p, q):
cross = p.x * q.y - q.x * p.y
signedArea += cross
cx += (p.x + q.x) * cross
cy += (p.y + q.y) * cross
signedArea *= 0.5
cx /= 6 * signedArea
cy /= 6 * signedArea
area = |signedArea|
let signedArea = 0, cx = 0, cy = 0
for (let i = 0; i < poly.length; i++) {
const p = poly[i]
const q = poly[(i + 1) % poly.length]
const cross = p.x * q.y - q.x * p.y
signedArea += cross
cx += (p.x + q.x) * cross
cy += (p.y + q.y) * cross
}
signedArea *= 0.5
cx /= 6 * signedArea
cy /= 6 * signedArea
const area = Math.abs(signedArea)