CSS Transform with Element Resizing

CSS Transform with element resizing

The problem I noticed is that when element scales, browser change its pixels ratio, not pixels amount. Element is smaller but it doesn't change its actual pixel size in DOM. Because of that I don't think that CSS-only solution exist.

I put scalable element into container which keeps the same pixel ratio as rest of elements. Using Java Script I simply change container's size. Everything is still based on CSS3 transform: scale. Maybe JS code could be simplier, but now it's all about the idea (just a proof of concept);) Fiddle with two examples: http://jsfiddle.net/qA5Tb/9/

HTML:

<div class="overall-scalable">
<div class="scalable" scalex='0.5' scaley='0.5'>
Nunc et nisi ante. Integer in blandit nisi. Nulla facilisi. Vestibulum vulputate sapien eget mauris elementum sollicitudin. Nullam id lobortis dolor. Nulla vitae nibh vitae sem volutpat pretium. Nunc et nisi ante. Integer in blandit nisi. Nulla facilisi. Vestibulum vulputate sapien eget mauris elementum sollicitudin. Nullam id lobortis dolor. Nulla vitae nibh vitae sem volutpat pretium.
</div>
</div>

CSS:

.overall-scalable {width: 350px; height: 150px; overflow: hidden; -webkit-transition: all 1s;}
.scalable {color: #666; width: 350px; height: 150px; -webkit-transform-origin: top left; -webkit-transition: all 1s;}

JS:

$('button').click(function() {
$('.scalable').each(function(){
rescale($(this));
})
});

function rescale(elem) {

var height = parseInt(elem.css('height'));
var width = parseInt(elem.css('width'));
var scalex = parseFloat(elem.attr('scalex'));
var scaley = parseFloat(elem.attr('scaley'));

if (!elem.hasClass('rescaled')){
var ratioX = scalex;
var ratioY = scaley;
}else{
var ratioX = 1;
var ratioY = 1;
}

elem.toggleClass('rescaled');
elem.css('-webkit-transform', 'scale('+ratioX +', '+ratioY+')');
elem.parent().css('width', parseInt(width*ratioX) + 'px');
elem.parent().css('height', parseInt(height*ratioY) + 'px');
}​

In a CSS transform, how to prevent shifting of scaled up element when scaling down

The reason that is happening is that you declare transform-origin on the first time when you hover the element.

Solution:
Move transform-origin: left top; from #test:hover to #test.

transition on element resizing when displaying a child element

Since you can't animate the display property, you can use max-width and transition to achieve the effect.
opacity was given to add a fade effect

.chip {    display: inline-block;    position: relative;    color: #747474;    line-height: 32px;    padding: 0 12px;    background-color: #e4e4e4;    border-radius: 5px;    vertical-align:middle;}
.chip > i { display:inline-block; max-width:0em; opacity:0; transition:max-width .5s,opacity .5s; overflow:hidden; vertical-align:middle;}
.chip:hover > i{ max-width:2em; opacity:1;}
<link href="https://use.fontawesome.com/releases/v5.0.9/css/all.css" rel="stylesheet"/><div class="chip">    Animation    <i class="fa fa-times"></i></div>

HTML, CSS: Resizing the IMG with transitioning while resizing

There is no need to define the same transition property for the image and the hover pseudoclass. If you don't define transition in .logo:hover, it will take the previously set value of half a second.

The problem here is that you must specify an initial width and height for the image in order to have it resize smoothly.

Dragging & Resizing CSS Transformed Elements

You can get the current transformation matrix that is applied to an element by using getComputedStyle(). You can use this to transform the current mouse position to its position in transformed space and see whether the click/drag events are within the element boundary and/or corners. Good resources for this:

http://www.useragentman.com/blog/2011/01/07/css3-matrix-transform-for-the-mathematically-challenged/

http://www.eleqtriq.com/2010/05/css-3d-matrix-transformations/

BTW, as you're experiencing, this is non-trivial to code. We had to do it for Sencha Animator, and it was a beast.

jQuery Drag/Resize with CSS Transform Scale

It's been a while since this question was asked. I have found (actually created) an answer. All it requires is setting callback handlers. No editing jquery-ui needed!

Note: zoomScale in this example is a global variable and the transform is set using animate (aided by jquery.transform.js) like so:

target.animate({
transform: 'scale(' + zoomScale + ')'
});

Take a look at this:

transform scale() fix for resizable:

$(this).resizable({
minWidth: -(contentElem.width()) * 10, // these need to be large and negative
minHeight: -(contentElem.height()) * 10, // so we can shrink our resizable while scaled
resize: function(event, ui) {

var changeWidth = ui.size.width - ui.originalSize.width; // find change in width
var newWidth = ui.originalSize.width + changeWidth / zoomScale; // adjust new width by our zoomScale

var changeHeight = ui.size.height - ui.originalSize.height; // find change in height
var newHeight = ui.originalSize.height + changeHeight / zoomScale; // adjust new height by our zoomScale

ui.size.width = newWidth;
ui.size.height = newHeight;

}
});

transform scale() fix for draggable:

$(this).draggable({
handle: '.drag-handle',
start: function(event, ui) {
ui.position.left = 0;
ui.position.top = 0;
},
drag: function(event, ui) {

var changeLeft = ui.position.left - ui.originalPosition.left; // find change in left
var newLeft = ui.originalPosition.left + changeLeft / (( zoomScale)); // adjust new left by our zoomScale

var changeTop = ui.position.top - ui.originalPosition.top; // find change in top
var newTop = ui.originalPosition.top + changeTop / zoomScale; // adjust new top by our zoomScale

ui.position.left = newLeft;
ui.position.top = newTop;

}
});

Let me know if you find any problems or further improvements on this. :)

Reference: jQuery-UI resizable/draggable with transform: scale() set

How do I calculate top and left when resizing translated and rotated elements

I'll post an answer here in case anyone else hits this problem.

After locating this post: https://www.py4u.net/discuss/914662 the math is a little more clear.

It first grabs the location of the object along with the rotation, then for the new position it gets the location and then calculates the diff. Here's my functionalized version:

function calcNewPos(curr_x, curr_y, curr_w, curr_h, new_w, new_h, angle) {
// convert angle to radians
angle = angle * Math.PI / 180

//initial position.
let pos = {left: curr_x, top: curr_y};

//Get position after rotation with original size
let x = -curr_width/2;
let y = curr_height/2;
let new_x = y * Math.sin(angle) + x * Math.cos(angle);
let new_y = y * Math.cos(angle) - x * Math.sin(angle);
let p1 = {left: new_x - x, top: new_y - y};

//Get position after rotation with new size
x = -new_w/2;
y = new_h/2;
new_x = y * Math.sin(angle) + x * Math.cos(angle);
new_y = y * Math.cos(angle) - x * Math.sin(angle);
let p2 = {left: new_x - x, top: new_y - y};

//Get the difference between the two positions
let offset = {left: p2.left - p1.left, top: p2.top - p1.top};

//Calculate the correction
return {left: pos.left - offset.left, top: pos.top + offset.top};
}

I'm sure the math could be reduced in size, but this example lays it out there in an easy to understand way.



Related Topics



Leave a reply



Submit