npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dof

v2.1.2

Published

Depth-of-field calculator for comparing camera lenses

Downloads

103

Readme

Depth of Field Calculator

A JavaScript tool for calculating the depth of field of camera lenses

Node module

The general idea is to create a lens object, with specified aperture, focal length, and crop factor values; and then retrieving the object's depth of field object for a given distance to the subject.

Install

yarn add dof

# or

npm install dof

Create a lens object

import { Lens } from 'dof'

const lens = new Lens()

That will give us a lens object with default values:

lens.focalLength // 35 (millimeters)
lens.aperture    // 'f/2'
lens.cropFactor  // 1 (i.e. standard full frame)

Let's change those to fit the particular lens we have in mind:

const lens = new Lens(35, 'f/2.5', 1.62);

Reusing default values

You can create function which generates lenses in bulk using your own set of defauls. For example, you might want to calculate the depth of field for many lenses with a common sensor crop factor of 1.62:

const lensMaker = createLensMaker({ cropFactor: 1.62 })

const lens1 = lensMaker()
const lens2 = lensMaker({ aperture: 'f/3.6' })

The complete list of configurable defaults:

focalLength: 35  // Number, in millimeters; this must be the actual focal length, not the 35mm equivalent value
aperture: 'f/2'  // String in the format `"f/2.5"`
cropFactor: 1    // Floating point number; sensor's crop factor compared to full frame

Managing multiple lenses

Lenses can be assigned arbitrary IDs to make it easier to keep track of them:

const lens = new Lens(35, 'f/2.5', 1.62, 'my-lens-ID');// Optional; string

console.log(lens.id) // 'my-lens-ID'

Calculate the depth of field

To perform a calculation you must specify the distance between the camera and the subject. You can either set a default or pass a particular distance. The value must be a number as measured in meters (default) or feet.

// Specify distance directly
const result1 = lens.dof(5)    // 5 mmeters, the default value
const result2 = lens.dof(22.6) // 22 meters and 60 centimeters

// Imperial units
const result2 = lens.dof(15, true) // 15 feet, the default value

The result object contains several properties:

result.dof       // Total length of the depth of field; float (e.g. `20.5`), meters or feet
result.eighthDof // One-eighth of the depth of field; float (e.g. `2.5625`), meters or feet
result.hf        // Hyperfocal distance; float, meters or feet
result.near      // Distance to the nearest edge of the in-focus field; float, meters or feet
result.far       // Distance to the farthest edge of the in-focus field; float, meters or feet
result.coc       // Circle of confusion; float, always millimeters

Note that the value of those properties may be Infinity. This is especially common with small sensor sizes (large crop factors), short focal lengths, and/or small apertures (large f/ values).

You can use the .toString() method to get the depth of field value as a string. For example, 20' 5.2" is 20 feet, 5.2 inches.

const str = result.toString()

All properties have an equivalent string representation, accessible through the .toString property:

result.toString.dof        // Same as `result.toString()`
result.toString.eighthDof
result.toString.hf
result.toString.near
result.toString.far

A shorthand way to quickly acquire the depth of field value:

const num = lens.dof().dof        // float
const str = lens.dof().toString() // string

Or with a specific distance value:

const num = lens.dof(15).dof        // float
const str = lens.dof(15).toString() // string

TypeScript support

Lens() instances use the DepthOfFieldLens type which can be imported from the module.

GUI Web App

Calculate the depth of field for multiple lenses and compare them side-by-side

Try it at patik.com/dof

Documentation

Screenshot of two lens configurations