What is the best method for expanding the width of a <rect> element with animateTransform?

How can I make a <rect> in SVG expand its width using animateTransform based on a specified value? Additionally, I want the color of the <rect> to change according to the following conditions:

  • If the <rect> value is between 0 and 29: {fill:red;}
  • If the <rect> value is between 30 and 49: {fill:blue;}
  • If the <rect> value is between 50 and 100: {fill:green;}

This is the SVG code snippet

<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 613 656">
  <defs>
    <style>.cls-1,.cls-4{fill:red;}.cls-2,.cls-5{fill:#29abe2;}.cls-3,.cls-6{fill:#22b573;}.cls-4,.cls-5,.cls-6{font-size:48px;font-family:MyriadPro-Regular, Myriad Pro;}</style>
  </defs>
  <title>Stackoverflow</title>
  <rect class="cls-1" x="45.5" y="67.5" width="104" height="104" />
  <rect class="cls-2" x="46" y="198" width="229" height="104" />
  <rect class="cls-3" x="46" y="340" width="395" height="104" />
  <rect class="cls-3" x="46" y="486" width="544" height="104">

  <animateTransform attributeName="transform"
                        type="translate"
                        from="0 0"
                        to="150 0"
                        begin="0s"
                        dur="2s"
                        repeatCount="indefinite"
                      />


  </rect>
  <text class="cls-4" transform="translate(190.86 128.57)">initial</text>
  <text class="cls-5" transform="translate(317.86 258.57)">30 - 49%</text>
  <text class="cls-6" transform="translate(455.86 408.57)"50 - 100%</text>
  <text class="cls-4" transform="translate(317.86 125.57)">0 - 29%</text>
</svg>

Answer №1

My suggestion is to avoid using <animateTransform> and instead opt for <animate> to manipulate the width attribute.

You can also incorporate an additional <animate> element to control the color.

<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 613 656">
  <defs>
    <style>.cls-1,.cls-4{fill:red;}.cls-2,.cls-5{fill:#29abe2;}.cls-3,.cls-6{fill:#22b573;}.cls-4,.cls-5,.cls-6{font-size:48px;font-family:MyriadPro-Regular, Myriad Pro;}</style>
  </defs>
  <title>Stackoverflow</title>
  <rect class="cls-1" x="45.5" y="67.5" width="104" height="104" />
  <rect class="cls-2" x="46" y="198" width="229" height="104" />
  <rect class="cls-3" x="46" y="340" width="395" height="104" />

  <rect class="cls-3" x="46" y="486" width="544" height="104">
    <animate attributeName="width"
             from="0"
             to="544"
             begin="0s"
             dur="2s"
             repeatCount="indefinite" />
    <animate attributeName="fill"
             values="red; #29abe2; #22b573"
             keyTimes="0; 0.3; 0.5"
             calcMode="discrete"
             begin="0s"
             dur="2s"
             repeatCount="indefinite" />
  </rect>

  <text class="cls-4" transform="translate(190.86 128.57)">initial</text>
  <text class="cls-5" transform="translate(317.86 258.57)">30 - 49%</text>
  <text class="cls-6" transform="translate(455.86 408.57)"50 - 100%</text>
  <text class="cls-4" transform="translate(317.86 125.57)">0 - 29%</text>
</svg>

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

How can you specify the environment in Google Tag Manager?

I have integrated google tag manager and google analytics using the vue-gtm plugin found at vue-gtm Within google tag manager, I have set up 3 environments under the same container. In a user-defined variable called GA from environment, I have created a ...

How can I use JavaScript to show a div upon clicking an input element?

I am looking to make a block div visible when an input field is clicked. <input class="indifferent" type="radio" name="decision" value="indifferent"> Indifferent </br> <div class="input" style="display: none;"> Please assist with our com ...

What is the best method to verify chrome.runtime.onInstalled in Chrome extension testing?

Is there a way to test the chrome.runtime.onInstalled handler? I am developing a Chrome extension that utilizes chrome storage to store specific data. In my upcoming release, I plan on making changes to the data model in chrome storage. To do this effectiv ...

How can we transform words with double "OO" in a paragraph into green squares using React? For instance, changing "clooney" to "cl[green square]ey"

import React, { Component } from 'react'; class App extends Component { constructor() { super(); this.state = { modifiedPar: "", newPar: "modified paragraph will be displayed here"}; } greenSquareMaker = word = ...

What is the best way to update the state by either adding to it or replacing it if the key

I'm currently working on a project involving radio buttons and I need to create dynamic state by setting the initial state on the first click, then updating it with subsequent clicks by either concatenating the new value onto the existing state or rep ...

Avoid making GET requests when clicking on a link

[UPDATE] I need help troubleshooting an issue with my ajax request. Here is the code snippet that I am working on: <a href="" class="undo_feedback">Undo</a> When I click on the link, it triggers an ajax POST request, but I encounter an error ...

Is it possible to ensure only one value is set as true in useReducer without manually setting the rest to false

I am seeking a more efficient method to ensure that only one value is set to true while setting the rest to false I came across this Python question and answer recommending an enum (I am not very familiar with that concept) Currently, I have the followin ...

Removing the JavaScript unicode character 8206 from a text string

I recently transitioned from VB.NET to JavaScript, and I am still getting familiar with the language. I have encountered an issue where a string I'm working with in JavaScript contains Unicode escape characters (0x5206, left-to-right mark) that I need ...

Building adaptable divs using bootstrap technology

I'm playing around with bootstrap and here is the code I came up with: <!doctype html> <html lang="en"> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet ...

The correct way to update component state when handling an onChange event in React using Typescript

How can I update the state for 'selectedValues' in a React component called CheckboxWindow when the onChange() function is triggered by clicking on a checkbox? export const CheckboxWindow: React.FC<Props> = props => { const [selected ...

I'm curious if the response order will mirror the URL order in my situation

QUERY: Upon reviewing the following link: Promise.all: Order of resolved values I am doubtful about its relevance to my specific scenario. Can I always expect responses to be in the same order as urls? EXAMPLE: var urls = []; for (var i = 0; i < d ...

Command for DiscordJS to verify users

I have a requirement to develop a verification command using discord.js v12. This command will assign a verified role based on the configuration specified in a config file. Config file: { "token": "my-token", "status": " ...

What methods can be used to ensure a div reaches all the way down to a sticky footer?

I'm currently working on a layout with a sticky footer, and I have a specific div that needs to extend all the way down to the footer. However, simply setting the height to 100% doesn't achieve the desired result. I've also experimented with ...

What is the proper technique for inserting a variable into the header portion of a v-data-table from a JavaScript file?

I'm currently developing an application using Vue.js with datatables. I need to display the headers of a v-data-table component, and ideally, I would like to pass a variable that can be dynamic based on the language selected on the page. Below is the ...

Creating an expand and collapse animation in a `flex` accordion can be achieved even when the container size is fixed using

Good day, I am in need of a fixed-height HTML/CSS/JS accordion. The requirement is for the accordion container's height to be fixed (100% body height) and for any panel content overflow, the scrollbar should appear inside the panel's content div ...

Using JavaScript to send formatted text through POST requests in a Rails application

Within APP A, I have the following formatted text: A beautiful painting because I created it because an artist endorsed it because my cat loves it I can access this formatted text in index.html.erb through product.body_html, which I then load into a Ja ...

The res.download() function in Express is failing to deliver the accurate URL to the client

When trying to utilize the res.download() method for downloading specific files from a server, there is an issue where triggering the res.download does not redirect the client to the correct URL. The files intended for download are located in a directory s ...

Ways to update the div's appearance depending on the current website's domain

There is a piece of code that is shared between two websites, referred to as www.firstsite.com and www.secondsite.com The goal is to conceal a specific div only when the user is on secondsite. The access to the HTML is limited, but there is an option to ...

Tips for incorporating a page route with an HTML extension in Next.js

I'm facing a challenge in converting a non-Next.js page to Next.js while maintaining my SEO ranking. To preserve the route structure with HTML extensions and enhance visual appeal, I have outlined the folder structure below: https://i.sstatic.net/zO1 ...

Strange behavior observed when resizing objects in Three.js WebGl

Everything was going smoothly with my code until I decided to enable WebGL. It seems that the function responsible for resizing my object every frame rate stopped working. function animate(){ window.requestAnimationFrame(animate); s.setPositio ...