#How to get and store 2D context of Canvas in react-three-fiber?

20 messages · Page 1 of 1 (latest)

karmic junco
#

A canvas can't have both 2d and 3d contexts. If you want to try and draw 2d on top of a 3d context (or vice versa) you have to have two canvases and have alpha on the top one.

merry sand
#

Thanks for your reply. I am using only one canvas now. I want to get 2d context of it.

karmic junco
#

I believe that r3f will automatically grab a 3d context for the canvas, so you can't get a 2d one.

merry sand
#

It makes sense. So what can I do?

karmic junco
#

Well, for one you could certainly implement the ribbon drawing in 3d; it might in some ways be even easier than in 2d.

merry sand
#

Let me try 🙂 thanks.

#

But how to get context concisely?

#

I tried getContext("3d") in RibbonDraw but failed.

#

Would you mind if I DM to you?

karmic junco
#

I'm not familiar enough with R3F, unfortunately, to be much specific help here. :/

oak barn
#

can you clarify the objective more?

what do you mean 2d "context"? the visual scene context i assume, not the context of a project itself?

we manage a lot of both in ot sketch - so if i can be of help lmk

merry sand
#

But I want to convert it to React and Typescript. In vue, creating canvas and getting context is done really easy in <script> tag. Here is the code.
this._canvas = document.createElement("canvas");
this._context = this._canvas.getContext("2d");

#

In Typescript, I created Ribbons component using react-three-fiber as you can see in the original post. To draw ribbon sections, I have to get the 2D context of canvas, I suppose.

#

To make a long story short, how can I draw 2D lines on react-three-fiber context?

oak barn
#

like this?

#

i still don't really have my head around what you're trying to do...especially this part

I created Canvas from react-three-fiber and tried to get 2D context of the canvas in child component - RibbonDraw.

i don't understand what you mean by 2d and RibbonDraw - is it a ribbon being drawn completely away from r3f?