Marinus Klasen

Marinus Klasen

  • Development
  • Consultancy
  • Developer Resources
  • Blog
  • Contact

August 24, 2023

A-Frame: Change an objects color on hover

Marinus Klasen

A-Frame is quite fun to work with, but it takes a while to get into.. As I’m doing a project in A-Frame at the moment, you’ll probably see some more insights on this library on my blog in the next couple of weeks.

Special thanks to Diarmid Mackenzie for helping me out in the A-Frame Slack Channel.

In this case, I’ve used an object file (.obj) and a material file (.mtl) and loaded it in A-Frame using the following code:

<a-asset-item id="arrow" src="/assets/objects/arrow.obj">
<a-asset-item id="arrow-mtl" src="/assets/objects/arrow.mtl">
<a-entity mouse-over class="link" obj-model="obj: #arrow; mtl:#arrow-mtl;"></a-entity>

Now, in order to see the object change it’s color when hovering over it, we need to add an A-Frame component that hooks into the ThreeJS object to then change the material color, as you see below:

AFRAME.registerComponent('mouse-over', {
    init() {
        this.el.addEventListener('model-loaded', () => {
            this.el.addEventListener('mouseenter', this.mouseenter);
            this.el.addEventListener('mouseleave', this.mouseleave);
        })
    },
    mouseenter(evt) {
        const model = evt.target.getObject3D('mesh');
        model.traverse((obj) => {
            if (!obj.material) return
            obj.material.color.setHex(0xFFFFFF)
        })
    },
    mouseleave(evt) {
        const model = evt.target.getObject3D('mesh');
        model.traverse((obj) => {
            if (!obj.material) return
            obj.material.color.setHex(0x333333)
        })
    },
});

That’s it! Light and breezy ๐Ÿ™‚

Happy developing!

About Marinus Klasen

Marinus has been working in software/web development for more than a decade. Since 2020 his attention shifted on sharing knowledge and developing products and tools for sharing knowledge.

Marinus Klasen on Twitter

Looking for a WordPress expert?
Hire top notch developers on Codeable!

Consult an expert

Comments

  1. Zhi Ji Wang says

    January 26, 2024 at 1:19 pm

    Hello Marinus.

    How to adjust sensitivity(mouse dragging/rotation) speed in latest Aframe (over 1.4.2), currently using Aframe 1.5.0?
    I want as same as this effect from https://jsfiddle.net/8gfmL1n1/, but it works at very old Aframe version.

    Thanks in advance!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get in touch

Contact me using the details below

Marinus Klasen

Wilhelmina van Pruisenweg 35, 2595 AN Den Haag

marinus@mklasen.com
twitter.com/marinusklasen
linkedin.com/in/marinusklasen

  • GitHub
  • LinkedIn
  • Twitter

Have you read?

  • Elementor Widget UsageMarch 27, 2025
  • Programmatically showing popups with ElementorMarch 25, 2025
  • The Elementor sanitize_settings errorSeptember 12, 2024
  • Announcing.. Wooping Shop Health!June 13, 2024
  • Swiper setup when using wp-scriptsMarch 14, 2024

Copyright © 2025 ยท Marinus Klasen | Webdesign by Team Rood