Best JavaScript code snippet using playwright-internal
mohr.js
Source:mohr.js  
1//Holds the global stress variables2function Stress(x,y,xy, p1, p2, h, t, ap, at) {3	this.x = parseFloat(x);		//The input sigma_x value4	this.y = parseFloat(y);		//The input sigma_y value5	this.xy = parseFloat(xy);	//The input tau_xy value67	this.p1 = parseFloat(p1);	//The calculated principal stress (1) value8	this.p2 = parseFloat(p2);	//The calculated principal stress (2) value9	this.h = parseFloat(h);		//The calculated hydrostatic stress value10	this.t = parseFloat(t);		//The calculated maximum shear value1112	this.ap = parseFloat(ap);	//The angle at which the principal stresses occur13	this.at = parseFloat(at);	//The angle at which the max shear stresses occur14}1516var main = function () {17	// Initial Stress parameters18	var Sx = 5.e1;19	var Sy = 0.e1;20	var Txy = 1.e1;21	var P1 = 0.e1;22	var P2 = 0.e1;23	var H = 0.e1;24	var T = 0.e1;25	var AP = 1.e1;26	var AT = 0.e1;2728	// Define stress29	stress = new Stress(Sx,Sy,Txy,P1,P2,H,T,AP,AT);3031	// Stress calculations32	calcStress = function(stress) {33		var Sx = stress.x, Sy = stress.y, Txy = stress.xy;	//Input stresses (3 off)34		var P1 = stress.p1, P2 = stress.p2, H = stress.h, T = stress.t;	//Output stresses (4 off, 2 principal and 2 at max shear)35		var AP = stress.ap, AT = stress.AT;			//Angles (2 off, @ principal and @ max shear)36		var halfDif = (Sx - Sy) / 2;37		stress.t = Math.sqrt(Txy * Txy + halfDif * halfDif);38		stress.h = (Sx + Sy) / 2.;39		stress.p1 = stress.h + stress.t;40		stress.p2 = stress.h - stress.t;41		stress.ap = Math.atan(stress.xy / halfDif) / 2 * 180 / Math.PI;42		stress.at = stress.ap + 45;43	}44	calcStress(stress);4546	// Establish SVG elements47	var inElDiv = $('#inElementContainer') // SVG container for inputElement48	var mohrDiv = $('#mohrContainer') // SVG container for section49	var pDiv = $('#principalContainer') // SVG container for inputElement50	var sDiv = $('#shearContainer') // SVG container for section51	52	//Width and height of the 4 graphic elements53	var wInEl = 30054	var hInEl = 30055	var wMohr = 40056	var hMohr = 30057	var wPEl = 30058	var hPEl = 30059	var wTEl = 30060	var hTEl = 3006162	var svgInElementCanvas = SVG('inElementContainer').size(wInEl,hInEl)	// SVG for input Element6364	var svgCanvas = SVG('mohrContainer').size(wMohr,hMohr) // SVG for section65	var svgGroup = svgCanvas.group().flip('y',hMohr/2.) // Create a parent group with a new RH coordinate system 66	var svgMohrCanvas = svgGroup.nested() // Nest a full-size SVG inside group6768	var svgPrincipalCanvas = SVG('principalContainer').size(wPEl,hPEl)	// SVG for input Element69	var svgShearCanvas = SVG('shearContainer').size(wTEl,hTEl)	// SVG for input Element7071	function drawInElem() { 	// Create and draw input Element (inElement) in the 1st Div72		//Establish the box size and placing variables73		var boxOuter = Math.min(wInEl, hInEl);	//Change this to refer to the Div to make this section responsive74		var boxCenter = boxOuter / 2;75		var boxSize = boxOuter * 0.4;76		var boxGap = boxSize * 0.2;77		var arrowLen = boxGap * 0.3;78		var boxNear = (boxCenter-boxGap-boxSize/2), boxFar = (boxCenter+boxGap+boxSize/2);79		var arrowTrans = boxOuter * 0.25;8081		//Blank the canvas, define arrow82		svgInElementCanvas.clear();83		var endArrow = svgInElementCanvas.marker(8, 5, function(add) {84			add.path('M 8 2.5 L 0 0 L 0 5 Z')85		})86		//Title for the canvas87		var text = svgInElementCanvas.text(function(add) {88			  add.tspan('Input Stress').x(0).y(20).font("weight", "bold");89			  add.tspan('State').x(0).y(40).font("weight", "bold");90		});9192		//Draw the centre blue box93		svgInBox = svgInElementCanvas.rect(boxSize,boxSize).translate(boxCenter-boxSize/2,boxCenter-boxSize/2).fill('rgb(0,162,232)').stroke({color:'black',width:2})9495		// Establish the arrow length variables (based on stresses from sliders)96		var Sx = stress.x, Sy = stress.y, Txy = stress.xy;97			//Note: Calculated values aren't required for display in the input element98		var xArrowLen = arrowLen + (boxFar - boxNear) / 400 * Math.abs(Sx);99		var yArrowLen = arrowLen + (boxFar - boxNear) / 400 * Math.abs(Sy);100		var tArrowLen = arrowLen + (boxFar - boxNear) / 400 * Math.abs(Txy);101102		// Draw the Sigma_x Stress Arrows103		if (Sx == 0) {104			//No Arrows to draw - no stress in this direction105		} else {106			var text = svgInElementCanvas.text(function(add) {107				  add.tspan('\u03C3x').x(boxCenter+arrowTrans+20).y(boxCenter-5)108				  add.tspan(Sx.toFixed(1) + 'MPa').x(boxCenter+arrowTrans+2).y(boxCenter+14)109			});110			if (Sx > 0) {111				//Stress is positive. Draw arrows accordingly112				svgArrow = svgInElementCanvas.line(boxFar,boxCenter,boxFar+xArrowLen,boxCenter).stroke({color:'black',width:2})113				svgArrow.marker('end', endArrow)114				svgArrow = svgInElementCanvas.line(boxNear,boxCenter,boxNear-xArrowLen,boxCenter).stroke({color:'black',width:2})115				svgArrow.marker('end', endArrow)116			} else {117				//Stress is negative. Draw arrows accordingly118				svgArrow = svgInElementCanvas.line(boxFar+xArrowLen,boxCenter,boxFar,boxCenter).stroke({color:'black',width:2})119				svgArrow.marker('end', endArrow)120				svgArrow = svgInElementCanvas.line(boxNear-xArrowLen,boxCenter,boxNear,boxCenter).stroke({color:'black',width:2})121				svgArrow.marker('end', endArrow)122			}123		}124125		// Draw the Sigma_y Stress Arrows126		if (Sy == 0) {127			//No Arrows to draw - no stress in this direction128		} else {129			var text = svgInElementCanvas.text(function(add) {130				add.tspan('\u03C3y').x(boxCenter+5).y(boxCenter-arrowTrans-40)131				add.tspan(Sy.toFixed(1) + 'MPa').x(boxCenter+5).y(boxCenter-arrowTrans-20)132			});133			if (Sy > 0) {134				//Stress is positive. Draw arrows accordingly135				svgArrow = svgInElementCanvas.line(boxCenter,boxFar,boxCenter,boxFar+yArrowLen).stroke({color:'black',width:2})136				svgArrow.marker('end', endArrow)137				svgArrow = svgInElementCanvas.line(boxCenter,boxNear,boxCenter,boxNear-yArrowLen).stroke({color:'black',width:2})138				svgArrow.marker('end', endArrow)139140			} else {141				svgArrow = svgInElementCanvas.line(boxCenter,boxFar+yArrowLen,boxCenter,boxFar).stroke({color:'black',width:2})142				svgArrow.marker('end', endArrow)143				svgArrow = svgInElementCanvas.line(boxCenter,boxNear-yArrowLen,boxCenter,boxNear).stroke({color:'black',width:2})144				svgArrow.marker('end', endArrow)145			}146		}147		148		// Draw the Shear Stress Arrows149		if (Txy == 0) {150			//No Arrows to draw - no stress in this direction151		} else {152			var text = svgInElementCanvas.text(function(add) {153				  add.tspan('\u03C4xy').x(boxCenter+arrowTrans+3).y(boxCenter-arrowTrans-5)154				add.tspan(Txy.toFixed(1) + 'MPa').x(boxCenter+arrowTrans+3).y(boxCenter-arrowTrans+15)155			});156			if (Txy > 0) {157				//Stress is positive. Draw arrows accordingly158				svgArrow = svgInElementCanvas.line(boxCenter,boxCenter+tArrowLen,boxCenter,boxCenter-tArrowLen).translate(arrowTrans,0).rotate(0).stroke({color:'black',width:2})159				svgArrow.marker('end', endArrow)160				svgArrow = svgInElementCanvas.line(boxCenter,boxCenter-tArrowLen,boxCenter,boxCenter+tArrowLen).translate(arrowTrans,0).rotate(90,boxCenter-arrowTrans,boxCenter).stroke({color:'black',width:2})161				svgArrow.marker('end', endArrow)162				svgArrow = svgInElementCanvas.line(boxCenter,boxCenter+tArrowLen,boxCenter,boxCenter-tArrowLen).translate(arrowTrans,0).rotate(180,boxCenter-arrowTrans,boxCenter).stroke({color:'black',width:2})163				svgArrow.marker('end', endArrow)164				svgArrow = svgInElementCanvas.line(boxCenter,boxCenter-tArrowLen,boxCenter,boxCenter+tArrowLen).translate(arrowTrans,0).rotate(270,boxCenter-arrowTrans,boxCenter).stroke({color:'black',width:2})165				svgArrow.marker('end', endArrow)166			} else {167				//Stress is negative. Draw arrows accordingly168				svgArrow = svgInElementCanvas.line(boxCenter,boxCenter-tArrowLen,boxCenter,boxCenter+tArrowLen).translate(arrowTrans,0).rotate(0).stroke({color:'black',width:2})169				svgArrow.marker('end', endArrow)170				svgArrow = svgInElementCanvas.line(boxCenter,boxCenter+tArrowLen,boxCenter,boxCenter-tArrowLen).translate(arrowTrans,0).rotate(90,boxCenter-arrowTrans,boxCenter).stroke({color:'black',width:2})171				svgArrow.marker('end', endArrow)172				svgArrow = svgInElementCanvas.line(boxCenter,boxCenter-tArrowLen,boxCenter,boxCenter+tArrowLen).translate(arrowTrans,0).rotate(180,boxCenter-arrowTrans,boxCenter).stroke({color:'black',width:2})173				svgArrow.marker('end', endArrow)174				svgArrow = svgInElementCanvas.line(boxCenter,boxCenter+tArrowLen,boxCenter,boxCenter-tArrowLen).translate(arrowTrans,0).rotate(270,boxCenter-arrowTrans,boxCenter).stroke({color:'black',width:2})175				svgArrow.marker('end', endArrow)176			}177		}178	}179	drawInElem();180	181	// Draw the Mohr's Circle in the 2nd Div182	//svgMohrCanvas.viewbox(0,0,wMohr,hMohr)183184	function drawCircle() {	//Draw Mohr's Circle on the centre canvas185		//Establish variables186		var Sx = stress.x, Sy = stress.y, Txy = stress.xy;187		var Sx = stress.x, Sy = stress.y, Txy = stress.xy;	//Input stresses (3 off)188		var P1 = stress.p1, P2 = stress.p2, H = stress.h, T = stress.t;	//Output stresses (4 off, 2 principal and 2 at max shear)189		var AP = stress.ap, AT = stress.AT;			//Angles (2 off, @ principal and @ max shear)190191		var halfDif = (Sx - Sy) / 2;192193		//Blank the canvas ready to draw on194		svgMohrCanvas.clear();195		196		//Title for the canvas & Axis labels197		var text = svgMohrCanvas.text(function(add) {198			  add.tspan('Mohr\'s Circle').x(0).y(20).font("weight", "bold");199		}).flip('y',hMohr/2);200		var text = svgMohrCanvas.text(function(add) {201			  add.tspan('\u03C3').x(wMohr-15).y(hMohr/2+15)202			  add.tspan('\u03C4').x(wMohr/2+8).y(hMohr-8)203		}).flip('y',hMohr/2);204205		//Draw Circle on the centre canvas206		svgCentroid = svgMohrCanvas.circle().radius(T).translate(wMohr/2+H,hMohr/2).fill('rgba(34,177,76,0.25)').stroke({color:'black',width:1})207208		//Draw horizontal and vertical Axes209		svgLine = svgMohrCanvas.line(0,hMohr/2,wMohr,hMohr/2).stroke({color:'black',width:1})210		svgLine = svgMohrCanvas.line(wMohr/2,0,wMohr/2,hMohr).stroke({color:'black',width:1})211212		//Draw Axis Scale Marks213		for (i=1;i<wMohr/20;i++) {	//Loop through the width of the frame214			if (i/5 == (i/5).toFixed(0)) {	//Long lines every 5215				//Positive tick216				svgLine = svgMohrCanvas.line(wMohr/2+i*10,hMohr/2+4,wMohr/2+i*10,hMohr/2-4).stroke({color:'black',width:1})217				//Negative tick218				svgLine = svgMohrCanvas.line(wMohr/2-i*10,hMohr/2+4,wMohr/2-i*10,hMohr/2-4).stroke({color:'black',width:1})219				var text = svgMohrCanvas.text(function(add) {220					  add.tspan(i*10).x(wMohr/2+i*10-10).y(hMohr/2+15)221					  add.tspan(-i*10).x(wMohr/2-i*10-20).y(hMohr/2+15)222				}).flip('y',hMohr/2);223			} else {224				svgLine = svgMohrCanvas.line(wMohr/2+i*10,hMohr/2+2,wMohr/2+i*10,hMohr/2-2).stroke({color:'black',width:1})	//Positive tick225				svgLine = svgMohrCanvas.line(wMohr/2-i*10,hMohr/2+2,wMohr/2-i*10,hMohr/2-2).stroke({color:'black',width:1})	//Negative tick226			}227		}228		for (i=1;i<hMohr/20;i++) {	//Loop through the height of the frame229			if (i/5 == (i/5).toFixed(0)) {	//Long lines every 5230				svgLine = svgMohrCanvas.line(wMohr/2+4,hMohr/2+i*10,wMohr/2-4,hMohr/2+i*10).stroke({color:'black',width:1})	//Positive tick231				svgLine = svgMohrCanvas.line(wMohr/2+4,hMohr/2-i*10,wMohr/2-4,hMohr/2-i*10).stroke({color:'black',width:1})	//Negative tick232				var text = svgMohrCanvas.text(function(add) {233					  add.tspan(i*10).x(wMohr/2+4).y(hMohr/2+i*10+4)234					  add.tspan(-i*10).x(wMohr/2+4).y(hMohr/2-i*10+4)235				}).flip('y',hMohr/2);236			} else {237				svgLine = svgMohrCanvas.line(wMohr/2+2,hMohr/2+i*10,wMohr/2-2,hMohr/2+i*10).stroke({color:'black',width:1})	//Positive tick238				svgLine = svgMohrCanvas.line(wMohr/2+2,hMohr/2-i*10,wMohr/2-2,hMohr/2-i*10).stroke({color:'black',width:1})	//Negative tick239			}240		}241242		//Draw line showing 'zero' angle on the centre canvas243		svgLine = svgMohrCanvas.line(wMohr/2+H,hMohr/2,wMohr/2+H+halfDif,hMohr/2-Txy).stroke({color:'rgb(0,162,232)',width:2})244		svgLine = svgMohrCanvas.circle().radius(2).translate(wMohr/2+H-halfDif,hMohr/2+Txy).fill('rgb(0,162,232)').stroke({color:'rgb(0,162,232)',width:1})245		//Draw line showing principal stresses on the centre canvas246		svgLine = svgMohrCanvas.line(wMohr/2+H,hMohr/2,wMohr/2+H+T,hMohr/2).stroke({color:'rgb(255,127,39)',width:2})247		svgLine = svgMohrCanvas.circle().radius(2).translate(wMohr/2+H-T,hMohr/2).fill('rgb(255,127,39)').stroke({color:'rgb(255,127,39)',width:1})248		//Draw line showing max shear stresses on the centre canvas249		svgLine = svgMohrCanvas.line(wMohr/2+H,hMohr/2,wMohr/2+H,hMohr/2+T).stroke({color:'rgb(255,242,0)',width:2})250		svgLine = svgMohrCanvas.circle().radius(2).translate(wMohr/2+H,hMohr/2-T).fill('rgb(255,242,0)').stroke({color:'rgb(255,242,0)',width:1})251	}252	drawCircle();253254	function drawPrincipal() { 	// Create and draw the Element showing the Principal Stresses255		//Import the required stresses256		var Sx = stress.x, Sy = stress.y, Txy = stress.xy;257		var P1 = stress.p1, P2 = stress.p2, H = stress.h, T = stress.t;258		var AP = stress.ap, AT = stress.at;259		//Establish the box size and placing variables260		var boxOuter = Math.min(wPEl, hPEl);	//Change this to refer to the Div to make this section responsive261		var boxCenter = boxOuter / 2;262		var boxSize = boxOuter * 0.4;263		var boxBorder = (boxOuter - boxSize)/2;264		var boxGap = boxOuter * 0.08;265		var arrowLen = boxGap * 0.3;266		var boxNear = (boxBorder-boxGap), boxFar = (boxBorder+boxSize+boxGap);267		var arrowTrans = boxOuter * 0.25;268269		//Blank the canvas, define arrow270		svgPrincipalCanvas.clear();271		var endArrow = svgPrincipalCanvas.marker(8, 5, function(add) {272			add.path('M 8 2.5 L 0 0 L 0 5 Z')273		})274275		//Title for the canvas276		var text = svgPrincipalCanvas.text(function(add) {277			  add.tspan('Principal').x(0).y(20).font("weight", "bold");278			  add.tspan('Stresses').x(0).y(40).font("weight", "bold");279		});280281		//Draw the centre orange box282		svgInBox = svgPrincipalCanvas.rect(boxSize,boxSize).translate(boxBorder,boxBorder).rotate(-AP).fill('rgb(255,127,39)').stroke({color:'black',width:2})283284		// Establish the arrow length variables (based on stresses from sliders)285		var xArrowLen = arrowLen + (boxFar - boxNear) / 400 * Math.abs(P1);286		var yArrowLen = arrowLen + (boxFar - boxNear) / 400 * Math.abs(P2);287288		// Draw the First Principal Stress Arrows289		if (P1 == 0) {290			//No Arrows to draw - no stress in this direction291		} else {292			var text = svgPrincipalCanvas.text(function(add) {293				  add.tspan('\u03C31').x(boxCenter+arrowTrans+20).y(boxCenter-5)294				  add.tspan(P1.toFixed(1) + 'MPa').x(boxCenter+arrowTrans+2).y(boxCenter+14)295			}).rotate(-AP,boxCenter,boxCenter);296			if (P1 > 0) {297				//Stress is positive. Draw arrows accordingly298				svgArrow = svgPrincipalCanvas.line(boxCenter-xArrowLen,boxCenter,boxCenter,boxCenter).translate(boxCenter,0).rotate(-AP,0,boxCenter).stroke({color:'black',width:2})299				svgArrow.marker('end', endArrow)300				svgArrow = svgPrincipalCanvas.line(boxCenter-xArrowLen,boxCenter,boxCenter,boxCenter).translate(boxCenter,0).rotate(180-AP,0,boxCenter).stroke({color:'black',width:2})301				svgArrow.marker('end', endArrow)302			} else {303				//Stress is negative. Draw arrows accordingly304				svgArrow = svgPrincipalCanvas.line(boxCenter,boxCenter,boxCenter-xArrowLen,boxCenter).translate(boxCenter,0).rotate(-AP,0,boxCenter).stroke({color:'black',width:2})305				svgArrow.marker('end', endArrow)306				svgArrow = svgPrincipalCanvas.line(boxCenter,boxCenter,boxCenter-xArrowLen,boxCenter).translate(boxCenter,0).rotate(180-AP,0,boxCenter).stroke({color:'black',width:2})307				svgArrow.marker('end', endArrow)308			}309		}310311		// Draw the Second Principal Stress Arrows312		if (P2 == 0) {313			//No Arrows to draw - no stress in this direction314		} else {315			var text = svgPrincipalCanvas.text(function(add) {316				add.tspan('\u03C32').x(boxCenter+5).y(boxCenter-arrowTrans-40)317				add.tspan(P2.toFixed(1) + 'MPa').x(boxCenter+5).y(boxCenter-arrowTrans-20)318			}).rotate(-AP,boxCenter,boxCenter);319			if (P2 > 0) {320				//Stress is positive. Draw arrows accordingly321				svgArrow = svgPrincipalCanvas.line(boxCenter-yArrowLen,boxCenter,boxCenter,boxCenter).translate(boxCenter,0).rotate(90-AP,0,boxCenter).stroke({color:'black',width:2})322				svgArrow.marker('end', endArrow)323				svgArrow = svgPrincipalCanvas.line(boxCenter-yArrowLen,boxCenter,boxCenter,boxCenter).translate(boxCenter,0).rotate(270-AP,0,boxCenter).stroke({color:'black',width:2})324				svgArrow.marker('end', endArrow)325			} else {326				//Stress is negative. Draw arrows accordingly327				svgArrow = svgPrincipalCanvas.line(boxCenter,boxCenter,boxCenter-yArrowLen,boxCenter).translate(boxCenter,0).rotate(90-AP,0,boxCenter).stroke({color:'black',width:2})328				svgArrow.marker('end', endArrow)329				svgArrow = svgPrincipalCanvas.line(boxCenter,boxCenter,boxCenter-yArrowLen,boxCenter).translate(boxCenter,0).rotate(270-AP,0,boxCenter).stroke({color:'black',width:2})330				svgArrow.marker('end', endArrow)331			}332		}333334		//Draw the angle indicator335		if (AP == 0) {336			//No angle to report - leave blank337		} else {338			if (AP > 0) {339				//Rotational angle is positive, position angle indicator and text as required.340				var xTrans=Math.sqrt(Math.sqrt((Math.max(Math.min(20/Math.sin(AP/57.296),boxSize),boxSize*Math.sin(AP/57.296)))/boxSize));341				var text = svgPrincipalCanvas.text(function(add) {342					add.tspan(AP.toFixed(1) + '\u00B0').x(boxCenter-boxSize*(0.5-xTrans)).y(boxCenter+boxSize/2+xTrans/4*Math.sin(AP/57.296)+5)343				});344				angleLine = svgPrincipalCanvas.line(boxCenter-boxSize/2,boxCenter+boxSize/2,boxCenter-boxSize/2+boxSize*Math.cos(AP/57.296),boxCenter+boxSize/2+boxSize*Math.sin(AP/57.296)).translate(0,0).rotate(-AP,boxCenter,boxCenter).stroke({color:'black',width:1})345			} else {346				//Rotational angle is negative, position angle indicator and text as required.347				var xTrans=Math.sqrt(Math.sqrt((Math.max(Math.min(20/Math.sin(-AP/57.296),boxSize),boxSize*Math.sin(-AP/57.296)))/boxSize));348				var text = svgPrincipalCanvas.text(function(add) {349					add.tspan(-AP.toFixed(1) + '\u00B0').x(boxCenter-boxSize*(0.5-xTrans)).y(boxCenter-boxSize/2+xTrans/4*Math.sin(AP/57.296)+5)350				});351				angleLine = svgPrincipalCanvas.line(boxCenter-boxSize/2,boxCenter-boxSize/2,boxCenter-boxSize/2+boxSize*Math.cos(AP/57.296),boxCenter-boxSize/2+boxSize*Math.sin(AP/57.296)).translate(0,0).rotate(-AP,boxCenter,boxCenter).stroke({color:'black',width:1})352			}353		}354	}355	drawPrincipal();356357	function drawShear() { 	// Create and draw the Element showing the Shear Stresses358		//Import the required stresses359		var Sx = stress.x, Sy = stress.y, Txy = stress.xy;360		var P1 = stress.p1, P2 = stress.p2, H = stress.h, T = stress.t;361		var AP = stress.ap, AT = stress.at;362		//Establish the box size and placing variables363		var boxOuter = 300;	//Change this to refer to the Div to make this section responsive364		var boxCenter = boxOuter / 2;365		var boxSize = boxOuter * 0.4;366		var boxBorder = (boxOuter - boxSize)/2;367		var boxGap = boxOuter * 0.08;368		var arrowLen = boxGap * 0.3;369		var boxNear = (boxBorder-boxGap), boxFar = (boxBorder+boxSize+boxGap);370		var arrowTrans = boxOuter * 0.25;371372		//Blank the canvas, define arrow373		svgShearCanvas.clear();374		var endArrow = svgShearCanvas.marker(8, 5, function(add) {375			add.path('M 8 2.5 L 0 0 L 0 5 Z')376		})377378		//Title for the canvas379		var text = svgShearCanvas.text(function(add) {380			  add.tspan('Max Shear').x(0).y(20).font("weight", "bold");381			  add.tspan('Stresses').x(0).y(40).font("weight", "bold");382		});383384		//Draw the centre yellow box385		svgInBox = svgShearCanvas.rect(boxSize,boxSize).translate(boxBorder,boxBorder).rotate(-AT).fill('rgb(255,242,0)').stroke({color:'black',width:2})386387		// Establish the arrow length variable (based on stresses from sliders)388		var hArrowLen = arrowLen + (boxFar - boxNear) / 200 * Math.abs(H);389		var tArrowLen = arrowLen + (boxFar - boxNear) / 300 * Math.abs(T);390391		// Draw the First Principal Stress Arrows392		if (H == 0) {393			//No Arrows to draw - no stress in this direction394		} else {395			var text = svgShearCanvas.text(function(add) {396				  add.tspan('\u03C3avg').x(boxCenter+arrowTrans+20).y(boxCenter-5)397				  add.tspan(H.toFixed(1) + 'MPa').x(boxCenter+arrowTrans+2).y(boxCenter+14)398			}).rotate(-AT,boxCenter,boxCenter);399			if (H > 0) {400				//Stress is positive. Draw arrows accordingly401				svgArrow = svgShearCanvas.line(boxCenter-hArrowLen,boxCenter,boxCenter,boxCenter).translate(boxCenter,0).rotate(-AT,0,boxCenter).stroke({color:'black',width:2})402				svgArrow.marker('end', endArrow)403				svgArrow = svgShearCanvas.line(boxCenter-hArrowLen,boxCenter,boxCenter,boxCenter).translate(boxCenter,0).rotate(180-AT,0,boxCenter).stroke({color:'black',width:2})404				svgArrow.marker('end', endArrow)405				svgArrow = svgShearCanvas.line(boxCenter-hArrowLen,boxCenter,boxCenter,boxCenter).translate(boxCenter,0).rotate(90-AT,0,boxCenter).stroke({color:'black',width:2})406				svgArrow.marker('end', endArrow)407				svgArrow = svgShearCanvas.line(boxCenter-hArrowLen,boxCenter,boxCenter,boxCenter).translate(boxCenter,0).rotate(270-AT,0,boxCenter).stroke({color:'black',width:2})408				svgArrow.marker('end', endArrow)409			} else {410				//Stress is negative. Draw arrows accordingly411				svgArrow = svgShearCanvas.line(boxCenter,boxCenter,boxCenter-hArrowLen,boxCenter).translate(boxCenter,0).rotate(-AT,0,boxCenter).stroke({color:'black',width:2})412				svgArrow.marker('end', endArrow)413				svgArrow = svgShearCanvas.line(boxCenter,boxCenter,boxCenter-hArrowLen,boxCenter).translate(boxCenter,0).rotate(180-AT,0,boxCenter).stroke({color:'black',width:2})414				svgArrow.marker('end', endArrow)415				svgArrow = svgShearCanvas.line(boxCenter,boxCenter,boxCenter-hArrowLen,boxCenter).translate(boxCenter,0).rotate(90-AT,0,boxCenter).stroke({color:'black',width:2})416				svgArrow.marker('end', endArrow)417				svgArrow = svgShearCanvas.line(boxCenter,boxCenter,boxCenter-hArrowLen,boxCenter).translate(boxCenter,0).rotate(270-AT,0,boxCenter).stroke({color:'black',width:2})418				svgArrow.marker('end', endArrow)419			}420		}421422		// Draw the Shear Stress Arrows423		if (T == 0) {424			//No Arrows to draw - no stress in this direction425		} else {426			var text = svgShearCanvas.text(function(add) {427				  add.tspan('\u03C4max').x(boxCenter+arrowTrans+3).y(boxCenter-arrowTrans-5)428				add.tspan(T.toFixed(1) + 'MPa').x(boxCenter+arrowTrans+3).y(boxCenter-arrowTrans+15)429			}).rotate(-AT,boxCenter,boxCenter);430			if (T > 0) {431				//Stress is positive. Draw arrows accordingly432				svgArrow = svgShearCanvas.line(boxCenter+arrowTrans,boxCenter-tArrowLen,boxCenter+arrowTrans,boxCenter+tArrowLen).rotate(0-AT,boxCenter,boxCenter).stroke({color:'black',width:2})433				svgArrow.marker('end', endArrow)434				svgArrow = svgShearCanvas.line(boxCenter+arrowTrans,boxCenter+tArrowLen,boxCenter+arrowTrans,boxCenter-tArrowLen).rotate(90-AT,boxCenter,boxCenter).stroke({color:'black',width:2})435				svgArrow.marker('end', endArrow)436				svgArrow = svgShearCanvas.line(boxCenter+arrowTrans,boxCenter-tArrowLen,boxCenter+arrowTrans,boxCenter+tArrowLen).rotate(180-AT,boxCenter,boxCenter).stroke({color:'black',width:2})437				svgArrow.marker('end', endArrow)438				svgArrow = svgShearCanvas.line(boxCenter+arrowTrans,boxCenter+tArrowLen,boxCenter+arrowTrans,boxCenter-tArrowLen).rotate(270-AT,boxCenter,boxCenter).stroke({color:'black',width:2})439				svgArrow.marker('end', endArrow)440			} //else {441				//Stress is negative. Draw arrows accordingly442				//svgArrow = svgShearCanvas.line(boxCenter,boxCenter+tArrowLen,boxCenter,boxCenter-tArrowLen).translate(arrowTrans,0).rotate(0).stroke({color:'black',width:2})443				//svgArrow.marker('end', endArrow)444				//svgArrow = svgShearCanvas.line(boxCenter,boxCenter+tArrowLen,boxCenter,boxCenter-tArrowLen).translate(arrowTrans,0).rotate(90,boxCenter-arrowTrans,boxCenter).stroke({color:'black',width:2})445				//svgArrow.marker('end', endArrow)446				//svgArrow = svgShearCanvas.line(boxCenter,boxCenter+tArrowLen,boxCenter,boxCenter-tArrowLen).translate(arrowTrans,0).rotate(180,boxCenter-arrowTrans,boxCenter).stroke({color:'black',width:2})447				//svgArrow.marker('end', endArrow)448				//svgArrow = svgShearCanvas.line(boxCenter,boxCenter+tArrowLen,boxCenter,boxCenter-tArrowLen).translate(arrowTrans,0).rotate(270,boxCenter-arrowTrans,boxCenter).stroke({color:'black',width:2})449				//svgArrow.marker('end', endArrow)450			//}451		}452453		//Draw the angle indicator454		if (AT == 0) {455			//No angle to report - leave blank456		} else {457			if (AT > 0) {458				//Rotational angle is positive, position angle indicator and text as required.459				var xTrans=Math.sqrt(Math.sqrt((Math.max(Math.min(20/Math.sin(AT/57.296),boxSize),boxSize*Math.sin(AT/57.296)))/boxSize));460				var text = svgShearCanvas.text(function(add) {461					add.tspan(AT.toFixed(1) + '\u00B0').x(boxCenter-boxSize*(0.5-xTrans)).y(boxCenter+boxSize/2+xTrans/4*Math.sin(AT/57.296)+5)462				});463				angleLine = svgShearCanvas.line(boxCenter-boxSize/2,boxCenter+boxSize/2,boxCenter-boxSize/2+boxSize*Math.cos(AT/57.296),boxCenter+boxSize/2+boxSize*Math.sin(AT/57.296)).translate(0,0).rotate(-AT,boxCenter,boxCenter).stroke({color:'black',width:1})464			} else {465				//Rotational angle is negative, position angle indicator and text as required.466				var xTrans=Math.sqrt(Math.sqrt((Math.max(Math.min(20/Math.sin(-AT/57.296),boxSize),boxSize*Math.sin(-AT/57.296)))/boxSize));467				var text = svgShearCanvas.text(function(add) {468					add.tspan(-AT.toFixed(1) + '\u00B0').x(boxCenter-boxSize*(0.5-xTrans)).y(boxCenter-boxSize/2+xTrans/4*Math.sin(AT/57.296)+5)469				});470				angleLine = svgShearCanvas.line(boxCenter-boxSize/2,boxCenter-boxSize/2,boxCenter-boxSize/2+boxSize*Math.cos(AT/57.296),boxCenter-boxSize/2+boxSize*Math.sin(AT/57.296)).translate(0,0).rotate(-AT,boxCenter,boxCenter).stroke({color:'black',width:1})471			}472		}473	}474	drawShear();475476	// Set up page elements477    sXSlider = $('#sX .slider').slider({478		min: -1.e2,479		max: 1.e2,480		step: 1.e0,481		create: function(event, ui) {482			$(this).slider('value',stress.x);483			$('#sX .sliderValue').html(stress.x.toFixed(1) + "MPa");484		},485		slide: function(event,ui){486			$('#sX .sliderValue').html(ui.value.toFixed(1) + "MPa")487			stress.x = ui.value488			calcStress(stress);489			drawCircle();490			drawInElem();491			drawPrincipal();492			drawShear();493		}494	});495496    sYSlider = $('#sY .slider').slider({497		min: -1.e2,498		max: 1.e2,499		step: 1.e0,500		create: function(event, ui) {501			$(this).slider('value',stress.y);502			$('#sY .sliderValue').html(stress.y.toFixed(1) + "MPa");503		},504		slide: function(event,ui){505			$('#sY .sliderValue').html(ui.value.toFixed(1) + "MPa")506			stress.y = ui.value507			calcStress(stress);508			drawCircle();509			drawInElem();510			drawPrincipal();511			drawShear();512		}513	});514    515    tZSlider = $('#tZ .slider').slider({516		min: -1.e2,517		max: 1.e2,518		step: 1.e0,519		create: function(event, ui) {520			$(this).slider('value',stress.xy);521			$('#tZ .sliderValue').html(stress.xy.toFixed(1) + "MPa");522		},523		slide: function(event,ui){524			$('#tZ .sliderValue').html(ui.value.toFixed(1) + "MPa")525			stress.xy = ui.value526			calcStress(stress);527			drawCircle();528			drawInElem();529			drawPrincipal();530			drawShear();531		}532	});533}534
...Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3  const browser = await chromium.launch({ headless: false });4  const context = await browser.newContext();5  const page = await context.newPage();6  const element = await page.$('text="Get started"');7  const box = await element.boxModel();8  const x = (box.border[0].x + box.border[1].x) / 2;9  const y = (box.border[0].y + box.border[3].y) / 2;10  const elementHandle = await page.boxNear(x, y);11  console.log(await elementHandle.textContent());12  await browser.close();13})();14const { chromium } = require('playwright');15(async () => {16  const browser = await chromium.launch({ headless: false });17  const context = await browser.newContext();18  const page = await context.newPage();19  const element = await page.$('text="Get started"');20  const box = await element.boxModel();21  const x = (box.border[0].x + box.border[1].x) / 2;22  const y = (box.border[0].y + box.border[3].y) / 2;23  const boxModel = await page.boxModel(x, y);24  console.log(boxModel);25  await browser.close();26})();Using AI Code Generation
1const { webkit } = require('playwright');2(async () => {3  const browser = await webkit.launch();4  const context = await browser.newContext();5  const page = await context.newPage();6  await page.goto('https:/Using AI Code Generation
1const { chromium } = require('playwright');2const path = require('path');3(async () => {4  const browser = await chromium.launch();5  const context = await browser.newContext();6  const page = await context.newPage();7  const box = await page._delegate._pageProxy._mainFrameSession._client.send('DOM.getBoxModel', { nodeId: 1 });8  const boxNear = await page._delegate._pageProxy._mainFrameSession._client.send('DOM.boxModel', { model: box.model, x: 100, y: 100 });9  console.log(boxNear);10  await browser.close();11})();12    <div style="width: 100px; height: 100px; background-color: red; position: absolute; left: 0px; top: 0px;"></div>Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3  const browser = await chromium.launch();4  const page = await browser.newPage();5  const box = await page.evaluate(() => {6    const element = document.querySelector('text="Docs"');7    return element.boxModel();8  });9  const boxNear = await page.evaluateHandle((box) => {10    return window.__playwright__internal__boxNear(box);11  }, box);12  const boxNearValue = await boxNear.jsonValue();13  console.log(boxNearValue);14  await browser.close();15})();16{17}Using AI Code Generation
1const { chromium } = require("playwright");2(async () => {3  const browser = await chromium.launch();4  const page = await browser.newPage();5  const box = await page._delegate._mainFrame._boxModel(6    await page._delegate._mainFrame._document._querySelectorAll(7      await page._delegate._mainFrame._document._documentElement(),8  );9  console.log(box);10  await browser.close();11})();12{13  content: [ { x: 0, y: 0 }, { x: 0, y: 0 }, { x: 0, y: 0 }, { x: 0, y: 0 } ],14    { x: 0, y: 0 },15    { x: 0, y: 0 },16    { x: 0, y: 0 },17    { x: 0, y: 0 }18    { x: 0, y: 0 },19    { x: 0, y: 0 },20    { x: 0, y: 0 },21    { x: 0, y: 0 }22    { x: 0, y: 0 },23    { x: 0, y: 0 },24    { x: 0, y: 0 },25    { x: 0, y: 0 }26}Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3    const browser = await chromium.launch();4    const page = await browser.newPage();5    const element = await page.$('input[name="q"]');6    const box = await element.boxModel();7    console.log(box);8    const boxNear = await page._delegate._pageProxy._session.send('DOM.getBoxModel', { objectId: box.modelId });9    console.log(boxNear);10    await browser.close();11})();12{13    { x: 8, y: 8 },14    { x: 792, y: 8 },15    { x: 792, y: 32 },16    { x: 8, y: 32 }17    { x: 0, y: 0 },18    { x: 800, y: 0 },19    { x: 800, y: 40 },20    { x: 0, y: 40 }21    { x: -1, y: -1 },22    { x: 801, y: -1 },23    { x: 801, y: 41 },24    { x: -1, y: 41 }25    { x: -8, y: -8 },26    { x: 808, y: -8 },27    { x: 808, y: 48 },28    { x: -8, y: 48 }29}30{31    { x: 8, y: 8 },32    { x: 792, y: 8 },33    { x: 792, y: 32 },34    { x: 8, y: 32 }35    { x: 0, y: 0 },36    { x: 800, y: 0 },37    { x: 800, y: 40 },38    { x: 0, y: 40 }39    { x: -1, y: -1 },40    { x: 801, y: -1 },41    { x: 801, yUsing AI Code Generation
1const { boxNear } = require('playwright/lib/internal/frames');2const { chromium } = require('playwright');3(async () => {4  const browser = await chromium.launch();5  const context = await browser.newContext();6  const page = await context.newPage();7  const input = await page.$('input[name="q"]');8  const box = await boxNear(page, input, { x: 10, y: 10 });9  console.log(box);10  await browser.close();11})();12const { boxNear } = require('playwright/lib/internal/frames');13const { chromium } = require('playwright');14(async () => {15  const browser = await chromium.launch();16  const context = await browser.newContext();17  const page = await context.newPage();18  const input = await page.$('input[name="q"]');19  const box = await boxNear(page, input, { x: 10, y: 10 });20  console.log(box);21  await browser.close();22})();23const { boxNear } = require('playwright-box-near');24const box = await boxNear(page, elementHandle, { x: 10, y: 10 });25console.log(box);Using AI Code Generation
1const box = await page.evaluateHandle(() => {2  return document.querySelector('div').getBoundingClientRect();3});4const { x, y, width, height } = await box.jsonValue();5await page.mouse.move(x + width / 2, y + height / 2);6await page.mouse.down();7await page.mouse.move(x + 100, y + 100, { steps: 10 });8await page.mouse.up();9await page.screenshot({ path: 'test.png' });10const box = await page.evaluateHandle(() => {11  return document.querySelector('div').getBoundingClientRect();12});13const { x, y, width, height } = await box.jsonValue();14await page.mouse.move(x + width / 2, y + height / 2);15await page.mouse.down();16await page.mouse.move(x + 100, y + 100, { steps: 10 });17await page.mouse.up();18await page.screenshot({ path: 'test.png' });19const box = await page.evaluateHandle(() => {20  return document.querySelector('div').getBoundingClientRect();21});22const { x, y, width, height } = await box.jsonValue();23await page.mouse.move(x + width / 2, y + height / 2);24await page.mouse.down();25await page.mouse.move(x + 100, y + 100, { steps: 10 });26await page.mouse.up();27await page.screenshot({ path: 'test.png' });28const box = await page.evaluateHandle(() => {29  return document.querySelector('div').getBoundingClientRect();30});31const { x, y, width, height } = await box.jsonValue();32await page.mouse.move(x + width / 2, y + height / 2);33await page.mouse.down();34await page.mouse.move(x + 100, y + 100, { steps: 10 });35await page.mouse.up();36await page.screenshot({ path: 'test.png' });37const box = await page.evaluateHandle(() => {38  return document.querySelector('div').getBoundingClientRect();39});40const { x, y, width, height } = awaitUsing AI Code Generation
1const { boxNear } = require("playwright/lib/server/dom");2const { boxModel } = require("playwright/lib/server/dom");3const { chromium } = require("playwright");4(async () => {5  const browser = await chromium.launch({ headless: false });6  const page = await browser.newPage();7  const box = await boxModel(page, "#twotabsearchtextbox");8  const boxNearText = await boxNear(page, "Search", "#twotabsearchtextbox");9  await browser.close();10})();11exports.boxNear = boxNear;12exports.boxModel = boxModel;13const { boxNear } = require("playwright/lib/server/dom");14const { boxModel } = require("playwright/lib/server/dom");15const { chromium } = require("playwright");16(async () => {17  const browser = await chromium.launch({ headless: false });18  const page = await browser.newPage();19  const box = await boxModel(page, "#twotabsearchtextbox");20  const boxNearText = await boxNear(page, "Search", "#twotabsearchtextbox");21  await browser.close();22})();23exports.boxNear = boxNear;24exports.boxModel = boxModel;25const { boxNear } = require("playwright/lib/server/dom");26const { boxModel } = require("Using AI Code Generation
1const { boxNear } = require('@playwright/test/lib/server/frames');2const { test } = require('@playwright/test');3const { expect } = require('@playwright/test');4test.describe('Test', () => {5    test('test', async ({ page }) => {6        const element = await page.$('input[name="q"]');7        const box = await element.evaluateHandle((element) => boxNear(element, 0, 0));8        expect(box).toBeTruthy();9    });10});11import { test, expect } from '@playwright/test';12test.describe('Test', () => {13    test('test', async ({ page }) => {14        const element = await page.$('input[name="q"]');15        const box = await element.evaluateHandle((element) => boxNear(element, 0, 0));16        expect(box).toBeTruthy();17    });18});19const { boxNear } = require('@playwright/test/lib/server/frames');20const { test } = require('@playwright/test');21const { expect } = require('@playwright/test');22test.describe('Test', () => {23    test('test', async ({ page }) => {24        await page.exposeBinding('boxNear', (source, ...args) => {25            return boxNear(source, ...args);26        });27        const element = await page.$('input[name="q"]');28        const box = await element.evaluateHandle((element) => boxNear(element, 0, 0));29        expect(box).toBeTruthy();30    });31});LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!
