JSFiddle - React, Tailwind, and code Playground

HTML

<!DOCTYPE html>
<html lang="de">

<meta charset="utf-8">
<title>e-Mail</title>

<body>

<canvas width="800" height="600" id="cv"></canvas>
</body>

</html>

JavaScript

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
}

class Vector {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
    
    mag() {
        return Math.hypot(this.x, this.y);
    }
    
    norm() {
        let a = this.mag();
        
        if (a*a < 1.0e-16) a = 1.0;
        
        return new Vector(this.x / a, this.y / a);
    }
}

class Segment {
    constructor(p, q) {
        this.p = p;
        this.q = q;
    }
}

function draw(cx, x) {
    if (x instanceof Point) {
        cx.fillRect(x.x - 2, x.y - 2, 5, 5);
    }
    
    if (x instanceof Segment) {
        cx.beginPath();
        cx.moveTo(x.p.x, x.p.y);
        cx.lineTo(x.q.x, x.q.y);
        cx.stroke();
    }
}

function diff(p, q) {
    return new Vector(q.x - p.x, q.y - p.y);
}

function dot(u, v) {
    return u.x * v.x + u.y * v.y;
}

function cross(u, v) {
    return u.x * v.y - u.y * v.x;
}

function within(p, dx, r) {
    let x = p.x - dx;
    let y = p.y;
    
    return x*x + y*y <= r*r;
}

function rot(p, u) {
    return new Point(p.x * u.x + p.y * u.y,
                    -p.x * u.y + p.y * u.x);
}


function collision(s, t, r) {
    let ds = diff(s.p, s.q);
    let ss = ds.mag();    
    let u = ds.norm();
    
    let a0 = rot(diff(s.p, t.p), u);
    let a1 = rot(diff(s.p, t.q), u);
    
    if (-r <= a0.y && a0.y <= r && -r <= a0.x && a0.x <= ss + r) {
        if (a0.x < 0) return within(a0, 0, r);
        if (a0.x > ss) return within(a0, ss, r);
        return true;
    }
    
    if (-r <= a1.y && a1.y <= r && -r <= a1.x && a1.x <= ss + r) {
        if (a1.x < 0) return within(a1, 0, r);
        if (a1.x > ss) return within(a1, ss, r);
        return true;
    }
    
    if (a0.y * a1.y < -0.9 * r*r) {        
        let a = -a0.y * (a1.x - a0.x) / (a1.y - a0.y) + a0.x;
        
        if (0 <= a && a <= ss) return true;
    }
        
    let dt = diff(t.p, t.q);
    let tt = dt.mag();    
    let v = dt.norm();
    
  ...