简体   繁体   English

图层是在JavaFX canvas上合成图形的唯一方法吗?

[英]Are layers the only way to composite graphics on JavaFX canvas?

I am working on a gui in JavaFX which needs to composite a large number of objects (often using alpha masks and similar) on the canvas. 我正在JavaFX中的gui上工作,该gui需要在画布上合成大量对象 (通常使用alpha蒙版等)。

For comparison on the HTML5 canvas this can easily be done by the drawImage function with the help of a temporary canvas object outside the DOM structure. 为了在HTML5画布上进行比较,可以使用drawImage函数在DOM结构外部的临时画布对象的帮助下轻松完成此操作。 For example to draw an image on the canvas with an alpha mask, I first draw the image on the temporary canvas, draw (ie blit) the mask over it using globalCompositeOperation = "destination-in" , then draw the temporary canvas on the original one using composite mode source-over . 例如,要在具有Alpha蒙版的画布上绘制图像,我首先在临时画布上绘制图像,然后使用globalCompositeOperation = "destination-in"在其上绘制(即blit)蒙版,然后在原始画布上绘制临时画布一种使用复合模式source-over The temporary canvas can be re-used for each such operation. 临时画布可以为每个这样的操作重新使用。 Easy as pie. 易如反掌。

However, from what I can see so far the recommended way of doing this in JavaFX is the use of grouped layers , ie multiple overlayed canvas nodes which never get "flattened". 但是,到目前为止,从我可以看到的角度来看,在JavaFX中推荐的完成此操作的方法是使用分组图层 ,即,多个永远不会“拉平”的覆盖画布节点。

I could have done it like this in HTML5 too but in my most recent project this would have resulted in dozens or hundreds of visible layers which is obviously extremely silly. 我也可以在HTML5中这样做,但是在我最近的项目中,这会导致数十或数百个可见层 ,这显然是非常愚蠢的。 My approach gave me excellent performance. 我的方法给了我出色的表现。

That being said, is there a reasonable way to do the same thing on the JavaFX canvas? 话虽这么说,是否有合理的方法在JavaFX画布上执行相同的操作? I consider manually performing pixel-by-pixel copying to be a clunky last-resort thing. 我认为手动执行逐像素复制是一件麻烦的事。

What am I missing? 我想念什么? Am I thinking about JavaFX in a wrong way? 我是否以错误的方式考虑JavaFX?

I have done this before on JavaFx and Android I didn't know they do so on HTML 5 so anyway 我之前在JavaFx和Android上做到了,但我不知道他们在HTML 5上做到了,所以无论如何
you can do the same as you do on HTML 5 you can create what is called mainCanvas that canvas contains the finished version of another one let's say tempCanvas in the temp canvas you draw what ever you want and apply the masks you want too then you take a snapshot of the canvas ( as Canvas is a Node you can use this code to take a snap shot of it) 您可以像在HTML 5上一样进行操作,可以创建一个称为mainCanvas画布,该画布包含另一个画布的完成版本。假设tempCanvas在临时画布中绘制了您想要的任何东西,然后也应用了想要的蒙版,然后画布的快照(由于CanvasNode您可以使用此代码对其进行快照)

WritableImage writableImage = new WritableImage(mainCanvas.getWidth(), mainCanvas.getHeight());
tempCanvas.snapshot(null, writableImage);

GraphicsContext context = mainCanvas.getGraphicsContext2D();
context.drawImage(writableImage,mainCanvas.getWidth(), mainCanvas.getHeight());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM