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

CSS2DRenderer: Introduce CSS2DObject.center #25673

Merged
merged 3 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions examples/css2d_label.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
camera.position.set( 10, 5, 20 );
camera.layers.enableAll();
camera.layers.toggle( 1 );

scene = new THREE.Scene();

const dirLight = new THREE.DirectionalLight( 0xffffff );
const dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
dirLight.position.set( 0, 0, 1 );
dirLight.layers.enableAll();
scene.add( dirLight );
Expand Down Expand Up @@ -128,36 +127,44 @@
const earthDiv = document.createElement( 'div' );
earthDiv.className = 'label';
earthDiv.textContent = 'Earth';
earthDiv.style.marginTop = '-1em';
earthDiv.style.backgroundColor = 'transparent';

const earthLabel = new CSS2DObject( earthDiv );
earthLabel.position.set( 0, EARTH_RADIUS, 0 );
earthLabel.position.set( 1.5 * EARTH_RADIUS, 0, 0 );
earthLabel.center.set( 0, 1 );
earth.add( earthLabel );
earthLabel.layers.set( 0 );

const earthMassDiv = document.createElement( 'div' );
earthMassDiv.className = 'label';
earthMassDiv.textContent = '5.97237e24 kg';
earthMassDiv.style.marginTop = '-1em';
earthMassDiv.style.backgroundColor = 'transparent';

const earthMassLabel = new CSS2DObject( earthMassDiv );
earthMassLabel.position.set( 0, - 2 * EARTH_RADIUS, 0 );
earthMassLabel.position.set( 1.5 * EARTH_RADIUS, 0, 0 );
earthMassLabel.center.set( 0, 0 );
earth.add( earthMassLabel );
earthMassLabel.layers.set( 1 );

const moonDiv = document.createElement( 'div' );
moonDiv.className = 'label';
moonDiv.textContent = 'Moon';
moonDiv.style.marginTop = '-1em';
moonDiv.style.backgroundColor = 'transparent';

const moonLabel = new CSS2DObject( moonDiv );
moonLabel.position.set( 0, MOON_RADIUS, 0 );
moonLabel.position.set( 1.5 * MOON_RADIUS, 0, 0 );
moonLabel.center.set( 0, 1 );
moon.add( moonLabel );
moonLabel.layers.set( 0 );

const moonMassDiv = document.createElement( 'div' );
moonMassDiv.className = 'label';
moonMassDiv.textContent = '7.342e22 kg';
moonMassDiv.style.marginTop = '-1em';
moonMassDiv.style.backgroundColor = 'transparent';

const moonMassLabel = new CSS2DObject( moonMassDiv );
moonMassLabel.position.set( 0, - 2 * MOON_RADIUS, 0 );
moonMassLabel.position.set( 1.5 * MOON_RADIUS, 0, 0 );
moonMassLabel.center.set( 0, 0 );
moon.add( moonMassLabel );
moonMassLabel.layers.set( 1 );

Expand Down Expand Up @@ -218,11 +225,15 @@

gui = new GUI();

gui.title( 'Camera Layers' );

gui.add( layers, 'Toggle Name' );
gui.add( layers, 'Toggle Mass' );
gui.add( layers, 'Enable All' );
gui.add( layers, 'Disable All' );

gui.open();

}

</script>
Expand Down
7 changes: 6 additions & 1 deletion examples/jsm/renderers/CSS2DRenderer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Matrix4,
Object3D,
Vector2,
Vector3
} from 'three';

Expand All @@ -19,6 +20,8 @@ class CSS2DObject extends Object3D {

this.element.setAttribute( 'draggable', false );

this.center = new Vector2( 0.5, 0.5 ); // ( 0, 0 ) is the lower left; ( 1, 1 ) is the top right

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

this.traverse( function ( object ) {
Expand All @@ -41,6 +44,8 @@ class CSS2DObject extends Object3D {

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

this.center = source.center;

return this;

}
Expand Down Expand Up @@ -125,7 +130,7 @@ class CSS2DRenderer {

const element = object.element;

element.style.transform = 'translate(-50%,-50%) translate(' + ( _vector.x * _widthHalf + _widthHalf ) + 'px,' + ( - _vector.y * _heightHalf + _heightHalf ) + 'px)';
element.style.transform = 'translate(' + ( - 100 * object.center.x ) + '%,' + ( - 100 * object.center.y ) + '%)' + 'translate(' + ( _vector.x * _widthHalf + _widthHalf ) + 'px,' + ( - _vector.y * _heightHalf + _heightHalf ) + 'px)';

if ( element.parentNode !== domElement ) {

Expand Down
Binary file modified examples/screenshots/css2d_label.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.