0.1.3 • Published 1 year ago
linear-tick v0.1.3
linear-tick
English | 中文
Draw everything on canvas with linear ticks
Installation
npm i linear-tickUsage
Basic Usage
import { LinearTick } from 'linear-tick';
const canvas = document.getElementById('myCanvas') as HTMLCanvasElement;
const linearTick = new LinearTick();
linearTick.canvas = canvas;
linearTick.ticks = [{
multiply: 10,
maxDensityToShow: 3,
drawCall: {
init(ctx) {
ctx.strokeStyle = 'red'
ctx.lineWidth = 2
ctx.beginPath()
},
each(ctx, store, state, params) {
ctx.moveTo(state.x, state.y)
ctx.lineTo(state.x, state.y + params.height)
},
end(ctx) { ctx.stroke() }
}
}];
linearTick.draw();Properties
canvas?: HTMLCanvasElement- The canvas elementkeepSameSizeWith?: HTMLElement- If set,params.widthandparams.heightwill be calculated based on this element's size andparams.pixelRatio, and automatically adjusted when the size changesdrawNextFrame: boolean- Whether to draw on the next framewhenResized?: WhenResized- Action to perform when the size changesinitStore?: () => STORE- Function to initialize custom stateinitDraw?: DrawCallFn<STORE, void | null>- Function executed before any drawing starts, returnsnullto abort drawingfinalDraw?: DrawCallFn<STORE>- Function executed after all drawing is completedticks: LinearTickDefine<STORE>[]- Tick definitions, recommended to be sorted bymultiplyfrom small to largeparams: LinearTickParams- Parameters
Tick Definition
The LinearTickDefine<STORE> interface defines the structure of a tick:
multiply: number- Multiplier for tick values, i.e., how many values correspond to one tickzeroOffset?: number- Offset for tick values, e.g., with offset 3 and multiply 5, ticks will be 3, 8, 13, 18...maxDensityToShow?: number- Ticks will be hidden when density is greater than this valueshy?: boolean- Whether to hide this tick when subsequent ticks overlap with itdrawCall: LinearTickDrawCall<STORE> | LinearTickDrawCall<STORE>[]- Drawing function definition, if an array, drawn in order
The LinearTickDrawCall<STORE> interface defines the drawing functions:
init?: DrawCallFn<STORE, void | null>- Executed before drawing starts, returnsnullto abort drawingeach: DrawCallFn<STORE>- Executed for each drawingfinal?: DrawCallFn<STORE>- Executed when drawing ends
The DrawCallFn<STORE> interface defines the drawing function:
ctx: CanvasRenderingContext2D- Canvas contextstore: STORE- Custom statestate: LinearTickDrawState- Drawing stateparams: LinearTickParams- Parameters
The LinearTickDrawState interface defines the drawing state:
x: number- X coordinate of the current drawing positiony: number- Y coordinate of the current drawing positionvalue: number- Tick value at the current drawing positionfirstValue: number- First tick value that can fit on the canvaslastValue: number- Last tick value that can fit on the canvas
Parameters
The LinearTickParams interface defines the following parameters:
min?: number- Minimum value of the drawing range, ticks smaller than this will not be drawnmax?: number- Maximum value of the drawing range, ticks larger than this will not be drawnvalue: number- Value corresponding to the anchor pointredundancy: number- Number of units the visible range should be extended by to prevent missing drawings at the edgesanchorX: number- X coordinate of the anchor point on the canvas, range 0, 1, 0 is leftmost, 1 is rightmostanchorY: number- Y coordinate of the anchor point on the canvas, range 0, 1, 0 is topmost, 1 is bottommostdirectionX: number- X component of the unit vector, in pixels. Recommended unit vector length is 1pxdirectionY: number- Y component of the unit vectordensity: number- Density, i.e., number of values per unit vectorminDensity: number- Minimum density, i.e., number of values per unit vector when zoomed out to maximum, will limit the minimum value ofdensitymaxDensity: number- Maximum density, i.e., number of values per unit vector when zoomed in to minimum, will limit the maximum value ofdensitywidth: number- Drawing pixel widthheight: number- Drawing pixel heightpixelRatio?: number- Pixel ratio
Responsiveness
const container = document.getElementById('container');
const linearTick = new LinearTick();
linearTick.canvas = document.getElementById('myCanvas') as HTMLCanvasElement;
linearTick.keepSameSizeWith = container;
linearTick.whenResized = WhenResized.Draw;This setup makes the canvas automatically adjust its size and redraw when the container element's size changes.
WhenResized is an enum that defines LinearTick's behavior when the container size changes:
DoNothing- Do nothing when the size changesDraw- Respond to size changes and redrawDrawNextFrame- Respond to size changes, but don't redraw immediately, wait until the next frame to redraw