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

scale-to-window-pixi

v1.2.0

Published

npm version of scale-to-window-pixi by kittykatattack at https://github.com/kittykatattack/scaleToWindow

Downloads

19

Readme

Scale and align an HTML element in the browser

This initially started off being the npm downloadable fork of scale-to-window-pixi by kittykatattack at https://github.com/kittykatattack/scaleToWindow. However for my purposes I have modified it so that it doesn't only apply to a canvas element but a container element with centered children inside of it. Use the function scaleToWindow to scale an HTML element to the maximum size of the browser's window. scaleToWindow will also align the element for the best vertical or horizontal fit inside the browser window. For example, if you have an element that's wider than it is tall, it will be centered vertically inside the browser. If the element is taller than it is wide, it will be centered horizontally.

Here's how to use scaleToWindow:

To install, you can use npm

npm install --save scale-to-window-pixi

New way of usage

import scaleToWindow from 'scale-to-window-pixi';
// in case you need ssr, it's good to wrap your window objects in some method
const getWindow = () => window;
const getDocument = () => document;
scaleToWindow(eleDict, getWindow, getDocument, backgroundColor);

scaleToWindow accepts 4 arguments:

eleDict: this is an object of shape { containerSel: '.app', childSelArr: ['.childOne', '.childTwo']}

scaleToWindow will find the element that matches the selector matched in containerSel, and apply the resize and center based on window size. All selectors in childSelArr

getWindow: this is a function that returns the global window object

getDocument: this is a function that returns the global document object

backgroundColor: (optional) this is the color code of the background color of the surrounding element of everything outside of your view

The scaleToWindow function also returns the scale value that the element is scaled to. You can find it like this:

scaleToWindow(...) will give you a number, like 1.98046875, which tells you the ratio by which the element was scaled. This might be an important value to know if you need to convert browser pixel coordinates to the scaled pixel values of the element. For example, if you have a pointer object which tracks the mouse's position in the browser, you might need to convert those pixel positions to the scaled element coordinates to find out if the mouse is touching something in the element. Some general code like this will do the trick:

pointer.x = pointer.x / scale;
pointer.y = pointer.y / scale;

Optionally, you might also want the element to rescale itself every time the size of the browser window is changed. If that’s the case, call scaleToWindow inside a window event listener:

window.addEventListener("resize", function(event){ 
  scaleToWindow(eleDict, getWindow, getDocument, backgroundColor);
});

For the best effect, make sure that you set the browser's default margins and padding to 0 on all HTML elements. If you don't do this, most browsers will add some padding around the element borders. This bit of CSS will do the trick:

<style>* {padding: 0; margin: 0}</style>

If you prefer, you can add this CSS style using JavaScript in your main program like this:

const newStyle = document.createElement("style");
const style = "* {padding: 0; margin: 0}";
newStyle.appendChild(document.createTextNode(style));
document.head.appendChild(newStyle);

Originial Methods

The older methods written by kittykatattack remain. To use, they are included in scale-to-window-pixi/scale-to-canvas-window For actual usage instructions, the old readme still exists under scaleCanvasToWindow/