-
Notifications
You must be signed in to change notification settings - Fork 921
Feature request: Shape support #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Not only shape: the support of rectangle and ellipse could make |
Good integration between opentype.js and react-canvas would be great! http://nodebox.github.io/opentype.js/ Being able to use the latest Canvas2D features would be fantastic! https://hacks.mozilla.org/2015/01/canvas-2d-new-docs-path2d-hit-regions/ |
I am currently experimenting this in a performant way (shape and rect + circle) with react-canvas, it is very experimental and most likely will be pushed mid-week as a POC on my fork. Though using a shape is already possible using a different canvas to render the stuff and then cache and use it as an image via data uri's but its not gonna cut i there is a decent high number of objects requiring dynamic re-draws. Use - cases :
|
Instead of adding more primitive I think that a simpler and more efficient solution would be to let us access to a |
I have create a basic version here https://github.com/fdecampredon/react-canvas/tree/draw-api I'll experiment a bit more with that when I'll have time to do so. |
I am using similar technique in my experiment but bundling more primitives as for path and commons. This can be common code and then primitives can be bundled :-) exposing the draw theoretically allows paths for 3rd party canvas libs to tap in ( say for charts ) |
@mjohnston would you be interested by this approach for a PR if I work a bit more on that one ? |
+1 from me. |
@fdecampredon I really like the idea of making the drawing engine more extensible. Would love to see a PR! Direction you're heading looks like a good one. |
Nice, ok so just a few questions. The
|
:Updated: How I have done it so far was to have 2 seperate classes exposed to the 3rd party.
import {CanvasLayer, BaseCanvasLayer} from 'react-canvas';
class Circle extends CanvasLayer{
constructor(){ /* do some cool stuff */ }
draw(ctx, layer){
/* use the lower level api awesomeness */
}
}
// maybe i should rename the exposed classes
class FrostBlurStyleLayer extends BaseCanvasLayer{
constructor(){ /* do aweosmeness */
draw(ctx, layer){
/* do aweosmeness, this layer actually performs frost blur by taking image data */
}
} |
@darkyen have you seen the PR #46 that I sent ? I changed a bit my way of doing things and tryed to mimic the way things work actually, in that PR I have 2 new top level method :
This way we can separate draw api engine extensions from component using them (and so we could imagine having multiple component using the same layer type and associated draw function but injecting different property in the layer). |
I intended to use the same code for declaring 3rd party libraries for Charting and other stuff can just use the same pattern and just by extending a single class. |
My current prototype with Inheritance does something like this class FrostyBlur extends LayoutLayer {
draw(ctx){
super.draw(ctx);
/* and then get reference to the pixel array and blur */
}
}
class Circle extends RenderLayer{
draw(ctx){
super.draw(ctx);
/* draw the circle */
/* super does other tasks here like scaling and opacity and etc */
}
}
// and the Layoutlayer itself is implemented like so
class LayoutLayer extends RenderLayer{
draw(ctx){
super.draw(ctx);
/* do all css style border,radius etc rendering here */
}
} The drawRenderLayer function then becomes pretty simple. |
I have mixed feelings about using es6 classes, though that appears to be where things are going. The question of how mixins work in this world is still an open one. Until things settle a bit more I would be in favor of the I imagine the existing canvas classes could be moved to this model (Image, Text, etc.) and |
I can make my style work with es5 style. No issues with that in fact that
|
I have problem is with the dual method, I just happen to dislike it.
|
You have a point there... I wonder if the same customization at the layer-level can be done by composition and configuration through props? For instance, rather than injecting different layer properties from the Component, the Layer would conditionalize it's drawing code based on |
@mjohnston @darkyen my original proposition was made to avoid a big refactoring if we are allowed to have a bigger refactoring while I most of the time try to avoid class an inheritance I think also that in this case it could make sense. I could make a proposal and a little prototype in that direction if you wish. |
Makes sense, PS here is are two doable new proposal using composition // Extending how react does it we can do something like this as per reacts code
var AwesomeComponent = ReactCanvas.createClass({
draw(ctx){
ctx.rect(20,20,40,40);
ctx.stroke();
}
});
// and have a boolean or something ... or
// @fdecampredon's way of ding it
elements, and to achieve the layout Layer we Another solution using pure react import React from 'react';
import {LayoutLayer, RenderLayer, Drawable} from 'react-canvas';
var AwesomeView = React.createClass({
onAnimationFrame(ctx){
/* call to draw */
// do your awesome stuff !
},
render (){
return (
<RenderLayer>
<Drawable
cachable={false}/>
</RenderLayer>
);
}
}); In the second example we can keep using react style of doing things and simply just call the parents.onAnimationFrame method from the element. That way react can also re-use the top Components and just change the Drawable component dynamically. This is also pure react-style composition. and i can submit a pr for the latter by saturday evening (Finally its friday !) (I feel the latter is more slightly inspired by rackt/react-router how they use and routes to define and compose incredible ui's. Also as per performance is concerned once the state is built react's hyperfast way of binding Perhaps it might be better to allow the person to pass the function name too :D |
any suggestions ? |
There is only one thing bothering me @darkyen actually the react component is just a wrapper on top of the render layer, and the render layer does not depends upon that component, that's has some advantage, the render layer itself is just a plain serializable json object without any logic. |
No. I would still only write thin wrappers over an independent layer. For var Drawable = React.createClass({
onAnimationFrame(ctx){
/* draw the thing by calling stuff */
this.props.onAnimationFrame(ctx);
},
render(){
// additional hidden Dom goes here
return <this.props.children />
}
});
var ClassyInput = React.createClass({
getInitialState(){
},
transferFocus(e){
this.refs.realInput.getDOMNode().focus();
return false;
},
onChange(){
/* sync state and bubble up and queue for re render */
},
af(){
/* update the value and render the input this occurs freely */
},
render(){
return (
<Drawable onTouchTapCapture={this.transferFocus}>
<input ref="realInput" />
</Drawable>
);
}
}); How about this ? AFAIK this will support A11y as well since a11y will be handled by cousin Dom. While render layer will our perform and since that Dom can be set to visibility hidden or something (like the checkbox or custom Dom element haxx ) we should be able to achice best of both worlds and If tomorrow a react based proper a11y implementation occurs it will be easy to be implemented |
Also in the above example it will be fully valid React View without the |
+1 |
Any updates on this? |
👍 Would be great to see that. |
Any updates on this? |
Would be great to have support for shapes like
react-art
:The text was updated successfully, but these errors were encountered: