-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
153 lines (130 loc) · 3.26 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// AtomicJS Setup
const atomic = new Atomic('#c', window.innerWidth, 600, 1, 1, 50);
let ctx = atomic.ctx;
const DOMGravity = document.getElementById('gravity');
// setup init
function init() {
for (let i = 0; i < 100; i++) {
let randCol = 'hsl(' + Math.random() * 360 + 'deg, 50%,50%)';
atomic.Poly.box(Math.random() * window.innerWidth, Math.random() * window.innerHeight, 30, 30, {
render: { fillStyle: randCol }
});
}
// atomic.Poly.circle(Math.random() * atomic.width, 300, 40, 24, 1);
atomic.Poly.triangle(atomic.canvas.width - 550, 300, 100, 100, {
static: true,
render: {
fillStyle: 'green',
// strokeStyle : 'blue',
// lineWidth : 4
}
});
atomic.Poly.triangle(atomic.canvas.width - 400, 300, 100, 100, {
static: true,
render: {
fillStyle: 'red'
}
});
atomic.Poly.box(300, 200, 200, 20, {
static: true,
render: { fillStyle: 'tomato' }
});
}
init();
// Click To Add Box
atomic.canvas.addEventListener('click', function (e) {
let randCol = 'hsl(' + Math.random() * 360 + 'deg, 50%,50%)';
atomic.Poly.box(e.offsetX - 15, e.offsetY - 15, 30, 30, {
mass: 1,
render: {
fillStyle: randCol
}
});
})
let tmpVDraw = {};
function drawPoly() {
let tmpV = {};
let index = 0;
let interval = 0;
let isDragging = false;
let isShift = false;
window.addEventListener('keydown', function (e) {
if (e.key === 'Shift') {
isShift = true;
}
})
window.addEventListener('keyup', function (e) {
if (e.key === 'Shift') {
isShift = false;
}
})
// set isDragging flag
atomic.canvas.addEventListener('mousedown', function (e) {
isDragging = true;
})
// save {x,y} at certain intervals and render it
atomic.canvas.addEventListener('mousemove', function (e) {
interval++;
if (isDragging && isShift) {
if (interval >= 5) {
let x = e.offsetX;
let y = e.offsetY;
tmpV[index] = { x: x, y: y };
tmpVDraw[index] = { x: x, y: y };
tmpVDraw[index + 1] = tmpVDraw[0];
index++;
interval = 0;
}
}
})
// reset all variables
atomic.canvas.addEventListener('mouseup', function (e) {
if (index > 2) {
atomic.createPoly(tmpV, {
// mass : 1,
render: { fillStyle: 'deepskyblue' }
});
}
tmpV = {};
tmpVDraw = {};
index = 0;
interval = 0;
isDragging = false;
})
}
drawPoly();
function showDrawing() {
if (tmpVDraw[0]) {
ctx.beginPath();
ctx.strokeStyle = 'black';
ctx.moveTo(tmpVDraw[0].x, tmpVDraw[0].y)
for (const i in tmpVDraw) {
ctx.lineTo(tmpVDraw[i].x, tmpVDraw[i].y)
}
ctx.stroke();
ctx.closePath();
}
}
// atomic.simluationConfig({
// iterations : 10,
// velocityIteration : 5,
// constraintsIteration : 5
// })
// animate loop
function animate() {
atomic.gravity = DOMGravity.checked ? 1 : 0;
atomic.frame(animate);
atomic.update();
atomic.render();
showDrawing();
// atomic.Render.dots();
// atomic.Render.pointIndex();
// atomic.Render.lines();
// atomic.Render.indexOfBodies();
// atomic.Render.centerOfMass();
// atomic.Render.boundingBox();
// atomic.Render.information();
atomic.showFps({ x: atomic.canvas.width - 100 });
atomic.drag();
}
animate();