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
|
import Voronoi from './modules/3rdparty/rhill-voronoi-core.js';
import Terrain from './modules/Terrain.js';
import Mouse from './modules/Mouse.js';
const $ = id => document.getElementById(id)
window.onload = () => {
let canvas = document.createElement('canvas');
const ct = canvas.getContext('2d');
let render;
let mouse = new Mouse(ct);
const setCanvasSize = () => {
canvas.setAttribute('width', window.innerWidth);
canvas.setAttribute('height', window.innerHeight);
const scale = Math.max(canvas.width, canvas.height);
ct.scale(scale, scale);
render();
};
window.addEventListener('resize', setCanvasSize);
const terrain = new Terrain();
render = () => {
ct.clearRect(0, 0, 1, 1);
terrain.renderGrid(ct);
mouse.render(ct);
};
mouse.onMove = () => {
terrain.applyBrush(mouse.x, mouse.y, (pt, strength) => {
const lerp = (a, b, theta) => ((theta-1)*a) + (theta*b);
pt.height = lerp(pt.height, pt.height + 10, strength);
}, 1, mouse.radius);
render();
}
setCanvasSize();
const root = $('root');
root.appendChild(canvas);
}
|