feat(canvas-fit): new type definition (#44065)

- definition file
- tests

https://github.com/hughsk/canvas-fit#usage

Thanks!
This commit is contained in:
Piotr Błażejewicz (Peter Blazejewicz) 2020-04-27 14:39:31 +02:00 committed by GitHub
parent 1869bedd41
commit a6bc3595f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,16 @@
/// <reference types="node" />
import fit = require('canvas-fit');
const canvas = document.createElement('canvas');
window.addEventListener('resize', fit(canvas), false);
window.addEventListener('resize', fit(canvas, window), false);
window.addEventListener('resize', fit(canvas, window, '20%'), false);
window.addEventListener('resize', fit(canvas, window, 20.5), false);
const resize = fit(canvas, window);
resize.scale = window.devicePixelRatio;
resize.parent = document.body;
resize.parent = () => {
return [window.innerWidth - 300, window.innerHeight];
};

35
types/canvas-fit/index.d.ts vendored Normal file
View File

@ -0,0 +1,35 @@
// Type definitions for canvas-fit 1.5
// Project: https://github.com/hughsk/canvas-fit
// Definitions by: Piotr Błażejewicz <https://github.com/peterblazejewicz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/**
* Creates a resize function for your canvas element. Calling this function will resize the canvas to fit its parent element.
*/
declare function fit(
canvas: HTMLCanvasElement | SVGElement,
parent?: EventTarget | Document | HTMLElement,
scale?: string | number,
): resize;
interface resize {
(this: any, ev: any): any;
/**
* Dynamically change the canvas' target scale.
* Note that you still need to manually trigger a resize after doing this.
*/
scale?: number;
/**
* Dynamically change the canvas' target scale.
* Note that you still need to manually trigger a resize after doing this.
* Instead of filling a given element, explicitly set the width and height of the canvas.
* Note that this value will still be scaled up according to resize.scale
*/
parent?: Element | (() => [number, number]);
}
/**
* Small module for fitting a canvas element within the bounds of its parent.
* Useful, for example, for making a canvas fill the screen. Works with SVG elements too!
*/
export = fit;

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"DOM"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"canvas-fit-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }