Unexpected outcomes occur when rotating elements in p5.js

The issue I'm facing is with the rotate() function, which is not rotating the image to the correct number of degrees. When I attempted to apply a rotate command as shown in the screenshot without any rotation, it rotated the line to the 11 o'clock position instead of the expected 9 o'clock position.

See the screen shot here

function setup() {
  createCanvas(200, 200);
  angleMode = DEGREES;
}

function draw() {
  noStroke();
  fill(200, 102, 0);
  circle(100, 100, 180); //draw pivot
  strokeWeight(5);
  stroke(0, 0, 0);
  circle(100, 100, 15); //draw pivot point
  translate(100, 100); //move origin
  // rotate(180);
  //rotate(radians(180));
  line(0, 0, 90, 0);
}

When using line 15 (rotate(radians(180));), the rotation functions as expected. Can anyone provide insight into why this difference occurs?

Answer №1

rotate() typically uses radians as its default parameter, not degrees. By converting degrees to radians, the argument becomes meaningful. If not converted, it results in a random number of turns, leading to an arbitrary rotation of 180 radians.

Your attempt to set angleMode using angleMode = DEGREES; does not have the intended effect. This action overrides the p5 function window.angleMode, triggering a warning message from p5:

You just changed the value of "angleMode," which was a p5 function. This may cause issues later on if not handled carefully.

function setup() {
  angleMode = DEGREES;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.0/p5.js"></script>

In p5, setters are generally functions rather than direct assignments. The correct method involves invoking the function, like angleMode(DEGREES). This allows rotate() to accept degrees by default, eliminating the need for the radians() conversion.

function setup() {
  createCanvas(200, 200);
  angleMode(DEGREES);
}

function draw() {
  noStroke();
  fill(200, 102, 0);
  circle(100, 100, 180); // draw pivot
  strokeWeight(5);
  stroke(0, 0, 0);
  circle(100, 100, 15); // draw pivot point
  translate(100, 100); // move origin
  rotate(180);
  line(0, 0, 90, 0);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.0/p5.js"></script>

Additionally, canvas transformations like translate() and rotate() accumulate, hence the use of push() and pop() to retain the context after these operations:

function setup() {
  createCanvas(200, 200);
  angleMode(DEGREES);
}

function draw() {
  push();
  noStroke();
  translate(100, 100);
  fill(200, 102, 0);
  circle(0, 0, 180);
  strokeWeight(5);
  stroke(0, 0, 0);
  circle(0, 0, 15);
  rotate(180);
  line(0, 0, 90, 0);
  pop();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.0/p5.js"></script>

Considering you are already translating, positioning at 0, 0 would be beneficial as the origin for your drawings.

Surprisingly, in this scenario, rotate() is unnecessary: line(0, 0, -90, 0); achieves the same outcome without transformations:

function setup() {
  createCanvas(200, 200);
  angleMode(DEGREES);
}

function draw() {
  const x = 100;
  const y = 100;
  noStroke();
  fill(200, 102, 0);
  circle(x, y, 180);
  strokeWeight(5);
  stroke(0, 0, 0);
  circle(x, y, 15);
  line(x, y, x - 90, y);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.0/p5.js"></script>

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

JavaScript Data Types in Node.js

Currently diving into the world of Node.js. Can someone clarify the meaning of the following code snippet? And also, what data type is being utilized here and what are its practical applications? var x = { a = { n: 0 } }; ...

Tips for eliminating duplicate entries in ag grid using Angular

Is there a way to eliminate the recurring assetCode entries in ag grid? The PRN and PRN1 values seem to be repeating unnecessarily. Check out the code below: list.component.ts ngOnInit() { this.rowData.push( { 'code': 'Machi ...

Click on a designated button to choose a specific file on an HTML page

I need to be able to select a specific file by clicking on another button. A helpful solution that utilizes JavaScript to trigger the selection of a hidden file can be found in this answer. You can view the implementation here. In my scenario, I alre ...

Is there a way to modify the CSS style of both an input and text simultaneously using a single function?

Users input data and then see the calculated result. Is it possible to change the CSS style of the text, "Fe," including its color and font-family, as well as the background-color of the input field simultaneously? I attempted the following: function co ...

javascript update HTML content

Hello, I am trying to call a function called changeDivHTML which passes an image. <a href="javascript:void(0)" onclick="changeDivHTML(<img src='.DIR_WS_IMAGES .$addimages_images[$item]['popimage'].'>)"> This function ad ...

The length of the JavaScript array is not accurate

In my code, there is an array named 'plotData' which contains multiple 'rows' of data, each represented by a 4-element array. The array 'plotData' gets updated by a $.post() function further down in the script. The placeholder ...

What is the best way for Cucumber to move on to the next annotation only after ensuring all async requests from the previous one have finished processing?

I am looking to set up a basic test using Selenium and Cucumber for logging into my web application and confirming that the main page is displayed correctly. Currently, all three tests are returning true even before the page is fully loaded. The issue ar ...

Incorporating Redis within an Express route

My goal is to store a value in a specific key in one route: /api/foo?redisKey="1" (setting value for the key id=1) Then, in another route, I want to retrieve the value: /api/bar?redisKey="1" (getting value for key id=1) Since redis operates asynchronou ...

Implementing user authentication in Rails with Devise and Backbone framework

I am just getting started with backbone.js. Currently, I am working on a rails application utilizing the "backbone-on-rails" gem. After successfully incorporating 3 models and rendering views with backbone, now I am looking to implement authentication usin ...

Is it possible for an onclick attribute to assign a function to document.getElementById without overwriting the existing value?

I want to create a numeric keypad in HTML with numbers 1-9, and I'm unsure if JavaScript can handle an onclick function for each key to show the number in the display div. What would be the most effective way to achieve this? <div id="display"> ...

Update the class information using jQuery after performing an action and serialize the button's ID for AJAX requests

I'm facing an issue with two buttons on my page: <td> <button id="1" class="green-button">Publish</button> <button id="1" class="yellow-button">Delete</button> </td> The green-button and red-button have different ...

Issue with Jquery event not triggering correctly following ajax data retrieval

This script uses jQuery and Ajax to populate a navigation bar with categories and subcategories dynamically. The first unordered list is static, while the second one is populated after receiving data via Ajax. There are some jQuery events that need to be i ...

Tips for accessing a component method in React from a WebSocket callback

Struggling with Javascript and React here. The goal is to update a component's state using push messages from a WebSocket. The issue lies in calling the function handlePushMessage from the WebSocket callback. Here's the code snippet: const Main ...

Instructions for removing and recreating an input field along with its parent elements when the value of a Select tag is changed

I currently have a table with four fields, as illustrated below: Semester | Exam Status | GPA | Fee Status My query is regarding the behavior when changing the value in Exam_Status: I noticed that the Select tag-> does not clear automatically. Specifi ...

How to access the ID value from the URL within a different component that was passed as a prop in

I have a scenario where I am building a reusable component with two child components in the following flow: Child Component 1 -> Parent Component -> Super Parent Main Component In the child component, I define a prop called 'url' which is ...

Steps to Create Javascript Image Zoom Effect on Mouse Hover

Looking to implement a feature on my website located at www.thetotempole.ca/javas2.html/. Basically, I have a table with images and I want them to enlarge when a user hovers over them and return to normal when the cursor is moved away. Is there a way to ac ...

Why is the clear method missing from @testing-library/user-event?

Currently, I am tackling a CMS project and have encountered an issue stating _userEvent.default.clear is not a function. import user from '@testing-library/user-event' test('can edit label', async () => { createArticleMock() ...

IE11/Edge exhibits slow performance with components that have large datasets exclusively

Take a moment to analyze the following code snippet: <GridBody Rows={rows} />. Let's say that in this scenario, rows.length could reach 2000 or more, with each array containing approximately 8 columns. I have encountered a performance bottleneck ...

Creating an outlined effect on a transparent image in a canvas: step-by-step guide

Currently, I am working on creating transparent images using canvas in HTML5 and I would like to incorporate borders into them. The issue I'm facing is that the "Stroke" property does not consider the transparency of the image and applies it as if it ...

Update the FontSize in the HTML dropdown menu using JavaScript

I am having trouble filling my Selection using a script. For example, when I try to populate my FontSizeMenu, I use the following code snippet: function FillFontSizeMenu() { FillSelection(GetPossibleFontSizes(), "fontSizeMenu"); } function GetPossib ...