google-chrome - 如何在没有扩展程序的情况下在 Chrome 中测量像素?

标签 google-chrome google-chrome-devtools pixel measurement

由于工作中的安全限制,我不允许安装 Chrome 扩展程序。 Chrome 在开发人员工具中内置了一个标尺,但我不知道如何像标尺允许的那样定义起点和终点。

是否有任何不需要安装 Chrome 扩展程序的测量像素的工具或技术?

最佳答案

您可以创建自己的标尺功能并将其粘贴到控制台中。这是一个基本示例:

var fromX, fromY;
var svg = document.createElementNS ('http://www.w3.org/2000/svg',"svg");
svg.setAttribute("style", "position: absolute; top:0;left:0;height: " + document.body.clientHeight + "px;width: 100%");
var line = document.createElementNS('http://www.w3.org/2000/svg','line');
line.setAttribute("style", "stroke-width: 4; stroke: red");

svg.appendChild(line);
document.body.appendChild(svg);

document.body.addEventListener("mousedown", function (e) {
  fromX = e.pageX;
  fromY = e.pageY;
});

document.body.addEventListener("mousemove", function (e) {
  if (fromX === undefined) {
    return;
  }

  line.setAttribute("x1", fromX);
  line.setAttribute("x2", e.pageX);
  line.setAttribute("y1", fromY);
  line.setAttribute("y2", e.pageY);

  console.log(
    [fromX, fromY], " to ", [e.pageX, e.pageY], "Distance:",
    Math.sqrt(Math.pow(fromX - e.pageX, 2) + Math.pow(fromY - e.pageY, 2))
  );
});

document.body.addEventListener("mouseup", function (e) {
  fromX = undefined;
  fromY = undefined;
});

您也可以将其另存为 snippet .

Chrome 扩展程序代码也只是 JavaScript,因此您可以 find the code used by the extension并将其保存为片段。这里的缺点是代码可能被压缩,并且依赖于浏览器中通常不可用的功能。

关于google-chrome - 如何在没有扩展程序的情况下在 Chrome 中测量像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37954379/

相关文章:

javascript - jQuery 延迟 AJAX 调用返回值

javascript - 如何在控制台停靠在底部而不是右侧的情况下在 Chrome 中启动测试?

google-chrome - 有没有办法在没有按钮的情况下在 Chrome 浏览器中提交?

java通过索引+1和-1迭代图像错误模型

CSS - 另一个 %/px 问题

css - Chrome 中的 Web 字体渲染

html - 背景附件 :fixed; 在 chrome 上出现奇怪的背景渲染

javascript - 从 DevTools 控制台使用 chrome.tabs.captureVisibleTab()

html - 多次应用 CSS 样式

javascript - 如果设置 double 值明显会导致不同的渲染,为什么 jQuery 只返回整数高度?