0.16.3 • Published 10 months ago

@daeinc/canvas v0.16.3

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

@daeinc/canvas

npm version badge npm bundle size badge

Install

npm i @daeinc/canvas

then,

import { createCanvas, ... } from "@daeinc/canvas"

Functions

Documentation is updated for 0.16.0.

createCanvas

const createCanvas: ({
  parent,
  context,
  width,
  height,
  pixelRatio,
  scaleContext,
  attributes,
}: {
  parent?: string | Element | null;
  context?: "2d" | "webgl" | "webgl2" | "webgpu";
  width: number;
  height: number;
  pixelRatio?: number;
  scaleContext?: boolean;
  attributes?:
    | CanvasRenderingContext2DSettings
    | WebGLContextAttributes;
})

Create a new canvas and return { canvas, context, width, height } in 2d or WebGPU and { canvas, gl, width, height } in webgl.

The parent can be either string (will be used for querySelector()) or Element. If parent is undefined or null, the canvas is not attached to the document. Returned width and height may not be the same as canvas.width and canvas.height due to pixelRatio scaling.

context supports 2d, webgl, webgl2 and webgpu and creates a proper context. When webgl context is created, gl.viewport() is internally called to scale context according to pixelRatio parameter.

createOffscreenCanvas

const createOffscreenCanvas: ({
  context,
  width,
  height,
  pixelRatio,
  scaleContext,
  attributes,
}: {
  context: "2d" | "webgl" | "webgl2";
  width: number;
  height: number;
  pixelRatio?: number;
  scaleContext?: boolean;
  attributes?:
    | CanvasRenderingContext2DSettings
    | WebGLContextAttributes;
})

Creates an OffscreenCanvas.

resizeCanvas

const resizeCanvas: ({
  canvas,
  context,
  width,
  height,
  pixelRatio,
  scaleContext,
  attributes,
}: {
  canvas: HTMLCanvasElement;
  context: "2d" | "webgl" | "webgl2" | "webgpu";
  width: number;
  height: number;
  pixelRatio?: number;
  scaleContext?: boolean;
  attributes?: CanvasRenderingContext2DSettings | WebGLContextAttributes;
}) => {
  canvas: HTMLCanvasElement;
  context?:
    | CanvasRenderingContext2D
    | WebGLRenderingContext
    | WebGL2RenderingContext;
  gl?: WebGLRenderingContext | WebGL2RenderingContext;
  width: number;
  height: number;
};

Resize canvas and return data { canvas, context?, gl?, width, height }. When scaleContext=true, it also scale the context to pixelRatio. scaleContext has no effect on WebGPU canvas.

License

MIT

0.16.3

10 months ago

0.16.1

10 months ago

0.16.2

10 months ago

0.15.0

2 years ago

0.14.5

3 years ago

0.14.0

3 years ago

0.14.1

3 years ago

0.14.2

3 years ago

0.14.3

3 years ago

0.14.4

3 years ago

0.11.0

3 years ago

0.10.1

3 years ago

0.12.0

3 years ago

0.11.1

3 years ago

0.13.0

3 years ago

0.12.1

3 years ago

0.10.0

3 years ago

0.3.0

3 years ago

0.9.0

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.7.1

3 years ago

0.9.1

3 years ago

0.5.0

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.7.0

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.1

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago