-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.js
68 lines (57 loc) · 2.45 KB
/
world.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
var World = function (ctx, canv, min, max, dT) {
this.ctx = ctx;
this.canv = canv;
this.min = min || new Vector(-100,-100);//TODO introduce world coordinates everywhere
this.max = max || new Vector(100,100);
this.dT = dT || 1;
this.t = 0;
this.drag = 0.005;//around 0.001 is reasonable, higher when interactive
this.objects = [];//list of all objects, updated in world.update
this.forceField = ()=>{return new Vector(0,0);}//function(pos){return new Vector(Math.random()/10-Math.random()/20,0.05);};//
this.mouseForceFactor = 0.12;
this.canvRect = this.canv.getBoundingClientRect();//FIXME Must be updated on windowsize change
this.update = () => {
ctx.clearRect(0,0,this.canv.width,this.canv.height);
this.t += this.dT;
this.objects.map(o => o.update(this));
this.ctx.beginPath();
this.ctx.lineWidth = 2;
this.objects.map(o => o.draw(this));
this.ctx.stroke();
}
this.addObject = obj => {
obj.draw(this);
this.objects.push(obj);
return obj;
}
this.toWorldVector = vec => {
var ret = new Vector();
ret.x = (vec.x / this.canvRect.width)*Math.abs(this.max.x - this.min.x)+this.min.x;
ret.y = (vec.y / this.canvRect.height)*Math.abs(this.max.y - this.min.y)+this.min.x;
return ret;
}
this.toCanvasCoordinates = vec => {
var ret = new Vector();
ret.x = ((vec.x-this.min.x) / Math.abs(this.max.x - this.min.x)) * this.canvRect.width;
ret.y = ((vec.y-this.min.x) / Math.abs(this.max.y - this.min.y)) * this.canvRect.height;
return ret;
}
this.normalizeMousePosition = (e, world) => {
return new Vector(e.offsetX, e.offsetY)
}
canv.onmousedown = e => {
var v = this.normalizeMousePosition(e, world)
this.objects.map(o => {if(o.mousedown) o.mousedown(v)})
};
canv.onmousemove = e => {
var v = this.normalizeMousePosition(e, world);
this.objects.map(o => {if(o.mousemove) o.mousemove(v)})
};
canv.onmouseup = e => {
var v = this.normalizeMousePosition(e, world)
this.objects.map(o => {if(o.mouseup) o.mouseup(v)})
};
canv.ontouchstart = e => {e.preventDefault(); canv.onmousedown(e);}
canv.ontouchmove = e => {e.preventDefault(); canv.onmousemove(e);}
canv.ontouchend = e => {e.preventDefault(); canv.onmouseup(e);}
}