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

The implementation of a custom event for jQuery rows is not functioning as expected

I need assistance with jQuery code to add click events only to columns in a row that have text in the first column. Specifically, I want to hide rows containing "1/" in the first column when clicked on for the first row. <table class="results"> < ...

Trouble with jQuery script causing initial word count issue in textarea upon page load

I have implemented a word count textarea jQuery script and I am trying to figure out how to update the word count onload to 2 when the text area initially displays "this example". Currently, it shows 0 words. Although I can set focus on it and move the c ...

Looking to display a visualization using data from a MongoDB database

I am seeking advice on the most effective way to achieve the following goal. I have been storing user activity data in MongoDB as a log, with entries similar to: { "date":"01-01-2002T08:20:30", "task":"contact-lead", "user":"username" } My objective ...

The attempt to remove the ajax-loaded page while preserving the original div is proving to be

I have a function that is triggered when a link is clicked. This function uses ajax to load a new page over a div. <pre><code> function displayContent(id) { loadPage_signup = "register.php"; loadPage_info = "userinfo.php"; $.aj ...

The functionality of Skrollr on mobile is currently not working properly due to the inability to prevent default actions

Encountering the following error on all chromium browsers while using the Skrollr library and accessing the website through mobile: "[Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See < ...

jQuery is not accurately applying the specified height value

There is a DIV element with a padding of 6px. Using the following jQuery code: $('.target').height(150); After inspecting the HTML in Chrome, it shows that the height is set to 162px instead of 150px as intended. Is there a way to make jQuery ...

ajax.php form connected to a database using php

Introduction: I am currently working on a server-side datatables library. The code snippet below is not displaying either $stmt or $row, making it difficult to identify the issue. It's frustrating because I was hoping to troubleshoot this without see ...

Managing responses from ajax requests that can handle both text and json formats

After submitting a form via Ajax and receiving a response from the servlet in either text or JSON format, I'm wondering if there is a way to handle both types of responses. I've read through the jQuery/Ajax documentation, but the JQuery Ajax page ...

Is there a way to modify the text within a hyperlink using jQuery?

Here is the link HTML code snippet: <a href="#" onclick="run(1); return false;" class="link" title="Remove item">[x]</a> I am looking to modify this using jQuery to display like this: <a href="#" onclick="run(1); return false;" class= ...

Issue with HTTP headers not being effective in $.AJAX request

Regardless of the method I attempt to use for setting headers in an AJAX call, and no matter which header I set, every time there is a header present, the AJAX call is aborted and does not go through. However, if I try to make the call without setting any ...

Ways to determine if a class is a part of the bootstrap framework

Is there a way to verify if a class like mt-3 or table-left is part of the bootstrap framework? Are there any online resources, tools, or plugins in VSCode that can assist me in determining if a class is a predefined bootstrap keyword? ...

The CSS property `touchmove pointer-events: none` appears to have a malfunction on Chrome for Android version 4.4 / ChromeView

For my project, I am utilizing CSS pointer-events to allow touchmove events to pass through a transparent div. This method has been effective on most platforms except for Chrome on Android. I am curious if this is a known issue with Chrome and if there are ...

Adjusting and arranging multiple thumbnail images within a container of specific width and height

I need a user-friendly method to achieve the following. The thumbnails will not be cropped, but their container will maintain a consistent height and width. The goal is for larger images to scale down responsively within the container, while smaller image ...

Tips for choosing a single row in a table using a checkbox

I need help with a table that has multiple rows and four columns. One column is for checkboxes, while the other three are select boxes set to read-only. I want users to be able to edit only one row at a time by checking the checkbox in the first column. If ...

Tips for accessing raw POST data in PHP using $_SERVER

I am facing a challenge with my dynamic page where I use on-page filters that modify the data through AJAX response. My concern is how to access raw POST data when it's not visible in the URL... The link on my page appears as "example.com/default/lis ...

Generating a text input field with multiple lines

<!DOCTYPE html> <html> <body> <p>Press the button to generate a File Upload Button.</p> <button onclick="myFunction()">Click here</button> <script> function myFunction() { var x = document.createElemen ...

Tips for sending <p> data to a router function with HTML

I am currently working on a page where users are presented with a list of unverified accounts, each containing its own form element. Each form has a "submit" button that triggers a POST call to /verify. However, I am facing an issue where the data within t ...

Can a star rating be generated from a JSON value?

I'm curious if it's possible to display a glyphicon-star based on the value in a JSON file using AJAX. Here is an example of my JSON file: [ { "star": 4 },{ "star": 3 },{ "star": 5 } ] Here is my AJAX code: $(function($){ ...

Ways to avoid scrolling on a fixed element

Here is the HTML setup I have... body .top .content The issue I am facing is that when scrolling reaches the end of the ul in the .top element, the background starts to scroll. This can be quite disorienting and makes the site slow on tablets. Even ...

Using HTML to retrieve data from a PHP/MySQL database

This issue has been consuming my thoughts lately. I have a CSS-styled HTML page and a PHP page that retrieves the latest record from a MySQL database. The challenge is to display this information in the "leftContent" div of the HTML page. The PHP script & ...