Troubleshooting: Difficulty with svg mask function when implementing an svg image in a react.js environment

I'm attempting to achieve an effect where an image appears above a background image when hovering over it, similar to this example... https://jsfiddle.net/12zqhqod/7/

  <img id="heretic" height="300px" width="300px" src="http://i.imgur.com/0bKGVYB.jpg" />
<div class="pic">
  <svg class="blur" width="100%" height="100%">
    <mask id="mask1">
      <circle cx="-50%" cy="-50%" r="40" fill="white" />
    </mask>
    <image mask="url(#mask1)" id="bird" alt="follow me" xlink:href="http://i.imgur.com/IGKVr8r.png" width="100%" height="100%"></image>
  </svg>
</div>

Here is the corresponding javascript...

 var mask1 = $('#mask1 circle')[0];
$('.pic').mousemove(function(event) {
  event.preventDefault();
  var upX = event.clientX;
  var upY = event.clientY;
  console.log(upX, upY);
  mask1.setAttribute("cy", (upY - 5) + 'px');
  mask1.setAttribute("cx", (upX) + 'px');
});

However, I've been unable to replicate this functionality in react.js and it ends up like this... https://jsfiddle.net/69z2wepo/32320/

<script src="https://facebook.github.io/react/js/jsfiddle-integration-babel.js"></script>

<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>

And here is the react.js code...

var Hello = React.createClass({
  render: function() {
    return <div>
                    <img id="heretic" height="300px" width="300px" src="http://i.imgur.com/0bKGVYB.jpg" />

    <div className="pic">
        <svg className="layers" width="100%" height="100%">
        <mask id="mask1">
            <circle cx="-50%" cy="-50%" r="40" fill="white" />
        </mask>
        <image className="imageTest" mask="url(#mask1)" alt="follow me" xlinkHref="http://i.imgur.com/IGKVr8r.png" width="100%" height="100%"></image>

            </svg>
     </div>
   </div>;
  }
});

ReactDOM.render(
  <Hello name="World" />,
  document.getElementById('container')
);

var mask1 = $('#mask1 circle')[0];
$('.pic').mousemove(function(event) {
  event.preventDefault();
  var upX = event.clientX;
  var upY = event.clientY;
  console.log(upX, upY);
  mask1.setAttribute("cy", (upY - 5) + 'px');
  mask1.setAttribute("cx", (upX) + 'px');
});

I've attempted setting the mask attribute manually using setAttribute in componentDidMount, but then the <image> disappears and fails to display upon hover. It seems that the mask circle remains at (0,0), even though the cx and cy attributes are updating correctly in the browser's dev tools.

Any assistance would be greatly appreciated.

Answer №1

It appears that you overlooked the styles. By simply adding the styles similar to those in your original fiddle:

.pic {
  position: absolute;
  height: 250px;
  top: 0;
}

Check out this functional example.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

React Dropdown Selection - Choose multiple options with advanced features

I am trying to create a select menu in React with additional functionality that I have not been able to locate through my searches. I must be searching for the wrong thing because even after going through the first 50 links on Google, I couldn't find ...

The key used with react-native-router-flux Actions is invalid and causes an error when invoked

The previous code was functioning properly on RN 0.57.4, but in order to publish the app on the play store, I had to upgrade it to version 0.61.4. Below is the main router being used: <Router sceneStyle={styles.allSceneStyle} navigationBarStyle={styles ...

Encountering a blank or 404 error page in React after running npm build - let's troubleshoot where the issue might be

Upon running npm build(react), my index.html displays a blank page. To address this issue, I initially attempted the following steps: Adding the following line in package.json homepage : "./" or "." or "absolute file PATH" ...

Iframe overlay feature functioning on Chrome but not on IE11

I have a Document viewer with a .less file containing the following styling: div.document-previewer-container { //height: 400px; //width: 300px; position: absolute; top: 0; bottom: 0; left: 0; right: 0; //padding: 5px 2px; > div.document-preview { h ...

Step-by-step guide on positioning mid and bottom text using Bootstrap grid system

After searching and trying various solutions, I am still unable to get the desired output. My goal is to use grids in Bootstrap to set the footer layout with the class "link-footer" positioned at the middle height of the div and the class "footer-copyright ...

The JsHelper::domReady function is malfunctioning!

While developing a website with CakePHP, I have encountered an issue... Utilizing the latest version of jQuery... and using JsHelper to accomplish something like this: $this->Js->domReady ( $this->Js->alert ('Page loaded !') ); ...

Adjust the DIV dimensions to fit the background image perfectly

My question involves a DIV element with a specific style applied to it. .vplayer-container .logo { position: absolute; bottom: 50px; right: 10px; background: url(../img/logo.png) no-repeat; border: 1px solid #000000; max-width: 50px; max-height: 50 ...

Attention: Two children with identical keys were found, ""

In my current project, I am utilizing ReactJS, NextJS 14, Tailwind CSS, and Framer Motion. When working with React, it's essential to assign unique keys to each repeated child component. In my case, this requirement carries over to NextJS as well. To ...

Encountering a Next.js TypeScript Build Error related to the Type 'OmitWithTag<InputFormProps, keyof PageProps, "default">' issue

`I am currently working on a project in Next Js using TypeScript. During the build process with npm run build, I encountered the following errors in the terminal: # Type 'OmitWithTag<InputFormProps, keyof PageProps, "default">' do ...

What is the most effective method for integrating a hosted React application into a website that is not built using React?

I currently have a complete React application hosted on AWS that I want to embed into another non-React site. Right now, I have it embedded using an iframe and it's working well. <!doctype html> <html lang="en> <head> <meta c ...

The close button for Fancybox gets cropped out when Foundation 3 and CSS border-box are used together

When the box-sizing is set to border-box, it can sometimes cut off the "close" button. This issue seems to be more prevalent when using Foundation 3 by Zurb. The snippet of code that may be causing conflicts with fancybox is as follows: * { box-sizin ...

Storing progress of a multi-step form in Laravel 5.6 on both the database and local drive

I've been working on implementing a multi-step form using JQuery Steps in Laravel 5.6. Instead of just saving all the data at the end, I want to save each step of the form in the MySQL database and on the user's local drive. This way, I can track ...

The Carousel is covering up my Mega-Menu and it is not visible on top

I'm facing an issue where my mega menu is hidden behind the carousel, and I need it to show on top. You can see how it looks before adding the carousel and after adding the carousel from Bootstrap. Here are the links to the Mega Menu CSS and Mega menu ...

What is the best way to determine if a jQuery element has a previous or next sibling element?

Let's say I have a list in an <ul>: <ul> <li></li> <li></li> <li class="test"></li> </ul> How can I check if the element with class .test has a next sibling using jQuery? if ($("li.t ...

Step-by-step guide to creating a border around text with a number included in between the lines

Is there a way to replicate the appearance of the image shown below using CSS, Bootstrap, or other client-side methods? ...

Tips for swapping a component with another component in React.js without the need for a page refresh

class Navigation extends Component { constructor(props) { super(props); this.state = { width: window.innerWidth, } } updateWidth = () => { if (this.state.width > 700) { this.setStat ...

The essence of a character undergoes a complete transformation once it is presented in

There seems to be an issue with the character "т", ascii code: 209 130 When this particular character is put in italics, its appearance changes drastically... Check out the т character in italics (take a look at the source code - it's fascinating) ...

Using @font-face to include several files for different font-weight variations

I have a collection of font files that I want to assign to specific font weights. @font-face { font-family: "custom-font"; src: url("font300.eot"); src: url("font300.eot?#iefix") format("embedded-opentype"), url("font300.woff2") format ...

Ensuring props are resilient even when an incorrect type is provided during testing

In my development using the MERN stack and Redux, I encountered an issue while testing props on one of my components. Despite defining all types and running tests, they still pass even with incorrect data entered. I have tried specifying the shape of each ...

Placing three div elements within a container, one of which has a height set to auto

I need help arranging 3 divs side by side using float:left. The height for two of the div's (child1 and child3) is fixed, but there is no set height for child2 - I would like child2 to have the same height as the container div. <div id="container ...