Best JavaScript code snippet using storybook-root
Game.js
Source:Game.js  
1import Chunk from "../Chunk";2class Game extends Phaser.Scene {3    constructor() {4        super({key: 'Game'});5        this.customControls = {6            active: false,7            touches: [],8            prevTouch: false,9            left: false,10            up: false,11            right: false,12            down: false,13            speed: 1,14        }15        this.maxSpeed = 100;16        this.waveStartY = 0;17        this.waveVelocityY = -10;18        this.difficulty = 0;19        this.score = 0;20        this.debug = document.getElementById('debug');21    }22    init() {23        this.smoothZoom(this.input);24    }25    smoothZoom(inputs) {26        let zoom = setTimeout(() => {27            this.cameras.main.zoomTo(2.5);28        }, 2000);29    }30    handleWaveTouched() {31        this.scene.stop();32        this.scene.start('GameOver');33    }34    handleWaveAcceleration(d) {35        if(d > 0) {36            this.difficulty = d;37            this.waveVelocityY -= this.difficulty * 2;38            const waveTiles = this.wave.getChildren();39            waveTiles.forEach((tile) => {40                tile.body.velocity.y = this.waveVelocityY;41            });42        }43        else {44            this.difficulty = 0;45        }46    }47    update() {48        // retrieve position of chunk where follow point is49        let snappedChunkX = (this.chunkSize * this.tileSize) * Math.round(this.playerBoats.getChildren()[0].x / (this.chunkSize * this.tileSize));50        let snappedChunkY = (this.chunkSize * this.tileSize) * Math.round(this.playerBoats.getChildren()[0].y / (this.chunkSize * this.tileSize));51        snappedChunkX = snappedChunkX / this.chunkSize / this.tileSize;52        snappedChunkY = snappedChunkY / this.chunkSize / this.tileSize;53        54        // get current wave tiles data55        const waveTiles = this.wave.getChildren();56        const maxX = Math.max.apply(Math,waveTiles.map((o) => o.x));57        const maxXTile = waveTiles.find((o) => o.x == maxX);58        const minX = Math.min.apply(Math,waveTiles.map((o) => o.x));59        // create chunks around position if they don't exist60        for(let x = snappedChunkX - 2; x < snappedChunkX + 3; x++) {61            for(let y = snappedChunkY - 4; y < snappedChunkY + 2; y++) {62                const existingChunk = this.getChunk(x, y);63                if(existingChunk == null) {64                    console.log(`Creating chunk in (${x}, ${y})`)65                    const newChunk = new Chunk(this, x, y);66                    this.chunks.push(newChunk);67                    68                    if(newChunk.x * this.chunkSize * this.tileSize < minX) {69                        for(let i = minX; i > newChunk.x * this.chunkSize * this.tileSize; i -= this.tileSize) {70                            this.waveTile = this.physics.add.sprite(i, Math.round(maxXTile.y), 'water', 64).refreshBody();71                            this.waveTile.depth = 20;72                            this.waveTile.setVelocityY(this.waveVelocityY);73                            this.wave.add(this.waveTile);74                        }75                    }76                    if(newChunk.x * this.chunkSize * this.tileSize > maxX) {77                        for(let i = maxX; i < newChunk.x * this.chunkSize * this.tileSize; i += this.tileSize) {78                            this.waveTile = this.physics.add.sprite(i, Math.round(maxXTile.y), 'water', 64).refreshBody();79                            this.waveTile.depth = 20;80                            this.waveTile.setVelocityY(this.waveVelocityY);81                            this.wave.add(this.waveTile);82                        }83                    }84                }85            }86        }87        // load existings chunks close to position & unload existing chunks far from position88        for(let i = 0; i < this.chunks.length; i++) {89            const chunk = this.chunks[i];90            if(Phaser.Math.Distance.Between(snappedChunkX, snappedChunkY, chunk.x, chunk.y) < 4) {91                if(chunk !== null) {92                    chunk.load();93                    //console.log(`Loading chunk in (${chunk.x}, ${chunk.y})`)94                }95            }96            else {97                if(chunk !== null) {98                    chunk.unload();99                    //console.log(`Unloading chunk in (${chunk.x}, ${chunk.y})`)100                }101            }102        }103        // increment difficulty & wave speed depending on wave distance from start104        const d = Math.round(Math.log(Math.abs(Math.round(maxXTile.y + 1)) - Math.round(Math.abs(this.waveStartY))));105        if(d > 0 && d > this.difficulty) {106            this.handleWaveAcceleration(d);107        }108        // touch controls109        if(this.customControls.up) {110            this.playerBoats.setVelocityY(this.playerBoats.getChildren()[0].body.velocity.y - this.customControls.speed * Math.abs(this.customControls.up * 0.5));111        }112        if(this.customControls.down) {113            this.playerBoats.setVelocityY(this.playerBoats.getChildren()[0].body.velocity.y + this.customControls.speed * Math.abs(this.customControls.down * 0.5));114        }115        if(this.customControls.right) {116            this.playerBoats.setVelocityX(this.playerBoats.getChildren()[0].body.velocity.x + this.customControls.speed * Math.abs(this.customControls.right * 0.5));117        }118        if(this.customControls.left) {119            this.playerBoats.setVelocityX(this.playerBoats.getChildren()[0].body.velocity.x - this.customControls.speed * Math.abs(this.customControls.left * 0.5));120        }121        // keyboard controls122        if (this.keyZ.isDown) {123            this.playerBoats.y -= this.playerSpeed;124        }125        if (this.keyS.isDown) {126            this.playerBoats.y += this.playerSpeed;127        }128        if (this.keyQ.isDown) {129            this.playerBoats.x -= this.playerSpeed;130        }131        if (this.keyD.isDown) {132            this.playerBoats.x += this.playerSpeed;133        }134        this.playerCharacters.setX(this.playerBoats.getChildren()[0].x);135        this.playerCharacters.setY(this.playerBoats.getChildren()[0].y - 16);136        this.cameras.main.centerOn(this.playerBoats.getChildren()[0].x, this.playerBoats.getChildren()[0].y - 100);137        this.debug.innerHTML = 'pV : (' + Math.round(this.playerBoats.getChildren()[0].body.velocity.x) + ', ' + Math.round(this.playerBoats.getChildren()[0].body.velocity.y) + ') wV : ' + this.waveVelocityY + ' maxS : ' + this.maxSpeed;138    }139    create() {140        // general141        this.chunkSize = 8;142        this.tileSize = 16;143        this.chunks = [];144        this.physics.world.fixedStep = false; 145        146        this.input.on('pointerdown', (e) => this.handlePress(e));147        this.input.on('pointermove', (e) => this.handleDrag(e));148        this.input.on('pointerup', (e) => this.handleRelease(e));149        this.input.on('gameout', (e) => this.handleOut(e));150        // camera151        this.cameras.main.setZoom(1.5);152        // player boats153        // key 46 : guy with pirate hat154        // key 24 : boat facing up155        this.playerBoats = this.physics.add.group();156        const raft = this.physics.add.sprite(0, -100, 'beach', 20).refreshBody();157        this.playerBoats.add(raft);158        this.playerBoats.setDepth(1);159        this.playerBoats.getChildren().forEach(b => b.body.setDrag(50));160        this.playerBoats.getChildren().forEach(b => b.body.setMaxSpeed(this.maxSpeed));161        this.playerCharacters = this.physics.add.group();162        const adam = this.physics.add.sprite(0, -100, 'characters', 12).refreshBody();163        this.playerCharacters.add(adam);164        this.playerCharacters.setDepth(10);165        this.playerSpeed = 2;166        // tsunami167        this.wave = this.add.group();168        for(let i = -16; i < 16; i++) {169            this.waveTile = this.physics.add.sprite(i * this.tileSize, this.waveStartY, 'water', 64).refreshBody();170            this.waveTile.depth = 20;171            this.waveTile.setVelocityY(this.waveVelocityY);172            this.wave.add(this.waveTile);173        }174        // collisions175        this.physics.add.overlap(this.playerBoats,this.wave, this.handleWaveTouched, null, this);176        // controls177        this.keyZ = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Z);178        this.keyS = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);179        this.keyQ = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Q);180        this.keyD = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D);181    }182    preload() {183        this.load.spritesheet("water", "assets/water.png", {184            frameWidth: 16,185            frameHeight: 16186        });187        this.load.spritesheet('boats', 'assets/boats.png', { 188            frameWidth: 32, 189            frameHeight: 32190        }); 191        this.load.spritesheet('characters', 'assets/characters.png', {192            frameWidth: 32,193            frameHeight: 32194        });195        this.load.spritesheet('beach', 'assets/beach.png', {196            frameWidth: 32,197            frameHeight: 32198        })199    }200    getChunk(x, y) {201        let chunk = null;202        for(let i = 0; i < this.chunks.length; i++) {203            if(this.chunks[i].x == x && this.chunks[i].y == y) {204                chunk = this.chunks[i];205            }206        }207        return chunk;208    }209    /**210     * Input functions211     */212     handlePress(e) {213        if(e.event.changedTouches) {214            const touches = e.event.changedTouches;215            for(let i = 0; i < touches.length; i++) {216                this.customControls.touches.push(this.copyTouch(touches[i]));217            }218        }219        else {220            this.customControls.active = true;221        }222    }223    handleDrag(e) {224        if(e.event.changedTouches) {225            const touches = e.event.changedTouches;226            for(let i = 0; i < touches.length; i++) {227                const idx = this.ongoingTouchIndexById(touches[i].identifier);228                if(idx >= 0) {              229                    const touch = this.customControls.touches[idx];230                    if(this.customControls.prevTouch) {231                        touch.movementX = touch.pageX - this.customControls.prevTouch.pageX;232                        touch.movementY = touch.pageY - this.customControls.prevTouch.pageY;233                        if(touch.movementY > 1) this.customControls.up = touch.movementY;234                        else this.customControls.up = false;235                        if(touch.movementY < -1) this.customControls.down = touch.movementY;236                        else this.customControls.down = false;237                        if(touch.movementX > 1) this.customControls.left = touch.movementX;238                        else this.customControls.left = false;239                        if(touch.movementX < -1) this.customControls.right = touch.movementX;240                        else this.customControls.right = false;241                       this.customControls.touches.splice(idx, 1, this.copyTouch(touches[i]));242                    }243                   this.customControls.prevTouch = touch;244                }245            }246        }247        else {248            if(this.customControls.active) {249                if(e.event.movementX > 0) this.customControls.left = e.event.movementX;250                if(e.event.movementX < 0) this.customControls.right = e.event.movementX;251                if(e.event.movementY > 0) this.customControls.up = e.event.movementY;252                if(e.event.movementY < 0) this.customControls.down = e.event.movementY;253            }254        }255    }256    handleRelease(e) {257        if(e.event.changedTouches) {258            const touches = e.event.changedTouches;259            for(let i = 0; i < touches.length; i++) {260                const idx = this.ongoingTouchIndexById(touches[i].identifier);261                if(idx >= 0) {262                    this.customControls.left = false;263                    this.customControls.up = false;264                    this.customControls.right = false;265                    this.customControls.down = false;266                    this.customControls.touches.splice(idx, 1);267                    this.customControls.prevTouch = false;268                }269            }270        }271        else {272            this.customControls.active = false;273            this.customControls.left = false;274            this.customControls.up = false;275            this.customControls.right = false;276            this.customControls.down = false;277        }278    }279    handleOut() {280        this.customControls.active = false;281        this.customControls.left = false;282        this.customControls.up = false;283        this.customControls.right = false;284        this.customControls.down = false;285    }286    copyTouch({ identifier, pageX, pageY }) {287        return { identifier, pageX, pageY };288    }289    ongoingTouchIndexById(idToFind) {290        for (var i = 0; i < this.customControls.touches.length; i++) {291            var id = this.customControls.touches[i].identifier;292            if (id == idToFind) {293            return i;294            }295        }296        return -1;297    }298}...plugin.js
Source:plugin.js  
...10******************************************************************/11window.RevealCustomControls = window.RevealCustomControls || {12    id: 'RevealCustomControls',13    init: function(deck) {14        initCustomControls(deck);15    }16};17const initCustomControls = function(Reveal){18	var config = Reveal.getConfig().customcontrols || {};19	var collapseIcon = config.collapseIcon || '<i class="fa fa-chevron-down"></i>';20	var expandIcon = config.expandIcon || '<i class="fa fa-chevron-up"></i>';21	var tooltip = config.tooltip || 'Show/hide controls';22	var div = document.createElement( 'div' );23	div.id = 'customcontrols';24	var toggleButton = document.createElement( 'button' );25	toggleButton.title = tooltip;26	toggleButton.innerHTML = '<span id="collapse-customcontrols">' + collapseIcon + '</span>' + '<span id="expand-customcontrols">' + expandIcon + '</span>';27	toggleButton.addEventListener('click', function( event ) {28		var div = document.querySelector("div#customcontrols");...Using AI Code Generation
1import { CustomControls } from 'storybook-root';2export default {3};4const Template = (args) => <CustomControls {...args} />;5export const Primary = Template.bind({});6Primary.args = {7};8import React from 'react';9export const CustomControls = ({ label, primary, backgroundColor, size }) => {10  const mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';11  return (12      className={['storybook-button', `storybook-button--${size}`, mode].join(' ')}13      style={{ backgroundColor }}14      {label}15  );16};17CustomControls.defaultProps = {18};19import React from 'react';20import { addParameters } from '@storybook/react';21import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport';22addParameters({23  viewport: {24  },25});26const path = require('path');27module.exports = {28  webpackFinal: async (config) => {29    config.resolve.modules.push(path.resolve('./'));30    return config;31  },32};33const path = require('path');34module.exports = async ({ config }) => {35  config.resolve.modules.push(path.resolve('./'));36  return config;37};38{39  "scripts": {40  },41  "devDependencies": {Using AI Code Generation
1import { CustomControls } from 'storybook-root';2import { CustomControls } from 'storybook-root';3import { CustomControls } from 'storybook-root';4import { CustomControls } from 'storybook-root';5import { CustomControls } from 'storybook-root';6import { CustomControls } from 'storybook-root';7import { CustomControls } from 'storybook-root';8import { CustomControls } from 'storybook-root';9import { CustomControls } from 'storybook-root';10import { CustomControls } from 'storybook-root';11import { CustomControls } from 'storybook-root';12import { CustomControls } from 'storybook-root';13import { CustomControls } from 'storybook-root';Using AI Code Generation
1import { CustomControls } from 'storybook-root';2import { CustomControls } from 'storybook-root';3import { CustomControls } from 'storybook-root';4import { CustomControls } from 'storybook-root';5import { CustomControls } from 'storybook-root';6import { CustomControls } from 'storybook-root';7import { CustomControls } from 'storybook-root';8import { CustomControls } from 'storybook-root';9import { CustomControls } from 'storybook-root';10import { CustomControls } from 'storybook-root';11import { CustomControls } from 'storybook-root';12import { CustomControls } from 'storybook-root';13import { CustomControls } from 'storybook-root';Using AI Code Generation
1import { CustomControls } from 'storybook-root-decorator';2import { withKnobs, text, boolean, number } from '@storybook/addon-knobs';3export default {4};5export const CustomControlsTest = () => {6  const name = text('Name', 'John Doe');7  const age = number('Age', 44);8  const content = `I am ${name} and I'm ${age} years old.`;9  return <div>{content}</div>;10};11export const CustomControlsTest2 = () => {12  const name = text('Name', 'John Doe');13  const age = number('Age', 44);14  const content = `I am ${name} and I'm ${age} years old.`;15  return <div>{content}</div>;16};17CustomControlsTest.story = {18  parameters: {19    knobs: {20    },21  },22};23CustomControlsTest2.story = {24  parameters: {25    knobs: {26    },27  },28};29import React from 'react';30import { useParameter } from '@storybook/api';31import { useStorybookState } from '@storybook/api';32import { useAddonState } from '@storybook/api';33import { addons } from '@storybook/addons';34import { useChannel } from '@storybook/api';35import { styled } from '@storybook/theming';36const Panel = styled.div({37});38const Button = styled.button({39  '&:hover': {40  },41});42const CustomControls = storyFn => {Using AI Code Generation
1import { CustomControls } from 'storybook-root-decorator';2export default {3};4export const CustomControls = () => {5  return (6  );7};8import { CustomControls } from 'storybook-root-decorator';9addDecorator(CustomControls);10addParameters({11  options: {12  }13});14import { CustomControls } from 'storybook-root-decorator';15addDecorator(CustomControls);16addParameters({17  options: {18  }19});20import { CustomControls } from 'storybook-root-decorator';21addDecorator(CustomControls);Using AI Code Generation
1import { CustomControls } from 'storybook-root';2import { action } from '@storybook/addon-actions';3export default {4};5export const WithCustomControls = () => ({6  components: { CustomControls },7  methods: { action: action('clicked') },8});9import CustomControls from './CustomControls.vue';10export { CustomControls };11    <button @click="$emit('click')">Custom Controls</button>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!!
