question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

THREE.CSS2DObject needs a clone method

See original GitHub issue

First of all THREE.CSS2DObject is an amazing component, I’ve been using it intensively for labeling with rich HTML labels and KPI’s my 3D objects. I normally include them into a Group with the object they label for a relative position and move them together when animated.

I’m not sure if many devs are doing a so intensive use of this component, but the issue comes when I have many objects that use the same 3D model, and I try to clone the parent Group to optimize performance. The clone method of the Group recursively will call clone for all the children, including the CSS2Object which lacks of a clone method.

I have implemented it easily, it’s not a big deal, but I think this method should be implemented in the source of three.js main branch.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Mugen87commented, Jun 4, 2020

BTW: It’s not really the clone method that needs a change. Instead, CSS2DObject.copy() needs to be implemented. And it’s important to ensure instances of CSS2DObject can be created without any parameters (which is currently not the case since element is mandatory).

This code seems to work:

var CSS2DObject = function ( element ) {

	Object3D.call( this );

	this.element = element || document.createElement( 'div' );

	this.element.style.position = 'absolute';

	this.addEventListener( 'removed', function () {

		this.traverse( function ( object ) {

			if ( object.element instanceof Element && object.element.parentNode !== null ) {

				object.element.parentNode.removeChild( object.element );

			}

		} );

	} );

};

CSS2DObject.prototype = Object.assign( Object.create( Object3D.prototype ), {

	constructor: CSS2DObject,

	copy: function ( source, recursive ) {

		Object3D.prototype.copy.call( this, source, recursive );

		this.element = source.element.cloneNode( true );

		return this;

	}

} );
1reaction
jscastro76commented, Jun 9, 2020

BTW: It’s not really the clone method that needs a change. Instead, CSS2DObject.copy() needs to be implemented. And it’s important to ensure instances of CSS2DObject can be created without any parameters (which is currently not the case since element is mandatory).

This code seems to work:

Thanks @Mugen87 for this quick implementation, this method is even simpler than mine. I’ve just updated my code and it runs perfectly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object3D#clone – three.js docs
This is the base class for most objects in three.js and provides a set of properties and methods for manipulating objects in 3D...
Read more >
Using the THREE.js CSS2DRenderer to position HTML ...
I've come across the same problem. Lines 131 or 135 in the CSS2DRenderer the the transform to translate(-50%,-50%) to center the html element ......
Read more >
[4K] Globe With Threejs and React Adding CSS2DObject and ...
Hey Everyone!I hope you all are doing well and staying safe. In this video, I made the following changes:- Added feature that creates...
Read more >
Copy a mesh in threejs | Dustin John Pfister at github pages
This is a post on the clone method of a THREE. ... To copy a mesh in threejs all I need to do...
Read more >
Three.js Aligning HTML Elements to 3D
The benefits to this approach is you can use all of HTML. Your HTML can have multiple ... We need to provide an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found