summaryrefslogtreecommitdiff
path: root/src/modules/Mouse.js
blob: 0b8f922e6a499fbf0583db14b0ccb92363e509d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Mouse {
	constructor(ct) {
		this.ct = ct;
		this.x = 0;
		this.y = 0;
		this.radius = 0.005;
		this.show = false;
		this.pressed = false;
		this.onMove = null;

		window.addEventListener('mousemove', e => {
			/* get current transform matrix */
			const matrix = this.ct.getTransform();
			matrix.invertSelf();

			const x = e.offsetX; const y = e.offsetY;
			this.x = matrix.a*x + matrix.b*y;
			this.y = matrix.c*x + matrix.d*y;
			
			if (this.onMove) this.onMove();
		});

		const root = document.getElementById('root');

		root.addEventListener('mouseover', e => {
			e = e ? e : window.event;
			this.show = true;
		});

		root.addEventListener('mouseout', e => {
			e = e ? e: window.event;
			this.show = false;
		});
	}

	render(ct) {
		if (!this.show) return;
		console.log(this.radius);
		ct.save();

		ct.strokeStyle = '#fff';
		
		ct.beginPath();
		ct.arc(this.x, this.y, this.radius, 0, 2*Math.PI);
		ct.closePath();
		ct.stroke();

		ct.restore();
	}
}


export default Mouse;