forked from processing/p5.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse.js
688 lines (662 loc) · 16.3 KB
/
mouse.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/**
* @module Events
* @submodule Mouse
* @for p5
* @requires core
* @requires constants
*/
'use strict';
var p5 = require('../core/core');
var constants = require('../core/constants');
/*
* These are helper vars that store the mouseX and mouseY vals
* between the time that a mouse event happens and the next frame
* of draw. This is done to deal with the asynchronicity of event
* calls interacting with the draw loop. When a mouse event occurs
* the _nextMouseX/Y vars are updated, then on each call of draw, mouseX/Y
* and pmouseX/Y are updated using the _nextMouseX/Y vals.
*/
p5.prototype._nextMouseX = 0;
p5.prototype._nextMouseY = 0;
/**
* The system variable mouseX always contains the current horizontal
* position of the mouse, relative to (0, 0) of the canvas.
*
* @property mouseX
*
* @example
* <div>
* <code>
* // Move the mouse across the canvas
* function draw() {
* background(244, 248, 252);
* line(mouseX, 0, mouseX, 100);
* }
* </code>
* </div>
*/
p5.prototype.mouseX = 0;
/**
* The system variable mouseY always contains the current vertical position
* of the mouse, relative to (0, 0) of the canvas.
*
* @property mouseY
*
* @example
* <div>
* <code>
* // Move the mouse across the canvas
* function draw() {
* background(244, 248, 252);
* line(0, mouseY, 100, mouseY);
*}
* </code>
* </div>
*/
p5.prototype.mouseY = 0;
/**
* The system variable pmouseX always contains the horizontal position of
* the mouse in the frame previous to the current frame, relative to (0, 0)
* of the canvas.
*
* @property pmouseX
*
* @example
* <div>
* <code>
* // Move the mouse across the canvas to leave a trail
* function setup() {
* //slow down the frameRate to make it more visible
* frameRate(10);
* }
*
* function draw() {
* background(244, 248, 252);
* line(mouseX, mouseY, pmouseX, pmouseY);
* print(pmouseX + " -> " + mouseX);
* }
*
* </code>
* </div>
*/
p5.prototype.pmouseX = 0;
/**
* The system variable pmouseY always contains the vertical position of the
* mouse in the frame previous to the current frame, relative to (0, 0) of
* the canvas.
*
* @property pmouseY
*
* @example
* <div>
* <code>
* function draw() {
* background(237, 34, 93);
* fill(0);
* //draw a square only if the mouse is not moving
* if(mouseY == pmouseY && mouseX == pmouseX)
* rect(20,20,60,60);
*
* print(pmouseY + " -> " + mouseY);
* }
*
* </code>
* </div>
*/
p5.prototype.pmouseY = 0;
/**
* The system variable winMouseX always contains the current horizontal
* position of the mouse, relative to (0, 0) of the window.
*
* @property winMouseX
*
* @example
* <div>
* <code>
* var myCanvas;
*
* function setup() {
* //use a variable to store a pointer to the canvas
* myCanvas = createCanvas(100, 100);
* }
*
* function draw() {
* background(237, 34, 93);
* fill(0);
*
* //move the canvas to the horizontal mouse position
* //relative to the window
* myCanvas.position(winMouseX+1, windowHeight/2);
*
* //the y of the square is relative to the canvas
* rect(20,mouseY,60,60);
* }
*
* </code>
* </div>
*/
p5.prototype.winMouseX = 0;
/**
* The system variable winMouseY always contains the current vertical
* position of the mouse, relative to (0, 0) of the window.
*
* @property winMouseY
*
* @example
* <div>
* <code>
*var myCanvas;
*
* function setup() {
* //use a variable to store a pointer to the canvas
* myCanvas = createCanvas(100, 100);
* }
*
* function draw() {
* background(237, 34, 93);
* fill(0);
*
* //move the canvas to the vertical mouse position
* //relative to the window
* myCanvas.position(windowWidth/2, winMouseY+1);
*
* //the x of the square is relative to the canvas
* rect(mouseX,20,60,60);
* }
*
* </code>
* </div>
*/
p5.prototype.winMouseY = 0;
/**
* The system variable pwinMouseX always contains the horizontal position
* of the mouse in the frame previous to the current frame, relative to
* (0, 0) of the window.
*
* @property pwinMouseX
*
* @example
* <div>
* <code>
*
* var myCanvas;
*
* function setup() {
* //use a variable to store a pointer to the canvas
* myCanvas = createCanvas(100, 100);
* noStroke();
* fill(237, 34, 93);
* }
*
* function draw() {
* clear();
* //the difference between previous and
* //current x position is the horizontal mouse speed
* var speed = abs(winMouseX-pwinMouseX);
* //change the size of the circle
* //according to the horizontal speed
* ellipse(50, 50, 10+speed*5, 10+speed*5);
* //move the canvas to the mouse position
* myCanvas.position( winMouseX+1, winMouseY+1);
* }
*
* </code>
* </div>
*/
p5.prototype.pwinMouseX = 0;
/**
* The system variable pwinMouseY always contains the vertical position of
* the mouse in the frame previous to the current frame, relative to (0, 0)
* of the window.
*
* @property pwinMouseY
*
*
* @example
* <div>
* <code>
*
* var myCanvas;
*
* function setup() {
* //use a variable to store a pointer to the canvas
* myCanvas = createCanvas(100, 100);
* noStroke();
* fill(237, 34, 93);
* }
*
* function draw() {
* clear();
* //the difference between previous and
* //current y position is the vertical mouse speed
* var speed = abs(winMouseY-pwinMouseY);
* //change the size of the circle
* //according to the vertical speed
* ellipse(50, 50, 10+speed*5, 10+speed*5);
* //move the canvas to the mouse position
* myCanvas.position( winMouseX+1, winMouseY+1);
* }
*
* </code>
* </div>
*/
p5.prototype.pwinMouseY = 0;
/**
* Processing automatically tracks if the mouse button is pressed and which
* button is pressed. The value of the system variable mouseButton is either
* LEFT, RIGHT, or CENTER depending on which button was pressed last.
* Warning: different browsers may track mouseButton differently.
*
* @property mouseButton
*
* @example
* <div>
* <code>
* function draw() {
* background(237, 34, 93);
* fill(0);
*
* if (mouseIsPressed) {
* if (mouseButton == LEFT)
* ellipse(50, 50, 50, 50);
* if (mouseButton == RIGHT)
* rect(25, 25, 50, 50);
* if (mouseButton == CENTER)
* triangle(23, 75, 50, 20, 78, 75);
* }
*
* print(mouseButton);
* }
* </code>
* </div>
*/
p5.prototype.mouseButton = 0;
/**
* The boolean system variable mouseIsPressed is true if the mouse is pressed
* and false if not.
*
* @property mouseIsPressed
*
* @example
* <div>
* <code>
* function draw() {
* background(237, 34, 93);
* fill(0);
*
* if (mouseIsPressed)
* ellipse(50, 50, 50, 50);
* else
* rect(25, 25, 50, 50);
*
* print(mouseIsPressed);
* }
* </code>
* </div>
*/
p5.prototype.mouseIsPressed = false;
p5.prototype.isMousePressed = false; // both are supported
p5.prototype._updateNextMouseCoords = function(e) {
if(e.type === 'touchstart' ||
e.type === 'touchmove' ||
e.type === 'touchend' || e.touches) {
this._setProperty('_nextMouseX', this._nextTouchX);
this._setProperty('_nextMouseY', this._nextTouchY);
} else {
if(this._curElement !== null) {
var mousePos = getMousePos(this._curElement.elt, e);
this._setProperty('_nextMouseX', mousePos.x);
this._setProperty('_nextMouseY', mousePos.y);
}
}
this._setProperty('winMouseX', e.pageX);
this._setProperty('winMouseY', e.pageY);
};
p5.prototype._updateMouseCoords = function() {
this._setProperty('pmouseX', this.mouseX);
this._setProperty('pmouseY', this.mouseY);
this._setProperty('mouseX', this._nextMouseX);
this._setProperty('mouseY', this._nextMouseY);
this._setProperty('pwinMouseX', this.winMouseX);
this._setProperty('pwinMouseY', this.winMouseY);
};
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
p5.prototype._setMouseButton = function(e) {
if (e.button === 1) {
this._setProperty('mouseButton', constants.CENTER);
} else if (e.button === 2) {
this._setProperty('mouseButton', constants.RIGHT);
} else {
this._setProperty('mouseButton', constants.LEFT);
}
};
/**
* The mouseMoved() function is called every time the mouse moves and a mouse
* button is not pressed.<br><br>
* Browsers may have different default
* behaviors attached to various mouse events. To prevent any default
* behavior for this event, add "return false" to the end of the method.
*
* @method mouseMoved
* @example
* <div>
* <code>
* // Move the mouse across the page
* // to change its value
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function mouseMoved() {
* value = value + 5;
* if (value > 255) {
* value = 0;
* }
* }
* </code>
* </div>
*
* <div class="norender">
* <code>
* function mouseMoved() {
* ellipse(mouseX, mouseY, 5, 5);
* // prevent default
* return false;
* }
* </code>
* </div>
*/
/**
* The mouseDragged() function is called once every time the mouse moves and
* a mouse button is pressed. If no mouseDragged() function is defined, the
* touchMoved() function will be called instead if it is defined.<br><br>
* Browsers may have different default
* behaviors attached to various mouse events. To prevent any default
* behavior for this event, add "return false" to the end of the method.
*
* @method mouseDragged
* @example
* <div>
* <code>
* // Drag the mouse across the page
* // to change its value
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function mouseDragged() {
* value = value + 5;
* if (value > 255) {
* value = 0;
* }
* }
* </code>
* </div>
*
* <div class="norender">
* <code>
* function mouseDragged() {
* ellipse(mouseX, mouseY, 5, 5);
* // prevent default
* return false;
* }
* </code>
* </div>
*/
p5.prototype._onmousemove = function(e){
var context = this._isGlobal ? window : this;
var executeDefault;
this._updateNextMouseCoords(e);
this._updateNextTouchCoords(e);
if (!this.isMousePressed) {
if (typeof context.mouseMoved === 'function') {
executeDefault = context.mouseMoved(e);
if(executeDefault === false) {
e.preventDefault();
}
}
}
else {
if (typeof context.mouseDragged === 'function') {
executeDefault = context.mouseDragged(e);
if(executeDefault === false) {
e.preventDefault();
}
} else if (typeof context.touchMoved === 'function') {
executeDefault = context.touchMoved(e);
if(executeDefault === false) {
e.preventDefault();
}
}
}
};
/**
* The mousePressed() function is called once after every time a mouse button
* is pressed. The mouseButton variable (see the related reference entry)
* can be used to determine which button has been pressed. If no
* mousePressed() function is defined, the touchStarted() function will be
* called instead if it is defined.<br><br>
* Browsers may have different default
* behaviors attached to various mouse events. To prevent any default
* behavior for this event, add "return false" to the end of the method.
*
* @method mousePressed
* @example
* <div>
* <code>
* // Click within the image to change
* // the value of the rectangle
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function mousePressed() {
* if (value == 0) {
* value = 255;
* } else {
* value = 0;
* }
* }
* </code>
* </div>
*
* <div class="norender">
* <code>
* function mousePressed() {
* ellipse(mouseX, mouseY, 5, 5);
* // prevent default
* return false;
* }
* </code>
* </div>
*/
p5.prototype._onmousedown = function(e) {
var context = this._isGlobal ? window : this;
var executeDefault;
this._setProperty('isMousePressed', true);
this._setProperty('mouseIsPressed', true);
this._setMouseButton(e);
this._updateNextMouseCoords(e);
this._updateNextTouchCoords(e);
if (typeof context.mousePressed === 'function') {
executeDefault = context.mousePressed(e);
if(executeDefault === false) {
e.preventDefault();
}
} else if (typeof context.touchStarted === 'function') {
executeDefault = context.touchStarted(e);
if(executeDefault === false) {
e.preventDefault();
}
}
};
/**
* The mouseReleased() function is called every time a mouse button is
* released. If no mouseReleased() function is defined, the touchEnded()
* function will be called instead if it is defined.<br><br>
* Browsers may have different default
* behaviors attached to various mouse events. To prevent any default
* behavior for this event, add "return false" to the end of the method.
*
*
* @method mouseReleased
* @example
* <div>
* <code>
* // Click within the image to change
* // the value of the rectangle
* // after the mouse has been clicked
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function mouseReleased() {
* if (value == 0) {
* value = 255;
* } else {
* value = 0;
* }
* }
* </code>
* </div>
*
* <div class="norender">
* <code>
* function mouseReleased() {
* ellipse(mouseX, mouseY, 5, 5);
* // prevent default
* return false;
* }
* </code>
* </div>
*/
p5.prototype._onmouseup = function(e) {
var context = this._isGlobal ? window : this;
var executeDefault;
this._setProperty('isMousePressed', false);
this._setProperty('mouseIsPressed', false);
if (typeof context.mouseReleased === 'function') {
executeDefault = context.mouseReleased(e);
if(executeDefault === false) {
e.preventDefault();
}
} else if (typeof context.touchEnded === 'function') {
executeDefault = context.touchEnded(e);
if(executeDefault === false) {
e.preventDefault();
}
}
};
p5.prototype._ondragend = p5.prototype._onmouseup;
p5.prototype._ondragover = p5.prototype._onmousemove;
/**
* The mouseClicked() function is called once after a mouse button has been
* pressed and then released.<br><br>
* Browsers may have different default
* behaviors attached to various mouse events. To prevent any default
* behavior for this event, add "return false" to the end of the method.
*
* @method mouseClicked
* @example
* <div>
* <code>
* // Click within the image to change
* // the value of the rectangle
* // after the mouse has been clicked
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function mouseClicked() {
* if (value == 0) {
* value = 255;
* } else {
* value = 0;
* }
* }
* </code>
* </div>
*
* <div class="norender">
* <code>
* function mouseClicked() {
* ellipse(mouseX, mouseY, 5, 5);
* // prevent default
* return false;
* }
* </code>
* </div>
*/
p5.prototype._onclick = function(e) {
var context = this._isGlobal ? window : this;
if (typeof context.mouseClicked === 'function') {
var executeDefault = context.mouseClicked(e);
if(executeDefault === false) {
e.preventDefault();
}
}
};
/**
* The function mouseWheel() is executed every time a vertical mouse wheel
* event is detected either triggered by an actual mouse wheel or by a
* touchpad.<br><br>
* The event.delta property returns the amount the mouse wheel
* have scrolled. The values can be positive or negative depending on the
* scroll direction (on OS X with "natural" scrolling enabled, the signs
* are inverted).<br><br>
* Browsers may have different default behaviors attached to various
* mouse events. To prevent any default behavior for this event, add
* "return false" to the end of the method.<br><br>
* Due to the current support of the "wheel" event on Safari, the function
* may only work as expected if "return false" is included while using Safari.
*
* @method mouseWheel
*
* @example
* <div>
* <code>
* var pos = 25;
*
* function draw() {
* background(237, 34, 93);
* fill(0);
* rect(25, pos, 50, 50);
* }
*
* function mouseWheel(event) {
* print(event.delta);
* //move the square according to the vertical scroll amount
* pos += event.delta;
* //uncomment to block page scrolling
* //return false;
* }
* </code>
* </div>
*/
p5.prototype._onwheel = function(e) {
var context = this._isGlobal ? window : this;
if (typeof context.mouseWheel === 'function') {
e.delta = e.deltaY;
var executeDefault = context.mouseWheel(e);
if(executeDefault === false) {
e.preventDefault();
}
}
};
module.exports = p5;