KineticJS icon indicating copy to clipboard operation
KineticJS copied to clipboard

KineticJS and Compositing methods

Open v1r opened this issue 14 years ago • 19 comments

Hi Eric,

First, Thanks for this awesome framework..

I'm wondering what is the right way to use composting methods in kineticJS..

http://jsfiddle.net/v1r00z/RNLxb/

Thanks.

v1r avatar Oct 02 '12 20:10 v1r

@v1r http://jsfiddle.net/RNLxb/4/ you have to draw your layers, setup an Composite method and draw the layer again

BlackScorp avatar Oct 05 '12 08:10 BlackScorp

cheers bro!

v1r avatar Oct 06 '12 02:10 v1r

@BlackScorp Thanks for the help!

ericdrowell avatar Oct 06 '12 02:10 ericdrowell

thank you for KineticJS @ericdrowell :P

BlackScorp avatar Oct 06 '12 08:10 BlackScorp

There's a problem with this method, if you call stage.draw(); anywhere, this option isn't stored anywhere, so it will be drawn with default composition. Other way is to define drawFunc where you will change Composite method and call default shape draw method.

0xE1 avatar Oct 06 '12 12:10 0xE1

i just tryed it on the jsfiddle you can call stage.draw() anywhere i gues there must be some problems with your code

BlackScorp avatar Oct 06 '12 15:10 BlackScorp

Seems it can various on situation, in my case i'm changing stage size, maybe in that case it's being redrawn and this happens. I have solved this problem with setDrawFunc method which targets function like this: function comp_xor(ctx) { ctx.save(); ctx.globalCompositeOperation = "xor"; this.drawFunc(ctx); ctx.restore(); }

Also, if you change globalCompositeOperation somewhere after, stage will be drawn by last set composite option.

p.s. in my case there's a lot of manipulations with stage (resizing), layers (hiding, showing, opacity), groups (moving, hiding, showing, opacity) and shapes (all above + resizing), so there's a lot of redrawing...

0xE1 avatar Oct 06 '12 16:10 0xE1

why not just setup the compositeoperation in beforedraw function of layers or groups? stage.draw basicly just loop through layers and call draw functions, each layer loop throught children(Nodes/Groups) and call their draw functions, so layer.beforeDraw(function(){this.getContext().globalCompositeOperation = "xor"}); could maybe fix it

BlackScorp avatar Oct 06 '12 17:10 BlackScorp

I've tried that, and it turned out horribly because all shapes in layer was drawn with this option, not only those i needed. I need to apply compositeoperation to single shape, not whole layer. Like i need to apply a linear gradient on top of complex image with transparency, than there's a way with filters, or composite operation. If you need to make this gradient to move, easiest way to do it, is composite operation, if not only.

0xE1 avatar Oct 06 '12 18:10 0xE1

Sounds like you should set the beforedraw of the shapes to use composition. And then restore in afterdraw.

davonium avatar Oct 06 '12 18:10 davonium

Well yes, except there's no before/afterdraw for shapes. It's only available for layers.

0xE1 avatar Oct 06 '12 18:10 0xE1

@0xE1 actually it's the same thing. To hook into before/after redraw for shapes, just do this:

shape.getLayer().beforeDraw(...);

in order to redraw a shape, it's entire layer has to be redrawn (this is why KineticJS enables you to create user defined layers so that you can group shapes intelligently)

ericdrowell avatar Oct 06 '12 18:10 ericdrowell

Still, not same as i said =) Keyword is "shapes" as you said, i don't need all shapes, i need single particular shape to be affected. if you change glogalCompositeOperation before draw and restore after, it will be applied to ALL shapes within layer. if you define drawFunc, it will be applied to exactly this shape.

0xE1 avatar Oct 06 '12 18:10 0xE1

I think I have a solution, but it includes som coding. Let me try it then I'll be back with the result ;)

davonium avatar Oct 06 '12 19:10 davonium

Ok, sorry if keeping you waiting.

Here's my suggestion: http://jsfiddle.net/davonium/RNLxb/7/

You can send any shape to the makeShapeComposite-function and it will be "updated" to use the compositeOperation you specify.

davonium avatar Oct 06 '12 19:10 davonium

nice one davonium

v1r avatar Oct 06 '12 21:10 v1r

Just realised that this might mess up the buffer layer as well..

davonium avatar Oct 06 '12 23:10 davonium

Is there some news to this problem? This is once bad thing in this awesome framework :]

kanimaru avatar Oct 30 '12 21:10 kanimaru

Thanks for that piece of code, davonium. I'm trying to make this work for a model (a shape group that I need to repeat x times), that is composed in javascript and then cloned and added to the layer. I invoke the compositing like this:

var model = new Kinetic.Group(); var sourceShape= new Kinetic.Rect(...); // This should make a hole over the previous shape var destShape = makeShapeComposite(new Kinetic.Rect(...),"xor");

stage.add(layer); model.add(sourceShape); model.add(destShape); for (var i=0;i<config.numShapes;i++) { ______ layer.add(model.clone()); } layer.draw();

(It's quite simplified, I'll try to explain myself better later with a fiddle) When used like that, the compose operation does not work as expected, just draws every shape in normal composition. But if I add the original composed model to the layer, not a clone of it, it draws the way i'm looking for (I can see the background through the other shape).

I guess cloned models lack a context in the moment of cloning, and then use the default mode for composing. Any help on how could this be achieved?

Sorry for the long comment and thanks in advance

macl avatar Nov 15 '12 08:11 macl