Tips for creating animated images with a mask or clip using HTML, CSS, and jQuery

I am attempting to animate the movement of an image under a stationary mask. I have 2 methods for achieving this: one using the mask property and the other using clip

First method using Mask :

View working script at http://jsfiddle.net/2Aecz/ or below

<html>
<head>
<style>
  #myimage {
      clip-path: url(#mask1);
      position: absolute;

   }
</style>
</head>

<body>

<img id="myimage" src="http://lorempixel.com/100/100" >
<svg width="0" height="0">
  <defs>
    <clipPath id="mask1">
      <circle cx="50" cy="50" r="50" />
    </clipPath>
  </defs>
</svg>


<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script>
$( document ).ready(function() {
    $( "#myimage" ).click(function() {
     $( "#myimage" ).animate({ "left": "+=5px" }, "slow" );
   var left = $('path, polygon, circle').attr('cx');
    $('path, polygon, circle').attr('cx', left-5);
    });
});
</script>

</body>

Second method using Clip

View working script at http://jsfiddle.net/XdtNy/ or below

<html>
<head>
<style>
#myimage {
  mask: url("#mask2");
  position: absolute;
}


</style>


</head>

<body>
<img id="myimage" src="http://lorempixel.com/100/100" >
<svg width="0" height="0">
<defs>
  <mask id="mask2" >
    <circle cx="50" cy="50" r="40" style="fill: #ffffff"/>
  </mask>
</defs>
</svg>


<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script>
$( document ).ready(function() {
$( "#myimage" ).click(function() {
        $( "#myimage" ).animate({ "left": "+=5px" }, "slow" );
        var left = $('path, polygon, circle').attr('cx');
        $('path, polygon, circle').attr('cx', left-5);
    });
});
</script>

</body>

My challenge :

How can I move the image and mask together (in reverse direction) smoothly? Is there another way to animate this?

Answer №1

It appears that this method of masking may not be fully supported in various browsers, making it less suitable for production code.

In my opinion, a better approach would be to apply the image as a background to a block object and then animate the background itself. This allows the object to remain stationary while the movement is only applied to the background offset.

For example, you can see a demonstration here: http://jsfiddle.net/R5FQY/

HTML:

<div id="myimage2"> </div>
<svg width="0" height="0">
  <defs>
    <clipPath id="mask">
      <circle cx="50" cy="50" r="50" />
    </clipPath>
  </defs>
</svg>

CSS:

#myimage2 {
  background: transparent url('http://lorempixel.com/100/100');
  width: 100px;
  height: 100px;
  clip-path: url(#mask);
}

Javascript:

$( document ).ready(function() {
  $( "#myimage2" ).click(function() {
    $(this).animate({ 'backgroundPosition': "+=5px" }, "slow");
  });
});

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 to modify the height and width of a div layer?

How can I adjust the height and width of the layer on the card? Also, I want only the close button to be clickable and everything else to be unclickable. Can anyone help me find a solution? <link rel="stylesheet" href="https://stackpath.bootstrapcdn. ...

Picture placed outside div within infobubble

I'm currently working with Google Maps to display an InfoWindow using the InfoBubble library. The issue I'm encountering is that when I try to show an image outside of a div using CSS properties, only half of the image displays within the div. B ...

Nesting maps in JavaScript is a powerful way to transform

I'm in the process of developing a budgeting app using React and JavaScript. At the moment, I have successfully generated a table displaying various costs. Name Budget Used $ Used % Available Food 300 300 100 0 Streaming services 600 600 100 ...

adjusting three sections within a flexible navigation bar

Having difficulties creating a dynamic navbar that changes on scroll and keeping the elements within the nav fixed when the menu options appear. For reference, you can check out this codepen: http://codepen.io/timothyfernandez/pen/azXQPV Any assistance w ...

Adaptive scrolling backdrop

As a complete novice in HTML/CSS, I am attempting to create my portfolio. My approach is to start with the most basic elements, beginning with the background. Since my main page is a large scrollable one, I want the background to fit the screen size of the ...

Arrange a div beneath another div that is positioned absolutely

Within my code, I have a div with the property of absolute positioning referred to as "abs". Right after this div, there is another div called "footer" which does not have any specified position value assigned to it. Even though I've experimented with ...

Tips for aligning a cluster of floating buttons at the center in Vuetify:

This is the code I am currently working with: <v-container height="0"> <v-row align="center" justify="center"> <v-hover v-slot:default="{ hover }" v-for="(option, index) in options" ...

What are the steps to developing a responsive tile using HTML and CSS?

I am exploring the functionalities of CSS and responsive design. To better understand how it works, I created a simple example which can be accessed here. This demonstration showcases a basic tile template. My goal is to display my tiles in 2, 3, 4, or m ...

adjusting the size and positioning of an image within a parent div using React

Hey everyone, I'm a newcomer to the world of React. Currently, I'm working on a website and everything seems to be falling into place nicely. However, I've encountered a little hiccup. My goal is to write code within my SCSS folder that will ...

unable to retrieve parent ID

I am having trouble retrieving the ID of the parent's parent of the event target. It keeps coming back as undefined, but I have verified through firebug that the ID does exist. Below is my HTML markup: <div class="grid-stack-item ui-draggable ui- ...

CSS styles are not being displayed on elements that have been dynamically added using jQuery

I have searched through similar questions, but none of them addressed my issue. (they all seemed to be related to typos) This is just for fun. I am trying to create a table using jQuery based on JSON data. The problem is that the appended table rows do no ...

Creating two submenus in the sidebar: a step-by-step guide

I am looking to implement a feature where clicking on the sidebar menu will reveal 2 submenus. Here is what I have implemented: $(document).ready(function () { $("[data-toggle]").click(function () { var toggle_el = $(this).data("toggle"); ...

Set a maximum height for an image without changing its width dimension

I am facing an issue with an image displayed in a table. The image is a graph of profiling information that can be quite tall (one vertical pixel represents data from one source, while one horizontal pixel equals one unit of time). I would like to set a ma ...

Tips for efficiently loading large datasets into a ListBox in an ASP.NET MVC application

I am currently developing a web application using ASP.NET MVC. The application requires users to select an item from a ListBox which could potentially have over 30,000 entries. Is there a way to dynamically populate the ListBox with contents using an Aja ...

How do I use CSS to organize data by row and column within a table?

I'm looking to format the table data in a column-row layout for mobile devices. Can anyone provide an example or guidance on how to achieve this? I've browsed through some discussions but haven't found a solution that works with tables. The ...

Use jQuery to redirect the user if the URL does not match

Is there a way to automatically redirect users to a specific page if they are not currently on that page? For example, something like the following pseudo code: if not (url = example.com/index.php) redirect to example.com/index.php /if When a user vi ...

Struggling to locate the correct path for the CSS file in Express and Handlebars

As part of a student exercise, I am attempting to access an API containing information about presidents and style them using CSS. However, I am facing issues with linking the style.css file. I have tried various methods to change the path and public path ...

Can adjusting the viewport by using touch controls alter the size of the viewing screen?

When I tried to zoom in on a webpage using touch instead of the keyboard shortcut Ctrl +, I noticed that the elements stayed the same size but the screen itself was enlarged. It seems like this method does not affect the size of the viewport. However, whe ...

Displaying nested object properties in HTML through iteration

What is the correct way to access them if item.NestObj.textpropertyVal is showing incorrect? success: function(data){ var html = ""; $.each(data.mainOutterObj, function (index, item) { html += "<ul&g ...

Discover which students have conflicting schedules with themselves

To put it simply, my question involves a table display where student numbers (USN) are concatenated in groups and the entire row is displayed using a foreach loop. Row 1: Subject 1 - 22/07/2015 - 08:00 - 1XX10XX086, 1XX10XX087, 1XX09XX088 Row 2: Subject ...