jonobr1 / two.js

A renderer agnostic two-dimensional drawing api for the web.
https://two.js.org
MIT License
8.29k stars 454 forks source link

Duplicate Element ID #639

Open dulimarta opened 2 years ago

dulimarta commented 2 years ago

TwoJS Scene and Groups share the same ID

I noticed that TwoJS scene (which is a Group) and the first user-created group share the same ID: two-0. Here is the sample code I used:

import Two from "two.js"; // Version 0.8.9
import { Group } from "two.js/src/group";
import { Circle } from "two.js/src/shapes/circle";

const two = new Two({
  width: 512,
  height: 512,
  autostart: true,
});

two.scene.matrix.manual = true;
two.scene.matrix.translate(256, 256);
two.appendTo(document.body);
let layers: Group[] = [];

for (let k = 0; k < 3; k++) {
  const g = new Group();
  g.addTo(two.scene);
  layers.push(g);
}
const c1 = new Circle(0, 0, 128);
c1.stroke = "red";
c1.linewidth = 3.0;
c1.addTo(layers[0]);

Screenshots

The following is the screenshot from FireFox inspector panel:

Screenshot 2022-06-02 160744

I'm not submitting this issue as a bug report, but it may potentially becomes a bug in the future.

jonobr1 commented 2 years ago

Thanks for reporting. That looks like a bug. Which version of Two.js is this?

Nevermind, I see the version! I'll take a look at this.

jonobr1 commented 2 years ago

Oh, if you're going to import the individual modules instead of the project as a whole then you need to get the Two module as well. Change your import statement to this:

import Two from 'two.js/src/two';

Or don't import group and circle and access them off of the Two object like so:

import Two from "two.js"; // Version 0.8.9

const two = new Two({
  width: 512,
  height: 512,
  autostart: true,
});

two.scene.matrix.manual = true;
two.scene.matrix.translate(256, 256);
two.appendTo(document.body);
let layers: Two.Group[] = [];

for (let k = 0; k < 3; k++) {
  const g = new Group();
  g.addTo(two.scene);
  layers.push(g);
}
const c1 = new Two.Circle(0, 0, 128);
c1.stroke = "red";
c1.linewidth = 3.0;
c1.addTo(layers[0]);
dulimarta commented 2 years ago

Thank you for your prompt response. My project is in TypeScript and using Two.Group as a type like in the second line below

import Two from "two.js";

let layers: Two.Group[] = []; // Compiles with error
const g = new Two.Group(); // Compiles OK  

produces the following compiler error

Namespace '"two.js"' has no exported member 'Group'.
jonobr1 commented 2 years ago

I'm not well versed in Typescript, but I think you need to do this:

import Two from 'two.js';

const layer = new Two.Group();
const layers: typeof Two.Group[] = [layer];