Exploring FabricJs: Reassessing the coordinates of an object post-rotation

const canvas = new fabric.Canvas("c", {
  selection: false
});

const refRect = new fabric.Rect({
  left: 250,
  top: 150,
  width: 200,
  height: 100,
  fill: 'transparent',
  stroke: 'blue',
  originX: 'center',
  originY: 'center'
});

canvas.add(refRect);

const refPoints = [
{ x: 0, y: 0 }, { x: 200, y: 0 }, { x: 0, y: 100 },
  { x: 200, y: 100 }, { x: 30, y: 50 },
  { x: 50, y: 30 }

];

refPoints.map(function(point) {
  return new fabric.Circle({
    left: refRect.left - (refRect.width / 2) + point.x,
    top: refRect.top - (refRect.height / 2) + point.y,
    radius: 10,
    fill: 'blue',
    originX: 'center',
    originY: 'center'
  });
}).forEach(function(refPoint) {
  canvas.add(refPoint);

});


const tarRect = new fabric.Rect({
    left: 250, top: 150,
    width: 200, height: 100,
    fill: 'transparent',
    angle:45,
    stroke: 'red',
    originX: 'center',
    originY: 'center'
});

canvas.add(tarRect);

const matrix = tarRect.calcTransformMatrix();

refPoints.map(function(point) {
  return new fabric.Circle({
    left: tarRect.left - (tarRect.width / 2) + point.x,
    top: tarRect.top - (tarRect.height / 2) + point.y,
    radius: 5,
    fill: 'red',
    originX: 'center',
    originY: 'center'
  });
}).forEach(function(refPoint) {
  canvas.add(refPoint);
});
canvas {
  border: 1px solid;
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed8b8c8f9f848eaddcc3dac3dcdc">[email protected]</a>/dist/fabric.js"></script>
<canvas id="c" width="500" height="300"></canvas>

I am using a fabric rectangle object along with multiple points positioned relative to the rectangle. I have successfully integrated these points within the rectangle utilizing a straightforward approach.

However, if I were to rotate the rectangle object by an angle, I am uncertain about how to determine the updated positions of the points concerning the rotated rectangle.

As my expertise in mathematics is limited, I would appreciate any guidance or assistance on this matter.

Answer №1

Instead of relying solely on mathematical logic, consider utilizing a fabric group for your solution.

 const group = new fabric.Group([...points, rect]);

 canvas.add(group);

To further demonstrate my proposed solution, I have prepared an example which you can view here.

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

A guide on integrating a submission button that combines values from multiple dropdown forms and redirects to a specific URL

Is there a way to make the submit button on this form act based on the combined values of two different dropdown menus? For instance, if "west" is selected from the first dropdown and "winter" is selected from the second dropdown, I want it to navigate to ...

Create a cylindrical HTML5 canvas and place a camera view inside the cylinder

I have a unique project in mind that involves creating an HTML5 canvas cylinder with the camera view at its center. The walls of the cylinder will display images, and the entire structure should be able to rotate and zoom. If anyone has any advice on how ...

Simulating npm package with varied outputs

In my testing process, I am attempting to simulate the behavior of an npm package. Specifically, I want to create a scenario where the package returns a Promise that resolves to true in one test and rejects with an error in another. To achieve this, I hav ...

Loading a .css file in advance using NextJS

We have integrated NextJS and Material-UI into our website, but we are facing an issue with FOUC (Flash of Unstyled Content) upon page loading. After some investigation, I discovered that the JS files are loading faster than the CSS file, causing the probl ...

Tips for developing a sophisticated HTML quiz

I have spent countless hours perfecting this quiz. I have successfully created a quiz that reveals solutions at the end, but I want to take it one step further. I envision the answers appearing after each incorrect response from the user, and no answer sho ...

Understanding how to break down intricate JSON strings into classes using either VB or C# programming languages

I have a JSON string that needs to be parsed and eventually stored in database tables. My plan is to parse it into VB .NET classes (objects) and then store the data in the tables. I have Newtown imported into my .NET environment, but I'm not very fami ...

find all the possible combinations of elements from multiple arrays

I have a set of N arrays that contain objects with the same keys. arr[ {values:val1,names:someName},   {values:val2,names:otherName}, ] arr2[   {values:valx,names:someNamex}, {values:valy,names:otherNamey}, ] My goal is to combine all possible c ...

The specified type '{}' cannot be assigned to type 'ReactNode'

Can someone help me figure out why I am getting this error in Vercel but not locally? Error: 'FontAwesomeIcon' cannot be used as a JSX component. ./components/Services/ServiceContainer.tsx:25:6 12:01:54.967 Type error: 'FontAwesomeIcon&a ...

Identifying modifications to the content of a text area

Is there a way to determine if the value in my textareaId has changed due to JavaScript? For example: $("#buttonID").on("click, function(){ $("#textareaID").val("lorem ipsum"); }); $("#textareaID").change(function(){ //not working }); $ ...

Having trouble getting your Ajax script to function correctly when submitting a form?

I am currently facing an issue where I am loading a partial page, but I do not want the form on this page to redirect when the save button is clicked. I am unsure if I am using the script correctly. I would like to post to the controller when the submit b ...

Javascript Dilemma: Unable to Access 'object.style.left' and 'object.style.top' Values - What's the Issue?

Why am I unable to access and modify the object.style.left & object.style.top values? I am currently attempting to dynamically reposition a button on a webpage. In order to set new values for the style.left & style.top, I can utilize JavaScript l ...

ag-Grid incorporating new style elements

For my Angular application, I have a simple requirement of adding a CSS class when a row expands or collapses to highlight the row. I attempted to use gridOptions.getRowClass following the documentation at https://www.ag-grid.com/javascript-grid-row-styles ...

The redirect feature in Next.js 14 is experiencing delays

Having an issue with the redirect function in next.js 14. The problem is that the redirect process on the server takes approximately 5 minutes. I am currently using a VPS on Namecheap with WHM and cPanel, running the application on Apache server with passe ...

Developed a hierarchical JSON format using a JavaScript array

I am aiming to generate a properly structured nested JSON file from an array, with unique key values. Currently, I can only output the JSON without any nesting. The desired structure to be displayed in the console is : { "Key" : "data1", "header" ...

Steps for incrementing a number in an integer field with Node.js and MongoDB

I have a dataset that looks like this: { "_id": "6137392141bbb7723", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95f7e7fafafef0d5f6f4f2f9f0bbf6faf8">[email protected]</a>", ...

I am unable to store a session variable using the GET method

Good day, I am seeking assistance with my code and have been searching for four hours without finding the error. I am working on an exchange where variables are stored in a session. The issue I am facing is that the variable $discount gets cleared every ti ...

Convert ViewBag into a JSON array in order to create a bar chart in a .NET MVC application

I am struggling to convert ViewBag into a Json array in order to create a chart within the .Net MVC framework. Despite attempting to parse the Json data based on solutions from previous queries, the chart is not displaying anything. Here is my code: Contr ...

Using regular expressions, you can eliminate a specific segment of a string and substitute

Provide a string in the following format: lastname/firstname/_/country/postalCode/_/regionId/city/addressFirst/addressSecond/_/phone I am creating a function that will extract the specified address parts and remove any extra parts while maintaining maxim ...

Tips for accessing jQuery UI tab elements and adjusting visibility for specific panels

How can I retrieve the panel numbers of jQuery UI tabs and adjust their visibility using CSS? I am looking to identify the panel numbers of each tab and control the visibility of certain tabs. <div id="tabs"> <ul> <li><a href="#"> ...

Modify jQuery to update the background image of a data attribute when hovering over it

Something seems to be off with this topic. I am attempting to hover over a link and change the background image of a div element. The goal is to always display a different picture based on what is set in the data-rhomboid-img attribute. <div id="img- ...