sc0ttj / component

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

Feature/canvas object event handlers #73

Closed sc0ttj closed 1 year ago

sc0ttj commented 1 year ago

Interactive canvas improvements

Event handlers

// new API - event handlers

// log an objects params on "click"," hover", "drag", "release"
myRect.on('click', props => console.log(props)); 
myRect.on('drag', props => console.log(props)); 

Define custom "canvas objects"

// new API - define custom "canvas objects" with `ctx.create(name, drawFunc)`

// define a new type of "interactive canvas object", called "coolShape"
ctx.create('coolShape', function(x, y, z, foo) {
  [ ctx, ctx.shadowCtx ].forEach(c => {
    // draw stuff here
    c.rect(x, y, w, h);
    c.lineTo(x, y);
    // etc
  });
});

// instantiate a "coolShape" object that can be hovered over, clicked, etc 
const myNewShape = ctx.create.coolShape(x, y, z, foo);

// use it the same as any other "canvas object" 
myNewShape.update(...);
myNewShape.draw(...);
myNewShape.on('hover', props => console.log(props));

myShape.adjust()

new API - alternative way to update "canvas object" styles:

// define style properties on the canvas object itself, ready to draw later
myStar.lineWidth = 3;
myStar.fillStyle = '#c00';
// ...etc

myStar.draw(styleObj)      // `styleObj` is optional, but will override above styles, if given

Alternative syntax, provides some extra features

new API - give "canvas objects" props as an object, instead of params list:

Fixes