create-canvas-context
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -0,1 +1,2 @@ | ||
/// <reference types="offscreencanvas" /> | ||
import { PlainFunction, Unpack } from "./types"; | ||
@@ -2,0 +3,0 @@ export declare function isNumber(value: number | unknown): value is number; |
@@ -1,34 +0,35 @@ | ||
import { ContextAttributes, ContextRenderingContext, ContextType, OffscreenContextRenderingContext } from "./types"; | ||
/// <reference types="offscreencanvas" /> | ||
import { ContextAttributes, ContextRenderingContext, ContextType, OffscreenContextRenderingContext, OffscreenContextType } from "./types"; | ||
interface Options { | ||
canvas?: HTMLCanvasElement | OffscreenCanvas; | ||
height?: number; | ||
offscreen?: boolean; | ||
width?: number; | ||
height?: number; | ||
} | ||
export declare function createCanvasContext<T extends ContextType>(type: T, options?: (Options & { | ||
export declare function createCanvasContext<T extends ContextType>(type: T, options?: ContextAttributes<T> | (Options & { | ||
canvas?: undefined; | ||
offscreen?: false; | ||
}) | ContextAttributes<T>): [ContextRenderingContext<T> | null, HTMLCanvasElement | null]; | ||
export declare function createCanvasContext<T extends ContextType>(type: T, options?: (Options & { | ||
})): [ContextRenderingContext<T> | null, HTMLCanvasElement | null]; | ||
export declare function createCanvasContext<T extends OffscreenContextType>(type: T, options?: ContextAttributes<T> | (Options & { | ||
canvas?: undefined; | ||
offscreen: true; | ||
}) | ContextAttributes<T>): [ | ||
OffscreenContextRenderingContext<T> | ContextRenderingContext<T> | null, | ||
})): [ | ||
ContextRenderingContext<T> | OffscreenContextRenderingContext<T> | null, | ||
HTMLCanvasElement | OffscreenCanvas | null | ||
]; | ||
export declare function createCanvasContext<T extends ContextType>(type: T, options?: (Options & { | ||
export declare function createCanvasContext<T extends ContextType>(type: T, options?: ContextAttributes<T> | (Options & { | ||
canvas: HTMLCanvasElement; | ||
offscreen?: false; | ||
}) | ContextAttributes<T>): [ContextRenderingContext<T> | null, HTMLCanvasElement]; | ||
export declare function createCanvasContext<T extends ContextType>(type: T, options?: (Options & { | ||
})): [ContextRenderingContext<T> | null, HTMLCanvasElement]; | ||
export declare function createCanvasContext<T extends OffscreenContextType>(type: T, options?: ContextAttributes<T> | (Options & { | ||
canvas: HTMLCanvasElement; | ||
offscreen: true; | ||
}) | ContextAttributes<T>): [ | ||
OffscreenContextRenderingContext<T> | ContextRenderingContext<T> | null, | ||
})): [ | ||
ContextRenderingContext<T> | OffscreenContextRenderingContext<T> | null, | ||
HTMLCanvasElement | OffscreenCanvas | ||
]; | ||
export declare function createCanvasContext<T extends ContextType>(type: T, options?: (Options & { | ||
export declare function createCanvasContext<T extends OffscreenContextType>(type: T, options?: ContextAttributes<T> | (Options & { | ||
canvas: OffscreenCanvas; | ||
offscreen?: true; | ||
}) | ContextAttributes<T>): [OffscreenContextRenderingContext<T> | null, OffscreenCanvas]; | ||
})): [OffscreenContextRenderingContext<T> | null, OffscreenCanvas]; | ||
export {}; |
@@ -0,1 +1,2 @@ | ||
/// <reference types="offscreencanvas" /> | ||
export declare type Unpack<T> = T extends (infer U)[] ? U : T; | ||
@@ -6,2 +7,3 @@ export declare type Guard<T> = (value: T | unknown) => value is T; | ||
export declare type ContextType = "2d" | "bitmaprenderer" | "webgl" | "webgl2"; | ||
export declare type OffscreenContextType = "2d"; | ||
interface ContextsRenderingContext { | ||
@@ -8,0 +10,0 @@ "2d": CanvasRenderingContext2D; |
{ | ||
"name": "create-canvas-context", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Create a canvas and get a rendering context from it.", | ||
@@ -20,12 +20,31 @@ "author": "Marc Bouchenoire", | ||
"main": "./dist/create-canvas-context.js", | ||
"umd:main": "./dist/create-canvas-context.umd.js", | ||
"unpkg": "./dist/create-canvas-context.umd.js", | ||
"module": "./dist/create-canvas-context.module.js", | ||
"exports": "./dist/create-canvas-context.modern.js", | ||
"esmodule": "./dist/create-canvas-context.modern.js", | ||
"esnext": "./dist/create-canvas-context.modern.js", | ||
"exports": { | ||
".": { | ||
"require": "./dist/create-canvas-context.js", | ||
"import": "./dist/create-canvas-context.modern.js", | ||
"types": "./dist/index.d.ts" | ||
} | ||
}, | ||
"types": "./dist/index.d.ts", | ||
"lint-staged": { | ||
"**/*.{ts,tsx}": [ | ||
"eslint --fix", | ||
"tsc-mixed --project tsconfig.json", | ||
"prettier --write" | ||
] | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"scripts": { | ||
"build": "microbundle --tsconfig tsconfig.build.json", | ||
"clean": "jest --clearCache && rimraf node_modules", | ||
"lint": "eslint '{src,tests}/**/*.{ts,tsx}' --fix", | ||
"clean": "jest --clearCache && rimraf dist node_modules", | ||
"lint": "yarn lint:eslint && yarn lint:tsc && yarn lint:prettier", | ||
"lint:eslint": "eslint '**/*.{ts,tsx}' --fix", | ||
"lint:prettier": "prettier --write '**/*.{ts,tsx}' --loglevel silent", | ||
"lint:tsc": "tsc --project tsconfig.json", | ||
"prepare": "rimraf dist && yarn build", | ||
@@ -35,20 +54,12 @@ "release": "yarn login && np", | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^26.0.20", | ||
"@typescript-eslint/eslint-plugin": "^4.16.1", | ||
"@typescript-eslint/parser": "^4.16.1", | ||
"eslint": "^7.19.0", | ||
"eslint-config-prettier": "^8.1.0", | ||
"eslint-import-resolver-typescript": "^2.3.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-prettier": "^3.3.1", | ||
"husky": "^4.3.8", | ||
"@bouchenoiremarc/eslint-config": "^1.6.0", | ||
"@swc-node/jest": "^1.3.1", | ||
"@types/jest": "^27.0.1", | ||
"@types/offscreencanvas": "^2019.6.3", | ||
"eslint": "^7.30.0", | ||
"husky": "4.3.8", | ||
"jest": "^26.6.3", | ||
"jest-electron": "^0.1.11", | ||
"lint-staged": "^10.5.3", | ||
"lint-staged": "^11.0.0", | ||
"microbundle": "^0.13.0", | ||
@@ -58,5 +69,5 @@ "np": "^7.4.0", | ||
"rimraf": "^3.0.2", | ||
"ts-jest": "^26.5.0", | ||
"tsc-mixed": "^1.0.0", | ||
"typescript": "^4.1.3" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # create-canvas-context | ||
[![npm](https://img.shields.io/npm/v/create-canvas-context?color=%2385f)](https://www.npmjs.com/package/create-canvas-context) [![gzipped](https://img.shields.io/bundlephobia/minzip/create-canvas-context?label=gzipped&color=%23d5e)](https://www.npmjs.com/package/create-canvas-context) [![license](https://img.shields.io/github/license/bouchenoiremarc/create-canvas-context?color=%23e48)](https://github.com/bouchenoiremarc/create-canvas-context/blob/main/LICENSE) | ||
[![build](https://github.com/bouchenoiremarc/create-canvas-context/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/bouchenoiremarc/create-canvas-context/actions/workflows/ci.yml) [![npm](https://img.shields.io/npm/v/create-canvas-context?color=%230cf)](https://www.npmjs.com/package/create-canvas-context) [![gzipped](https://img.shields.io/bundlephobia/minzip/create-canvas-context?label=gzipped&color=%2385f)](https://www.npmjs.com/package/create-canvas-context) [![license](https://img.shields.io/github/license/bouchenoiremarc/create-canvas-context?color=%23e4b)](https://github.com/bouchenoiremarc/create-canvas-context/blob/main/LICENSE) | ||
@@ -38,12 +38,9 @@ ## Installation | ||
Invoke it while specifying a context type (`"2d"`, `"bitmaprenderer"`, `"webgl"` or `"webgl2"`). | ||
Invoke it while specifying a context type (`"2d"`, `"bitmaprenderer"`, `"webgl"` or `"webgl2"`) and access in return the specified rendering context and its canvas as a pair. | ||
```tsx | ||
createCanvasContext("2d") | ||
``` | ||
const [context, canvas] = createCanvasContext("2d") | ||
Access in return the specified rendering context and its canvas as a pair. | ||
```tsx | ||
const [context, canvas] = createCanvasContext("2d") | ||
// context: CanvasRenderingContext2D | ||
// canvas: HTMLCanvasElement | ||
``` | ||
@@ -54,3 +51,3 @@ | ||
```tsx | ||
const [context, canvas] = createCanvasContext("2d", { | ||
const [context, canvas] = createCanvasContext("webgl", { | ||
canvas: document.createElement("canvas"), | ||
@@ -60,2 +57,5 @@ offscreen: true, | ||
}) | ||
// context: WebGLRenderingContext | ||
// canvas: OffscreenCanvas | ||
``` | ||
@@ -62,0 +62,0 @@ |
@@ -6,3 +6,4 @@ import { isCanvas, isHTMLCanvasElement, isNumber } from "./guards" | ||
ContextType, | ||
OffscreenContextRenderingContext | ||
OffscreenContextRenderingContext, | ||
OffscreenContextType | ||
} from "./types" | ||
@@ -18,5 +19,5 @@ import { isBrowser } from "./utils/is-browser" | ||
canvas?: HTMLCanvasElement | OffscreenCanvas | ||
height?: number | ||
offscreen?: boolean | ||
width?: number | ||
height?: number | ||
} | ||
@@ -41,12 +42,12 @@ | ||
options?: | ||
| ContextAttributes<T> | ||
| (Options & { canvas?: undefined; offscreen?: false }) | ||
| ContextAttributes<T> | ||
): [ContextRenderingContext<T> | null, HTMLCanvasElement | null] | ||
export function createCanvasContext<T extends ContextType>( | ||
export function createCanvasContext<T extends OffscreenContextType>( | ||
type: T, | ||
options?: | ||
| ContextAttributes<T> | ||
| (Options & { canvas?: undefined; offscreen: true }) | ||
| ContextAttributes<T> | ||
): [ | ||
OffscreenContextRenderingContext<T> | ContextRenderingContext<T> | null, | ||
ContextRenderingContext<T> | OffscreenContextRenderingContext<T> | null, | ||
HTMLCanvasElement | OffscreenCanvas | null | ||
@@ -57,25 +58,30 @@ ] | ||
options?: | ||
| ContextAttributes<T> | ||
| (Options & { canvas: HTMLCanvasElement; offscreen?: false }) | ||
| ContextAttributes<T> | ||
): [ContextRenderingContext<T> | null, HTMLCanvasElement] | ||
export function createCanvasContext<T extends ContextType>( | ||
export function createCanvasContext<T extends OffscreenContextType>( | ||
type: T, | ||
options?: | ||
| ContextAttributes<T> | ||
| (Options & { canvas: HTMLCanvasElement; offscreen: true }) | ||
| ContextAttributes<T> | ||
): [ | ||
OffscreenContextRenderingContext<T> | ContextRenderingContext<T> | null, | ||
ContextRenderingContext<T> | OffscreenContextRenderingContext<T> | null, | ||
HTMLCanvasElement | OffscreenCanvas | ||
] | ||
export function createCanvasContext<T extends ContextType>( | ||
export function createCanvasContext<T extends OffscreenContextType>( | ||
type: T, | ||
options?: | ||
| ContextAttributes<T> | ||
| (Options & { canvas: OffscreenCanvas; offscreen?: true }) | ||
| ContextAttributes<T> | ||
): [OffscreenContextRenderingContext<T> | null, OffscreenCanvas] | ||
export function createCanvasContext<T extends ContextType>( | ||
type: T, | ||
options: Options | ContextAttributes<T> = defaultOptions | ||
) { | ||
const { canvas: optionsCanvas, offscreen, width, height, ...attributes } = { | ||
export function createCanvasContext< | ||
T extends ContextType | OffscreenContextType | ||
>(type: T, options: ContextAttributes<T> | Options = defaultOptions) { | ||
const { | ||
canvas: optionsCanvas, | ||
offscreen, | ||
width, | ||
height, | ||
...attributes | ||
} = { | ||
...defaultOptions, | ||
@@ -103,5 +109,7 @@ ...options | ||
const context = isCanvas(canvas) ? canvas.getContext(type, attributes) : null | ||
const context = isCanvas(canvas) | ||
? canvas.getContext(type as OffscreenContextType, attributes) | ||
: null | ||
return [context, canvas] | ||
} |
@@ -11,2 +11,4 @@ export type Unpack<T> = T extends (infer U)[] ? U : T | ||
export type OffscreenContextType = "2d" | ||
interface ContextsRenderingContext { | ||
@@ -33,10 +35,8 @@ "2d": CanvasRenderingContext2D | ||
export type ContextRenderingContext< | ||
T extends ContextType | ||
> = ContextsRenderingContext[T] | ||
export type ContextRenderingContext<T extends ContextType> = | ||
ContextsRenderingContext[T] | ||
export type OffscreenContextRenderingContext< | ||
T extends ContextType | ||
> = OffscreenContextsRenderingContext[T] | ||
export type OffscreenContextRenderingContext<T extends ContextType> = | ||
OffscreenContextsRenderingContext[T] | ||
export type ContextAttributes<T extends ContextType> = ContextsAttributes[T] |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41875
15
256