-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
86 lines (75 loc) · 2.08 KB
/
script.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
window.onload = function () {
var canvas = document.getElementById("spawner");
var ctx = canvas.getContext("2d");
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight - 50;
var year = 0;
var paused = false;
var trees = 0;
document.getElementById('year').innerHTML = "Year: " + year;
const imageURL = ["tree.png", "map.png"];
const images = [];
var imageCount = 0;
function allLoaded() {
ctx.drawImage(images[1], 0, 0, images[1].width * 1.5, images[1].height * 1.5);
}
function drawTree(x, y) {
ctx.drawImage(images[0], x - 15, y - 15, images[0].width / 8, images[0].height / 8);
}
imageURL.forEach(src => {
const image = new Image();
image.src = src;
image.onload = () => {
imageCount += 1;
if (imageCount === imageURL.length) {
allLoaded();
}
}
images.push(image);
});
function findPos(obj) {
var curleft = 0, curtop = 0;
if (obj.offsetParent) {
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return { x: curleft, y: curtop };
}
return undefined;
}
function rgbToHex(r, g, b) {
if (r > 255 || g > 255 || b > 255)
throw "Invalid color component";
return ((r << 16) | (g << 8) | b).toString(16);
}
$('#spawner').mousedown(function (e) {
var pos = findPos(this);
var x = e.pageX - pos.x;
var y = e.pageY - pos.y;
drawTree(x, y)
var coord = "x=" + x + ", y=" + y;
var c = this.getContext('2d');
var p = c.getImageData(x, y, 1, 1).data;
var hex = "#" + ("000000" + rgbToHex(p[0], p[1], p[2])).slice(-6);
trees+=10;
});
function togglep() {
if (!paused) {
paused = true;
} else {
paused = false;
}
}
document.getElementById('pause').onclick = function () {
togglep();
}
function update() {
if (!paused) {
year++;
document.getElementById('year').innerHTML = "Year: " + year;
document.getElementById('trees').innerHTML = "Trees: " + trees;
}
}
setInterval(function () { update(); }, 500)
};