Super duper alpha.. Use at your own risk. lol
There's a playground link here on Stackblitz.
- THERE ARE NO TESTS!! (duh, huge red flag!)
- Missing events:
- onMouseDown
- onMouseUp
- onKeyPress
- onKeyDown
- onKeyUp
- touch events?
- Have yet to figure out focus management scheme
- Screen reader updates
- Components:
- Ellipse? Circle?
- Clipping: A component to define a clipping area? Maybe this could be done with the
g
component?
- Do I want to allow transformations (scale, rotate, etc) on other existing components?
See storybook examples for usage.
Basically:
- Define a component
import { defineComp, rect, text } from 'canvas-render-components';
function MyComp(props: MyCompProps, ctx: CanvasRenderingContext2D) => {
// There are hooks like React, just prefixed with `crc` instead of use:
const [count, setCount] = crcState(0);
// You can return other crc elements, like react, but JSX is annoying to hook up
// So I don't have that in this example:
return [
rect({
x: 10,
y: 10,
width: 100,
height: 100,
fillStyle: 'blue',
onClick: () => setCount(count + 1)
}),
text({
x: 10,
y: 10,
width: 100,
text: 'Click Me',
fillStyle: 'white'
})
];
}
export const myComp = defineComp(MyComp)
- Mount the component to an existing
HTMLCanvasElement
:
import { myComp } from './MyComp';
import { crc } from 'canvas-render-components';
const canvas = document.querySelector('#my-canvas-id');
crc(canvas, myComp);
Sorry, this isn't really documentation, just the basic idea:
crc(canvasElement, crcElement)
- Mount or update an existing canvas element with a crc elementdefineComp(compFn)
- used to create a more ergonomic means of consuming crc components and returning crc elements when setting up JSX is too annoying (it's always too annoying).
path
: Renders an arbitraryPath2D
rect
: Renders a rectangletext
: Text rendering (including multiline, singleline, ellipsis overflow, etc)line
: Renders a series of coordinates as connected line segmentsverticalLine
/horizontalLine
special components for rendering vertical or horizontal line segments, which includes a bit calledalignToPixelGrid
that allows you to ensure 1px lines are really 1px. (it's a canvas quirk)svgPath
: Renders svg path data as a shapeimg
: Loads and renders an imageg
: A grouping component that allows the group application of transformations such as scale, rotation, etc.
crcRef
- basically a simplified version of react'suseRef
crcState
- A simplified version of react'suseState
crcMemo
- Basically react'suseMemo
. This is VERY useful for memoizingPath2D
objects that need to be passed to other hooks. Strongly recommended for that use case.crcWhenChanged
- Looks like react'suseEffect
.. it is NOT. It takes a callback that will execute SYNCHRONOUSLY when dependencies change. It also allows the return of a teardown. This is specifically for use cases where one might need to execute some logic only when some dependencies change. DO NOT USE if you need to synchronously update some _state when dependencies change, usecrcMemo
instead.crcCursor
- A hook to allow the setup of CSS cursor (pointer) changes when hovering a givenPath2D
.crcClick
,crcDblClick
,crcMouseMove
, (et al)... Hooks to allow setup of events related to a givenPath2D
.crcRectPath
- A simplified hook that returns a memoizedPath2D
for a rectangle (A common task).crcLinePath
- A hook for memoizedPath2D
objects from coordinates.crcSvgPath
- A hook for memoizedPath2D
objects from svg path data strings.