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

What can trigger an endless re-render loop in a class component?

Recently delved into React and trying to grasp the concept behind the infinite render loop in the code I'm currently working on. Provided below is an example of the problematic code snippet that triggers the issue. The error message displayed reads: " ...

The functionality of getStaticProps is not operational when being used within the components

Following a recent issue I encountered, the question arises: typeError: Cannot read property 'map' of undefined. I attempted to utilize a component with getStaticProps in my pages/index.js, but alas, it doesn't function properly in conjunct ...

Unable to utilize a custom function within JQuery

I'm facing an issue with using the function I created. The codes are provided below and I keep encountering a "not a function error." var adjustTransparency = function () { //defining the function if ($(this).css('opacity&apo ...

Having difficulty passing an object in a GraphQL mutation

I've been trying to figure out how to pass an array of objects in a GraphQL mutation from the React JS side. I attempted passing the object in React but encountered an error stating that it's not the same type. [input type][1] [from api side][2] ...

Is there a way to align my anchor tag so that my link appears perfectly centered both vertically and horizontally on the page?

Despite my efforts to find a solution, I am stuck with this issue. Here is the code I have been working on: <a href='/Customers' class='centre'>Start</a> I attempted to wrap it in a div tag and also wanted to add a small gr ...

Guide to setting up an npm package on a self-contained server without internet access

I'm facing an issue while trying to set up the react-cookie dependency in my react project. The project is being run on a self-contained offline network without any internet access. I have packaged the dependency into a tarball file and uploaded it to ...

The React syntax is malfunctioning

componentDidMount() { const restaurants = Restaurant.all() restaurants.then( rests => { this.setState({ restaurants: rests }) }) } render() { const { restauran ...

Selecting a specific Dropdown Menu from a list of many by opening only that one

I have been experimenting with Material UI dropdown menus and encountered an issue when trying to implement multiple dropdowns within the same menu. Despite following the documentation, I couldn't find a suitable example for this scenario. While I ma ...

Iframe not functioning properly on Internet Explorer versions 8-11 when using WordPress

I'm having trouble getting my WordPress video to display when using an iframe. When I try to view it in Internet Explorer, the video automatically loads in Windows Media Player instead of playing through the iframe. The video files are local mp4s and ...

Functionality of Ajax: Data fields containing numerous values

I'm confused about the data fields in the ajax function. Typically, the syntax for an ajax function looks something like this: $.ajax({ url: "/aaa/bbb/ccc", method: "SomeMethod", data: someData, success: function (res ...

In a local setting, the KendoUI chips are displaying without their borders

I recently tested out the code from kendo on StackBlitz and it worked perfectly: https://i.stack.imgur.com/Nrp2A.png However, when running the same code on my localhost, all the styling for the chip is missing: https://i.stack.imgur.com/1pUH9.png The c ...

What is the reason for index.html requesting the css or js modules as if they were directories when loading?

I am currently developing a server using expressjs: 'use strict'; var express = require('express'); var logger = require('morgan'); var path = require('path'); var bodyParser = require('body-parser'); va ...

React Application for Multiple Tenants

I'm currently developing a React multi tenant application and am looking for the most effective method to generate and utilize tenant-specific variables. By running my app with: npm run start tenant1 I can access tenant1 within Webpack using the fo ...

Sort table rows by checkbox value in javascript

Is there a way to use javascript or JQuery to group the rows in this table by Office or Age based on the checkbox option selected? For example, if the Office checkbox is checked, the table rows should be grouped by Office. If the Age checkbox is checked, ...

React JS does not allow TextField and Select to change

I am relatively new to full stack development and I am currently working on a project to enhance my understanding of frontend development with React JS. While working on this project, I have been using Redux without any issues so far. However, I am facing ...

Refreshing a React form

this.state = { name: "", arr: [], story: "" }; add(e) { e.preventDefault(); this.setState({ story: e.target.value }); this.state.arr.push(this.state.story); this.form.reset(); } <form action=""> <input onChange={this.b} type="t ...

Space within a series of photographs

Looking for some assistance: I am trying to maintain a consistent margin of 10px between posts and between photos in a photoset. However, when a post is a photoset, the margins of the bottom photo and the overall post add up to 20px. I want to retain the ...

Hide the load more button when there is no additional content available for Ajax loading

Currently, I am working on a WordPress query for a custom post type and would like to implement a "load more" button using Ajax to allow users to load additional articles. To handle the scenario where there are no more posts to load, I have included an "el ...

Tips on achieving horizontal scrolling for div overflow instead of wrapping onto a new line

It's a frequent occurrence in mobile apps to have a navbar with selectable text options that overflow horizontally without breaking into a new line. Instead, users must scroll horizontally to access all the options available. Is there a way for me to ...

React and Redux: Error Alert - A change in state was detected during dispatch operations

While using 'controlled' components (utilizing setState() within the component), I am encountering an intermittent error when trying to save form data. The UserForm's onSave function calls back to the saveUser method in the component code pr ...