Best JavaScript code snippet using wpt
circle.js
Source:circle.js
1let points = [], utils = Bezier.getUtils();2setup() {3 let w = this.width/4;4 let h = this.height/3;5 points.push({x: 0.30 * this.width, y: 0.25 * this.height});6 points.push({x: 0.80 * this.width, y: 0.50 * this.height});7 points.push({x: 0.30 * this.width, y: 0.75 * this.height});8 if (this.parameters.showCurve) {9 points[1].x = 0.66 * this.width;10 points[1].y = 0.75 * this.height;11 }12 setMovable(points);13}14draw() {15 clear();16 if (points.length === 3) {17 let [p1, p2, p3] = points;18 let c = this.caclulateCenter(p1, p2, p3);19 if (c) {20 if (this.parameters.showCurve) {21 this.showCurve(p1, p2, p3, c);22 } else {23 this.showChords(p1, p2, p3, c);24 }25 setColor(`black`);26 circle(c.x, c.y, 2);27 text(`center`, c.x+5, c.y+5);28 setFill(`#FF000030`)29 setStroke(`red`);30 circle(c.x, c.y, c.r);31 noFill();32 setStroke(`black`)33 arc(c.x, c.y, c.r, c.s, c.e);34 }35 }36 setColor(`black`);37 let lbl = this.parameters.showCurve ? [`start`, `B`, `end`] : [`p1`, `p2`, `p3`];38 points.forEach((p,i) => {39 circle(p.x, p.y, 3);40 text(lbl[i], p.x+7, p.y+7);41 });42}43caclulateCenter(p1, p2, p3) {44 // deltas45 const dx1 = (p2.x - p1.x),46 dy1 = (p2.y - p1.y),47 dx2 = (p3.x - p2.x),48 dy2 = (p3.y - p2.y);49 // perpendiculars (quarter circle turned)50 const dx1p = dx1 * cos(PI/2) - dy1 * sin(PI/2),51 dy1p = dx1 * sin(PI/2) + dy1 * cos(PI/2),52 dx2p = dx2 * cos(PI/2) - dy2 * sin(PI/2),53 dy2p = dx2 * sin(PI/2) + dy2 * cos(PI/2);54 // chord midpoints55 const mx1 = (p1.x + p2.x)/2,56 my1 = (p1.y + p2.y)/2,57 mx2 = (p2.x + p3.x)/2,58 my2 = (p2.y + p3.y)/2;59 // midpoint offsets60 const mx1n = mx1 + dx1p,61 my1n = my1 + dy1p,62 mx2n = mx2 + dx2p,63 my2n = my2 + dy2p;64 // intersection of these lines:65 const i = utils.lli8(mx1,my1,mx1n,my1n, mx2,my2,mx2n,my2n);66 if (!i) return false;67 const r = utils.dist(i,p1);68 // determine arc start/mid/end values, and direction (cw/ccw correction)69 let s = atan2(p1.y - i.y, p1.x - i.x),70 m = atan2(p2.y - i.y, p2.x - i.x),71 e = atan2(p3.y - i.y, p3.x - i.x),72 __;73 if (s<e) {74 if (s>m || m>e) { s += TAU; }75 if (s>e) { __=e; e=s; s=__; }76 } else {77 if (e<m && m<s) { __=e; e=s; s=__; } else { e += TAU; }78 }79 // assign and done.80 i.s = s;81 i.e = e;82 i.r = r;83 return i;84}85showChords(p1, p2, p3, c) {86 setStroke(randomColor() );87 line(p1.x, p1.y, p2.x, p2.y);88 let m1 = { x: (p1.x+p2.x)/2, y: (p1.y+p2.y)/2 };89 line(m1.x, m1.y, c.x + (c.x-m1.x)/2, c.y + (c.y-m1.y)/2);90 setStroke(randomColor() );91 line(p3.x, p3.y, p2.x, p2.y);92 let m2 = { x: (p3.x+p2.x)/2, y: (p3.y+p2.y)/2 };93 line(m2.x, m2.y, c.x + (c.x-m2.x)/2, c.y + (c.y-m2.y)/2);94 setStroke(randomColor() );95 line(p3.x, p3.y, p1.x, p1.y);96 let m3 = { x: (p3.x+p1.x)/2, y: (p3.y+p1.y)/2 };97 line(m3.x, m3.y, c.x + (c.x-m3.x)/2, c.y + (c.y-m3.y)/2);98}99showCurve(p1, p2, p3, c) {100 const d1 = dist(p1.x, p1.y, p2.x, p2.y),101 d2 = dist(p3.x, p3.y, p2.x, p2.y),102 t = d1 / (d1 + d2),103 { A, B, C, S, E } = Bezier.getABC(3, p1, p2, p3, t);104 setStroke(`lightblue`);105 line(B.x,B.y,S.x,S.y);106 line(B.x,B.y,E.x,E.y);107 text(`t=${t.toFixed(2)}`, B.x+10, B.y+20);108 // Check which length we need to use for our e1-e2 segment,109 // corrected for whether B is "above" or "below" the baseline:110 const angle = (atan2(E.y-S.y, E.x-S.x) - atan2(B.y-S.y, B.x-S.x) + TAU) % TAU,111 bc = (angle < 0 || angle > PI ? -1 : 1) * dist(S.x, S.y, E.x, E.y)/3,112 de1 = t * bc,113 de2 = (1-t) * bc;114 // We then determine the circle-aligned slope as normalized dx/dy115 const tangent = [116 { x: B.x - 10 * (B.y-c.y), y: B.y + 10 * (B.x-c.x) },117 { x: B.x + 10 * (B.y-c.y), y: B.y - 10 * (B.x-c.x) }118 ],119 tlength = dist(tangent[0].x, tangent[0].y, tangent[1].x, tangent[1].y),120 dx = (tangent[1].x - tangent[0].x)/tlength,121 dy = (tangent[1].y - tangent[0].y)/tlength;122 line(tangent[0].x, tangent[0].y, tangent[1].x, tangent[1].y);123 // and then construct e1 and e2 as per the chapter text124 const e1 = { x: B.x + de1 * dx, y: B.y + de1 * dy};125 circle(e1.x, e1.y, 3);126 text(`e1`, e1.x+15, e1.y+10);127 const e2 = { x: B.x - de2 * dx, y: B.y - de2 * dy };128 circle(e2.x, e2.y, 3);129 text(`e2`, e2.x+15, e2.y+10);...
bezierCubic2Q2.js
Source:bezierCubic2Q2.js
1/**2 * @file ä¸æ¬¡è´å¡å°è½¬äºæ¬¡è´å¡å°3 * @author mengke01(kekee000@gmail.com)4 *5 * references:6 * https://github.com/search?utf8=%E2%9C%93&q=svg2ttf7 * http://www.caffeineowl.com/graphics/2d/vectorial/cubic2quad01.html8 *9 */10function findIntersection(p1, c1, c2, p2) {11 var d1 = { x: c1.x - p1.x, y: c1.y - p1.y }12 var d2 = { x: c2.x - p2.x, y: c2.y - p2.y }13 var det = d2.x * d1.y - d2.y * d1.x;14 if (Math.abs(det) < 1e-6) return null;15 var u = ((p2.y - p1.y) * d2.x - (p2.x - p1.x) * d2.y) / det16 var v = ((p2.y - p1.y) * d1.x - (p2.x - p1.x) * d1.y) / det17 if (u <= 0 || v <= 0) return null;18 return {19 x: p1.x + d1.x * u,20 y: p1.y + d1.y * u21 }22}23function toQuad(p1, c1, c2, p2) {24 var pt = findIntersection(p1, c1, c2, p2);25 if (!pt) pt = {26 x: (p1.x + p2.x) / 2,27 y: (p1.y + p2.y) / 228 }29 return [p1, pt, p2]30}31/**32 * ä¸æ¬¡è´å¡å°è½¬äºæ¬¡è´å¡å°33 *34 * @param {Object} p1 å¼å§ç¹35 * @param {Object} c1 æ§å¶ç¹136 * @param {Object} c2 æ§å¶ç¹237 * @param {Object} p2 ç»æç¹38 * @return {Array} äºæ¬¡è´å¡å°æ§å¶ç¹39 */40function bezierCubic2Q2(p1, c1, c2, p2, level) {41 level = level || 0;42 // å¤ææ端æ
åµï¼æ§å¶ç¹åèµ·æ¢ç¹ä¸æ ·43 if (p1.x === c1.x && p1.y === c1.y && c2.x === p2.x && c2.y === p2.y) {44 return [45 [46 p1,47 {48 x: (p1.x + p2.x) / 2,49 y: (p1.y + p2.y) / 250 },51 p252 ]53 ];54 }55 var mx = p2.x - 3 * c2.x + 3 * c1.x - p1.x;56 var my = p2.y - 3 * c2.y + 3 * c1.y - p1.y;57 // control points near58 if (mx * mx + my * my <= 16 || level > 4) {59 return [60 toQuad(p1, c1, c2, p2)61 ];62 }63 // Split to 2 qubic beziers by midpoints64 // (p2 + 3*c2 + 3*c1 + p1)/865 var mp = {66 x: (p2.x + 3 * c2.x + 3 * c1.x + p1.x) / 8,67 y: (p2.y + 3 * c2.y + 3 * c1.y + p1.y) / 868 };69 return bezierCubic2Q2(70 p1,71 {72 x: (p1.x + c1.x) / 2,73 y: (p1.y + c1.y) / 274 },75 {76 x: (p1.x + 2 * c1.x + c2.x) / 4,77 y: (p1.y + 2 * c1.y + c2.y) / 478 },79 mp,80 level + 181 ).concat(bezierCubic2Q2(82 mp,83 {84 x: (p2.x + c1.x + 2 * c2.x) / 4,85 y: (p2.y + c1.y + 2 * c2.y) / 486 },87 {88 x: (p2.x + c2.x) / 2,89 y: (p2.y + c2.y) / 290 },91 p2,92 level + 193 ))94}...
StrokeSegment.js
Source:StrokeSegment.js
1import { StrokePoint } from "./StrokePoint.js";2export class StrokeSegment {3 constructor(stroke, p1, p2) {4 this.stroke = stroke;5 this.p1 = new StrokePoint(p1[0], p1[1], 0.001, 0.001);6 this.p2 = new StrokePoint(p2[0], p2[1], 0.001, 0.001);7 }8 *[Symbol.iterator]() {9 yield this.p1.x;10 yield this.p1.y;11 yield this.p2.x;12 yield this.p2.y;13 }14 intersects(other) {15 //let orientations1 = orientation(this.p1, other.p1, this.p2);16 //let orientations2 = orientation(this.p1, other.p1, other.p2);17 //let orientations3 = orientation(this.p2, other.p2, this.p1);18 //let orientations4 = orientation(this.p2, other.p2, other.p1);19 let orientation1L = orientation(this.p1, this.p2, other.p1);20 let orientation1R = orientation(this.p1, this.p2, other.p2);21 let orientation2L = orientation(other.p1, other.p2, this.p1);22 let orientation2R = orientation(other.p1, other.p2, this.p2);23 // General case24 if (orientation1L != orientation1R &&25 orientation2L != orientation2R) {26 return true;27 }28 // Special Cases29 // p1, q1 and p2 are colinear and p2 lies on segment p1q130 else if (orientation1L == 0 && onSegment(this.p1, this.p2, other.p1)) {31 return true;32 }33 // p1, q1 and q2 are colinear and q2 lies on segment p1q134 else if (orientation1R == 0 && onSegment(this.p1, other.p2, other.p1)) {35 return true; }36 // p2, q2 and p1 are colinear and p1 lies on segment p2q237 else if (orientation2L == 0 && onSegment(this.p2, this.p1, other.p2)) {38 return true; }39 // p2, q2 and q1 are colinear and q1 lies on segment p2q240 else if (orientation2R == 0 && onSegment(this.p2, other.p1, other.p2)) {41 return true; }42 else {43 return false; // Doesn't fall in any of the above cases44 }45 }46}47function orientation(p, q, r) {48 let val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);49 // Lines are Colinear50 if (val == 0) return 0;51 return (val > 0) ? 1: 2;52}53function onSegment(p, q, r) {54 return false;55 if (q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) &&56 q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y))57 return true;58 return false;...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.runTest('www.google.com', function(err, data) {4 if (err) return console.error(err);5 console.log(data);6});7var wpt = require('webpagetest');8var wpt = new WebPageTest('www.webpagetest.org');9wpt.runTest('www.google.com', { location: 'ec2-eu-west-1' }, function(err, data) {10 if (err) return console.error(err);11 console.log(data);12});13var wpt = require('webpagetest');14var wpt = new WebPageTest('www.webpagetest.org');15wpt.runTest('www.google.com', { location: 'ec2-eu-west-1', mobile: true }, function(err, data) {16 if (err) return console.error(err);17 console.log(data);18});19var wpt = require('webpagetest');20var wpt = new WebPageTest('www.webpagetest.org');21wpt.runTest('www.google.com', { location: 'ec2-eu-west-1', mobile: true, connectivity: '3G' }, function(err, data) {22 if (err) return console.error(err);23 console.log(data);24});25var wpt = require('webpagetest');26var wpt = new WebPageTest('www.webpagetest.org');27wpt.runTest('www.google.com', { location: 'ec2-eu-west-1', mobile: true, connectivity: '3G', firstViewOnly: true }, function(err, data) {28 if (err) return console.error(err);29 console.log(data);30});31var wpt = require('webpagetest');32var wpt = new WebPageTest('www.webpagetest.org');33wpt.runTest('www.google.com', { location: 'ec2-eu-west-1', mobile: true, connectivity: '3G', firstViewOnly: true, runs: 5 }, function(err, data) {34 if (err) return console.error(err);
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9var wpt = require('wpt');10var wpt = new WebPageTest('www.webpagetest.org');11 if (err) {12 console.log(err);13 } else {14 console.log(data);15 }16});17var wpt = require('wpt');18var wpt = new WebPageTest('www.webpagetest.org');19 if (err) {20 console.log(err);21 } else {22 console.log(data);23 }24});25var wpt = require('wpt');26var wpt = new WebPageTest('www.webpagetest.org');27 if (err) {28 console.log(err);29 } else {30 console.log(data);31 }32});33var wpt = require('wpt');34var wpt = new WebPageTest('www.webpagetest.org');35 if (err) {36 console.log(err);37 } else {38 console.log(data);39 }40});41var wpt = require('wpt');42var wpt = new WebPageTest('www.webpagetest.org');43 if (err) {44 console.log(err);45 } else {46 console.log(data);47 }48});49var wpt = require('wpt');50var wpt = new WebPageTest('www.webpagetest.org');51 if (err) {
Using AI Code Generation
1var wptools = require('wptools');2wptools.page('Barack Obama').then(function(page) {3 return page.getImages();4}).then(function(images) {5 console.log(images);6});
Using AI Code Generation
1var wptools = require('wptools');2wptools.page('Barack Obama').then(function(page) {3 page.p2().then(function(data) {4 console.log(data);5 });6});7var wptools = require('wptools');8wptools.page('Barack Obama').then(function(page) {9 page.p2().then(function(data) {10 console.log(data);11 });12});13var wptools = require('wptools');14wptools.page('Barack Obama').then(function(page) {15 page.p2().then(function(data) {16 console.log(data);17 });18});19var wptools = require('wptools');20wptools.page('Barack Obama').then(function(page) {21 page.p2().then(function(data) {22 console.log(data);23 });24});25var wptools = require('wptools');26wptools.page('Barack Obama').then(function(page) {27 page.p2().then(function(data) {28 console.log(data);29 });30});31var wptools = require('wptools');32wptools.page('Barack Obama').then(function(page) {33 page.p2().then(function(data) {34 console.log(data);35 });36});37var wptools = require('wptools');38wptools.page('Barack Obama').then(function(page) {39 page.p2().then(function(data) {40 console.log(data);41 });42});43var wptools = require('wptools');44wptools.page('Barack Obama').then(function(page) {45 page.p2().then(function(data) {46 console.log(data);47 });48});49var wptools = require('wptools');50wptools.page('Barack Obama').then(function(page) {51 page.p2().then(function(data) {52 console.log(data);53 });54});
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!!