Spin rectangular image within boundary using angular 6

I am trying to achieve a rotating effect on a rectangular image within a frame of the same dimensions, as shown below:

<div style="border:1px solid #d3d3d3;width:208px;height:250px">
 <img style="width:208px;height:250px" src="https://www.webslake.com/w_img/t_i/lpw.png" [style.transform]="styles"> </div>

   <button type="button" class="btn btn-undo btn-lg" (click)="rotateImage('left')">

Here is the function that controls the rotation:

rotateImage(direction) {    
    if (direction === 'left') {
      this.rotationAmount = this.rotationAmount + -90;
    } else if(direction === 'right') {
      this.rotationAmount = this.rotationAmount + 90;
    }
    this.styles =  'rotate(' + this.rotationAmount + 'deg)';

  }

I am looking for a way to rotate the image inside the frame without altering its width and height. I want to maintain the same dimensions throughout the rotation process, where the new width corresponds to the old height and vice versa.

Answer №1

Incorporated new logic to compute the top and left positions for styling purposes:

rotateImage(direction: string) {

 if (direction === 'left') {
   let temp = this.width;
   this.width = this.height;
   this.height = temp;
   this.rotationAmount = this.rotationAmount + -90;
   if (this.rotationAmount == -360) {
    this.rotationAmount = 0;
   }
  console.log(this.rotationAmount);
   if (this.rotationAmount === -180 || this.rotationAmount === 0) {
     this.top = 0;
     this.left = 0;
   } else {
     this.top = (this.width-this.height)/2 * (-1); 
     this.left = (this.width-this.height)/2;
     console.log(this.top, this.left)
   }
} 

 else if(direction === 'right') {
   this.rotationAmount = this.rotationAmount + 90;
 }
 this.styles =  'rotate(' + this.rotationAmount + 'deg)';
}

Here is the link to the working code on stackblitz https://stackblitz.com/edit/angular-gz9zqs?embed=1&file=src/app/app.component.ts

Answer №2

Rotate images in Angular using Renderer2 instead of manual code.

   <button type="button" class="btn btn-undo btn-lg imagepreview" (click)="rotateImage('left')">
   <button type="button" class="btn btn-undo btn-lg imagepreview" (click)="rotateImage('right')">

Include Renderer2 in your TS file:

import {Renderer2} from '@angular/core';

Add the renderer to your constructor:

private renderer: Renderer2

Implement the rotateImage method:

rotateImage(direction) {
    this.angle += direction == 'left' ? -90 : 90;
    this.renderer.setStyle(document.querySelector('.imagepreview'), 'transform', `rotate(${this.angle}deg)`);
  }

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

Change the WordPress Divi Builder nav bar to switch from transparent to white when scrolling or upon reaching a specific point on the page

Currently, I am in the process of revamping our company's WordPress website using the Divi Builder. For the past couple of days, I have been scouring the internet trying to find a solution that would allow the navigation bar to change CSS classes as t ...

Tips for expanding a text input field vertically, not horizontally like in Facebook, while typing or by holding down the Shift key and pressing Enter

I've come across numerous plugins that enable vertical resizing of a textarea and others that allow horizontal resizing of an input field. However, I have yet to find one that enables vertical resizing of an input field similar to Facebook's comm ...

Creating a three-row CSS layout where the middle row aligns to the right side

I am working on developing a mobile device layout with 3 blocks. The first and third blocks contain text fields, while the second block is filled with a map. However, all of my blocks don't look good when they are too wide. The browser window can have ...

Managing varied PHP submissions using a single 'isset' declaration

I'm working on loading data from a MySQL database with PHP and displaying images in HTML. When a specific image is clicked, I want to show text associated with that image using MySQL/PHP. I'm looking for a way to achieve this without having multi ...

Learn how to integrate Bootstrap with Vue.js TreeView in this tutorial

If you're looking to create a treeview using Vue.js, the code structure would resemble something like this: HTML: <!-- item template --> <script type="text/x-template" id="item-template"> <li> <div ...

Is there a way to transform time into a percentage with the help of the moment

I am looking to convert a specific time range into a percentage, but I'm unsure if moment.js is capable of handling this task. For example: let start = 08:00:00 // until let end = 09:00:00 In theory, this equates to 100%, however, my frontend data ...

I found myself pondering the method to achieve the slightly blue tinted box that sits behind the image

Can you assist me in achieving this specific look? I have my MUI app bar and home page set up, but I'm unsure how to create the blue-ish box behind the image. Should I place the container within my app bar or integrate it into my homepage file? Additi ...

Searching for the precise draggable grid line position in rulerguides.js - tips and tricks!

Currently, I am utilizing rulerguides.js in my project. I have customized it for a specific div to display rulers and grid lines. You can refer to this FIDDLE. The rulers are functioning properly, but the draggable grid lines are being calculated based on ...

Looking for assistance with my website's navigation bar and logo

Looking for assistance in moving my navigation buttons to each side of the logo. As a beginner in web design and CSS, I've been struggling with this task. Essentially, I want the "home" and "about" buttons on the left side of the logo, 16px apart from ...

Error with Angular PWA: A fatal error has occurred as the module '@schematics/angular/utility' cannot be located

Trying to execute the command below has been giving me trouble: ng add @angular/pwa Every time I run it, an error pops up saying: An unhandled exception occurred: Cannot find module '@schematics/angular/utility' I attempted to install @schemati ...

Ionic Troubleshoot: Issue with Reading Property

Encountering an error: A TypeError occurs: Cannot Read Property of "username" of Undefined The HTML code responsible for the error is as follows: <ion-content padding style="text-align: center; margin-top: 35px"> <form (ngSubmit)="logFor ...

Looping through color transitions upon hover using CSS

I am trying to create a color transition effect on hover, where the background changes from yellow to red and then back to yellow in a loop. I'm having trouble figuring out how to make this transition repeat continuously. Do I need to incorporate Java ...

Clicking on an Angular routerLink that points to the current route does not function unless the page is re

Currently, I am working on an E-commerce project. In the shop page, when a user clicks on the "View More" button for a product, I redirect them to the product details page with the specific product id. <a [routerLink]="['/product-details' ...

Is there a method for PHP to detect changes in front-end routing (specifically with Angular)?

A scenario I am encountering involves having an angular form incorporated within a php page. The issue arises when the angular form is submitted and is supposed to navigate to a different page. In actuality, once the angular form is completed, it should u ...

Styling with CSS: Enhancing list items with separators

Is there a way to include a separator image in my list using CSS? li { list-style: none; background-image: url("SlicingImage/button_unselect.png"); height: 53px; width: 180px; } This is the code for the separator image: url("SlicingImage ...

CSS styling is not being applied to buttons within Ionic 2

I'm completely new to Ionic and hybrid apps as a whole. I've been experimenting with a test app, but for some reason, none of the CSS seems to be working. Could someone please help me figure out what mistake I might have made? Here are my files: ...

What is the best way to reset the values in react-multiple-datepicker?

Having trouble clearing values assigned in react-multiple-datepicker library. import MultipleDatePicker from "react-multiple-datepicker"; import React from "react"; class Addjob extends React.Component { constructor(props) { super ...

What's the best way to make sure my table fits perfectly within its container's width?

I've encountered an issue with a table: If you want to see how the table was created, check it out on jsFiddle I attempted to modify the CSS like this : table{ width:100%; } td span { float:right; width:20%; text-align:center ...

Tips for establishing a fixed point at which divs cease to shrink as the browser size decreases

There are numerous dynamically designed websites where divs or images shrink as the browser size decreases. A great example of this is http://en.wikipedia.org/wiki/Main_Page The div containing the text shrinks proportionally to the browser size until it ...

Prevent the ability to add options dynamically to a drop-down select list

I have implemented a dynamic data retrieval feature from my database using jQuery's .getJSON function and appended the data to an HTML select drop-down list. The JavaScript code for this functionality is as follows: $.getJSON('url', f ...