summaryrefslogtreecommitdiff
path: root/src/main.js
blob: 4527a90e869fab078f8440cf648e008ec502dfd9 (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
import Terrain from './Terrain.js';
import Canvas from './Canvas.js';

const $ = id => document.getElementById(id)

window.onload = () => {
	const canvas = new Canvas('root');
	const terrain = new Terrain();

	const BRUSH_RADIUS = 80;
	let brushing = false;
	canvas.onMouseDown = e => {
		if (e.button == 0) brushing = true;
	};
	canvas.onMouseUp = e => {
		if (e.button == 0) brushing = false;
	};

	const pos = canvas.mouse.drawingPos;
	canvas.onMouseMove = () => {
		if (brushing)
			terrain.applyBrush(pos.x, pos.y, (pt, strength) => {
				pt.hue += 10 * strength;		
			}, 1, canvas.pixelsToUnits(BRUSH_RADIUS));
		canvas.draw();
	};
	
	canvas.onDraw = ct => {
		terrain.renderGrid(ct);

		ct.strokeStyle = '#fff';
		ct.lineWidth = canvas.pixelsToUnits(1);
		ct.beginPath();
		ct.arc(pos.x, pos.y, canvas.pixelsToUnits(BRUSH_RADIUS), 0, 2*Math.PI);
		ct.closePath();
		ct.stroke();
	};
	canvas.draw();
}