From a6bc3595f9e46aa2d0aa5b7b1b1984bcbb95f615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20B=C5=82a=C5=BCejewicz=20=28Peter=20Blazejewicz=29?= Date: Mon, 27 Apr 2020 14:39:31 +0200 Subject: [PATCH] feat(canvas-fit): new type definition (#44065) - definition file - tests https://github.com/hughsk/canvas-fit#usage Thanks! --- types/canvas-fit/canvas-fit-tests.ts | 16 +++++++++++++ types/canvas-fit/index.d.ts | 35 ++++++++++++++++++++++++++++ types/canvas-fit/tsconfig.json | 24 +++++++++++++++++++ types/canvas-fit/tslint.json | 1 + 4 files changed, 76 insertions(+) create mode 100644 types/canvas-fit/canvas-fit-tests.ts create mode 100644 types/canvas-fit/index.d.ts create mode 100644 types/canvas-fit/tsconfig.json create mode 100644 types/canvas-fit/tslint.json diff --git a/types/canvas-fit/canvas-fit-tests.ts b/types/canvas-fit/canvas-fit-tests.ts new file mode 100644 index 0000000000..e6006dd007 --- /dev/null +++ b/types/canvas-fit/canvas-fit-tests.ts @@ -0,0 +1,16 @@ +/// + +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]; +}; diff --git a/types/canvas-fit/index.d.ts b/types/canvas-fit/index.d.ts new file mode 100644 index 0000000000..e76b3dfd26 --- /dev/null +++ b/types/canvas-fit/index.d.ts @@ -0,0 +1,35 @@ +// Type definitions for canvas-fit 1.5 +// Project: https://github.com/hughsk/canvas-fit +// Definitions by: Piotr Błażejewicz +// 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; diff --git a/types/canvas-fit/tsconfig.json b/types/canvas-fit/tsconfig.json new file mode 100644 index 0000000000..06b048e8d1 --- /dev/null +++ b/types/canvas-fit/tsconfig.json @@ -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" + ] +} diff --git a/types/canvas-fit/tslint.json b/types/canvas-fit/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/canvas-fit/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }