btco / qx82

Tiny Javascript engine for retro games
MIT License
421 stars 19 forks source link

Possibility to append the canvas to a custom element #9

Closed csarnataro closed 6 months ago

csarnataro commented 6 months ago

Hello, thank you for this awesome library, I really like it.

I've used it in a demo a few weeks ago (source code)

image

Basically, I built a toy voting machine with an Arduino connected to the Internet. Then I showed the results of the votes in a boxing-match-like game. The fight was JS vs TS 😄

The game had to be displayed in a specific component in an Electron app. For this reason, instead of appending the canvas to the document.body, I needed to append it to a specific element in the DOM, so I patched my local version of the library to achieve this.

But if you think this feature can be interesting for other people, I can create a PR in the next few days.

My idea was to add an entry in the config file, like:

export const CONFIG = {
  CONTAINER_ID = 'my-container',
  [...]

and then update internal/main.js like this:

  const containerId = CONFIG.CONTAINER_ID
  const container = document.getElementById(containerId) || document.body
  container.appendChild(realCanvas);

What do you think?

btco commented 6 months ago

Ah yes, this is a great idea! I tried to do it but it was tricky because of the sizing logic, but if you have a PR that makes this work, I'd love to see it!

csarnataro commented 6 months ago

I haven't thought specifically about the sizing logic, actually. What I did in my demo was to use simple CSS to give the desired width and height. Is there something specific to that logic I should care of, in your opinion?

csarnataro commented 6 months ago

I think this issue is duplicate of https://github.com/btco/qx82/issues/4 right? When I created #9 I didn't notice there was another issue regarding using a custom element, sorry. Should we close this one maybe?

btco commented 6 months ago

True, it's a duplicate of #4! Will close it.

btco commented 6 months ago

Duplicate of #4

btco commented 6 months ago

There is now also a way to specify: