* @module Events * @submodule Pointer * @for p5
(p5, fn, lifecycles)
| 5 | */ |
| 6 | |
| 7 | function pointer(p5, fn, lifecycles){ |
| 8 | lifecycles.presetup = function(){ |
| 9 | const events = [ |
| 10 | 'pointerdown', |
| 11 | 'pointerup', |
| 12 | 'pointermove', |
| 13 | 'dragend', |
| 14 | 'dragover', |
| 15 | 'click', |
| 16 | 'dblclick', |
| 17 | 'wheel' |
| 18 | ]; |
| 19 | for(const event of events){ |
| 20 | window.addEventListener(event, this[`_on${event}`].bind(this), { |
| 21 | passive: false, |
| 22 | signal: this._removeSignal |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | window.addEventListener('blur', () => { |
| 27 | this.mouseIsPressed = false; |
| 28 | }, { signal: this._removeSignal }); |
| 29 | }; |
| 30 | |
| 31 | /** |
| 32 | * A `Number` system variable that tracks the mouse's horizontal movement. |
| 33 | * |
| 34 | * `movedX` tracks how many pixels the mouse moves left or right between |
| 35 | * frames. `movedX` will have a negative value if the mouse moves left between |
| 36 | * frames and a positive value if it moves right. `movedX` can be calculated |
| 37 | * as `mouseX - pmouseX`. |
| 38 | * |
| 39 | * Note: `movedX` continues updating even when |
| 40 | * <a href="#/p5/requestPointerLock">requestPointerLock()</a> is active. |
| 41 | * |
| 42 | * @property {Number} movedX |
| 43 | * @readOnly |
| 44 | * |
| 45 | * @example |
| 46 | * function setup() { |
| 47 | * createCanvas(100, 100); |
| 48 | * |
| 49 | * describe( |
| 50 | * 'A gray square. The text ">>" appears when the user moves the mouse to the right. The text "<<" appears when the user moves the mouse to the left.' |
| 51 | * ); |
| 52 | * } |
| 53 | * |
| 54 | * function draw() { |
| 55 | * background(200); |
| 56 | * |
| 57 | * // Style the text. |
| 58 | * textAlign(CENTER); |
| 59 | * textSize(16); |
| 60 | * |
| 61 | * // Display >> when movedX is positive and |
| 62 | * // << when it's negative. |
| 63 | * if (movedX > 0) { |
| 64 | * text('>>', 50, 50); |
no test coverage detected