Adjust the SVG clipping mask size when the cursor hovers over it

I am looking for a way to display images with a mask effect. I have tried using css clip-path, but due to poor browser support, I want to switch to an svg method for clipping. My main question is how can I resize the mask when the user hovers over it?

Something similar to what is shown here: https://i.sstatic.net/s13zO.png

This is the code I am currently using:

<svg>
 <defs>
    <!--defines the shape to use for clipping-->
    <circle id="circle" cx="100" cy="100" r="100" />
  </defs>
  <clipPath id="clip">
    <!--creates the clipping mask from the shape-->
    <use xlink:href="#circle" overflow="visible"></use>
  </clipPath>
  <!--group containing the image with clipping applied-->
  <g clip-path="url(#clip)">
    <image overflow="visible" xlink:href="model_detail.jpg"></image>
  </g>
</svg

Answer №1

If you need to resize both the circle and the clipped image in your scenario, one way to achieve this is by scaling the svg element on hover. Here's an example of how you can do it:

svg {
  display: block;
  position: absolute;
  margin: auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  transform: scale(1);
  transition: transform 0.5s;
}
svg:hover {
  transform: scale(1.5);
}
<svg viewBox="0 0 200 200" width="200">
 <defs>
    <!--defines the shape to use for clipping-->
    <circle id="circle" cx="100" cy="100" r="100" />
  </defs>
  <clipPath id="clip">
    <!--creates the clipping mask from the shape-->
    <use xlink:href="#circle" overflow="visible"></use>
  </clipPath>
  <!--group containing the image with clipping applied-->
  <g clip-path="url(#clip)">
    <image overflow="visible" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/darwin300.jpg"></image>
  </g>
</svg>

It's important to note that I've included a viewBox and a width attribute in the svg for proper sizing. Without these attributes, the svg element defaults to 300px/150px size, leading to cutoff parts of the circle falling outside the canvas.

UPDATE

The OP has raised a question about scaling only the mask without affecting the image. In response, here's an approach using transitions to scale the circle independently when hovering over the svg element:

#c {transform: scale(1); transition: transform 0.5s;}

svg:hover #c {transform: scale(1.5);}

Here's a functional example of how this can be implemented:

svg {
  border: 1px solid;
  display: block;
  position: absolute;
  margin: auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

#c {transform: scale(1);transition: transform 0.5s;}

svg:hover #c {transform: scale(1.5);}
<svg viewBox="-150 -150 300 300" width="200">
 <defs>
    <clipPath id="clip">
      <!--creates the clipping mask from the shape-->
      <circle id="c" r="100" />
    </clipPath>
  </defs>
  <image clip-path="url(#clip)" id="img" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/darwin300.jpg" x="-150" y="-150" width="300" height="300"></image> 
</svg>

Answer №2

If you want to achieve this effect with JavaScript, you can simply add an event listener for onMouseOver on a specific class. On the other hand, if CSS is preferred, you can create an animation like the one shown below.


.zoom {
  padding: 50px;
  background-color: green;
  transition: transform .2s; /* Animation */
  width: 200px;
  height: 200px;
  margin: 0 auto;
}

.zoom:hover {
  transform: scale(1.5); /* (150% zoom - Note: if the zoom is too large, it will go outside of the viewport) */
}

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

Is there a way for me to choose every element excluding those contained within a specific div?

Check out my code snippet: <div class="car"> <div class="make">NISSAN</div> <div class="model">MICRA</div> </div> <div class="discontinued"> <div class="car"> <div class="make">FOR ...

Expand your view to avoid overflow

Take a look at this image When viewing with standard resolution (1386 x 788), everything looks fine. However, when attempting to zoom out in Internet Explorer, things get messy: Why does this happen and how can it be fixed? Here's the code causing t ...

Locate the textbox that pertains to the element currently in focus

My HTML is structured as follows: <tr> <td colspan="2" class="borderBottomCell"> <div class="benefitInfo" style="WIDTH: 99%! important;"> <asp:DropDownList ...

Gone Without a Trace: Where Did the WP Admin

I am brand new to creating WordPress themes and I have just started working on my very first one. I have managed to get some content up and running, and the home page is functioning properly. However, when I log into WordPress and view the site, I noticed ...

Guide to generating customized CSS styles on-the-fly in Vue (similar to Angular's dynamic styling capabilities)

When working with Angular, we have the capability to dynamically set CSS properties. For example: <style ng-if="color"> .theme-color { color: {{color}}; } .theme-background-color { background-color: {{color}}; } .theme-border-color { border-color: { ...

I'm missing out on that gray background

input your code hereI am working with the following html/php code snippet: <div class="footerbottom"> <span class="footermenuitem"> <span class="footermenutitle">PRODUCTS</span> <?php $menu = menu_navigation_links('menu-pro ...

An error occurred when executing Selenium through Python due to an invalid selector issue

While attempting to save a document, I encountered an issue. Using Firefox and the Firefox IDE, the following target worked perfectly: css=div.ui-dialog-buttonset button:contains('Yes, ') However, when trying to implement it in Python with the ...

Uploading files along with additional data fields

I am facing a dilemma with my HTML form that has enctype="multipart/form-data" attribute. I have a DTO class with all the necessary setters and getters. Since I am submitting the form as multipart, I cannot use getParameter() method directly. In my servlet ...

Is there a way to dynamically refresh the options in a select dropdown using jQuery?

I'm facing an issue with updating the options in a select element. Here is how my current select element looks: <select id="productId"> </select> Below is the jQuery code I am using: ... if (data.success) { var items = []; $.ea ...

Step-by-step guide to importing a directory in ReactJS

Is there a way to create an input that can upload an entire directory with all its folders intact? I attempted the following code: <input ref={wrapperRef} id="file-upload" name="file-upload" type="file" ...

My JavaScript/jQuery code isn't functioning properly - is there a restriction on the number of api calls that can be made on

Below is the code I have implemented from jquery ui tabs: <script> $(function(){ // Tabs $('#tabs1').tabs(); $('#tabs2').tabs(); $('#tabs3').tabs(); //hover states on the sta ...

Guidance on incorporating static files with Spring MVC and Thymeleaf

I'm seeking guidance on how to properly incorporate static files such as CSS and images in my Spring MVC application using Thymeleaf. Despite researching extensively on this topic, I have not found a solution that works for me. Based on the recommenda ...

Ways to create a back-and-forth transition across a sequence

Is it possible to create an animation that changes the width of an element once, then reverts back after a pause? I want this transition to occur over a three-second interval followed by a two-second delay. How can I achieve this? Below is the code I have ...

Is there a way to dynamically update the content of <li> tag with values from an array every x seconds

I am trying to create a li list using jQuery that will update every 2 seconds with new content, similar to game news updates. However, I'm encountering an issue during the first cycle where it skips the second item in the array. The subsequent cycles ...

Customize the MUI Slider Component with unique colored thumbs

Check out MUI's slider component at this link. I'm using it to let users select a value range with two thumbs. You can find the documentation for MUI's multi-thumb slider here. If you want to customize the style of the slider thumb, visit ...

Maintain your personalized cursor even when you click on a link with the power of

My favorite trick for changing my cursor to a custom image involves using this specific css code. * { cursor: url(../images/cursor.png), auto; } But I've noticed that whenever I click on a link or button, the cursor reverts back to its default l ...

Having trouble with jQuery on my webpage

Looking for assistance with a technical issue I'm facing... I am currently working with AngularJS and have integrated jQuery into my project. However, I've encountered an issue where the jQuery code is not functioning as expected on the HTML pag ...

How about having one row with five divs of equal width, each with a margin-right of 20px?

Is there a way to achieve 5 floating divs on one row with a 20px margin-right between each, except for the last-child? This is the desired structure: <div class="f-item pull-left">1</div> <div class="f-item pull-left">2</div> < ...

The position of the HTML class shifts when the window is resized

I am in the process of creating a basic website layout that resembles this design. However, I am facing an issue where the webpage does not adapt well to different window sizes. Specifically, the elements with the classes .gameInfo and .playerList overlap ...

Using jQuery, select a JavaScript array from an external file by pointing to it based on the option chosen in a dropdown menu

Currently, I am in the process of developing a page translation feature using jQuery. It functions flawlessly when all languages are contained in a single lang.js file within an array. Nonetheless, if the project expands significantly with numerous languag ...