Hacker Newsnew | past | comments | ask | show | jobs | submit | kasbah's commentslogin

Is it not possible to use `window.devicePixelRatio` instead of calibration?

https://developer.mozilla.org/en-US/docs/Web/API/Window/devi...


Not really. All major browsers just use the value the OS gives them, which is usually rounded to 0.5 or 0.25, which helps keep integer CSS px values an integer number of device pixels. So you could be off as much as 12% if you are on a device that rounded down from 1.12 to 1.0.

Also, even if they didn't, there's no standard for what the correct DPI should be for a device; it theoretically should depend on viewing distance, but it's impractical to constantly change the screen DPI depending on how far away the user's eyes are :)

OP could, however, use a better default than 96 DPI for mobile devices. Most are targeting ~160-ish.


> All major browsers round this to 0.5 or 0.25, which helps keep integer CSS px values an integer number of device pixels.

This is completely false. No browser that I know of does any such thing, nor would it make any sense to do so (nor would it achieve the goal you specify to any meaningful extent).

The closest thing that does happen is that browsers use integer fractions of pixels as their basic layout unit: Firefox and its kin sixtieths, Chrome and its kin sixty-fourths.

But the rest of your answer is correct; and to add a proper citation: “the reference pixel is the visual angle of one pixel on a device with a device pixel density of 96dpi and a distance from the reader of an arm’s length” <https://drafts.csswg.org/css-values-4/#reference-pixel>.


Maybe it's better to say that browsers just take what the OS tells them, rather than actually deriving a device pixel ratio from first principles according to the CSS spec. Because, yeah, there's some weird devices with DPRs like 2.625, though _most_ are multiples of 0.25: https://yesviz.com/viewport/. But note how the same DPR can give a varying CSS PPI, which makes using it useless for this purpose.


“Taking whatever the OS tells them” is rather different!

It also gets multiplied by the browser’s zoom, which in both Firefox and Chrome include values like 80%, 90% and 110%.

So for me on HN at 120% on my 1.5× laptop display, devicePixelRatio is… 1.8181818181818181. Huh. Wonder why it’s not 1.8. ¯\_(ツ)_/¯


1.8 probably would produce a non-integer number of CSS pixels. The browser needs (wants?) to pick a number that divides both the width and height without remainders. For 1920x1080, 1.8 doesn't (works for the height, but not the width) but 1.8̅1̅ does.


Although that has a plausible sound, I don’t think it’s it: the window size is what matters, not the screen size, and you can’t guarantee any sort of divisibility for either anyway. For example, my screen is actually 2560×1440, which is 1706⅔×960 in CSS pixels given the real devicePixelRatio of 1.5. The established rule when you need an integer is, at least on Wayland, to round things down to the nearest integer; I’m not certain about other platforms. Certainly everything that deals in integer pixels sees 1706×960.

Chromium doesn’t exhibit this behaviour; it’s just Firefox on some of its zoom levels. And when I saw 90% being 0.9090909090909091 (90⁄99 instead of 90⁄100) it triggered a memory of observing this five or seven years ago on my Surface Book (3000×2000 @ 2×). I think it is just that they’ve chosen to display different, slightly inaccurate percentage labels.


It's pretty straight forward to include the output of `wasm-pack` into a vite project. The output is a node module in a folder with the wasm files and a single "main" JS file.

Because I wanted to load WASM in a web worker for my project [1] I needed to use vite-plugin-wasm and `wasm-pack build --target web` but without that constraint you should be able to import the main JS file from the wasm-pack output directory using wasm-pack's default `bundler` target and no vite plugins.

[1]: https://github.com/kasbah/calm-go-pattern-search


To me the interesting thing is the logic programming "rules" and their overlap with game rules. Inspired by work at Stanford on the logic programming based Game Description Language [1] I implemented Tic Tac Toe in Datascript yesterday: https://github.com/kasbah/datascript-games/blob/e06a37025bf9...

I am still not clear whether there isn't a more succinct rule definition than what I have there. In the Stanford paper you have rules like:

   (<= (column ?n ?x)
     (true (cell 1 ?n ?x))
     (true (cell 2 ?n ?x))
     (true (cell 3 ?n ?x)))
But in Datascript I have to do much more rigmarole around shuffling the data around:

  [(column ?n ?x) 
    [?current "ident" "current"] 
    [?coord0 "type" "coord"] 
    [?coord1 "type" "coord"] 
    [?coord2 "type" "coord"]
    [?coord0 "m" 0] 
    [?coord1 "m" 1] 
    [?coord2 "m" 2] 
    [?coord0 "n" ?n] 
    [?coord1 "n" ?n] 
    [?coord2 "n" ?n] 
    [?coord0 "name" ?key0] [?current ?key0 ?x]
    [?coord1 "name" ?key1] [?current ?key1 ?x]
    [?coord2 "name" ?key2] [?current ?key2 ?x]]
I don't know if this is down to Datascript/Datomic Datalog limitations or more the limitations of my understanding.

How do you approach your experiments? If you have any of your work to share or some tips on what I am doing I'd be very interested.

[1]: https://www.cs.uic.edu/~hinrichs/papers/love2006general.pdf


This is a consequence of the Datomic information model which is focused on handling a single universe of triples (to facilitate natural schema growth) instead of independent n-ary relations. However unlike when building serious business applications, for most simple games you probably won't ever care about the ease of handling the schema growth of persisted data. You would likely care more once you think about supporting long-lived multiplayer environments or introducing played-defined concepts within the game.


Interesting, thanks. I am especially interested in the idea of introducing player-defined concepts.

Would you be able to recommend a Datalog implementation that allows independant n-ary relations. Ideally one I can use from Python or Javascript in a sort of sandboxed way, as I am doing with Datascript, but if you have any recommendation at all it would be helpful to me.


DataScript already supports processing n-ary relations, it's just not how the data is naturally stored when you use `d/transact!`. Even though it's all in-memory anyway (ignoring the recent addition of durable storage on the JVM) the main benefit you get when 'storing' data is the suite of persistent B-Tree EAVT indexes. DataScript also let's you store plain vectors (and most other objects) as values, which you can access from the Datalog, so it's very flexible really. And learning Clojure is good fun.

If you want to try something more exotic I would be tempted by https://logica.dev/ + some flavour of SQLite (potentially in-memory/WASM).


"Reticulating splines"



Use Lobste.rs with a filter: https://lobste.rs/filters


Shameless plug: the Kitspace page helps you build one yourself.

https://kitspace.org/boards/github.com/ottercast/ottercastau...


I was going to post this! I really enjoyed this episode (and also, all the other episodes of your podcast).


I find this DIY yellow drum machine robot from 14 years ago much more impressive.

https://www.youtube.com/watch?v=_RyodnisVvU


thank you i completely forgot about this cutest of all robots!


In all seriousness Python's json.tool is a great option here.

    cat big.json | python3 -m json.tool
or

    python3 -m json.tool big.json


There's also json_pp(1), provided by Perl.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: