Best JavaScript code snippet using wpt
script.js
Source:script.js
...31 }32 })33 figures.splice(0, 1, { ...initialState[0] }, { ...initialState[1] })34} else figures = [{ ...initialState[0] }, { ...initialState[1] }]35drawFigures();36function canvasClick(event) {37 let clickX = event.pageX - canvas.offsetLeft;38 let clickY = event.pageY - canvas.offsetTop;39 for (let i = figures.length - 1; i >= 0; i--) {40 let figure = figures[i];41 if (figure.figure == 'elipse') {42 let e = Math.sqrt(1 - Math.pow(figure.radiusY, 2) / Math.pow(figure.radiusX, 2))43 let clickDistanceFromCenter = Math.sqrt(Math.pow(figure.x - clickX, 2) + Math.pow(figure.y - clickY, 2))44 let accessDistanceFromCenter = figure.radiusY / Math.sqrt(1 - Math.pow(e, 2) * Math.pow((figure.x - clickX) / clickDistanceFromCenter, 2))45 if (clickDistanceFromCenter <= accessDistanceFromCenter) {46 isDragging = true;47 if (previousSelectedFigure != null) previousSelectedFigure.isSelected = false;48 previousSelectedFigure = figure;49 previousSelectedFigure.isSelected = true;50 figures = figures.filter(item => item.isSelected == false)51 figures.push(figure)52 if (i == 0) {53 figures.splice(0, 1, { ...initialState[0] }, { ...initialState[1] })54 }55 drawFigures();56 return;57 }58 } else {59 if (clickX > figure.x && figure.x + figure.w > clickX && clickY > figure.y && figure.y + figure.h > clickY) {60 isDragging = true;61 if (previousSelectedFigure != null) previousSelectedFigure.isSelected = false;62 previousSelectedFigure = figure;63 previousSelectedFigure.isSelected = true;64 figures = figures.filter(item => item.isSelected == false)65 figures.push(figure)66 if (i == 1) {67 figures.splice(0, 1, { ...initialState[0] }, { ...initialState[1] })68 }69 console.log(figures)70 drawFigures();71 return;72 }73 }74 }75}76function mouseUp() {77 isDragging = false;78 if (isEnter == false) {79 figures = figures.filter(item => item.isSelected == false)80 drawFigures();81 }82}83document.addEventListener('keydown', event => {84 if (event.key == 'Delete') {85 figures = figures.filter(item => item.isSelected == false)86 drawFigures();87 }88})89function dragFigure(event) {90 if (previousSelectedFigure != null) {91 if (previousSelectedFigure.figure == 'elipse') {92 if (event.pageX - canvas.offsetLeft < window.innerWidth * 0.3 + previousSelectedFigure.radiusX ||93 event.pageX - canvas.offsetLeft > window.innerWidth * 0.9 - previousSelectedFigure.radiusX ||94 event.pageY - canvas.offsetTop < window.innerHeight * 0.15 + previousSelectedFigure.radiusY ||95 event.pageY - canvas.offsetTop > window.innerHeight * 0.9 - previousSelectedFigure.radiusY) {96 isEnter = false;97 } else isEnter = true;98 } else {99 if (event.pageX - canvas.offsetLeft < window.innerWidth * 0.3 ||100 event.pageX - canvas.offsetLeft > window.innerWidth * 0.9 - previousSelectedFigure.w ||101 event.pageY - canvas.offsetTop < window.innerHeight * 0.15 ||102 event.pageY - canvas.offsetTop > window.innerHeight * 0.9 - previousSelectedFigure.h) {103 isEnter = false;104 } else isEnter = true;105 }106 }107 if (isDragging == true) {108 if (previousSelectedFigure != null) {109 const x = event.pageX - canvas.offsetLeft;110 const y = event.pageY - canvas.offsetTop;111 previousSelectedFigure.x = x;112 previousSelectedFigure.y = y;113 drawFigures();114 }115 }116}117function drawFigures() {118 ctx.clearRect(0, 0, canvas.width, canvas.height);119 frame();120 figures.forEach(figure => {121 if (figure.figure == 'elipse') {122 ctx.beginPath();123 ctx.ellipse(figure.x, figure.y, figure.radiusX, figure.radiusY, 0, Math.PI * 2, false);124 ctx.fillStyle = 'blue';125 ctx.strokeStyle = "black";126 if (figure.isSelected) {127 ctx.lineWidth = 5;128 }129 else {130 ctx.lineWidth = 1;131 }...
Main.js
Source:Main.js
...17 let color = randomRGBA();18 let circle = new Circle(posX, posY, FIGURE_SIZE/2, color, context);19 figures.push(circle);20}21function drawFigures() {22 clearCanvas('#F8F8FF', canvas);23 for (let i = 0; i < figures.length; i++) {24 figures[i].draw();25 }26}27function drawFigures() {28 clearCanvas('#F8F8FF', canvas);29 for (let i = 0; i < figures.length; i++) {30 figures[i].draw();31 }32}33function findClickedFigure(x, y) {34 for (let index = 0; index < figures.length; index++) {35 const element = figures[index];36 if (element.isPointInside(x, y)) {37 return element;38 } 39 }40}41function initExample() {42 // Inicializar figuras de forma aleatoria43 for (let index = 0; index < NUM_FIGURES; index++) {44 if (Math.random() > 0.5) {45 addRectangle();46 } else {47 addCircle();48 }49 }50 //dibujar figuras51 drawFigures();52 // Inicializar listeners53 canvas.addEventListener('click', event => {54 // Se limpia la propiedad highlighted de la ultima figura clickedad para buscar la nueva55 if (lastClickedFigure != null) {56 lastClickedFigure.setHighlighted(false);57 lastClickedFigure = null;58 }59 let clickedFigure = findClickedFigure(event.layerX, event.layerY);60 if (clickedFigure != null) { 61 clickedFigure.setHighlighted(true);62 lastClickedFigure = clickedFigure; 63 }64 65 drawFigures();66 });67}...
Using AI Code Generation
1var wpt = require('./wpt.js');2var figures = wpt.drawFigures(5);3console.log(figures);4var drawFigures = function (n) {5 var figures = [];6 for (var i = 0; i < n; i++) {7 figures.push('Figure ' + i);8 }9 return figures;10}11module.exports.drawFigures = drawFigures;12var wpt = require('./wpt.js');13var figures = wpt.drawFigures(5);14console.log(figures);15var drawFigures = function (n) {16 var figures = [];17 for (var i = 0; i < n; i++) {18 figures.push('Figure ' + i);19 }20 return figures;21}22module.exports.drawFigures = drawFigures;23var wpt = require('./wpt.js');24var figures = wpt.drawFigures(5);25console.log(figures);26var drawFigures = function (n) {27 var figures = [];28 for (var i = 0; i < n; i++) {29 figures.push('Figure ' + i);30 }31 return figures;32}33module.exports.drawFigures = drawFigures;34var wpt = require('./wpt.js');35var figures = wpt.drawFigures(5);36console.log(figures);37var drawFigures = function (n) {38 var figures = [];39 for (var i = 0; i < n;
Using AI Code Generation
1var wpt = require('./wpt.js');2wpt.drawFigures(1,2,3);3exports.drawFigures = function(a,b,c){4 console.log(a+b+c);5}6In Node.js, we can also use the following syntax to import a file:7var wpt = require('./wpt');8console.log(__filename);9console.log(__dirname);10console.log(exports);11console.log(module.exports);12{}13{}14(function (exports, require, module, __filename, __dirname) {15});
Using AI Code Generation
1var wpt = require('./wpt');2var figs = require('./figures');3wpt.drawFigures(figs);4exports.drawFigures = function(figs){5};6var figures = [{},{}];7module.exports = figures;
Using AI Code Generation
1var wpt = require("./wpt.js");2wpt.drawFigures();3exports.drawFigures = function drawFigures(){4 console.log("drawFigures method");5}6var wpt = require("./wpt.js");7wpt.drawFigures();8exports.drawFigures = function(){9 console.log("drawFigures method");10}11var wpt = require("./wpt.js");12wpt.drawFigures();13function drawFigures(){14 console.log("drawFigures method");15}16var wpt = require("./wpt.js");17wpt.drawFigures();18function drawFigures(){19 console.log("drawFigures method");20}
Using AI Code Generation
1wpt.drawFigures("figures");2wpt.drawFigures = function (canvasId) {3 var canvas = document.getElementById(canvasId);4 var ctx = canvas.getContext("2d");5 ctx.fillStyle = "rgba(255,0,0,0.5)";6 ctx.fillRect(10, 10, 100, 100);7 ctx.fillStyle = "rgba(0,0,255,0.5)";8 ctx.fillRect(30, 30, 100, 100);9};10wpt.drawFigures("figures");11wpt.drawFigures = function (canvasId) {12 var canvas = document.getElementById(canvasId);13 if (canvas.getContext) {14 var ctx = canvas.getContext("2d");15 ctx.fillStyle = "rgba(255,0,0,0.5)";16 ctx.fillRect(10, 10, 100, 100);17 ctx.fillStyle = "rgba(0,0,255,0.5)";18 ctx.fillRect(30, 30, 100, 100);19 }20};
Using AI Code Generation
1var wpt = require('wpt');2wpt.drawFigures();3exports.drawFigures = function () {4 console.log('Draw figures');5};6module.exports.drawFigures = function () {7 console.log('Draw figures');8};9exports.drawFigures = function drawFigures() {10 console.log('Draw figures');11};12module.exports.drawFigures = function drawFigures() {13 console.log('Draw figures');14};15exports.drawFigures = drawFigures;16function drawFigures() {17 console.log('Draw figures');18};19module.exports.drawFigures = drawFigures;20function drawFigures() {21 console.log('Draw figures');22};23exports.drawFigures = function () {24 console.log('Draw figures');25};26exports.drawFigures = function drawFigures() {27 console.log('Draw figures');28};29exports.drawFigures = drawFigures;30module.exports.drawFigures = function () {31 console.log('Draw figures');32};33module.exports.drawFigures = function drawFigures() {34 console.log('Draw figures');35};36module.exports.drawFigures = drawFigures;37exports.drawFigures = function () {38 console.log('Draw figures');39};40exports.drawFigures = function drawFigures() {41 console.log('Draw figures');42};43exports.drawFigures = drawFigures;44module.exports.drawFigures = function () {45 console.log('Draw figures');46};47module.exports.drawFigures = function drawFigures() {48 console.log('Draw figures');49};50module.exports.drawFigures = drawFigures;51exports.drawFigures = function () {52 console.log('Draw figures');53};54exports.drawFigures = function drawFigures() {55 console.log('Draw figures');56};57exports.drawFigures = drawFigures;58module.exports.drawFigures = function () {59 console.log('Draw figures');60};61module.exports.drawFigures = function drawFigures() {62 console.log('Draw figures');63};64module.exports.drawFigures = drawFigures;65exports.drawFigures = function () {66 console.log('Draw figures');67};
Using AI Code Generation
1var wpt = require("./wpt");2var fig = require("./figures");3var figs = [];4figs.push(new fig.Rectangle(10, 20, 100, 200));5figs.push(new fig.Circle(100, 100, 50));6figs.push(new fig.Rectangle(200, 200, 50, 50));7wpt.drawFigures(figs);8var Rectangle = function (x, y, w, h) {9 this.x = x;10 this.y = y;11 this.width = w;12 this.height = h;13};14var Circle = function (x, y, r) {15 this.x = x;16 this.y = y;17 this.radius = r;18};19exports.Rectangle = Rectangle;20exports.Circle = Circle;21var drawFigures = function (figs) {22 for (var i = 0; i < figs.length; i++) {23 if (figs[i] instanceof Rectangle) {24 console.log("Drawing a rectangle at (" + figs[i].x + ", " + figs[i].y + ") with width " + figs[i].width + " and height " + figs[i].height);25 }26 else if (figs[i] instanceof Circle) {27 console.log("Drawing a circle at (" + figs[i].x + ", " + figs[i].y + ") with radius " + figs[i].radius);28 }29 }30};31exports.drawFigures = drawFigures;32Drawing a rectangle at (10, 20) with width 100 and height 20033Drawing a circle at (100, 100) with radius 5034Drawing a rectangle at (200, 200) with width 50 and height 50
Using AI Code Generation
1var wpt = require('./wpt');2wpt.drawFigures(4);3var wpt = function() {4 this.drawFigures = function(n) {5 for (var i = 1; i <= n; i++) {6 console.log(i);7 }8 }9}10module.exports = new wpt();
Using AI Code Generation
1var canvas = document.getElementById("myCanvas");2var ctx = canvas.getContext("2d");3var x = 0;4var y = 0;5var w = 100;6var h = 100;7var r = 50;8ctx.clearRect(0, 0, canvas.width, canvas.height);9ctx.beginPath();10ctx.moveTo(0,0);11ctx.lineTo(100,100);12ctx.stroke();13ctx.closePath();14ctx.beginPath();15ctx.rect(x,y,w,h);16ctx.stroke();17ctx.closePath();18ctx.beginPath();19ctx.arc(x,y,r,0,2*Math.PI);20ctx.stroke();21ctx.closePath();22ctx.beginPath();23ctx.moveTo(100,100);24ctx.lineTo(100,200);25ctx.lineTo(200,200);26ctx.lineTo(100,100);27ctx.stroke();28ctx.closePath();29ctx.beginPath();30ctx.rect(200,200,100,100);31ctx.stroke();32ctx.closePath();33ctx.beginPath();34ctx.moveTo(300,300);35ctx.lineTo(300,400);36ctx.lineTo(400,450);37ctx.lineTo(500,400);38ctx.lineTo(500,300);39ctx.lineTo(300,300);40ctx.stroke();41ctx.closePath();42ctx.beginPath();43ctx.moveTo(400,400);44ctx.lineTo(400,500);45ctx.lineTo(500,550);46ctx.lineTo(600,500);47ctx.lineTo(600,400);48ctx.lineTo(500,350);49ctx.lineTo(400,400);50ctx.stroke();51ctx.closePath();52ctx.beginPath();53ctx.moveTo(500,500);54ctx.lineTo(500,600);55ctx.lineTo(600,650);56ctx.lineTo(700,600);57ctx.lineTo(700,500);58ctx.lineTo(600,450);59ctx.lineTo(500,500);60ctx.stroke();61ctx.closePath();62ctx.beginPath();63ctx.moveTo(600,600);64ctx.lineTo(600,700);65ctx.lineTo(700,750);66ctx.lineTo(800,700);67ctx.lineTo(800,600);68ctx.lineTo(700,550);69ctx.lineTo(600,600);70ctx.stroke();71ctx.closePath();72ctx.beginPath();73ctx.moveTo(700,700);74ctx.lineTo(700,800);75ctx.lineTo(800,850
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!!