-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (57 loc) · 1.65 KB
/
script.js
File metadata and controls
65 lines (57 loc) · 1.65 KB
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
var Canvas = {
img : null,
img_name : "",
ctx : null,
grid : {
x : 25, y : 20
},
memory : [],
init : function (canvas) {
this.ctx = canvas.getContext("2d");
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
canvas.addEventListener("click", function(event) {
var mouse = getMousePos(canvas, event);
if (Canvas.img == null)
return;
console.log(Canvas.getTile(mouse));
var pos = Canvas.snapToGrid(mouse);
Canvas.drawImage(pos.x, pos.y);
Canvas.save(Canvas.getTile(mouse));
}, false);
},
save : function(mouse) {
this.memory.push({
"point" : [mouse.x, mouse.y],
"image" : this.img_name,
"solid" : true
})
},
dump : function() {
document.getElementsByName("export")[0].value = JSON.stringify(this.memory);
},
drawImage : function(x, y) {
if (this.img != null)
this.ctx.drawImage(this.img, x, y);
},
snapToGrid : function(pos) {
pos = this.getTile(pos);
return { "x" : pos.x * 32, "y" : pos.y * 32 }
},
getTile : function(pos) {
return { "x" : Math.floor(pos.x / 32), "y" : Math.floor(pos.y / 32) };
}
};
Canvas.init(document.getElementById("myCanvas"));
$(".select").on("click", function() {
Canvas.img_name = this.name;
Canvas.img = this;
});
$(".export").on("click", function() {
Canvas.dump();
});