-
Notifications
You must be signed in to change notification settings - Fork 41
/
eventRate.js
169 lines (151 loc) · 4.11 KB
/
eventRate.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
var points = [];
var coalescedPoints = [];
var eventCount = 0;
var startTime;
var canvas;
var frameCounter;
function GetContext() {
return document.getElementById("canvas").getContext("2d");
}
window.addEventListener('resize', function(e) {
InitializeCanvas();
});
function drawPoints(points, isCoalesced) {
var context = canvas.getContext('2d');
for (var i = 0; i < points.length; ++i) {
var radius = isCoalesced ? 2.0 : 4.0;
context.beginPath();
context.arc(points[i].x * scale, points[i].y * scale, radius * scale, 0, 2 * 3.14159, false);
context.closePath();
if (isCoalesced)
context.fillStyle = 'rgba(100,0,0,0.5)';
else
context.fillStyle = 'rgba(0,0,100,0.5)';
context.fill();
}
}
function onFrame()
{
frameCounter++;
if (!startTime || frameCounter % parseInt(document.getElementById('throttle').value) == 0) {
drawPoints(points, false);
drawPoints(coalescedPoints, true);
points = [];
coalescedPoints = [];
var extraWork = parseInt(document.getElementById('work').value);
if (extraWork > 0) {
var start = Date.now();
while(Date.now() - start < extraWork) {
;
}
}
}
if (startTime)
window.requestAnimationFrame(onFrame);
}
function startDraw()
{
startTime = performance.now();
eventCount = 0;
frameCounter = 0;
canvas.getContext("2d").clearRect(0, 0, canvas.width, canvas.height);
window.requestAnimationFrame(onFrame);
}
function endDraw()
{
var duration = performance.now() - startTime;
var rate = Math.round(eventCount / duration * 1000);
document.getElementById('rate').textContent = rate;
startTime = undefined;
}
function addCoalescedPoint(x, y)
{
coalescedPoints.push({x:x, y:y});
}
function addPoint(x, y)
{
eventCount++;
points.push({x:x, y:y});
}
window.onload = function() {
canvas = document.getElementById('canvas');
if (window.PointerEvent) {
var primaryButtonDown = false;
canvas.addEventListener('pointerdown', function(e) {
if (e.button == 0 && e.isPrimary) {
primaryButtonDown = true;
startDraw();
addPoint(e.pageX, e.pageY);
e.preventDefault();
}
});
canvas.addEventListener('pointermove', function(e) {
if (primaryButtonDown && e.isPrimary) {
if (e.getCoalescedEvents) {
e.getCoalescedEvents().forEach(function(ce) {
addCoalescedPoint(ce.pageX, ce.pageY);
});
}
addPoint(e.pageX, e.pageY);
e.preventDefault();
}
});
canvas.addEventListener('pointerup', function(e) {
if (e.button == 0 && e.isPrimary) {
endDraw();
primaryButtonDown = false;
e.preventDefault();
}
});
} else {
var mouseDown = false;
canvas.addEventListener('mousedown', function(e) {
if (e.button == 0) {
mouseDown = true;
startDraw();
addPoint(e.pageX, e.pageY);
e.preventDefault();
}
});
canvas.addEventListener('mouseup', function(e) {
if (e.button == 0) {
endDraw();
mouseDown = false;
e.preventDefault();
}
});
canvas.addEventListener('mousemove', function(e) {
if (mouseDown) {
addPoint(e.pageX, e.pageY);
e.preventDefault();
}
});
canvas.addEventListener('touchstart', function(e) {
if (e.touches.length == e.changedTouches.length) {
startDraw();
addPoint(e.touches[0].pageX, e.touches[0].pageY);
}
e.preventDefault();
});
canvas.addEventListener('touchend', function(e) {
if (e.touches.length == 0) {
endDraw();
}
e.preventDefault();
});
canvas.addEventListener('touchmove', function(e) {
var touch = e.touches[0];
addPoint(touch.pageX, touch.pageY);
e.preventDefault();
});
}
InitializeCanvas();
}
var scale = 1;
function InitializeCanvas() {
var elem = document.getElementById('canvas');
var container = document.getElementById('container');
scale = window.devicePixelRatio ? window.devicePixelRatio : 1;
elem.width = container.clientWidth * scale;
elem.height = container.clientHeight * scale;
}