-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofMain.js
198 lines (169 loc) · 4.18 KB
/
ofMain.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { SimplexNoise2D } from "https://code4fukui.github.io/SimplexNoise/SimplexNoise2D.js";
//import { PerlinNoise2D } from "https://code4fukui.github.io/PerlinNoise/PerlinNoise2D.js";
import { Vector2 } from "./Vector2.js";
import { RandomXorshift } from "https://code4fukui.github.io/RandomXorshift/RandomXorshift.js";
export const cos = Math.cos;
export const sin = Math.sin;
export const atan2 = Math.atan2;
export const PI = Math.PI;
let canvas;
let g;
if (globalThis.document) {
document.body.style.margin = 0;
document.body.style.height = "100vh";
canvas = document.createElement("canvas");
canvas.style.width = "100%";
canvas.style.height = "100%";
document.body.appendChild(canvas);
g = canvas.getContext("2d");
document.body.onclick = () => {
document.body.style.cursor = "none";
canvas.requestFullscreen();
document.addEventListener("fullscreenchange", () => {
if (!document.fullscreenElement) {
document.body.style.cursor = "default";
}
});
};
const onresize = () => {
const dpr = devicePixelRatio;
canvas.width = innerWidth * dpr;
canvas.height = innerHeight * dpr;
};
addEventListener("resize", onresize);
onresize();
}
export const OF_WINDOW = 1;
/*
export const ofSetupOpenGL = (w, h, opt) => {
canvas.width = w;
canvas.height = h;
};
*/
export const ofGetWindowSize = () => {
return new Vector2(canvas.width, canvas.height);
};
export const ofTranslate = (vec2) => {
g.translate(vec2.x, vec2.y);
};
export const ofSetColor = (color) => {
g.fillStyle = g.strokeStyle = color;
};
let fillmode = false;
let firstpoint = true;
export const ofBeginShape = () => {
firstpoint = true;
g.beginPath();
};
export const ofFill = () => {
fillmode = true;
};
export const ofNoFill = () => {
fillmode = false;
};
export const ofVertex = (vec) => {
if (firstpoint) {
g.moveTo(vec.x, vec.y);
firstpoint = false;
} else {
g.lineTo(vec.x, vec.y);
}
};
export const ofEndShape = (opt) => {
if (fillmode) {
g.fill();
} else {
g.stroke();
}
};
export const ofSetWindowTitle = (title) => {
document.title = title;
};
let bgcolor = "rgb(0,0,0)";
export const ofBackground = (color) => {
bgcolor = `rgb(${color}, ${color}, ${color})`;
};
export const ofColor = (r, g, b) => {
if (g === undefined) {
return `rgb(${r},${r},${r})`;
}
return `rgb(${r},${g},${b})`;
};
export const ofSetLineWidth = (n) => {
g.lineWidth = n;
};
const rnd = new RandomXorshift();
//rnd.setSeed(100);
const noise = new SimplexNoise2D(rnd);
//const noise = new PerlinNoise2D();
export const ofSeedRandom = (n) => {
rnd.setSeed(n);
//noise = new SimplexNoise2D(rnd);
};
export const ofRandom = (n = 1) => {
return (rnd.next() * n) >> 0;
};
let framecnt = 0;
export const ofGetFrameNum = () => {
return framecnt;
};
export const ofMap = (n, min, max, min2, max2) => {
//if (n < min) n = min;
//if (n > max) n = max;
return (n - min) / (max - min) * (max2 - min2) + min2;
};
export const ofNoise = (x, y) => {
return (noise.noise(x, y) / 2) + 0.5;
};
/*
let lastFrameTime = performance.now();
let fps;
const calcFPS = () => {
const now = performance.now();
const delta = now - lastFrameTime;
lastFrameTime = now;
fps = 1000 / delta; // FPS = 1000ms / フレーム間の時間差
};
*/
const calcFPS = async () => {
return new Promise((resolve) => {
const dts = [];
const tick = () => {
dts.push(performance.now());
if (dts.length <= 10) {
requestAnimationFrame(tick);
return;
}
let t = dts[0];
let dtsum = 0;
for (let i = 1; i < dts.length; i++) {
const dt = dts[i] - t;
t = dts[i];
dtsum += dt;
}
const fps = 1000 / (dtsum / (dts.length - 1));
resolve(fps);
};
tick();
});
};
let fps = 60; // default
export const ofGetFramePerSecond = () => {
return fps;
};
export const ofRunApp = async (app) => {
fps = await calcFPS();
console.log("fps", fps);
app.setup();
const tick = () => {
app.update();
g.save();
g.fillStyle = bgcolor;
g.fillRect(0, 0, canvas.width, canvas.height);
app.draw(fps);
g.restore();
framecnt++;
requestAnimationFrame(tick);
};
tick();
};