sc0ttj / component

A tiny library for isomorphic JavaScript components
MIT License
2 stars 1 forks source link

Feature: canvas add-on #46

Closed sc0ttj closed 3 years ago

sc0ttj commented 3 years ago

An add-on which extends the canvas 2d context, to provide the following features:

See these libraries for ideas:

sc0ttj commented 3 years ago

https://github.com/sc0ttj/component/pull/52

This PR includes a lot of the above, except sprites, tiling/maps, and stuff that requires interactivity.

Interactivity (like click, hover, drag & drop, gamepads, keyboard) can't easily be done with the canvas API alone - it doesn't keep a reference to anything it draws.

To solve this, each thing you draw (rectangle, star, image) needs an JS Object created as a reference to it:

These reference objects usually have an update() method, for updating its properties, and a render() method for rendering to canvas once the props have been updated.

...I've yet to decide how I want to implement this, and whether or not it should go into a separate add-on specifically for defining "game objects", or similar - Ctx would automatically use it, if it's available.

Nothing too grand is needed - Component already has a setState/render cycle, and nicely separated states and views - the references to the things drawn/rendered, and the re-rendering of them, can (probably) be hooked into the existing component lifecycle, using just states and views.

sc0ttj commented 3 years ago

Done .. See PR #52

This add-on may need re-visiting to enable adding interactivity, easy sprites, tiling/tiled "maps" - anything past that would be out of scope for this add-on, which is basically for drawing/rendering stuff only.