element-scale
v1.0.0
Published
A lightweight element scaling library that automatically adjusts element size based on device pixel ratio
Maintainers
Readme
element-scale
A lightweight element scaling library that automatically adjusts element size based on device pixel ratio, designed for high-DPI displays and responsive layouts.
Features
- 🚀 Automatic scaling based on
window.devicePixelRatio - 🎯 Supports both CSS
transform: scale()andzoomproperties - 📐 Built-in ResizeObserver for responsive scaling
- 🖼️ Automatic canvas handling with inverse scaling
- 📦 Lightweight with zero dependencies
- 🔒 TypeScript support with full type definitions
Installation
npm install element-scaleor
pnpm add element-scaleor
yarn add element-scaleUsage
Basic Usage
import { startScale, ElementScale } from 'element-scale';
const container = document.getElementById('container') as HTMLElement;
const scaleInstance = startScale({
elements: ['.box', '#header', document.querySelector('.footer') as HTMLElement],
container: container
});With Options
import { startScale } from 'element-scale';
const container = document.getElementById('container') as HTMLElement;
const scaleInstance = startScale({
elements: ['.box', '#header'],
container: container,
useZoom: true,
minScale: 0.5,
maxScale: 1,
targetScale: 1,
callback: (scale, elements) => {
console.log(`Current scale: ${scale}`);
}
});Class API Usage
import { ElementScale } from 'element-scale';
const container = document.getElementById('container') as HTMLElement;
const scaleManager = new ElementScale({
elements: ['.box', '#header'],
container: container,
useZoom: false,
minScale: 0.6,
maxScale: 1,
targetScale: 1,
callback: (scale, elements) => {
console.log('Scale updated:', scale);
}
});
// Later, to destroy the instance
scaleManager.destroy();Destroy Scale
import { startScale } from 'element-scale';
const container = document.getElementById('container') as HTMLElement;
const scaleInstance = startScale({
elements: ['.box'],
container: container
});
// Destroy after 5 seconds
setTimeout(() => {
scaleInstance.destroy();
}, 5000);API
startScale(options)
Creates and starts an ElementScale instance.
startScale(options: ElementScaleOptions): ElementScalenew ElementScale(options)
Class constructor for more control over the scaling instance.
constructor(options: ElementScaleOptions)ElementScaleOptions
| Property | Type | Default | Description |
|----------|------|---------|-------------|
| elements | ScaleElement[] | Required | Array of elements to scale (CSS selectors or HTMLElement) |
| container | HTMLElement | Required | Container element to observe for resize events |
| useZoom | boolean | true | Use CSS zoom instead of transform: scale() |
| minScale | number | 0.6 | Minimum scale factor |
| maxScale | number | 1 | Maximum scale factor |
| targetScale | number | 1 | Target scale factor for calculation |
| callback | (scale: number, elements: ScaleItem[]) => void | undefined | Callback function called when scale changes |
ScaleElement
type ScaleElement = string | HTMLElement;string: CSS selector (e.g.,.box,#header)HTMLElement: Direct DOM element reference
ScaleItem
interface ScaleItem {
el: HTMLElement;
reset: () => void;
update: (scale: number) => void;
}Methods
runScale()
Manually trigger scale recalculation.
scaleInstance.runScale(): voiddestroy()
Stop scaling, clean up observers, and reset all elements.
scaleInstance.destroy(): voidProperties
isInitialized
Check if the instance is initialized.
scaleInstance.isInitialized: booleanHow It Works
- Device Detection: Detects the operating system (Mac or Windows)
- DPR Calculation: Gets
window.devicePixelRatiowith fallback for older browsers - Scale Calculation: Computes scale factor using formula:
min(maxScale, max(targetScale / dpr, minScale)) - Auto-observation: Uses ResizeObserver to monitor container size changes
- Canvas Handling: Automatically applies inverse scaling to canvas elements within the container
Scale Rules
- Scale ≥ 1: No scaling applied, all styles are reset
- Scale < 1: Elements are scaled down using either
transform: scale()orzoombased on theuseZoomoption - Canvas elements: Inversely scaled (multiplied by 1/scale) to maintain clarity at high DPI
Browser Support
- Chrome 60+
- Firefox 55+
- Safari 13.1+
- Edge 79+
Requires ResizeObserver support. For older browsers, consider using a polyfill.
Type Definitions
Full TypeScript type definitions are included and exported:
import type {
ScaleElement,
ScaleItem,
ElementScaleOptions,
DestroyScale
} from 'element-scale';License
MIT © SinJayXie
