在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
最满意答案
您的代码不完整。
你接缝就像这样调用你的函数:
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 ...
-
从python中的连续列表中识别连续数字组(Identify groups of continuous numbers from consecutive list in python)[2021-06-16]
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 ... -
列表上的Python连续操作以错误的顺序执行(Python consecutive operations on list getting executed in wrong order)[2022-10-25]
您的代码不完整。 你接缝就像这样调用你的函数: 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 ...
-
服务代码执行顺序错误(service code executed in wrong order)[2023-05-18]
好的,我发现了问题所在: 当我正在测试我的服务时,我将参数(对象)传递给第一次服务调用,然后传递给服务的第二次调用(但是第一次调用时对象被更改了) - 我忘了该对象是通过引用传递的:) 我发现我无法在服务调用中更改消息(用于记录目的) - 我通过消息检查器(方法AfterReceiveRequest)记录消息,该消息检查器早于服务执行调用。 Ok, i found out what the problem is: when i'm testing my service i'm passing paramet ... -
代码以错误的顺序执行(Code is executed in the wrong order)[2022-05-01]
ajax回调函数中的return语句是无用的。 该函数是异步调用的,您不能指望任何东西将返回的值传递给任何东西。 如果将console.log()调用放在 “success”回调中,您将看到一个值。 您可能需要更改“GetProducer”函数,以便它也需要一个回调参数: GetProducer(producerId, function(info) { ... }); 然后,函数或多或少看起来像这样: function GetProducer(producerId, callback) { ...