Having trouble centering SVGs on the screen

My goal is to shift elements of the SVG to the center of the screen upon clicking, but I haven't been successful so far. It's puzzling because it worked flawlessly with a different SVG.

I've tried following the advice given in this post, and while I did manage to move it slightly towards the center, it's not completely there. Your assistance would be greatly appreciated.

Answer №1

The issue lies in the fact that each of your shapes begins with a sizable M (move) command, causing the bounding rectangle to be overly large as the starting point of the draw is approximately halfway across the screen from the filled path. To resolve this, you need to adjust your bounding rectangle calculation to account for this initial offset and ensure that you factor in the viewBox to Width/Height ratio during the calculations.

Answer №2

It's a bit unclear what your goal is here. To clarify, you can try the following suggestions:

  1. Encapsulate the shape in a symbol with a precise viewBox dimension. For example:

    <symbol id="braga_porto" viewBox="2848 1665 3985 4275">

  2. Utilize the symbol to ensure it aligns correctly in its original position:

    <use href="#braga_porto" x="2848" y="1665" width="3985" height="4275"...

Please cross-reference the values of the x, y, width, and height attributes with the viewBox dimensions of the symbol.

  1. If you need to scale up the shape to fit the width of the SVG canvas, adjust the position (x,y) of the <use> element to 0,0 (top-left corner of the canvas). The use should then occupy the entire width (e.g. 12969). Calculate the appropriate height to maintain the aspect ratio (e.g. 4275 * 12969 / 3985).

Compare the new width of the use element with the viewBox value of the SVG container.

use.addEventListener("click",()=>{
  use.setAttribute("x","0");
  use.setAttribute("y","0");
  use.setAttribute("width",12969);
  use.setAttribute("height",4275 * 12969 / 3985);
});
svg{border:solid}
<svg viewBox="0 0 12969 26674">
    <symbol id="braga_porto" viewBox="2848 1665 3985 4275">
    <path id="Braga" data-z="32" class="Braga" d="M5329 1730c-66-17-64 0-92 15l-110 113c-121 4-292 156-324 162-42-3-181 2-190-1l-63-42-153 42c-101 28-190 153-246 162-51-1-120-63-171 40-4 70-22 172-11 235-20 5-54 13-72 31l-21 36...
    </symbol>

     <use href="#braga_porto" x="2848" y="1665" width="3985" height="4275" id="use" />
  </svg>

To determine the precise viewBox values for the symbol, I utilized the getBBox() method.

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

Tips on how to import a CSS module in next.js

Struggling to incorporate the react-datepicker module in my React App, I'm encountering difficulties loading the CSS module of react-datepicker. The app is being rendered on the server side using next.js. In an attempt to resolve this issue, I experi ...

User-initiated closure of popup triggers error during Google sign in

After successfully implementing Google signin locally, I encountered an issue when attempting to login on a server. The error message displayed was: 'Uncaught: popup closed by user' Despite disabling adblockers and other potential interference, ...

Is there a method available to minimize the size of a local storage list containing strings?

Hey there, I am trying to load a large 2.5MB json file in my browser so that I can use it for some typeAhead functions. Unfortunately, I'm facing an issue with my local storage being constantly full. When using Firefox, I receive the following error ...

What method can be used to ensure Moment.js gives priority to the day over the month when interpreting dates with ambiguity

Is there a way to set Moment.js to default to assuming that the day comes before the month in ambiguous situations? For instance: moment("10-05-2018") would interpret as 10 May 2018 moment("06/08/2018") would be assumed as 06 August 2018 moment("01 03 ...

Utilize the v-model directive for dynamic searching within a v-for rendered array in Vue.js

I haven't used Vue.js in a while. I am currently working on a simple app where I'm rendering items using a v-for from an array. I want to implement an input box with a v-model to search through the list of items (presets). Code <div class="r ...

Is it possible for the 'error' attribute to be deemed invalid or inappropriate within ajax?

I am encountering an issue with my mobile cordova application. When attempting to log in, I am facing the following error: Error: JavaScript runtime error - Object doesn't support property or method 'error' The error is being traced back t ...

Encountered an error while trying to set up the route due to Router.use() needing

Within my app.js file, I have the following code: app.use('/', require('./routes')); //old routes app.use('/api', require('./api')); Additionally, I have an api folder containing an index.js file. This is what the ...

Filtering an array in Angular JS based on user input

My current issue revolves around creating a search input field with a search filter in an ng-repeat. I have successfully implemented the search input field, and now I am trying to figure out how to select all the items that match my search criteria using a ...

Dealing with HTML and Escaping Challenges in jQuery Functions

Here is a string I have: var items = "<div class='item'><div class='item-img' style='background-image: url('images.123.jpg')'></div></div>" I am looking to update the inner HTML of a div: $ ...

Using AngularJS to link the ng-model and name attribute for a form input

Currently, I am in the process of implementing a form using AngularJS. However, I have encountered an issue that is puzzling me and I cannot seem to figure out why it is happening. The problem at hand is: Whenever I use the same ngModel name as the name ...

Extract the content from a <Span> element without specifying its ID

Is there a way to make it so that when a user clicks on an HTML tag, the text is copied to their clipboard? I also need to ensure that this functionality does not apply to a specific tag ID/name as I am unable to add those to my span. Is there a way to aut ...

What could be the reason for document.body.style.backgroundColor not working properly?

I am currently experimenting with a logic that triggers the screen to turn black upon pressing the print screen key. Despite changing the background color, it is not functioning as expected. The console has detected a change in value. What could be going ...

The dimensions of the d3 div remain constant despite any modifications to its attributes

In my angular application, I am trying to customize the width and height of div elements in d3 when I select a legend. Strangely, I am able to adjust the width and height of the svg element without any issues. Here is the issue illustrated: https://i.ssta ...

Node.js can be utilized to make multiple API requests simultaneously

I am facing an issue while trying to make multiple external API calls within a for loop. Only one iteration from the for loop is sending back a response. It seems that handling multi-API requests this way is not ideal. Can you suggest a better approach fo ...

Is there a way to update protractor.conf.js configurations using the command line?

Currently, I have set up Protractor to run on our integration server. In the protractor.conf.js file, I have configured the multiCapabilities as follows: multiCapabilities: [{ 'browserName': 'firefox', 'platform': &a ...

Remove search results in real-time

I'm currently working on implementing a search feature for a web application. While I have made some progress, I am facing an issue with removing items when the user backspaces so that the displayed items match the current search query or if the searc ...

Adding up the values within a range of numbers in an array using a callback function

Can you help me spot the issue in this code snippet? function range(start, end){ var arrayRange = []; for(i= start; i<=end; i++){ arrayRange.push(i) } return(arrayRange); } var r = range(1,10); console.log(r); function sumRange(sumArray){ ...

If there is only one item, jCarousel will not display anything

After incorporating jCarousel into jQuery ui Tabs, I created a page which can be found at the following link: The issue arises when clicking on the 3rd tab (containing only one item), as nothing is visible until the left arrow is clicked to navigate back. ...

Resetting React state can occur during routing in Ionic applications

I've been working on implementing React states in a way that allows all components and pages to easily access important variables with updates reflected across the app. However, I've encountered an issue where my state/context is reset to its ini ...

Utilizing amCharts Postprocessing for fetching data for visual representation

My goal is to utilize amcharts for displaying data retrieved from my server. Unfortunately, the API format doesn't align directly with amCharts. It seems that I need to utilize the postProcess function to preprocess the data, but I am struggling due t ...