由于工作中的安全限制,我不允许安装 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/