What's the best way to rotate an SVG <use> element around its center point?

Utilizing an SVG within another SVG, I am trying to manipulate the rotation of the <use> element in the SVG around its own center. I have attempted to use tips from a previous question to set the transform-origin and transform-box properties in the CSS, but it seems to be ineffective.

Displayed below are my codes. You can interact with the slider to rotate the element (carrot) I am looking to animate. How can I ensure the carrot rotates around its own center?

const slider = document.getElementById("slider")
const myUse = document.getElementById("myUse")
slider.oninput = function(){
  myUse.style.transform = "rotate("+slider.value+"deg)"
}
#myUse{
    transform-origin: 50% 50%;
    transform-box: fill-box;
}
<svg id="carrot" fill="#000000" viewBox="0 0 50 50" width="50px" height="50px">
  <path d="M30 13c-1-3-5-4-7-2h-1l1-11h-3l-1 9-4-7-3 1 5 8-8-4-1 2 9 5h-1c-2 2-3 5-2 7l2 2 5-3 1 2-5 3 8 9 5-3 2 2-5 3 12 14 3-2-8-25-5 3-1-2 5-3-3-8"></path>
</svg>


<svg>
  <use id="myUse" x="30" y="30" href="#carrot" style="transform: rotate(0deg);"></use>
</svg>

Rotation
<input id="slider" type="range" min="0" max="360" value="0" step='1' >

Expected outcome: https://i.sstatic.net/Hy65m.png

Answer №1

To prevent overflow, you can eliminate the use of x and y, or implement translate3d:

const slider = document.getElementById("slider")
const myUse = document.getElementById("myUse")
slider.oninput = function () {
  myUse.style.setProperty('--r', `${this.value}deg`);
}
#myUse {
    transform-origin: 50% 50%;
    transform-box: fill-box;
    transform: translate3d(25px, 25px, 0) rotate(var(--r, 0deg));
}
svg {
  border: 1px dotted red;
}
<svg id="carrot" fill="#000000" viewBox="0 0 50 50" width="50px" height="50px">
  <path d="M30 13c-1-3-5-4-7-2h-1l1-11h-3l-1 9-4-7-3 1 5 8-8-4-1 2 9 5h-1c-2 2-3 5-2 7l2 2 5-3 1 2-5 3 8 9 5-3 2 2-5 3 12 14 3-2-8-25-5 3-1-2 5-3-3-8"></path>
</svg>


<svg viewBox="0 0 100 100" width="100px" height="100px">
  <use id="myUse" href="#carrot"></use>
</svg>

Rotation
<input id="slider" type="range" min="0" max="360" value="0" step='1' >

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

Modify the CSS of a single element when a class is applied to a different element

I need to apply a specific CSS attribute to my submit button on the comments form when a user is logged in to Wordpress! When the .comment-form class contains p class="logged-in-as" instead of the standard p class="comment-notes", I want the p.form-submit ...

The Google Maps API swipe feature on mobile devices is causing issues with screen scrolling

When users visit my website on mobile, they encounter a situation where Google Maps suddenly covers the entire page, preventing them from scrolling away. This is because swiping up or down only moves the map view within Google Maps, instead of scrolling th ...

Having trouble retrieving the chosen option from the select elements

Below is a portion of the code that I have written to capture the selected values of class code and section from a dropdown menu. Currently, only the first element of the select is being retrieved instead of the chosen element. HTML Code: <div id="dia ...

What is the process for creating a selector that links to an element that has been dynamically generated?

Can anyone help me figure out how to create a selector that links to a dynamically created element without using an event on the dynamic element? By dynamically created element, I mean an element that is not present in the HTML at the beginning. I know t ...

The cancel button is experiencing unexpected behavior when used in conjunction with the jQuery load event

I'm experiencing an issue with a cancel button on my website. After clicking the cancel button, it cancels the action but then proceeds to reload the page. I attempted to fix this by adding the "preventDefault" method, but it did not resolve the iss ...

How can one effectively style text to appear italic using CSS and HTML?

What is the proper method to italicize text? I have come across these four different approaches: <i>Italic Text</i> <em>Italic Text</em> <span class="italic">Italic Text</span> <span class="footnote">Italic Tex ...

Changing the text of a link when hovering - with a transition

Seeking a straightforward way to change text on a link upon :Hover. I desire a gentle transition (text emerges from below) and fallback to default if JavaScript is disabled. HTML <div class="bot-text"> <a href="">Visit this site</a> ...

Certain images are not being retrieved by Express on Linux, although they are functioning properly on Windows

When using a template to display HTML code, everything works smoothly on Windows. However, when transferring the code to a Linux machine, an issue arises - "Cannot GET /SmallVacImages/1.jpg". It seems that the index.html file can load images from the publi ...

JavaScript - Combine objects by summing values with matching keys

Below is the given array : [ { "Date": "2019.07.08", "Organisation": "A", "Client": "Client1", "Product": "Pen", "Quantity": "1120" }, { "Date": "2019.07.08", "Organisation": "A", "Client": "Client1", "Product": " ...

Cycle through matching elements for managing unique slick carousels

My experience with using slick carousel and custom pagination has been positive so far. However, I'm now faced with the challenge of incorporating multiple carousels on a single page. While I have come across solutions, implementing them alongside my ...

Changing Table Cell Color with Bootstrap Based on Value

I have implemented Bootstrap tables into my project using the tables provided on this website Bootstrap Tables and I am populating data from my MongoDB to these tables. One of the fields in my table is labeled "ACTIVE" and I am looking for a way to dynami ...

Unable to retrieve user attributes (provided as res.locals.user) in express and hbs view

Here is a snippet of code I use to verify user tokens and store the user information in req.locals: exports.isLoggedIn = async (req, res, next) => { if (req.cookies.jwt) { try { const decoded = await promisify(jwt.verify)( ...

Issue with Mootools Ajax call and form submission

I'm dealing with a table that displays data from a database. I'm trying to implement a way to (a) delete rows from the table and (b) edit the content of a row in real-time. Deleting rows is working perfectly, but editing the content is proving to ...

Is there a way to create an input field that accepts only numbers and behaves like a password field simultaneously?

I am attempting to develop an input field for entering a PIN. I would like the PIN to be masked on mobile devices, similar to how passwords are obscured in password input fields. I came across a suggestion on stackoverflow regarding this, but unfortunately ...

Sorting Angular components in reverse alphabetical order from A to Z

I feel like the answer to this problem is staring me right in the face, but no matter how hard I look, I just can't seem to find it. When dealing with a large ng-repeat, I have multiple sort options that I pass as an array. The user has the ability t ...

Tips for maintaining the webcam video in the center of the screen even when the height is set to 100% and the window is being resized

When using React, I encountered an issue with centering a webcam video while maintaining its height at 100% of the screen. Although the video fills the screen vertically correctly, adjusting the width of the window causes the video to lose its centered pos ...

Angular 2+ Service for tracking application modifications and sending them to the server

Currently I am facing a challenge in my Angular 4 project regarding the implementation of the following functionality. The Process: Users interact with the application and it undergoes changes These modifications are stored locally using loca ...

Obtain a nested array of objects from Mongoose's Model.find() method and then make modifications to the inner array

I need to retrieve an array of objects with a specific ID from my database within a server route, and then update a property of an object within that array (rather than returning just the objectID, I want to return the Document as an object with the specif ...

Is it possible to send props to a component within the render method?

I have a button component in my render method (DownloadReportsButton) that will trigger a modal onClick and perform an API call. The logic for the modal and the API call have already been moved to the button component, but how do I pass my data (an array ...

An error occured when attempting to read the 'status' property of an undefined value in node.js, express.js, and mysql

I recently started working with node.js and encountered a challenge in fetching data from mysql and sending it to an API response. Below is my setup: db.js: var mysql = require('mysql2'); var util = require('util') const pool = mysql ...