How to use onMouseButton method in redwood

Best JavaScript code snippet using redwood

input.js

Source:input.js Github

copy

Full Screen

...159//160function resetDoubleClickTimer() {161 doubleClickTimer = null;162}163function onMouseButton(e, down) {164 var evt, pos, bmask;165 if (! conf.focused) {166 return true;167 }168 if (conf.notify) {169 conf.notify(e);170 }171 evt = (e ? e : window.event);172 pos = Util.getEventPosition(e, conf.target, conf.scale);173 if (e.touches || e.changedTouches) {174 // Touch device175 // When two touches occur within 500 ms of each other and are176 // closer than 20 pixels together a double click is triggered.177 if (down == 1) {178 if (doubleClickTimer == null) {179 lastTouchPos = pos;180 } else {181 clearTimeout(doubleClickTimer); 182 // When the distance between the two touches is small enough183 // force the position of the latter touch to the position of184 // the first.185 var xs = lastTouchPos.x - pos.x;186 var ys = lastTouchPos.y - pos.y;187 var d = Math.sqrt((xs * xs) + (ys * ys));188 // The goal is to trigger on a certain physical width, the189 // devicePixelRatio brings us a bit closer but is not optimal.190 if (d < 20 * window.devicePixelRatio) {191 pos = lastTouchPos;192 }193 }194 doubleClickTimer = setTimeout(resetDoubleClickTimer, 500);195 }196 bmask = conf.touchButton;197 // If bmask is set198 } else if (evt.which) {199 /* everything except IE */200 bmask = 1 << evt.button;201 } else {202 /* IE including 9 */203 bmask = (evt.button & 0x1) + // Left204 (evt.button & 0x2) * 2 + // Right205 (evt.button & 0x4) / 2; // Middle206 }207 //Util.Debug("mouse " + pos.x + "," + pos.y + " down: " + down +208 // " bmask: " + bmask + "(evt.button: " + evt.button + ")");209 if (conf.onMouseButton) {210 Util.Debug("onMouseButton " + (down ? "down" : "up") +211 ", x: " + pos.x + ", y: " + pos.y + ", bmask: " + bmask);212 conf.onMouseButton(pos.x, pos.y, down, bmask);213 }214 Util.stopEvent(e);215 return false;216}217function onMouseDown(e) {218 captureMouse();219 onMouseButton(e, 1);220}221function onMouseUp(e) {222 if (!mouseCaptured) {223 return;224 }225 onMouseButton(e, 0);226 releaseMouse();227}228function onMouseWheel(e) {229 var evt, pos, bmask, wheelData;230 if (! conf.focused) {231 return true;232 }233 if (conf.notify) {234 conf.notify(e);235 }236 evt = (e ? e : window.event);237 pos = Util.getEventPosition(e, conf.target, conf.scale);238 wheelData = evt.detail ? evt.detail * -1 : evt.wheelDelta / 40;239 if (wheelData > 0) {240 bmask = 1 << 3;241 } else {242 bmask = 1 << 4;243 }244 //Util.Debug('mouse scroll by ' + wheelData + ':' + pos.x + "," + pos.y);245 if (conf.onMouseButton) {246 conf.onMouseButton(pos.x, pos.y, 1, bmask);247 conf.onMouseButton(pos.x, pos.y, 0, bmask);248 }249 Util.stopEvent(e);250 return false;251}252function onMouseMove(e) {253 var evt, pos;254 if (! conf.focused) {255 return true;256 }257 if (conf.notify) {258 conf.notify(e);259 }260 evt = (e ? e : window.event);261 pos = Util.getEventPosition(e, conf.target, conf.scale);...

Full Screen

Full Screen

mouse.js

Source:mouse.js Github

copy

Full Screen

1/*2 * noVNC: HTML5 VNC client3 * Copyright (C) 2012 Joel Martin4 * Copyright (C) 2013 Samuel Mannehed for Cendio AB5 * Licensed under MPL 2.0 or any later version (see LICENSE.txt)6 */7import * as Log from '../util/logging.js';8import { isTouchDevice } from '../util/browser.js';9import { setCapture, stopEvent, getPointerEvent } from '../util/events.js';10var WHEEL_STEP = 10; // Delta threshold for a mouse wheel step11var WHEEL_STEP_TIMEOUT = 50; // ms12var WHEEL_LINE_HEIGHT = 19;13export default function Mouse(target) {14 this._target = target || document;15 this._doubleClickTimer = null;16 this._lastTouchPos = null;17 this._pos = null;18 this._wheelStepXTimer = null;19 this._wheelStepYTimer = null;20 this._accumulatedWheelDeltaX = 0;21 this._accumulatedWheelDeltaY = 0;22 this._eventHandlers = {23 'mousedown': this._handleMouseDown.bind(this),24 'mouseup': this._handleMouseUp.bind(this),25 'mousemove': this._handleMouseMove.bind(this),26 'mousewheel': this._handleMouseWheel.bind(this),27 'mousedisable': this._handleMouseDisable.bind(this)28 };29};30Mouse.prototype = {31 // ===== PROPERTIES =====32 touchButton: 1, // Button mask (1, 2, 4) for touch devices (0 means ignore clicks)33 // ===== EVENT HANDLERS =====34 onmousebutton: function () {}, // Handler for mouse button click/release35 onmousemove: function () {}, // Handler for mouse movement36 // ===== PRIVATE METHODS =====37 _resetDoubleClickTimer: function () {38 this._doubleClickTimer = null;39 },40 _handleMouseButton: function (e, down) {41 this._updateMousePosition(e);42 var pos = this._pos;43 var bmask;44 if (e.touches || e.changedTouches) {45 // Touch device46 // When two touches occur within 500 ms of each other and are47 // close enough together a double click is triggered.48 if (down == 1) {49 if (this._doubleClickTimer === null) {50 this._lastTouchPos = pos;51 } else {52 clearTimeout(this._doubleClickTimer);53 // When the distance between the two touches is small enough54 // force the position of the latter touch to the position of55 // the first.56 var xs = this._lastTouchPos.x - pos.x;57 var ys = this._lastTouchPos.y - pos.y;58 var d = Math.sqrt((xs * xs) + (ys * ys));59 // The goal is to trigger on a certain physical width, the60 // devicePixelRatio brings us a bit closer but is not optimal.61 var threshold = 20 * (window.devicePixelRatio || 1);62 if (d < threshold) {63 pos = this._lastTouchPos;64 }65 }66 this._doubleClickTimer = setTimeout(this._resetDoubleClickTimer.bind(this), 500);67 }68 bmask = this.touchButton;69 // If bmask is set70 } else if (e.which) {71 /* everything except IE */72 bmask = 1 << e.button;73 } else {74 /* IE including 9 */75 bmask = (e.button & 0x1) + // Left76 (e.button & 0x2) * 2 + // Right77 (e.button & 0x4) / 2; // Middle78 }79 Log.Debug("onmousebutton " + (down ? "down" : "up") +80 ", x: " + pos.x + ", y: " + pos.y + ", bmask: " + bmask);81 this.onmousebutton(pos.x, pos.y, down, bmask);82 stopEvent(e);83 },84 _handleMouseDown: function (e) {85 // Touch events have implicit capture86 if (e.type === "mousedown") {87 setCapture(this._target);88 }89 this._handleMouseButton(e, 1);90 },91 _handleMouseUp: function (e) {92 this._handleMouseButton(e, 0);93 },94 // Mouse wheel events are sent in steps over VNC. This means that the VNC95 // protocol can't handle a wheel event with specific distance or speed.96 // Therefor, if we get a lot of small mouse wheel events we combine them.97 _generateWheelStepX: function () {98 if (this._accumulatedWheelDeltaX < 0) {99 this.onmousebutton(this._pos.x, this._pos.y, 1, 1 << 5);100 this.onmousebutton(this._pos.x, this._pos.y, 0, 1 << 5);101 } else if (this._accumulatedWheelDeltaX > 0) {102 this.onmousebutton(this._pos.x, this._pos.y, 1, 1 << 6);103 this.onmousebutton(this._pos.x, this._pos.y, 0, 1 << 6);104 }105 this._accumulatedWheelDeltaX = 0;106 },107 _generateWheelStepY: function () {108 if (this._accumulatedWheelDeltaY < 0) {109 this.onmousebutton(this._pos.x, this._pos.y, 1, 1 << 3);110 this.onmousebutton(this._pos.x, this._pos.y, 0, 1 << 3);111 } else if (this._accumulatedWheelDeltaY > 0) {112 this.onmousebutton(this._pos.x, this._pos.y, 1, 1 << 4);113 this.onmousebutton(this._pos.x, this._pos.y, 0, 1 << 4);114 }115 this._accumulatedWheelDeltaY = 0;116 },117 _resetWheelStepTimers: function () {118 window.clearTimeout(this._wheelStepXTimer);119 window.clearTimeout(this._wheelStepYTimer);120 this._wheelStepXTimer = null;121 this._wheelStepYTimer = null;122 },123 _handleMouseWheel: function (e) {124 this._resetWheelStepTimers();125 this._updateMousePosition(e);126 var dX = e.deltaX;127 var dY = e.deltaY;128 // Pixel units unless it's non-zero.129 // Note that if deltamode is line or page won't matter since we aren't130 // sending the mouse wheel delta to the server anyway.131 // The difference between pixel and line can be important however since132 // we have a threshold that can be smaller than the line height.133 if (e.deltaMode !== 0) {134 dX *= WHEEL_LINE_HEIGHT;135 dY *= WHEEL_LINE_HEIGHT;136 }137 this._accumulatedWheelDeltaX += dX;138 this._accumulatedWheelDeltaY += dY;139 // Generate a mouse wheel step event when the accumulated delta140 // for one of the axes is large enough.141 // Small delta events that do not pass the threshold get sent142 // after a timeout.143 if (Math.abs(this._accumulatedWheelDeltaX) > WHEEL_STEP) {144 this._generateWheelStepX();145 } else {146 this._wheelStepXTimer =147 window.setTimeout(this._generateWheelStepX.bind(this),148 WHEEL_STEP_TIMEOUT);149 }150 if (Math.abs(this._accumulatedWheelDeltaY) > WHEEL_STEP) {151 this._generateWheelStepY();152 } else {153 this._wheelStepYTimer =154 window.setTimeout(this._generateWheelStepY.bind(this),155 WHEEL_STEP_TIMEOUT);156 }157 stopEvent(e);158 },159 _handleMouseMove: function (e) {160 this._updateMousePosition(e);161 this.onmousemove(this._pos.x, this._pos.y);162 stopEvent(e);163 },164 _handleMouseDisable: function (e) {165 /*166 * Stop propagation if inside canvas area167 * Note: This is only needed for the 'click' event as it fails168 * to fire properly for the target element so we have169 * to listen on the document element instead.170 */171 if (e.target == this._target) {172 stopEvent(e);173 }174 },175 // Update coordinates relative to target176 _updateMousePosition: function(e) {177 e = getPointerEvent(e);178 var bounds = this._target.getBoundingClientRect();179 var x, y;180 // Clip to target bounds181 if (e.clientX < bounds.left) {182 x = 0;183 } else if (e.clientX >= bounds.right) {184 x = bounds.width - 1;185 } else {186 x = e.clientX - bounds.left;187 }188 if (e.clientY < bounds.top) {189 y = 0;190 } else if (e.clientY >= bounds.bottom) {191 y = bounds.height - 1;192 } else {193 y = e.clientY - bounds.top;194 }195 this._pos = {x:x, y:y};196 },197 // ===== PUBLIC METHODS =====198 grab: function () {199 var c = this._target;200 if (isTouchDevice) {201 c.addEventListener('touchstart', this._eventHandlers.mousedown);202 c.addEventListener('touchend', this._eventHandlers.mouseup);203 c.addEventListener('touchmove', this._eventHandlers.mousemove);204 }205 c.addEventListener('mousedown', this._eventHandlers.mousedown);206 c.addEventListener('mouseup', this._eventHandlers.mouseup);207 c.addEventListener('mousemove', this._eventHandlers.mousemove);208 c.addEventListener('wheel', this._eventHandlers.mousewheel);209 /* Prevent middle-click pasting (see above for why we bind to document) */210 document.addEventListener('click', this._eventHandlers.mousedisable);211 /* preventDefault() on mousedown doesn't stop this event for some212 reason so we have to explicitly block it */213 c.addEventListener('contextmenu', this._eventHandlers.mousedisable);214 },215 ungrab: function () {216 var c = this._target;217 this._resetWheelStepTimers();218 if (isTouchDevice) {219 c.removeEventListener('touchstart', this._eventHandlers.mousedown);220 c.removeEventListener('touchend', this._eventHandlers.mouseup);221 c.removeEventListener('touchmove', this._eventHandlers.mousemove);222 }223 c.removeEventListener('mousedown', this._eventHandlers.mousedown);224 c.removeEventListener('mouseup', this._eventHandlers.mouseup);225 c.removeEventListener('mousemove', this._eventHandlers.mousemove);226 c.removeEventListener('wheel', this._eventHandlers.mousewheel);227 document.removeEventListener('click', this._eventHandlers.mousedisable);228 c.removeEventListener('contextmenu', this._eventHandlers.mousedisable);229 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require("redwood");2redwood.onMouseButton(function(button, state, x, y) {3 redwood.log("mouse button " + button + " pressed at " + x + ", " + y);4});5#### redwood.onMouseMove(function(x, y))6var redwood = require("redwood");7redwood.onMouseMove(function(x, y) {8 redwood.log("mouse moved to " + x + ", " + y);9});10#### redwood.onMouseWheel(function(direction, x, y))11var redwood = require("redwood");12redwood.onMouseWheel(function(direction, x, y) {13 redwood.log("mouse wheel scrolled " + direction + " at " + x + ", " + y);14});15#### redwood.onKey(function(key, state, x, y))16var redwood = require("redwood");17redwood.onKey(function(key, state, x, y) {18 redwood.log("key " + key + " " + (state ? "pressed" : "released") + " at " + x + ", " + y);19});20#### redwood.onResize(function(width, height))21var redwood = require("redwood");22redwood.onResize(function(width, height) {23 redwood.log("window resized to " + width + ", " + height);24});25#### redwood.onIdle(function())

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require("redwoodjs")2redwood.onMouseButton(function(button, pressed) {3 console.log("Mouse button " + button + " was " + (pressed ? "pressed" : "released"));4});5### redwood.onMouseMove(callback)6var redwood = require("redwoodjs")7redwood.onMouseMove(function(x, y) {8 console.log("Mouse moved to (" + x + ", " + y + ")");9});10### redwood.onMouseWheel(callback)11var redwood = require("redwoodjs")12redwood.onMouseWheel(function(x, y) {13 console.log("Mouse wheel moved to (" + x + ", " + y + ")");14});15### redwood.onMouseEnter(callback)16var redwood = require("redwoodjs")17redwood.onMouseEnter(function() {18 console.log("Mouse entered window");19});20### redwood.onMouseLeave(callback)21var redwood = require("redwoodjs")22redwood.onMouseLeave(function() {23 console.log("Mouse left window");24});25### redwood.onFocus(callback)26var redwood = require("redwoodjs")27redwood.onFocus(function() {28 console.log("Window gained focus");29});30### redwood.onBlur(callback)31var redwood = require("redwoodjs")32redwood.onBlur(function() {33 console.log("Window lost focus");34});35### redwood.onKey(callback)36var redwood = require("redwoodjs")37redwood.onKey(function(key, scancode, action, mods) {38 console.log("Key " + key + " was pressed");39});40### redwood.onChar(callback)

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var square = new redwood.Square({3});4square.onMouseButton(function(button, x, y, isDown) {5 if (isDown) {6 square.setColor('green');7 } else {8 square.setColor('red');9 }10});11redwood.add(square);12redwood.start();

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('../redwood');2var app = redwood();3var canvas = app.canvas();4var mouse = app.mouse();5var circle = canvas.circle(50, 50, 50);6circle.color('red');7var text = canvas.text(50, 50, "Click Me!");8text.color('black');9text.size(20);10text.font('Helvetica');11text.align('center');12text.baseline('middle');13text.weight('bold');14text.style('italic');15text.decoration('underline');16mouse.onMouseButton(function(button, state, x, y) {17 if (state === 1) {18 if (circle.hitTest(x, y)) {19 text.text("You clicked me!");20 }21 }22});23app.start();

Full Screen

Using AI Code Generation

copy

Full Screen

1var r = new Rectangle(0,0,100,100);2r.fillColor = "red";3r.center = view.center;4function onMouseDown(event){5 r.fillColor = "green";6}7function onMouseUp(event){8 r.fillColor = "red";9}10function onMouseDrag(event){11 r.position = event.point;12}13function onMouseMove(event){14 r.position = event.point;15}16function onMouseEnter(event){17 r.fillColor = "blue";18}19function onMouseLeave(event){20 r.fillColor = "red";21}22function onMouseDown(event){23 r.fillColor = "green";24}25function onMouseUp(event){26 r.fillColor = "red";27}28function onMouseDrag(event){29 r.position = event.point;30}31function onMouseMove(event){32 r.position = event.point;33}34function onMouseEnter(event){35 r.fillColor = "blue";36}37function onMouseLeave(event){38 r.fillColor = "red";39}40function onMouseDown(event){41 r.fillColor = "green";42}43function onMouseUp(event){44 r.fillColor = "red";45}46function onMouseDrag(event){47 r.position = event.point;48}49function onMouseMove(event){50 r.position = event.point;51}52function onMouseEnter(event){53 r.fillColor = "blue";54}55function onMouseLeave(event){56 r.fillColor = "red";57}58function onMouseDown(event){59 r.fillColor = "green";60}61function onMouseUp(event){62 r.fillColor = "red";63}64function onMouseDrag(event){65 r.position = event.point;66}

Full Screen

Using AI Code Generation

copy

Full Screen

1var backgroundColor = "white";2onMouseButton = function()3{4 if (mouseButton == LEFT)5 {6 backgroundColor = "blue";7 }8 else if (mouseButton == RIGHT)9 {10 backgroundColor = "red";11 }12};13draw = function()14{15 background(backgroundColor);16 fill(0, 0, 0);17 text("Click the left mouse button to change the background to blue", 10, 30);18 text("Click the right mouse button to change the background to red", 10, 60);19};

Full Screen

Using AI Code Generation

copy

Full Screen

1var rect = new Rectangle(0, 0, 100, 100);2rect.fillColor = new Color(Math.random(), Math.random(), Math.random());3scene.root.addChild(rect);4var mouseButtonEvent = new MouseButtonEvent();5mouseButtonEvent.onMouseButton = function(e) {6 if(e.button == MouseButtonEvent.LEFT_BUTTON) {7 var rect = new Rectangle(0, 0, 100, 100);8 rect.fillColor = new Color(Math.random(), Math.random(), Math.random());9 scene.root.addChild(rect);10 }11}12scene.addEvent(mouseButtonEvent);13scene.onUpdate = function(e) {14 if(mouseButtonEvent.leftButtonDown) {15 rect.x = mouseButtonEvent.x;16 rect.y = mouseButtonEvent.y;17 }18}19scene.onDraw = function(e) {20 if(mouseButtonEvent.leftButtonDown) {21 rect.draw(e.graphics);22 }23}24application.addScene(scene);25application.setMainScene(scene);26application.run();

Full Screen

Using AI Code Generation

copy

Full Screen

1var robot = new Robot(100,100,0);2var x = 0;3var y = 0;4var line = new Line(0,0,0,0);5robot.draw();6onMouseButton(function(event) {7 robot.moveTo(event.x,event.y);8 line = new Line(x,y,event.x,event.y);9 line.draw();10 x = event.x;11 y = event.y;12});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run redwood automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful