-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestOnCanvas.js
87 lines (77 loc) · 2.45 KB
/
testOnCanvas.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
let canvas = document.getElementById("canvas");;
let ctx = canvas.getContext("2d");
let width = innerHeight/1.5; //square
let height = innerHeight/1.5;
canvas.width = width;
canvas.height = height;
let pTime = 0, mTime = 0, x = 0;
let nbAnt = 500;
let nbFood = 10;
function FpsCtrl(fps, callback) {
var delay = 1000 / fps, // calc. time per frame
time = null, // start time
frame = -1, // frame count
tref; // rAF time reference
function loop(timestamp) {
if (time === null) time = timestamp; // init start time
var seg = Math.floor((timestamp - time) / delay); // calc frame no.
if (seg > frame) { // moved to next frame?
frame = seg; // update
callback({ // callback function
time: timestamp,
frame: frame
})
}
tref = requestAnimationFrame(loop)
}
this.isPlaying = false;
this.frameRate = function(newfps) {
if (!arguments.length) return fps;
fps = newfps;
delay = 1000 / fps;
frame = -1;
time = null;
};
this.start = function() {
if (!this.isPlaying) {
this.isPlaying = true;
tref = requestAnimationFrame(loop);
}
};
this.pause = function() {
if (this.isPlaying) {
cancelAnimationFrame(tref);
this.isPlaying = false;
time = null;
frame = -1;
}
};
}
function draw() {
ctx.strokeRect(width/2,height/2,100,50);
}
function zoom(event) {
event.preventDefault();
scale += event.deltaY * -0.001;
// Restrict scale
scale = Math.min(Math.max(.125, scale), 4);
// Apply scale transform
canvas.style.transform = `scale(${scale})`;
}
var fpsTarget = 60;
var oneS = false;
var tenS = false;
var fps = new FpsCtrl(fpsTarget, function(e) {
pTime = e.time;
var x = (pTime - mTime) * 0.1;
if (x > canvas.width) mTime = pTime;
draw();
ctx.font = "12px Arial";
ctx.fillStyle = "black";
ctx.fillText("FPS: " + fps.frameRate(), ctx.canvas.width-50, 15);
ctx.strokeStyle = "black";
ctx.strokeRect(0,0,width-1,height-1);
});
fps.start();
let scale = 1;
canvas.onwheel = zoom;