首页 \ 问答 \ 在JavaScript中如何将像素转换为英寸?(How do you convert pixels to printed inches in JavaScript?)

在JavaScript中如何将像素转换为英寸?(How do you convert pixels to printed inches in JavaScript?)

我想调整SPAN元素的样式的字体,直到SPAN的文本在纸上打印出来时为7.5英寸宽,但JavaScript只报告SPAN的clientWidth属性(以像素为单位)。

<span id="test">123456</span>

接着:

#test {
  font-size:1.2in; /* adjust this for yourself until printout measures 7.5in wide */
}

接着:

console.log(document.getElementById('test').clientWidth);

我已经在一台机器上通过实验确定它使用大约90 DPI作为转换因子,因为上面的代码约为675,至少在Firefox 3下。

在不同的浏览器,打印机,屏幕等配置下,这个数字不一定相同。

那么,我如何找到浏览器正在使用的DPI呢? 我能在我的系统上拨打什么来取回“90”?


I want to resize the font of a SPAN element's style until it the SPAN's text is 7.5 inches wide when printed out on paper, but JavaScript only reports the SPAN's clientWidth property in pixels.

<span id="test">123456</span>

And then:

#test {
  font-size:1.2in; /* adjust this for yourself until printout measures 7.5in wide */
}

And then:

console.log(document.getElementById('test').clientWidth);

I've determined experimentally on one machine that it uses approximately 90 DPI as a conversion factor, because the above code logs approximately 675, at least under Firefox 3.

This number is not necessarily the same under different browser, printer, screen, etc. configurations.

So, how do I find the DPI the browser is using? What can I call to get back "90" on my system?


原文:https://stackoverflow.com/questions/159183
更新时间:2024-02-06 06:02

最满意答案

您的代码不完整。

你接缝就像这样调用你的函数:

c = [[3], [1], [4], [5], [7], [0], [2], [6]]
p = [[0, 1, 2, 4, 5, 6, 7],
     [0, 2, 3, 4, 5, 6, 7],
     [0, 1, 2, 3, 6, 7, 4],
     [0, 1, 2, 3, 6, 5, 5],
     [0, 1, 2, 3, 4, 6, 7],
     [1, 2, 3, 4, 6, 7, 5],
     [0, 1, 3, 4, 5, 6, 7],
     [0, 1, 2, 3, 4, 5, 7]]

for i in range(1000):
    c_main, p_main = temp(c, p)

注意:如果修复了你的代码:添加range并为p中的每一行添加逗号。

但是在你的temp()函数中,你正在修改p的内容。 所以,你可能没有你期望的。 因为在每次迭代时都重复使用相同的p 。 所以有时它会变得不一致。

你想要什么,当然是这样的:

import random

def temp(c):
    # -- raw matrix
    p = [[col for col in range(8)] for row in range(len(c))]

    # -- drop a number
    for p_row, c_row in zip(p, c):
        p_row.pop(c_row[0])

    # -- shuffle
    for row in p:
        random.shuffle(row)

    return p

你可以像这样使用它:

cols = [[3], [1], [4], [5], [7], [0], [2], [6]]
print(temp(cols))

你得到:

[[6, 1, 4, 5, 7, 2, 0],
 [3, 0, 5, 4, 2, 7, 6],
 [2, 7, 0, 3, 6, 5, 1],
 [1, 2, 7, 4, 6, 3, 0],
 [3, 1, 4, 6, 2, 0, 5],
 [4, 5, 6, 3, 7, 1, 2],
 [0, 6, 1, 5, 7, 3, 4],
 [4, 3, 7, 0, 1, 5, 2]]

Your code is not complete.

You seam to call your function like this:

c = [[3], [1], [4], [5], [7], [0], [2], [6]]
p = [[0, 1, 2, 4, 5, 6, 7],
     [0, 2, 3, 4, 5, 6, 7],
     [0, 1, 2, 3, 6, 7, 4],
     [0, 1, 2, 3, 6, 5, 5],
     [0, 1, 2, 3, 4, 6, 7],
     [1, 2, 3, 4, 6, 7, 5],
     [0, 1, 3, 4, 5, 6, 7],
     [0, 1, 2, 3, 4, 5, 7]]

for i in range(1000):
    c_main, p_main = temp(c, p)

note: if fixed your code: add range and add comma for each lines in p.

But inside your temp() function, your are modifying the content of p. So, you may not have what you expect. Because you reuse the same p at each iteration. So sometimes it becomes inconsistent.

What you want, is certainly something like that:

import random

def temp(c):
    # -- raw matrix
    p = [[col for col in range(8)] for row in range(len(c))]

    # -- drop a number
    for p_row, c_row in zip(p, c):
        p_row.pop(c_row[0])

    # -- shuffle
    for row in p:
        random.shuffle(row)

    return p

You can use it like this:

cols = [[3], [1], [4], [5], [7], [0], [2], [6]]
print(temp(cols))

You get:

[[6, 1, 4, 5, 7, 2, 0],
 [3, 0, 5, 4, 2, 7, 6],
 [2, 7, 0, 3, 6, 5, 1],
 [1, 2, 7, 4, 6, 3, 0],
 [3, 1, 4, 6, 2, 0, 5],
 [4, 5, 6, 3, 7, 1, 2],
 [0, 6, 1, 5, 7, 3, 4],
 [4, 3, 7, 0, 1, 5, 2]]

相关问答

更多
  • 我假设输入列表具有不同的元素。 import random def randomize_carefully(elems, n_repeat=2): s = set(elems) res = [] for n in range(n_repeat): if res: # Avoid the last placed element lst = list(s.difference({res[-1]})) ...
  • 显然,我可以调用entityManager.flush() 实际上你必须打电话给它。 但是,在这种情况下,数据会在整个事务提交之前保存到数据库中。 这是错误的:数据被同步到数据库,但事务仍未提交,除非您手动提交并控制数据库。 如果您没有在EJB中配置任何内容,并且您的持久性单元是JTA(请参阅带有注释的此问题 ),那么只有在方法从EJB层返回后才会提交事务。 我认为操作顺序与添加到事务中的操作顺序相同。 从我的例子可以看出事实并非如此。 不,JPA规范不强制实现这样做。 这就是为什么有一个flush()操作 ...
  • 操作不一定按顺序执行。 这是一个证明这一点的测试: import tensorflow as tf sess = tf.InteractiveSession() a = tf.Variable(1.0) b = tf.Variable(10.0) c = tf.Variable(0.0) grp = tf.group(tf.assign(c, a), tf.assign(c, b)) # this is the group op for i in range(100): sess.run(tf.gl ...
  • 它似乎是Chrome devtools控制台的一个bug功能。 例如,如果您运行此代码段,您将获得 { "b": 1 } 但如果同时打开Chrome控制台,您将获得以下内容: {b: 1} a:1 b:1 正如您所看到的,第一行反映了记录时的obj状态,但是当您单击它时,它的内容似乎会更新。 在记录对象的右侧还有一个蓝色信息工具提示,上面写着:“刚刚评估下面的值”,这确认它是一个功能,而不是一个bug。 function fn(obj){ console.log(obj); ...
  • 睡眠发生在正确的地方,问题是你有第四步失踪。 真正发生的是 label1.Text = "Start"; Thread.Sleep(2000); label1.Text = "Finish"; DrawUpdatedValuesOfLabel1OnTheUI(); 直到执行返回到“消息循环”,用户界面才会更新,您需要将控制返回到消息循环,同时等待两秒钟,以便获得更新的用户界面。 如果你使用的是.NET 4.5,最简单的方法是将你的Sleep时间改变为带有异步/等待的Dela ...
  • L1 = [5,3,2,7,1] L2 = [3,5,6,8,9,21,2] L3 = [5,3,6,7,3,9] cons_l = [] L = [L2] + [L3] #+[L4] #+ ...+ ..... ### Add any number of list here.. j = 0 for l1 in L1: cons_l.append([]) cons_l[j].append(l1) for l in range(0, len(L)): if l1+l+1 in ...
  • 您的代码不完整。 你接缝就像这样调用你的函数: c = [[3], [1], [4], [5], [7], [0], [2], [6]] p = [[0, 1, 2, 4, 5, 6, 7], [0, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 6, 7, 4], [0, 1, 2, 3, 6, 5, 5], [0, 1, 2, 3, 4, 6, 7], [1, 2, 3, 4, 6, 7, 5], [0, 1, 3, 4, 5 ...
  • 承诺是异步的。 因此,一个接一个地开始多个承诺并不意味着它们被执行/它们按顺序完成。 如果我的理解是正确的,你的代码基本上做了以下(psuedo-js-code): var chain1 = [c1promise1, c2promise2]; Promise.all(chain1).then( // wait for execution var chain2 = [c2promise1, c2promise2]; ); Promise.all(chain2).then( // at this poi ...
  • 好的,我发现了问题所在: 当我正在测试我的服务时,我将参数(对象)传递给第一次服务调用,然后传递给服务的第二次调用(但是第一次调用时对象被更改了) - 我忘了该对象是通过引用传递的:) 我发现我无法在服务调用中更改消息(用于记录目的) - 我通过消息检查器(方法AfterReceiveRequest)记录消息,该消息检查器早于服务执行调用。 Ok, i found out what the problem is: when i'm testing my service i'm passing paramet ...
  • ajax回调函数中的return语句是无用的。 该函数是异步调用的,您不能指望任何东西将返回的值传递给任何东西。 如果将console.log()调用放在 “success”回调中,您将看到一个值。 您可能需要更改“GetProducer”函数,以便它也需要一个回调参数: GetProducer(producerId, function(info) { ... }); 然后,函数或多或少看起来像这样: function GetProducer(producerId, callback) { ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)