]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/third_party/prometheus-cpp/3rdparty/civetweb/test/canvasdir/canvas.js
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / third_party / prometheus-cpp / 3rdparty / civetweb / test / canvasdir / canvas.js
1 /*
2 Copyright (c) 2013 Suffick at Codepen (http://codepen.io/suffick) and GitHub (https://github.com/suffick)
3 Permission is hereby granted, free of charge, to any person obtaining a copy
4 of this software and associated documentation files (the "Software"), to deal
5 in the Software without restriction, including without limitation the rights
6 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 copies of the Software, and to permit persons to whom the Software is
8 furnished to do so, subject to the following conditions:
9 The above copyright notice and this permission notice shall be included in
10 all copies or substantial portions of the Software.
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
17 THE SOFTWARE.
18 */
19
20 // settings
21
22 var physics_accuracy = 3,
23 mouse_influence = 20,
24 mouse_cut = 5,
25 gravity = 1200,
26 cloth_height = 40,
27 cloth_width = 76,
28 start_y = 10,
29 spacing = 7,
30 tear_distance = 600;
31
32
33 window.requestAnimFrame =
34 window.requestAnimationFrame ||
35 window.webkitRequestAnimationFrame ||
36 window.mozRequestAnimationFrame ||
37 window.oRequestAnimationFrame ||
38 window.msRequestAnimationFrame ||
39 function (callback) {
40 window.setTimeout(callback, 1000 / 60);
41 };
42
43 var canvas,
44 ctx,
45 cloth,
46 boundsx,
47 boundsy,
48 mouse = {
49 down: false,
50 button: 1,
51 x: 0,
52 y: 0,
53 px: 0,
54 py: 0
55 };
56
57 var Point = function (x, y) {
58
59 this.x = x;
60 this.y = y;
61 this.px = x;
62 this.py = y;
63 this.vx = 0;
64 this.vy = 0;
65 this.pin_x = null;
66 this.pin_y = null;
67
68 this.constraints = [];
69 };
70
71 Point.prototype.update = function (delta) {
72
73 if (mouse.down) {
74
75 var diff_x = this.x - mouse.x,
76 diff_y = this.y - mouse.y,
77 dist = Math.sqrt(diff_x * diff_x + diff_y * diff_y);
78
79 if (mouse.button == 1) {
80
81 if (dist < mouse_influence) {
82 this.px = this.x - (mouse.x - mouse.px) * 1.8;
83 this.py = this.y - (mouse.y - mouse.py) * 1.8;
84 }
85
86 } else if (dist < mouse_cut) this.constraints = [];
87 }
88
89 this.add_force(0, gravity);
90
91 delta *= delta;
92 nx = this.x + ((this.x - this.px) * .99) + ((this.vx / 2) * delta);
93 ny = this.y + ((this.y - this.py) * .99) + ((this.vy / 2) * delta);
94
95 this.px = this.x;
96 this.py = this.y;
97
98 this.x = nx;
99 this.y = ny;
100
101 this.vy = this.vx = 0
102 };
103
104 Point.prototype.draw = function () {
105
106 if (this.constraints.length <= 0) return;
107
108 var i = this.constraints.length;
109 while (i--) this.constraints[i].draw();
110 };
111
112 Point.prototype.resolve_constraints = function () {
113
114 if (this.pin_x != null && this.pin_y != null) {
115
116 this.x = this.pin_x;
117 this.y = this.pin_y;
118 return;
119 }
120
121 var i = this.constraints.length;
122 while (i--) this.constraints[i].resolve();
123
124 if (this.x > boundsx) {
125
126 this.x = 2 * boundsx - this.x;
127
128 } else if (this.x < 1) {
129
130 this.x = 2 - this.x;
131 }
132
133 if (this.y > boundsy) {
134
135 this.y = 2 * boundsy - this.y;
136
137 } else if (this.y < 1) {
138
139 this.y = 2 - this.y;
140 }
141 };
142
143 Point.prototype.attach = function (point) {
144
145 this.constraints.push(
146 new Constraint(this, point)
147 );
148 };
149
150 Point.prototype.remove_constraint = function (lnk) {
151
152 var i = this.constraints.length;
153 while (i--)
154 if (this.constraints[i] == lnk) this.constraints.splice(i, 1);
155 };
156
157 Point.prototype.add_force = function (x, y) {
158
159 this.vx += x;
160 this.vy += y;
161 };
162
163 Point.prototype.pin = function (pinx, piny) {
164 this.pin_x = pinx;
165 this.pin_y = piny;
166 };
167
168 var Constraint = function (p1, p2) {
169
170 this.p1 = p1;
171 this.p2 = p2;
172 this.length = spacing;
173 };
174
175 Constraint.prototype.resolve = function () {
176
177 var diff_x = this.p1.x - this.p2.x,
178 diff_y = this.p1.y - this.p2.y,
179 dist = Math.sqrt(diff_x * diff_x + diff_y * diff_y),
180 diff = (this.length - dist) / dist;
181
182 if (dist > tear_distance) this.p1.remove_constraint(this);
183
184 var px = diff_x * diff * 0.5;
185 var py = diff_y * diff * 0.5;
186
187 this.p1.x += px;
188 this.p1.y += py;
189 this.p2.x -= px;
190 this.p2.y -= py;
191 };
192
193 Constraint.prototype.draw = function () {
194
195 ctx.moveTo(this.p1.x, this.p1.y);
196 ctx.lineTo(this.p2.x, this.p2.y);
197 };
198
199 var Cloth = function () {
200
201 this.points = [];
202
203 var start_x = canvas.width / 2 - cloth_width * spacing / 2;
204
205 for (var y = 0; y <= cloth_height; y++) {
206
207 for (var x = 0; x <= cloth_width; x++) {
208
209 var p = new Point(start_x + x * spacing, start_y + y * spacing);
210
211 x != 0 && p.attach(this.points[this.points.length - 1]);
212 y == 0 && p.pin(p.x, p.y);
213 y != 0 && p.attach(this.points[x + (y - 1) * (cloth_width + 1)])
214
215 this.points.push(p);
216 }
217 }
218 };
219
220 Cloth.prototype.update = function () {
221
222 var i = physics_accuracy;
223
224 while (i--) {
225 var p = this.points.length;
226 while (p--) this.points[p].resolve_constraints();
227 }
228
229 i = this.points.length;
230 while (i--) this.points[i].update(.016);
231 };
232
233 Cloth.prototype.draw = function () {
234
235 ctx.beginPath();
236
237 var i = cloth.points.length;
238 while (i--) cloth.points[i].draw();
239
240 ctx.stroke();
241 };
242
243 function update() {
244
245 ctx.clearRect(0, 0, canvas.width, canvas.height);
246
247 cloth.update();
248 cloth.draw();
249
250 requestAnimFrame(update);
251 }
252
253 function start() {
254
255 canvas.onmousedown = function (e) {
256 mouse.button = e.which;
257 mouse.px = mouse.x;
258 mouse.py = mouse.y;
259 var rect = canvas.getBoundingClientRect();
260 mouse.x = e.clientX - rect.left,
261 mouse.y = e.clientY - rect.top,
262 mouse.down = true;
263 e.preventDefault();
264 };
265
266 canvas.onmouseup = function (e) {
267 mouse.down = false;
268 e.preventDefault();
269 };
270
271 canvas.onmousemove = function (e) {
272 mouse.px = mouse.x;
273 mouse.py = mouse.y;
274 var rect = canvas.getBoundingClientRect();
275 mouse.x = e.clientX - rect.left,
276 mouse.y = e.clientY - rect.top,
277 e.preventDefault();
278 };
279
280 canvas.oncontextmenu = function (e) {
281 e.preventDefault();
282 };
283
284 boundsx = canvas.width - 1;
285 boundsy = canvas.height - 1;
286
287 ctx.strokeStyle = '#888';
288 cloth = new Cloth();
289 update();
290 }
291
292 window.onload = function () {
293
294 canvas = document.getElementById('canvas');
295 ctx = canvas.getContext('2d');
296
297 canvas.width = 560;
298 canvas.height = 350;
299
300 start();
301 };