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

vw-scale-calculator

v2.0.5

Published

Calculate scale based on viewport width and reference points

Downloads

28

Readme

vw-scale-calculator

vw-scale-calculator is a utility package to calculate a scale factor based on the viewport width, with customizable reference points. This is especially useful for responsive web design where elements need to scale based on the user's screen size.

Table of Contents

Installation

You can install vw-scale-calculator using npm:

npm install vw-scale-calculator

Or with yarn:

yarn add vw-scale-calculator

Usage

First, import the VWScaleCalculator class and create an instance with your desired reference points.

Importing and Initializing

import { VWScaleCalculator, ScaleCalculatorOptions } from 'vw-scale-calculator';

// Define reference points and options
const options: ScaleCalculatorOptions = {
  referencePoints: [
    { width: 600, scale: 0.5 },
    { width: 1200, scale: 1.0 },
    { width: 1800, scale: 1.5 }
  ],
  minScale: 0.5,
  maxScale: 1.5
};

// Create an instance of VWScaleCalculator
const calculator = new VWScaleCalculator(options);

// Calculate the scale based on the current viewport width
const scale = calculator.calculateScale(window.innerWidth);
console.log(`Current scale: ${scale}`);

// Set up resize handling
const cleanupResizeListener = calculator.onResize((scale) => {
  console.log(`Updated scale on resize: ${scale}`);
});

// Call `cleanupResizeListener` to remove the event listener when no longer needed

API

VWScaleCalculator

Constructor

new VWScaleCalculator(referencePoints: ReferencePoint[]);
  • referencePoints: An array of ReferencePoint objects that define the widths and corresponding scale factors.

Methods

  • calculateScale(viewportWidth: number): number

    Calculates the scale factor based on the provided viewport width.

    • viewportWidth: The current viewport width to calculate the scale for.

    Returns the calculated scale factor as a number.

  • setReferencePoints(referencePoints: ReferencePoint[]): void

    Updates the reference points used for calculating the scale.

    • referencePoints: An array of new ReferencePoint objects.

ReferencePoint

A TypeScript type defining the structure for a reference point:

type ReferencePoint = {
  width: number; // The viewport width at which this scale should apply.
  scale: number; // The scale factor to use at the specified width.
};

Examples

Example: Dynamically Adjusting Element Scale

If you want to dynamically adjust an element's scale based on the window width, you can use the following code:

import { VWScaleCalculator, ReferencePoint } from 'vw-scale-calculator';

const referencePoints: ReferencePoint[] = [
  { width: 1263, scale: 0.6 },
  { width: 1519, scale: 0.66 },
  { width: 1903, scale: 0.72 },
];

const calculator = new VWScaleCalculator(referencePoints);

const updateElementScale = () => {
  const scale = calculator.calculateScale(window.innerWidth);
  document.getElementById('scalable-element').style.transform = `scale(${scale})`;
};

window.addEventListener('resize', updateElementScale);
updateElementScale(); // Initialize on page load

Development

Prerequisites

Building the Project

To build the project, run:

npm run build

This will compile the TypeScript code into JavaScript and output it into the dist folder.

Running Tests

If you have set up tests, you can run them using:

npm test

Contributing

Contributions are welcome! Please fork the Github repository and submit a pull request for any features, bug fixes, or enhancements. Make sure to follow the code style and add relevant tests.

License

This project is licensed under the MIT License. See the LICENSE file for details.


### How to Use the README

- Copy the content above into a new `README.md` file in the root of your project.
- Adjust any sections as necessary to fit your project specifics, especially under **Development** and **Contributing** if you have additional guidelines or tools.
- Make sure to update any placeholder text (e.g., "Your Name") with your actual details.

This README provides a thorough overview of your package, including installation, usage, and contribution guidelines, which will help users understand and utilize your package effectively.