diff options
author | sanine <sanine.not@pm.me> | 2022-05-29 14:50:55 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-05-29 14:50:55 -0500 |
commit | 3bc2838360627f78a91a7cad81f3bc97711410c2 (patch) | |
tree | 54d260b45895c1923a785efce817b94836660f35 /src/main.js | |
parent | d35e02ecdcef6d4f120bd572f2aae36b467b7761 (diff) |
refactor: move everything into src/ subdirectory
Diffstat (limited to 'src/main.js')
-rw-r--r-- | src/main.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/main.js b/src/main.js new file mode 100644 index 0000000..ade4e27 --- /dev/null +++ b/src/main.js @@ -0,0 +1,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); +} |