Skip to content

Simple example to apply a CanvasTexture #2288

Answered by mwmwmw
sirPixieJerry asked this question in Q&A
Discussion options

You must be logged in to vote

You can either create the canvas imperatively (document.createElement) or use a reference to a canvas you've added to the dom (OP's code).

Once you have the canvas, you apply it to your geometry using <canvasMaterial />

In this example, I'm drawing the canvas in useFrame, but you can draw the canvas anywhere. Pass the reference to the canvas in using the image property.

https://codesandbox.io/s/canvas-animated-texture-10p7ws

        <meshBasicMaterial>
          <canvasTexture
            ref={textureRef} <- if you're animating the canvas, you'll need to set needsUpdate to true 
            attach="map"
            image={canvasRef.current} 
          />
        </meshBasicMaterial>

Replies: 3 comments 5 replies

Comment options

You must be logged in to vote
2 replies
@abenjaminov
Comment options

@latelier22
Comment options

Comment options

You must be logged in to vote
2 replies
@cedricwaxwing
Comment options

@carolinex
Comment options

Answer selected by sirPixieJerry
Comment options

You must be logged in to vote
1 reply
@Muhammad-Athar
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
7 participants