Skip to content
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

How to style the canvas element in React? #24

Closed
danlin604 opened this issue Jun 6, 2018 · 5 comments
Closed

How to style the canvas element in React? #24

danlin604 opened this issue Jun 6, 2018 · 5 comments

Comments

@danlin604
Copy link

danlin604 commented Jun 6, 2018

The canvas is wrapped in unmarked div tags.

<div>
  <canvas>
</div>

Is there a way to apply styles to the canvas so I can manipulate it as a background of another element (position: 'absolute', top: 0, left: 0, zIndex: -1)? So far, my react styling does nothing but push the canvas down.

Additional info, I would like to be able to do this without needing an external css file. So far, I have the and it would be great if I can somehow manipulate it's zIndex, position, and all other typical canvas styles.

@danlin604 danlin604 changed the title How to style the canvas element in React How to style the canvas element in React? Jun 6, 2018
@Alynva
Copy link

Alynva commented Jul 27, 2018

If you put the <P5Wrapper /> into a component that have how to use some selector, you can use the document.querySelector(".MyComponent[ > .otherParentsElements] > div > canvas") like this, to get the <canvas /> element.

@danlin604
Copy link
Author

danlin604 commented Aug 8, 2018

I found a css alternative around this using styled-components:

const ItemContainer = styled.article`
  ...
  canvas {
    flex: 1;
  }
`

I can style the parent container that contains <P5 Wrapper> and target <canvas> child within.

@slothdude
Copy link

I found a css alternative around this using styled-components:

const ItemContainer = styled.article`
  ...
  canvas {
    flex: 1;
  }
`

I can style the parent container that contains <P5 Wrapper> and target <canvas> child within.

I tried this by doing this

in react component

<div className = "background">
     <P5Wrapper sketch={background}/>
</div>

.css file

.background{
  z-index: -1;
}
canvas{
    z-index: -1;
}

But the canvas is still in front. Any ideas?

@Alynva
Copy link

Alynva commented Oct 23, 2018

Edit: I usually use z-index on elements at the same level, not between parent end child, but with you want...

@slothdude may be position: absolute; in canvas.

But why either z-index is defined to -1?

@slothdude
Copy link

position:absolute / position:fixed fixed it, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants