-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrawing.js
84 lines (78 loc) · 2.31 KB
/
drawing.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
var body= new Box2D.b2BodyDef();
var edgeshape=[];
var count=0;
var bodycreated;
var verticesList=[];
var embox2dTest_drawing = function() {
//constructor
}
embox2dTest_drawing.prototype.setNiceViewCenter = function() {
//called once when the user changes to this test from another test
PTM = 30;
setViewCenterWorld( new b2Vec2(0,0), true );
}
embox2dTest_drawing.prototype.setup = function() {
//set up the Box2D scene here - the world is already created
canvas.onmousedown=function()
{
edgeshape=[];
verticesList=[];
context.save();
context.setTransform(1,0,0,1,0,0);
var x=event.pageX-canvas.offsetLeft;
var y=event.pageY-canvas.offsetTop;
context.beginPath();
context.moveTo(x,y);
body.set_position(new b2Vec2(0,0));
body.set_type(b2_dynamicBody);
canvas.onmousemove=function()
{
var x=event.pageX-canvas.offsetLeft;
var y=event.pageY-canvas.offsetTop;
verticesList.push({ x : mousePosWorld.x, y : mousePosWorld.y});
context.strokeStyle='white';
//run= false;
context.lineWidth=1;
count++;
if(count%10==0){
context.lineTo(x,y);
context.stroke();
edgeshape.push(new b2Vec2(mousePosWorld.x,mousePosWorld.y));
}
}
}
canvas.onmouseup=function()
{
context.restore();
//console.log('mouseup');
canvas.onmousemove=function()
{
var x=mousePosPixel.x;
var y=mousePosPixel.y;
}
if(edgeshape.length !== 0){
shape= new createChainShape(edgeshape,false);
console.log(shape);
bodycreated=world.CreateBody(body);
bodycreated.CreateFixture(shape,1.0);
bodycreated.userData={'verticesList' : verticesList};
console.log(verticesList.length);
}
for(var j in edgeshape){
Box2D.destroy(edgeshape[j]);
}
}
}
embox2dTest_drawing.prototype.step = function() {
//this function will be called at the beginning of every time step
}
embox2dTest_drawing.prototype.onKeyDown = function(canvas, evt) {
if ( evt.keyCode == 65 ) { // 'a'
//do something when the 'a' key is pressed
}
}
embox2dTest_drawing.prototype.onKeyUp = function(canvas, evt) {
if ( evt.keyCode == 65 ) { // 'a'
//do something when the 'a' key is released
}
}