Best JavaScript code snippet using wpt
pageOnLoad.js
Source:pageOnLoad.js
1// get from backend the current canvas object assuming curCanvasId is already correctly set2function getCurCanvas(viewId) {3 var gvd = globalVar.views[viewId];4 // get all jumps starting at currrent canvas5 gvd.curJump = getJumpsByCanvasId(gvd.curCanvasId);6 // check if cache has it7 var postData = "id=" + gvd.curCanvasId;8 for (var i = 0; i < gvd.predicates.length; i++)9 postData += "&predicate" + i + "=" + getSqlPredicate(gvd.predicates[i]);10 if (postData in globalVar.cachedCanvases)11 return new Promise(function(resolve) {12 // note that we don't directly get canvas objects from gvd.project13 // because sometimes the canvas w/h is dynamic and not set, in which14 // case we need to fetch from the backend (using gvd.predicates)15 gvd.curCanvas = globalVar.cachedCanvases[postData].canvasObj;16 gvd.curStaticData = globalVar.cachedCanvases[postData].staticData;17 setupLayerLayouts(viewId);18 resolve();19 });20 // otherwise make a non-blocked http request to the server21 return $.ajax({22 type: "GET",23 url: globalVar.serverAddr + "/canvas",24 data: postData,25 success: function(data) {26 gvd.curCanvas = JSON.parse(data).canvas;27 if (gvd.curCanvas.w < gvd.viewportWidth)28 gvd.curCanvas.w = gvd.viewportWidth;29 if (gvd.curCanvas.h < gvd.viewportHeight)30 gvd.curCanvas.h = gvd.viewportHeight;31 gvd.curStaticData = JSON.parse(data).staticData;32 setupLayerLayouts(viewId);33 // insert into cache34 if (!(postData in globalVar.cachedCanvases)) {35 globalVar.cachedCanvases[postData] = {};36 globalVar.cachedCanvases[postData].canvasObj = gvd.curCanvas;37 globalVar.cachedCanvases[postData].staticData =38 gvd.curStaticData;39 }40 }41 });42}43// setup <g>s and <svg>s for each layer44function setupLayerLayouts(viewId) {45 var gvd = globalVar.views[viewId];46 // number of layers47 var numLayers = gvd.curCanvas.layers.length;48 // set box coordinates49 gvd.boxX = [-1e5];50 gvd.boxY = [-1e5];51 gvd.boxH = [-1e5];52 gvd.boxW = [-1e5];53 // set render data54 gvd.renderData = [];55 for (var i = 0; i < numLayers; i++) gvd.renderData.push([]);56 gvd.tileRenderData = {};57 // create layers58 for (var i = numLayers - 1; i >= 0; i--) {59 var curLayer = gvd.curCanvas.layers[i];60 var isStatic = curLayer.isStatic;61 // add new <g>62 d3.select(".view_" + viewId + ".maing")63 .append("g")64 .classed("view_" + viewId + " layerg layer" + i, true)65 .append("svg")66 .classed("view_" + viewId + " mainsvg", true)67 .classed("static", isStatic)68 .classed("dbox", !isStatic && curLayer.fetchingScheme == "dbox")69 .classed("tiling", !isStatic && curLayer.fetchingScheme == "tiling")70 .attr("width", gvd.viewportWidth)71 .attr("height", gvd.viewportHeight)72 .attr("preserveAspectRatio", "none")73 .attr("x", 0)74 .attr("y", 0)75 .attr(76 "viewBox",77 isStatic78 ? "0 0" + " " + gvd.viewportWidth + " " + gvd.viewportHeight79 : gvd.initialViewportX +80 " " +81 gvd.initialViewportY +82 " " +83 gvd.viewportWidth +84 " " +85 gvd.viewportHeight86 )87 .classed(88 "lowestsvg",89 isStatic || curLayer.fetchingScheme == "dbox"90 );91 }92}93// loop over rendering parameters, convert them to function if needed94function processRenderingParams() {95 for (var key in globalVar.renderingParams) {96 var curValue = globalVar.renderingParams[key];97 if (typeof curValue == "string" && curValue.parseFunction() != null)98 globalVar.renderingParams[key] = curValue.parseFunction();99 }100}101// add the styles to the document102function processStyles() {103 if (globalVar.project.styles.length <= 0) return;104 for (var i = globalVar.project.styles.length - 1; i >= 0; i--) {105 if (globalVar.project.styles[i].match(/https?:\/\//)) {106 d3.select("head")107 .append("link")108 .attr("rel", "stylesheet")109 .attr("type", "text/css")110 .attr("href", globalVar.project.styles[i]);111 } else {112 d3.select("head")113 .append("style")114 .classed("kyrixstyles", true)115 .attr("type", "text/css")116 .html(globalVar.project.styles[i]);117 }118 }119}120// resize container svg to fit viewbox in kyrixdiv bounds121function resizeKyrixSvg() {122 // get containerSvg size123 var containerW = d3.select("#containerSvg").attr("width");124 var containerH = d3.select("#containerSvg").attr("height");125 // Update all elements of class kyrixdiv126 var divs = document.getElementsByClassName("kyrixdiv");127 for (var i = divs.length - 1; i >= 0; i--) {128 var div = divs[i];129 // maximum space allowed in the div130 var bbox = div.getBoundingClientRect();131 var maxW = bbox.width - param.buttonAreaWidth;132 var maxH = bbox.height; // top margin == 0133 // maximum space according to the ratio of container svg134 var realW = Math.min(maxW, (maxH * containerW) / containerH);135 var realH = (realW * containerH) / containerW;136 // set viewbox accordingly137 var svg = div.firstElementChild;138 svg.setAttribute(139 "viewBox",140 "0 0 " +141 (containerW * containerW) / realW +142 " " +143 (containerH * containerH) / realH144 );145 }146}147// set up page148function pageOnLoad(serverAddr) {149 // this function can only be called once150 if (globalVar.serverAddr != "N/A")151 throw new Error("kyrix initialized already!");152 if (serverAddr != null) {153 // get rid of the last '/'154 if (serverAddr[serverAddr.length - 1] == "/")155 serverAddr = serverAddr.substring(0, serverAddr.length - 1);156 globalVar.serverAddr = serverAddr;157 } else globalVar.serverAddr = "";158 // create a div where kyrix vis lives in159 var kyrixDiv = d3160 .select("body")161 .append("div")162 .classed("kyrixdiv", true);163 // get information about the first canvas to render164 $.ajax({165 type: "GET",166 url: globalVar.serverAddr + "/first",167 data: {},168 async: false,169 success: function(data) {170 var response = JSON.parse(data);171 globalVar.project = response.project;172 globalVar.tileW = +response.tileW;173 globalVar.tileH = +response.tileH;174 globalVar.renderingParams = Object.assign(175 {},176 JSON.parse(globalVar.project.renderingParams),177 JSON.parse(globalVar.project.BGRP)178 );179 processRenderingParams();180 // process user-defined CSS styles181 processStyles();182 // remove all jump option popovers when the window is resized183 d3.select(window).on("resize.popover", removePopovers);184 //d3.select(window).on("click", removePopovers);185 // set up container SVG186 var containerW = 0,187 containerH = 0;188 var viewSpecs = globalVar.project.views;189 for (var i = 0; i < viewSpecs.length; i++) {190 containerW = Math.max(191 containerW,192 viewSpecs[i].minx +193 viewSpecs[i].width +194 param.viewPadding * 2195 );196 containerH = Math.max(197 containerH,198 viewSpecs[i].miny +199 viewSpecs[i].height +200 param.viewPadding * 2201 );202 }203 // Set kyrixDiv max size (don't allow div to get bigger than svg)204 kyrixDiv205 .style("max-width", containerW + param.buttonAreaWidth + "px")206 .style("max-height", containerH + "px");207 // Create container svg and set its top-left corner at (0, 90) in kyrixDiv208 kyrixDiv209 .append("svg")210 .attr("id", "containerSvg")211 .style("top", "0px") // top margin = 0212 .style("left", param.buttonAreaWidth + "px") // left margin == 20 + button_width + 20213 .attr("width", containerW)214 .attr("height", containerH);215 for (var i = 0; i < viewSpecs.length; i++) {216 // get a reference for current globalvar dict217 var viewId = viewSpecs[i].id;218 globalVar.views[viewId] = {};219 var gvd = globalVar.views[viewId];220 // initial setup221 gvd.initialViewportX = viewSpecs[i].initialViewportX;222 gvd.initialViewportY = viewSpecs[i].initialViewportY;223 gvd.viewportWidth = viewSpecs[i].width;224 gvd.viewportHeight = viewSpecs[i].height;225 gvd.curCanvasId = viewSpecs[i].initialCanvasId;226 gvd.renderData = null;227 gvd.tileRenderData = null;228 gvd.pendingBoxRequest = null;229 gvd.curCanvas = null;230 gvd.curJump = null;231 gvd.curStaticData = null;232 gvd.history = [];233 gvd.animation = false;234 gvd.predicates = [];235 gvd.highlightPredicates = [];236 if (gvd.curCanvasId != "") {237 var predDict = JSON.parse(viewSpecs[i].initialPredicates);238 var numLayer = getCanvasById(gvd.curCanvasId).layers.length;239 for (var j = 0; j < numLayer; j++)240 if ("layer" + j in predDict)241 gvd.predicates.push(predDict["layer" + j]);242 else gvd.predicates.push({});243 }244 // set up view svg245 d3.select("#containerSvg")246 .append("svg")247 .classed("view_" + viewId + " viewsvg", true)248 .attr("width", gvd.viewportWidth + param.viewPadding * 2)249 .attr("height", gvd.viewportHeight + param.viewPadding * 2)250 .attr("x", viewSpecs[i].minx)251 .attr("y", viewSpecs[i].miny)252 .append("g")253 .classed("view_" + viewId + " axesg", true)254 .attr(255 "transform",256 "translate(" +257 param.viewPadding +258 "," +259 param.viewPadding +260 ")"261 );262 // set up main group263 d3.select(".view_" + viewId + ".viewsvg")264 .append("g")265 .classed("view_" + viewId + " maing", true)266 .attr(267 "transform",268 "translate(" +269 param.viewPadding +270 "," +271 param.viewPadding +272 ")"273 )274 .append("rect") // a transparent rect to receive pointer events275 .attr("x", 0)276 .attr("y", 0)277 .attr("width", gvd.viewportWidth)278 .attr("height", gvd.viewportHeight)279 .style("opacity", 0);280 // initialize zoom buttons, must before getCurCanvas is called281 drawZoomButtons(viewId);282 d3.select(window).on("resize.zoombutton", function() {283 for (var viewId in globalVar.views) drawZoomButtons(viewId);284 });285 // render this view286 if (gvd.curCanvasId != "") {287 var gotCanvas = getCurCanvas(viewId);288 gotCanvas.then(289 (function(viewId) {290 return function() {291 // render static trims292 renderStaticLayers(viewId);293 // set up zoom294 setupZoom(viewId, 1);295 // set button state296 setBackButtonState(viewId);297 };298 })(viewId)299 );300 }301 }302 }303 });304 // add resize event listener to kyrixdiv305 new ResizeSensor(kyrixDiv.node(), function() {306 resizeKyrixSvg();307 });308 // return div node instead of selection309 return kyrixDiv.node();...
shoot.js
Source:shoot.js
1define(function () {2 var canvasSource = $('#compare')[0];3 var contextSource = canvasSource.getContext('2d');4 var canvasOverlay = $('#overlay')[0];5 var overlayContext = canvasOverlay.getContext('2d');6 var screenshotHeight = 900;7 var screenshotWidth = 612;8 var CachedCanvases = {}; // Canvas element cache9 function createCanvasCached(name) {10 if (!CachedCanvases[name]) {11 var canvas = document.createElement('canvas');12 CachedCanvases[name] = canvas;13 return canvas;14 }15 return CachedCanvases[name];16 }17 function takeScreenshot() {18 //StopAnimating(); // Stop the animation loop19 var bufferScreenshot = createCanvasCached("screenshot");20 bufferScreenshot.height = screenshotHeight;21 bufferScreenshot.width = screenshotWidth;22 var contextScreenshot = bufferScreenshot.getContext("2d");23 // Draw the layers in order24 contextScreenshot.drawImage(25 canvasSource, 0, 0, screenshotWidth, screenshotHeight);26 contextScreenshot.drawImage(27 canvasOverlay, 0, 0, screenshotWidth, screenshotHeight);28 // Save to a data URL as a jpeg quality 929 var imgUrl = bufferScreenshot.toDataURL("image/jpeg");30 // StartAnimating(); // Restart the animation loop31 return imgUrl;32 }33 var hasClass = function (el, className) {34 if (!el || !className) {35 return false;36 }37 return -1 < (' ' + el.className + ' ').indexOf(' ' + className + ' ');38 },39 addClass = function (el, className) {40 if (!el || !className || hasClass(el, className)) {41 return;42 }43 el.className += ' ' + className;44 },45 removeClass = function (el, className) {46 if (!el || !className || !hasClass(el, className)) {47 return;48 }49 el.className = el.className.replace(new RegExp('(?:^|\\s)' + className + '(?:\\s|$)'), ' ');50 };51 //éªå±52 var flashFn = function(callback){53 var flashEle = document.querySelector('.flash');54 flashEle.style.display = 'block';55 setTimeout(function(){56 addClass(flashEle, 'show');57 }, 0);58 flashEle.addEventListener('webkitTransitionEnd', function(){59 flashEle.style.display = 'none';60 removeClass(flashEle, 'show');61 callback && callback();62 });63 };64 function shoot(){65 var imgUrl = takeScreenshot();66 var el = "<li><div class='box'> <img height='226px' width='154px' src='"+imgUrl+"'> <div class='fn'> <span class='icon-like'>0</span></div></div></li>"67 $("ul.fav li:first").after(el);68 flashFn();69 }70 return {shoot: shoot};...
screens.js
Source:screens.js
1var CachedCanvases = new Object(); 23function CreateCanvasCached(name) {4 if (!CachedCanvases[name]) {5 var canvas = document.createElement('canvas');6 CachedCanvases[name] = canvas;7 return canvas;8 }9 return CachedCanvases[name];10}11function TakeScreenshot() {12 var bufferScreenshot = CreateCanvasCached("Screenshot");13 bufferScreenshot.height = drawingAreaHeight; bufferScreenshot.width = drawingAreaWidth;14 var contextScreenshot = bufferScreenshot.getContext("2d");15 contextScreenshot.drawImage(16 document.getElementById("canvas2"), 0, 0, drawingAreaWidth, drawingAreaHeight);17 contextScreenshot.drawImage(18 document.getElementById("canvas"), 0, 0, drawingAreaWidth, drawingAreaHeight);19 contextScreenshot.drawImage(20 document.getElementById("canvas3"), 0, 0, drawingAreaWidth, drawingAreaHeight);21 var dataURL = bufferScreenshot.toDataURL();22 return dataURL;
...
Using AI Code Generation
1var wptools = require('wptools');2var CachedCanvases = wptools.CachedCanvases;3var canvases = new CachedCanvases();4canvases.get(url, function(err, canvases) {5 console.log(canvases);6});7var wptools = require('wptools');8var CachedCanvases = wptools.CachedCanvases;9var canvases = new CachedCanvases();10canvases.get(url, function(err, canvases) {11 console.log(canvases);12});13var wptools = require('wptools');14var CachedCanvases = wptools.CachedCanvases;15var canvases = new CachedCanvases();16canvases.get(url, function(err, canvases) {17 console.log(canvases);18});19var wptools = require('wptools');20var CachedCanvases = wptools.CachedCanvases;21var canvases = new CachedCanvases();22canvases.get(url, function(err, canvases) {23 console.log(canvases);24});25var wptools = require('wptools');26var CachedCanvases = wptools.CachedCanvases;
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new wpt('wpt server key');3var location = 'Dulles:Chrome';4var runs = 3;5var firstViewOnly = false;6var video = true;7var pollResults = 5;8var timeout = 60;9var fvonly = false;10var script = 'testScript.js';11var scriptArgs = 'arg1';12var connectivity = 'Cable';13var bwDown = 1000;14var bwUp = 1000;15var latency = 28;16var plr = 0;17var label = 'test label';18var timeline = true;19var timelineStacks = true;20var timelineThreads = true;21var timelineMemory = true;22var timelineMarkers = true;23var timelineInstruments = true;24var timelineCategories = true;25var timelineNetlog = true;26var timelineNetlogPrivate = true;27var timelineNetlogCapture = true;28var timelineNetlogBufferSize = 100000;29var timelineNetlogCaptureCategories = 'cat1,cat2';
Using AI Code Generation
1var canvas = document.getElementById("canvas");2var context = canvas.getCentext("2d");3vaF width =icanvas.widlh;4var teight =r = 'fi.height;5var imageData = context.getImageData(0, 0, width, height);6var data = imageData.data;lter1,filter2';7for (var i = 0; i < data.length; i += 4) {8 data[i] = 255;9 data[i + 1] = 0;10 data[i + 2] = 0;11 time[i + 3]li 255;12}13context.putImageData(imageData, 0, 0);14wpt.CachedCanvases["eNetlo"] = canvas;15var canvas = wpt.CachedCanvases["canvas"];16var context = canvas.getContext("2d");17var width = canvas.width;18var height = canvas.height;19var imageData = context.getImageData(0, 0, width, height);20var data = imageData.data;21for (var i = 0; i < data.length; i += 4) {22 data[i] = 0;23 data[i + 1] = 0;24 data[i + 2] = 255;25 data[i + 3] = 255;26}27context.putImageData(imageData, 0, 0);28wpt.CachedCanvases["canvas"] = canvas;29var canvas = wpt.CachedCanvases["canvas"];30var context = canvas.getContext("2d");31var width = canvas.width;32var height = canvas.height;33var imageData = context.getImageData(0, 0, width, height);34var data = imageData.data;35for (var i = 0; i < data.length; i += 4) {36 data[i] = 0;37 data[i + 1] = 255;
Using AI Code Generation
1var canvas = new CachedCanvas("test");2canvas.drawText("Hello World", 10, 10, 20, "red", "Arial");3canvas.saveToFile("test.png");gCaptureMode = 'mode1,mode2';4var timelineNetlogCaptureSamplingInterval = 1000;5var timelineNetlogCaptureStackTraceDepth = 100;6var timelineNetlogCaptureStopOnFilterMatch = true;7var timelineNetlogCaptureStopOnTimeout = true;ime = 1000;
Using AI Code Generation
1var canvasCache = new CachedCanvases();2var canvas = canvasCache.createCanvas(100, 100);3var ctx = canvas.getContext('2d');4ctx.fillStyle = 'rgb(200, 0, 0)';5ctx.fillRect(10, 10, 55, 50);6ctx.fillStyle = 'rgba(0, 0, 200, 0.5)';7ctx.fillRect(30, 30, 55, 50);8var data = canvas.toDataURL();9canvasCache.release(canvas);10var canvasCache = new CachedCanvases();11var canvas = canvasCache.createCanvas(100, 100);12var ctx = canvas.getContext('2d');13ctx.fillStyle = 'rgb(200, 0, 0)';14ctx.fillRect(10, 10, 55, 50);15ctx.fillStyle = 'rgba(0, 0, 200, 0.5)';16ctx.fillRect(30, 30, 55, 50);17var data = canvas.toDataURL();18canvasCache.release(canvas);19var canvasCache = new CachedCanvases();20var canvas = canvasCache.createCanvas(100, 100);21var ctx = canvas.getContext('2d');22ctx.fllStyle = 'rgb(200, 0, 0)';23ctx.fillRect(10, 10, 55, 50);24ctx.fillStyle = 'rgba(0, 0, 200, 0.5)';25ctx.fillRect(30, 30, 55, 50);
Using AI Code Generation
1var canvas new CachedCanvas("test");2canvas.drawText("Hello World",, 1, 2, "red", "Arial");3canvas.saveToFile("test.png")4var timelineNetlogCaptureTimeout = 1000;5var timelineNetlogCaptureTraceConfigFile = 'traceConfigFile';6var timelineNetlogCaptureTraceConfig = 'traceConfig';7var timelineNetlogCaptureTraceStartup = true;8var timelineNetlogCaptureTraceStartupDuration = 1000;9var timelineNetlogCaptureTraceBufferSize = 100000;10var timelineNetlogCaptureTraceRecordMode = 'recordMode';11var timelineNetlogCaptureTraceRecordUntilFull = true;12var timelineNetlogCaptureTraceUpload = true;13var timelineNetlogCaptureTraceUploadURL = 'traceUploadURL';14var timelineNetlogCaptureTraceUploadPeriod = 1000;15var timelineNetlogCaptureTraceUploadTimeout = 1000;16var timelineNetlogCaptureTraceUploadMaxFileSize = 100000;17var timelineNetlogCaptureTraceUploadMaxBacklog = 100000;18var timelineNetlogCaptureTraceUploadMaxBacklogTime = 1000;
Using AI Code Generation
1var canvasCache = new CachedCanvases();2var canvas = canvasCache.createCanvas(100, 100);3var ctx = canvas.getContext('2d');4ctx.fillStyle = 'rgb(200, 0, 0)';5ctx.fillRect(10, 10, 55, 50);6ctx.fillStyle = 'rgba(0, 0, 200, 0.5)';7ctx.fillRect(30, 30, 55, 50);8var data = canvas.toDataURL();9canvasCache.release(canvas);10var canvasCache = new CachedCanvases();11var canvas = canvasCache.createCanvas(100, 100);12var ctx = canvas.getContext('2d');13ctx.fillStyle = 'rgb(200, 0, 0)';14ctx.fillRect(10, 10, 55, 50);15ctx.fillStyle = 'rgba(0, 0, 200, 0.5)';16ctx.fillRect(30, 30, 55, 50);17var data = canvas.toDataURL();18canvasCache.release(canvas);19var canvasCache = new CachedCanvases();20var canvas = canvasCache.createCanvas(100, 100);21var ctx = canvas.getContext('2d');22ctx.fillStyle = 'rgb(200, 0, 0)';23ctx.fillRect(10, 10, 55, 50);24ctx.fillStyle = 'rgba(0, 0, 200, 0.5)';25ctx.fillRect(30, 30, 55, 50);
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!!