Best JavaScript code snippet using devicefarmer-stf
jquery.pointerlock.js
Source:jquery.pointerlock.js
1// This code was adapted from https://developer.mozilla.org/en-US/docs/WebAPI/Pointer_Lock2// Note: at the time of writing, only Mozilla and WebKit support Pointer Lock.3if (!document.addEventListener) {4 document.addEventListener = function () {};5}6(function () {7var extendjQuery = function ($) {8$.fn.pointerLock = function (_params) {9 var params = $.extend({10 requireAlt: false,11 on: 'mousedown',12 until: 'mouseup',13 fullscreenElement: undefined,14 movement: undefined,15 }, _params);16 var mouseMoveListener = function(e) {17 if (!mouseMoveListener.getMovementFromEvent) {18 if (undefined !== e.originalEvent.movementX) {19 mouseMoveListener.getMovementFromEvent = function (e) {return {x: e.originalEvent.movementX, y: e.originalEvent.movementY};};20 } else if (undefined !== e.originalEvent.mozMovementX) {21 mouseMoveListener.getMovementFromEvent = function (e) {return {x: e.originalEvent.mozMovementX, y: e.originalEvent.mozMovementY};};22 } else if (undefined !== e.originalEvent.webkitMovementX) {23 mouseMoveListener.getMovementFromEvent = function (e) {return {x: e.originalEvent.webkitMovementX, y: e.originalEvent.webkitMovementY};};24 } else {25 mouseMoveListener.getMovementFromEvent = function (e) {26 if (mouseMoveListener.getMovementFromEvent.ePrevious) {27 movementX = e.originalEvent.screenX - mouseMoveListener.getMovementFromEvent.ePrevious.screenX;28 movementY = e.originalEvent.screenY - mouseMoveListener.getMovementFromEvent.ePrevious.screenY;29 } else {30 movementX = 0;31 movementY = 0;32 }33 mouseMoveListener.getMovementFromEvent.ePrevious = e.originalEvent;34 return {x: movementX, y: movementY};35 };36 }37 }38 var movement = mouseMoveListener.getMovementFromEvent(e);39 params.movement(movement.x, movement.y);40 };41 function fullscreenChange() {42 if (document.webkitFullscreenElement === params.fullscreenElement ||43 document.mozFullscreenElement === params.fullscreenElement ||44 document.mozFullScreenElement === params.fullscreenElement) { // Older API upper case 'S'.45 // Element is fullscreen, now we can request pointer lock46 this.requestPointerLock();47 }48 };49 if (params.fullscreenElement) {50 params.fullscreenElement.requestFullscreen = params.fullscreenElement.requestFullscreen ||51 params.fullscreenElement.mozRequestFullscreen ||52 params.fullscreenElement.mozRequestFullScreen || // Older API upper case 'S'.53 params.fullscreenElement.webkitRequestFullscreen;54 }55 this.each(function () {56 var elem = this;57 var $elem = $(elem);58 elem.requestPointerLock = elem.requestPointerLock ||59 elem.mozRequestPointerLock ||60 elem.webkitRequestPointerLock || function () {};61 $elem.on(params.on, function (e) {62 $(document.body).addClass('unselectable');63 var pointerLockChange = function () {64 if (document.mozPointerLockElement === this ||65 document.webkitPointerLockElement === this) {66 // console.log("Pointer Lock was successful.");67 } else {68 // console.log("Pointer Lock was lost.");69 }70 };71 $(document).one(params.until, function () {72 $(document).unbind("mousemove", mouseMoveListener);73 if (mouseMoveListener.getMovementFromEvent) {74 delete mouseMoveListener.getMovementFromEvent.ePrevious;75 }76 $(document.body).removeClass('unselectable');77 $.each(['pointerlockchange', 'mozpointerlockchange', 'webkitpointerlockchange'], function (i,pointerlockEventName) {78 $(document).unbind(pointerlockEventName, pointerLockChange);79 });80 document.exitPointerLock();81 });82 $(document).on("mousemove", mouseMoveListener);83 $.each(['pointerlockchange', 'mozpointerlockchange', 'webkitpointerlockchange'], function (i,pointerlockEventName) {84 $(document).on(pointerlockEventName, $.proxy(pointerLockChange, elem));85 });86 if (params.fullscreenElement) {87 params.fullscreenElement.requestFullscreen();88 } else {89 if (!params.requireAlt || e.altKey) {90 elem.requestPointerLock();91 }92 }93 });94 $.each(['fullscreenchange', 'mozfullscreenchange', 'webkitfullscreenchange'], function (i,fullscreenChangeEventName) {95 document.addEventListener(fullscreenChangeEventName, $.proxy(fullscreenChange, elem), false);96 });97 });98};99};100if (window.jQuery) {101 extendjQuery(jQuery);102} else if (window.require) {103 console.error("jQuery not defined, attempting to load it through AMD require");104 require(["jquery"], function (jQuery) {105 extendjQuery(jQuery);106 console.log("extended jQuery with jquery.pointerlock through AMD");107 });108} else {109 console.error("jQuery not defined; jquery.pointerlock has nothing to extend");110}111}());112document.exitPointerLock = document.exitPointerLock ||113 document.mozExitPointerLock ||114 document.webkitExitPointerLock || function () {};115function pointerLockError() {116 console.log("Error while locking pointer.");117}118document.addEventListener('pointerlockerror', pointerLockError, false);119document.addEventListener('mozpointerlockerror', pointerLockError, false);...
bindEvents.js
Source:bindEvents.js
1const coordsForTouchEvent = function(el, e) {2 const tx = e.changedTouches[0].clientX;3 const ty = e.changedTouches[0].clientY;4 const p = el.getBoundingClientRect();5 return [tx - p.left, ty - p.top];6};7const position = function(el, e) {8 const p = el.getBoundingClientRect();9 return {10 left: e.clientX - p.left,11 top: e.clientY - p.top,12 };13};14const buttonIsDown = function(e) {15 if (e.buttons != null) {16 return e.buttons === 1;17 } else {18 return e.which > 0;19 }20};21const bindEvents = function(lc, canvas, panWithKeyboard) {22 if (panWithKeyboard == null) {23 panWithKeyboard = false;24 }25 const unsubs = [];26 const mouseMoveListener = e => {27 e.preventDefault();28 const p = position(canvas, e);29 lc.pointerMove(p.left, p.top);30 };31 var mouseUpListener = e => {32 e.preventDefault();33 canvas.onselectstart = () => true; // enable selection while dragging34 const p = position(canvas, e);35 lc.pointerUp(p.left, p.top);36 document.removeEventListener("mousemove", mouseMoveListener);37 document.removeEventListener("mouseup", mouseUpListener);38 canvas.addEventListener("mousemove", mouseMoveListener);39 };40 canvas.addEventListener("mousedown", e => {41 if (e.target.tagName.toLowerCase() !== "canvas") {42 return;43 }44 const down = true;45 e.preventDefault();46 canvas.onselectstart = () => false; // disable selection while dragging47 const p = position(canvas, e);48 lc.pointerDown(p.left, p.top);49 canvas.removeEventListener("mousemove", mouseMoveListener);50 document.addEventListener("mousemove", mouseMoveListener);51 document.addEventListener("mouseup", mouseUpListener);52 });53 const touchMoveListener = function(e) {54 e.preventDefault();55 lc.pointerMove(...coordsForTouchEvent(canvas, e));56 };57 var touchEndListener = function(e) {58 e.preventDefault();59 lc.pointerUp(...coordsForTouchEvent(canvas, e));60 document.removeEventListener("touchmove", touchMoveListener);61 document.removeEventListener("touchend", touchEndListener);62 document.removeEventListener("touchcancel", touchEndListener);63 };64 canvas.addEventListener("touchstart", function(e) {65 if (e.target.tagName.toLowerCase() !== "canvas") {66 return;67 }68 e.preventDefault();69 if (e.touches.length === 1) {70 lc.pointerDown(...coordsForTouchEvent(canvas, e));71 document.addEventListener("touchmove", touchMoveListener);72 document.addEventListener("touchend", touchEndListener);73 document.addEventListener("touchcancel", touchEndListener);74 } else {75 lc.pointerMove(...coordsForTouchEvent(canvas, e));76 }77 });78 if (panWithKeyboard) {79 console.warn("Keyboard panning is deprecated.");80 const listener = function(e) {81 switch (e.keyCode) {82 case 37:83 lc.pan(-10, 0);84 break;85 case 38:86 lc.pan(0, -10);87 break;88 case 39:89 lc.pan(10, 0);90 break;91 case 40:92 lc.pan(0, 10);93 break;94 }95 lc.repaintAllLayers();96 };97 document.addEventListener("keydown", listener);98 unsubs.push(() => document.removeEventListener(listener));99 }100 return () => unsubs.map(f => f());101};...
MouseMoveListener.ts
Source:MouseMoveListener.ts
1import {2 SimpleEventListener,3 ListenEvent,4 NativeEventListener,5} from './SimpleEventListener'6import { Point } from '../../CustomTypes/Point'7export class MouseMoveListener extends NativeEventListener {8 protected static instance: MouseMoveListener9 protected static value: Point = { x: 0, y: 0 }10 public static getInstance(): ListenEvent {11 if (MouseMoveListener.instance == null)12 MouseMoveListener.instance = new MouseMoveListener()13 return MouseMoveListener.instance14 }15 private constructor() {16 super('mousemove')17 }18 public getValue() {19 return MouseMoveListener.value20 }21 public UpdateValue(e: MouseEvent) {22 MouseMoveListener.value = { x: e.clientX, y: e.clientY }23 }...
Using AI Code Generation
1var stf = require('devicefarmer-stf');2stf.getDevices(function(err, devices) {3 if (err) {4 console.error(err);5 return;6 }7 devices.forEach(function(device) {8 console.log(device);9 stf.mouseMoveListener(device.serial, function(err, data) {10 if (err) {11 console.error(err);12 return;13 }14 console.log(data);15 });16 });17});
Using AI Code Generation
1var stf = require('devicefarmer-stf');2var util = require('util');3 if (err) {4 console.log("Error connecting to STF server: " + err);5 return;6 }7 stf.getDevices(function(err, devices) {8 if (err) {9 console.log("Error getting devices: " + err);10 return;11 }12 var device = devices[0];13 console.log("Using device: " + util.inspect(device));14 stf.mouseMoveListener(device, function(err, data) {15 if (err) {16 console.log("Error getting mouseMoveListener: " + err);17 return;18 }19 console.log("mouseMoveListener: " + util.inspect(data));20 });21 });22});23var stf = require('devicefarmer-stf');24var util = require('util');25 if (err) {26 console.log("Error connecting to STF server: " + err);27 return;28 }29 stf.getDevices(function(err, devices) {30 if (err) {31 console.log("Error getting devices: " + err);32 return;33 }34 var device = devices[0];35 console.log("Using device: " + util.inspect(device));36 stf.mouseMoveListener(device, function(err, data) {37 if (err) {38 console.log("Error getting mouseMoveListener: " + err);39 return;40 }41 console.log("mouseMoveListener: " + util.inspect(data));42 });43 });44});45stf.mouseMoveListener(device, function(err, data) {46 if (err) {47 console.log("Error getting mouseMoveListener: " + err);48 return;49 }50 console.log("mouseMoveListener: " + util.inspect(data));51});
Using AI Code Generation
1var devicefarmer = require('devicefarmer-stf-client');2var client = devicefarmer.createClient({3});4client.getDevices(function (err, devices) {5 if (err) {6 console.log('Error: ' + err);7 } else {8 console.log('Devices: ' + devices);9 }10});11client.getDevices(function (err, devices) {12 if (err) {13 console.log('Error: ' + err);14 } else {15 console.log('Devices: ' + devices);16 }17});18client.getDevice('deviceid', function (err, device) {19 if (err) {20 console.log('Error: ' + err);21 } else {22 console.log('Device: ' + device);23 }24});25client.getDevice('deviceid', function (err, device) {26 if (err) {27 console.log('Error: ' + err);28 } else {29 console.log('Device: ' + device);30 }31});32client.getDevice('deviceid', function (err, device) {33 if (err) {34 console.log('Error: ' + err);35 } else {36 console.log('Device: ' + device);37 }38});39client.getDevice('deviceid', function (err, device) {40 if (err) {41 console.log('Error: ' + err);42 } else {43 console.log('Device: ' + device);44 }45});46client.getDevice('deviceid', function (err, device) {47 if (err) {48 console.log('Error: ' + err);49 } else {50 console.log('Device: ' + device);51 }52});53client.getDevice('deviceid', function (err, device) {54 if (err) {55 console.log('Error: ' + err);56 } else {57 console.log('Device: ' + device);58 }59});60client.getDevice('deviceid', function (err, device) {61 if (err) {62 console.log('Error: ' + err);63 } else {64 console.log('Device: ' + device);65 }66});67client.getDevice('deviceid', function (err, device) {68 if (err) {69 console.log('Error: ' + err);70 } else {71 console.log('Device: ' + device);
Using AI Code Generation
1var stf = require('devicefarmer-stf-client');2var device = client.getDevice('serialnumber');3var x = 100;4var y = 100;5device.mouseMove(x, y);6device.mouseMove(x + 100, y + 100);7device.mouseMove(x + 200, y + 200);8var stf = require('devicefarmer-stf-client');9var device = client.getDevice('serialnumber');10var x = 100;11var y = 100;12device.mouseMove(x, y);13device.mouseMove(x + 100, y + 100);14device.mouseMove(x + 200, y + 200);15mouseMoveListener(x,y)16var stf = require('devicefarmer-stf-client');17var device = client.getDevice('serialnumber');18var x = 100;19var y = 100;20device.mouseMoveListener(x, y);21device.mouseMoveListener(x + 100, y + 100);22device.mouseMoveListener(x + 200, y + 200);23mouseDown()24var stf = require('devicefarmer-st
Using AI Code Generation
1var stf = require('devicefarmer-stf-client');2var device = client.getDevice('deviceSerial');3var mouseMoveListener = device.mouseMoveListener();4mouseMoveListener.on('data', function(data) {5 console.log(data);6});7mouseMoveListener.on('error', function(error) {8 console.log(error);9});10mouseMoveListener.on('end', function() {11 console.log('end');12});13var stf = require('devicefarmer-stf-client');14var device = client.getDevice('deviceSerial');15var touchMoveListener = device.touchMoveListener();16touchMoveListener.on('data', function(data) {17 console.log(data);18});19touchMoveListener.on('error', function(error) {20 console.log(error);21});22touchMoveListener.on('end', function() {23 console.log('end');24});25var stf = require('devicefarmer-stf-client');26var device = client.getDevice('deviceSerial');27var touchMoveListener = device.touchMoveListener();28touchMoveListener.on('data', function(data) {29 console.log(data);30});31touchMoveListener.on('error', function(error) {32 console.log(error);33});34touchMoveListener.on('end', function() {35 console.log('end');36});37var stf = require('devicefarmer-stf-client');38var device = client.getDevice('deviceSerial');39var touchMoveListener = device.touchMoveListener();40touchMoveListener.on('data', function(data) {41 console.log(data);42});43touchMoveListener.on('error', function(error) {44 console.log(error);45});46touchMoveListener.on('end', function() {47 console.log('end');48});49var stf = require('devicefarmer-stf-client');
Using AI Code Generation
1var stf = require('stf-client');2client.mouseMoveListener(function(data){3 console.log(data);4});5var stf = require('stf-client');6client.mouseMove(100,100);7var stf = require('stf-client');8client.mouseMove(100,100);9var stf = require('stf-client');10client.mouseMove(100,100);11var stf = require('stf-client');12client.mouseMove(100,100);13var stf = require('stf-client');14client.mouseMove(100,100);15var stf = require('stf-client');16client.mouseMove(100,100);17var stf = require('stf-client');18client.mouseMove(100,100);19var stf = require('stf-client');20client.mouseMove(100,100);21var stf = require('stf-client');22client.mouseMove(100,100);23var stf = require('stf-client');24client.mouseMove(100,100);
Using AI Code Generation
1var stf = require('devicefarmer-stf');2var device = service.getDevice('your device serial');3device.getScreen().then(function(screen) {4 screen.mouseMoveListener(function(x, y) {5 console.log('Mouse moved to ' + x + ',' + y);6 });7});8var stf = require('devicefarmer-stf');9var device = service.getDevice('your device serial');10device.getScreen().then(function(screen) {11 screen.mouseMoveListener(function(x, y) {12 console.log('Mouse moved to ' + x + ',' + y);13 });14});15var stf = require('devicefarmer-stf');16var device = service.getDevice('your device serial');17device.getScreen().then(function(screen) {18 screen.mouseMoveListener(function(x, y) {19 console.log('Mouse moved to ' + x + ',' + y);20 });21});22var stf = require('devicefarmer-stf');23var device = service.getDevice('your device serial');24device.getScreen().then(function(screen) {25 screen.mouseMoveListener(function(x, y) {26 console.log('Mouse moved to ' + x + ',' + y);27 });28});29var stf = require('devicefarmer-stf');
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!