-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·114 lines (99 loc) · 2.3 KB
/
main.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
import { startAccelerometerIncludingGravity } from './accelerometer.js';
import { Ragdoll } from './ragdoll.js';
class TimeDelta {
constructor(max) {
this.t = new Date();
this.max = max;
}
get delta() {
let t2 = new Date();
let d = t2 - this.t;
this.t = t2;
return d > max ? max : d;
}
}
class MyChalky extends HTMLElement {
constructor() {
super();
let shadow = this.attachShadow({ mode: 'open' });
const wrapper = document.createElement('div');
const canvas = document.createElement('canvas');
wrapper.setAttribute('id', 'wrapper');
canvas.setAttribute('id', 'canvas');
const ctx = canvas.getContext('2d');
var style = document.createElement('style');
style.textContent = `
#canvas {
border:1px solid black;
}
#wrapper {
padding: 0;
border:1px solid green;
width: inherit;
height: inherit;
overflow: hidden;
resize: both;
}
`;
this.acl = {
x: 0,
y: 0,
z: 0
};
const myChalky = new Ragdoll(
Math.floor(canvas.width/2),
Math.floor(canvas.height/2),
1
);
myChalky.nodes[3].pinned = true;
const time = new TimeDelta();
const animation = () => {
canvas.height = wrapper.offsetHeight;
canvas.width = wrapper.offsetWidth;
myChalky.computePhysics(time.delta / 75, this.acl);
myChalky.draw(ctx);
requestAnimationFrame(animation);
}
requestAnimationFrame(animation);
canvas.addEventListener("click",
() => {
canvas.setAttribute('style', 'background:yellow;');
startAccelerometerIncludingGravity(
e => {
const so = window.orientation;console.log(so)
this.acl.x = so === 0 || so === 180 ? -e?.x : -e?.y;
this.acl.y = so === 0 || so === 180 ? -e?.y : -e?.x;
switch(so) {
case 0:
this.acl.x = e?.x;
this.acl.y = -e?.y;
break;
case 90:
this.acl.x = -e?.y;
this.acl.y = -e?.x;
break;
case 180:
this.acl.x = -e?.x;
this.acl.y = e?.y;
break;
case -90:
this.acl.x = e?.y;
this.acl.y = e?.x;
break;
}
}
);
}, {
once: true
}
);
// Attatch elements
shadow.appendChild(style);
shadow.appendChild(wrapper);
wrapper.appendChild(canvas);
}
// connectedCallback() {
// console.log("Connected")
// }
}
window.customElements.define('my-chalky', MyChalky);