in Education by
This question already has answers here: Simulating transform-origin using translate (2 answers) Closed 3 years ago. I went through CSS spec, MDN/Opera articles on transform-origin, , but couldn't understand exactly how transform-origin works. I have setup a jsfiddle. The div with class "test" is a "50px" wide square. There is a button clicking on which rotates the div 30 degrees. Sample HTML is as below
Sample CSS is as below .rotate { transform: rotate(30deg); transform-origin: 70px 80px; } If I set transform origin to the edges of the square like (0,0)/(50px,0)/(0,50px)/(50px, 50px) or any point on the circumference/area inside the square I can slightly visualize how the transform origin is working. But if I set the property to a point outside the bounds of the square, I couldn't visualize or understand where exactly the div is shifted to. In MDN/Opera article, there is a phrase as below, From MDN "This property is applied by first translating the element by the negated value of the property, then applying the element's transform, then translating by the property value.)" From Opera Technically the transform origin is applied by calculating the negation of the transform-origin value (-3em on the X axis, and the top of the element in the example above), translating the element to this value, then applying the transform that is specified in the transform property, then translating the element by the actual value of the transform-origin. It adds to further confusion if I try to follow the above steps and trace where exactly the div will be placed in the co-ordinate system. Any help will be highly appreciated, Thanks JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Here is a very good pen by Dan Wilson that will give you a visual explanation of how Transform Origin works: https://codepen.io/danwilson/full/EXqEZM var display = document.getElementById('transform-origin'); var origin = document.querySelector('.origin'); var style = document.documentElement.style; var autopilot; autopilot = setTimeout(() => { change(); autopilot = setInterval(change, 3000) }, 1500); function change(ex, ey) { var x = ex || (Math.random()); var y = ey || (Math.random()); style.setProperty('--x', x); style.setProperty('--y', y); display.textContent = Math.round(x * 100) + '% ' + Math.round(y * 100) + '%'; } document.documentElement.addEventListener('mouseup', onPress); document.documentElement.addEventListener('touchend', onPress); var width = window.innerWidth; var height = window.innerHeight; var box = origin.getBoundingClientRect(); var side = box.right - box.left; function onPress(e) { clearTimeout(autopilot); clearInterval(autopilot); var x = e.clientX || e.changedTouches[0].clientX; var y = e.clientY || e.changedTouches[0].clientY; if (window.innerWidth !== width || window.innerHeight !== height) { box = origin.getBoundingClientRect(); width = window.innerWidth; height = window.innerHeight; side = box.right - box.left; } var ex = (x - box.left) / side; var ey = (y - box.top) / side; change(ex,ey); } :root { --x: .5; --y: .5; --side: 30vmin; --duration: 1500ms; --easing: ease-in-out; } .shape { animation: rotate 1000ms infinite linear; transition: transform-origin var(--duration) var(--easing); transform-origin: calc(var(--x) * 100%) calc(var(--y) * 100%); width: var(--side); height: var(--side); background: linear-gradient(315deg, hsl(248, 40%,50%) 10%, transparent 10%, transparent), linear-gradient(225deg, hsl(348, 40%,50%) 10%, transparent 10%, transparent), linear-gradient(135deg, hsl(48, 40%,50%) 10%, transparent 10%, transparent), linear-gradient(45deg, hsl(168, 40%,50%) 10%, hsl(168, 70%,50%) 10%, hsl(168, 70%,50%)); position: relative; will-change: transform, transform-origin; } .point { --point: 1vmin; background: black; width: var(--point); height: var(--point); border-radius: 50%; position: absolute; top: calc(var(--point) * -.5); left: calc(var(--point) * -.5); transform: translate(calc(var(--x) * var(--side)), calc(var(--y) * var(--side))); transition: transform var(--duration) var(--easing); will-change: transform; opacity: .6; } .origin { width: var(--side); height: var(--side); position: absolute; z-index: 1; top: 50%; left: 50%; transform: translate(-50%, -50%); border: 1px dashed hsl(233, 70%, 50%); background: linear-gradient(315deg, hsla(248, 40%,50%, .2) 10%, transparent 10%, transparent), linear-gradient(225deg, hsla(348, 40%,50%, .2) 10%, transparent 10%, transparent), linear-gradient(135deg, hsla(48, 40%,50%, .2) 10%, transparent 10%, transparent), linear-gradient(45deg, hsla(168, 40%,50%, .2) 10%, transparent 10%, transparent); } @keyframes rotate { 100% { transform: rotate(360deg) } } p { position: absolute; top:50%; left:50%; transform: translate(-50%,var(--side)); text-align: center; z-index:10; } span { display: block; } #instruction { position: absolute; top: 2vmin; left: 50%; transform: translateX(-50%); opacity: .78; text-align: center; } body { min-height: 100vh; display: flex; justify-content: center; align-items: center; overflow: hidden; background: radial-gradient(circle, hsl(198, 70%, 96%), hsl(198, 70%, 90%)); font-family: system-ui, -apple-system, 'Segoe UI', sans-serif; } *, *::before, *::after { box-sizing: border-box; } <!-- Scale: qXPdbw Skew: PKJqJW -->

transform-origin:50% 50%

Press Anywhere to Change Run code snippetExpand snippet

Related questions

0 votes
    This question already has answers here: Simulating transform-origin using translate (2 answers) Closed 3 years ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    As I tried to add a video on my top-wrapper previously. (Thank you so much for those who leave ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I want to display the label of an input inside its input, so that when I click the input, the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I have a Div element that pops up when clicking on a icon. This works fine on desktop. But when ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    How can i wrap data in the table. Currently if template name is too long i get a scroll bar. I ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    Why text inside / list is not affected (color of "item1", "item2" is not changed) by CSS applied ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    Why text inside / list is not affected (color of "item1", "item2" is not changed) by CSS applied ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    Why text inside / list is not affected (color of "item1", "item2" is not changed) by CSS applied ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    Still learning code. I am building a contact page and wanted to include my header bar at the top ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Still learning code. I am building a contact page and wanted to include my header bar at the top ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I've got the following html Title News 10. April 2011 First News 09. Feb. 2011 Second News With ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Thanks for helping. I am trying to develop a static carousel (it won't move, but it acts like one) that stretches ... screen (but hidden, so no scrollbar). Here is the markup:...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I'm currently working on a lottery app and have coded the UI for it. The problem is that my page ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to remove the space between inline/inline-block elements? (41 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
...