-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
89 lines (71 loc) · 2.18 KB
/
main.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"use strict";
let perlinNoiseMaker = new PerlinNoise();
let canvasController= {canvas: null, context: null };
let updateButton = null;
let textureDimension = {width: 256, height: 256};
let seed =
{
offsetx : 0,
offsety : 0,
scalex : 0.05,
scaley : 0.05,
persistence: 0.5
};
let octaves = [0, 1, 2, 3];
function init()
{
canvasController.canvas = document.getElementById("canvas");
canvasController.context = canvasController.canvas.getContext("2d");
let updateButton = document.getElementById("update-button");
updateButton.addEventListener("click", ()=>
{
updateParameters();
update();
});
}
function updateParameters()
{
// Not a problem to get each input
textureDimension.width = parseFloat(document.getElementById("texture-width-input").value);
textureDimension.height = parseFloat(document.getElementById("texture-height-input").value);
seed.persistence = parseFloat(document.getElementById("persistence-input").value);
seed.offsetx = parseFloat(document.getElementById("offsetx-input").value);
seed.offsety = parseFloat(document.getElementById("offsety-input").value);
seed.scalex = parseFloat(document.getElementById("scalex-input").value);
seed.scaley = parseFloat(document.getElementById("scaley-input").value);
// Octaves
let newOctaves = [];
if(document.getElementById("octave0-checkbox").checked)
{
newOctaves.push(0);
}
if(document.getElementById("octave1-checkbox").checked)
{
newOctaves.push(1);
}
if(document.getElementById("octave2-checkbox").checked)
{
newOctaves.push(2);
}
if(document.getElementById("octave3-checkbox").checked)
{
newOctaves.push(3);
}
octaves = newOctaves;
}
function update()
{
let c = canvasController.canvas;
let ctx = canvasController.context;
let w = textureDimension.width;
let h = textureDimension.height;
c.width = w;
c.height = h;
let imgData = ctx.createImageData(w, h);
perlinNoiseMaker.getNoiseTexture(imgData, w, h, false, seed, octaves);
ctx.putImageData(imgData,0,0);
}
window.addEventListener("load", function()
{
init();
});