How can the <animate /> tag be triggered using only CSS and HTML?

Looking for a way to trigger the animation rules of the <animate /> tag using only HTML and CSS. Is there a new standard in HTML and CSS that allows something like

input:checked ~ svg animate {enabled:true;}
coming up in 2020? Or some other creative workaround without relying on JavaScript?

Challenging myself to avoid JavaScript completely in this project.

  <body>
    <input type="checkbox" />
    <svg class="curtain home" viewBox="0 0 100 100" preserveAspectRatio="none">
    <defs>
      <pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
    <image xlink:href="https://picsum.photos/id/1000/800/800" x="0" y="0" width="100" height="100" />
      </pattern>
    </defs>
    <polygon points="0,0 0,100 100,100 50,0" fill="url(#img1)">
    <animate attributeName="points" dur="5s" values="0,0 0,100 100,100 50,0 ; 50,40 30,60 80,10 50,70; 0,0 0,100 100,100 50,0" keyTimes="0;0.5;1" repeatCount="indefinite"/>
    </polygon>
    </svg> 
    </body>


ADDITIONAL

Exploring options to create the illusion of no animation until the input field is checked. Considering another HTML element that could interact well with the <animate /> tag to give the impression that animation starts and stops based on user interaction with the webpage.

Answer №1

One interesting technique is to implement a feature where you can switch between displaying two different polygons.

.hide {
  display:none;
}

input:checked~svg .hide {
  display:block;
}
input:checked~svg .show {
  display:none;
}
<body>
  <input type="checkbox" >
  <svg class="curtain home" viewBox="0 0 100 100" preserveAspectRatio="none">
        <defs>
          <pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
            <image xlink:href="https://picsum.photos/id/1000/800/800" x="0" y="0" width="100" height="100" />
          </pattern>
        </defs>
        <polygon points="0,0 0,100 100,100 50,0" fill="url(#img1)" class="show">
        </polygon>
        <polygon points="0,0 0,100 100,100 50,0" fill="url(#img1)" class="hide">
            <animate attributeName="points" dur="5s" values="0,0 0,100 100,100 50,0 ; 50,40 30,60 80,10 50,70; 0,0 0,100 100,100 50,0" keyTimes="0;0.5;1" repeatCount="indefinite"/>
        </polygon>
    </svg>
</body>

Another approach is to utilize the click event. By placing two rectangles on top of each other and using z-index in CSS, you can create an interactive experience:

Here is a simple example, but feel free to enhance the appearance of the red rectangle to make it more appealing as it will be the one users have to click on.

label {
  position:absolute;
  z-index:0;
}

input:checked + label {
 z-index:1;
}
<body>
  <input type="checkbox" id="input" >
  <label for="input"><svg viewBox="0 0 10 10" height="20">
        <rect id="stop" x=0 y=0 width="100%" height="100%" fill="red" />
  </svg></label>
  <label for="input"><svg viewBox="0 0 10 10" height="20">
        <rect id="start" x=0 y=0 width="100%" height="100%" fill="red" />
  </svg></label>

  <svg class="curtain home" viewBox="0 0 100 100" preserveAspectRatio="none">
        <defs>
          <pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
            <image xlink:href="https://picsum.photos/id/1000/800/800" x="0" y="0" width="100" height="100" />
          </pattern>
        </defs>
        <polygon points="0,0 0,100 100,100 50,0" fill="url(#img1)" class="hide">
            <animate attributeName="points" begin="start.click" end="stop.click"  dur="5s" values="0,0 0,100 100,100 50,0 ; 50,40 30,60 80,10 50,70; 0,0 0,100 100,100 50,0" keyTimes="0;0.5;1" repeatCount="indefinite"/>
        </polygon>
    </svg>
</body>

If you prefer a CSS-only solution, achieving this effect is quite straightforward with just a few lines of code:

img {
  width: 400px;
  display:block;
  clip-path: polygon(0 0, 0 100%, 100% 100%, 50% 0);
  animation:change 5s linear infinite paused;
}
input:checked + img {
  animation-play-state:running;
}

@keyframes change{
  50% {
    clip-path: polygon(50% 40%, 30% 60%, 80% 10%, 50% 70%)
  }
}
<input type="checkbox" >
<img src="https://picsum.photos/id/1000/800/800">

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

Python's Selenium encountering a NoSuchElementException with the error message "./ancestor-or-self::form"

Trying to create a Python script that allows me to input my credentials and tweet text in the Python console. The script should then log in and post a tweet using Selenium. Everything works fine, except for the final click on the Tweet button which is caus ...

Retrieve the link's "href" attribute and its text if they match the specified text

Is there a way to extract the href link and text from a specific type of link? <a href="google.com">boom</a> I am looking to retrieve only the 'google.com' part and the text 'boom' Furthermore, how can I filter out other ...

Retrieve all items on the webpage using the pattern "_ followed by 10 random characters" through JavaScript

On my webpage, there is a table containing table rows in the following format: <tr id="_C15DKNWCSV">text</tr> I am attempting to scan the webpage and extract all table rows that have an id in the format: id="_(10 RANDOM CHARACTERS)" I want ...

Center the text within the div and have it expand outwards from the middle if there is an abundance of text

I have a div that is in the shape of a square box and I want it to be centered. No matter how much text is inside, I would like it to remain in the middle. I am using JQuery to create my square box and would like to center it using CSS. Here is my code: &l ...

Steps to replace the content of an HTML file (such as modifying images) by clicking on an element in a separate HTML file

Currently, I am in the midst of a project and wondering if it is possible to dynamically modify the content of an HTML file, such as images and text, using JavaScript. My goal is to achieve this without relying on any frameworks, simply by clicking on el ...

jQuery UI Accordion - Adjusting Panel Height to Content Size

Currently, I am utilizing jQuery UI's Accordion feature from http://jqueryui.com/demos/accordion/, and my goal is to adjust it so that it resizes based on the contents of each panel rather than just fitting the largest one. In addition, I am seeking ...

Instructions on submitting a form containing multiple select lists using the enter key

After researching various threads, I have come across solutions using the JS onkeypress function to trigger actions with input fields. However, in my case, I need to implement this functionality with a form that consists of multiple select lists instead of ...

Is there a way to overlay a 'secret' grid on top of a canvas that features a background image?

I am currently working on developing an HTML/JS turn-based game, where I have implemented a canvas element using JavaScript. The canvas has a repeated background image to resemble a 10x10 squared board. However, I want to overlay a grid on top of it so tha ...

Steps to import shared CSS using Styled-components

In my project using react, I've implemented styled-components for styling. One requirement is to have a shared loading background. Here is the code snippet of how I defined it: const loadingAnimation = keyframes` 0% { background-position: 95% 95%; } ...

What is the process for implementing an image as the background instead of a color for each column in a Google chart?

I'm currently working with Google Chart and I have set up a column chart. However, I am looking to display a background image instead of a background color in each column. Is this achievable with Google Chart? If so, how can I accomplish this? If anyo ...

Showing today's date using PHP

As a Java developer who has primarily worked with Java for an extensive period of time, I recently came across a code written in PHP by a friend. This code required the usage of a remote web-service to retrieve a field named "dateAdded". My task was to mod ...

Accessing variables from child controllers with populated select inputs in Angular

I'm currently facing an issue involving 3 controllers: Parent Controller: DocumentController Child Controller1: XdataController Child Controller2: CompanyController The child controllers are used to populate data in three Selector inputs on the fron ...

Can Rollup be utilized solely for processing CSS files?

I am curious about whether Rollup can be used solely for processing CSS files, such as css, scss, less, etc. For example, if I have a file called index.css in my src folder (the entry folder) and want Rollup to process it in the dist folder (the output fol ...

Tips for receiving notifications when the Collapsible collapses

I'm having trouble figuring out how to receive notifications when the Collapsible is expanded and collapsed. Currently, I am not receiving any type of notification. Any suggestions on how to make this work? Below is my code: --Imported jQuery < ...

Setting the dimensions of an HTML - CSS block

I am trying to style a navigation bar using the following CSS code: #nav {} #nav a { position: relative; display: inline-block; color: #F0F0F0; width: 1em; height: 2em; line-height: 0.9em; } #nav a.ic ...

Effortlessly create a seamless transition in background color opacity once the base image has finished

I've set up a div with a sleek black background. Upon page load, I trigger an API request for an image, which is then displayed in a secondary div positioned behind the main one. After this, I aim to smoothly transition the overlaying div's opaci ...

What is the best way to adjust the height of a div based on its child content

I've been experimenting with various methods to extend the white background around the title to cover the rest of the content. I've tried using overflow, clear, min-height, max-height, and even the '*' selector but nothing seems to work ...

Is there a way I can replicate this expandable div?

I have successfully implemented a script that expands and collapses. However, I am facing an issue when trying to use two of these scripts on the same page. I attempted to simply add "_two" to all instances of "accordion," but it doesn't seem to be f ...

How does HTML calculate the percentage-based size of a child element when the parent element has padding?

After analyzing the scenario presented, it appears that the outer yellow box is 240px wide (200 width plus 20 padding on each side). The red child inside has its width set at 50%. Surprisingly, when rendered, the red box measures 140px in width. You can v ...

Creating an HTML document with unique characters

I am currently facing an issue with writing special characters in HTML. My program is designed to edit an HTML file and allow users to replace content using a text box. However, I am struggling to add characters like "ç", "á", "ö", etc. These character ...