简体   繁体   English

Javascript动画比例精灵

[英]Javascript animation scale sprite

在此处输入图片说明

This is the sprite I am using to animate an object through css. 这是我用来通过CSS为对象设置动画的精灵。 Every frame is 224px in width so the background-position property of the div is incremented by -224px every frame and the cycle goes on. 每帧的宽度为224px,因此div的background-position属性每帧增加-224px,然后循环继续进行。 The problem is that the width of my div is not fixed. 问题是我的div的宽度不固定。 I use percentage width of the div to stay resolution independent. 我使用div的百分比宽度来保持分辨率独立。 So I want my 224px wide frame to scale over the width of the div and move left the same amount every frame. 因此,我希望我的224px宽框架能够在div的宽度上缩放,并在每帧处向左移动相同的量。 Hope it is clear to you. 希望你清楚。 Here is the JS function used to animate the div. 这是用于为div设置动画的JS函数。

function moveTail() {
    if ( typeof moveTail.counter == 'undefined' ) {
        moveTail.counter = 0;
        moveTail.object = $('#genietail');
    }

    if( moveTail.counter == 42 )
        moveTail.counter = -1;
    ++moveTail.counter;
    moveTail.object.css('background-position', moveTail.counter*-224 + "px 0px" );
}

There are two ways of scaling a sprite image. 缩放子画面图像有两种方法。

The first one is to play with the background-size CSS attribute. 第一个是使用background-size CSS属性。 If your sprite size is for example 100px by 100px, by default background-size will take the value 100px 100px , but if you set it to 200px 200px it will double the image size that will be displayed. 例如,如果您的精灵大小为100px x 100px,则默认情况下background-size的值为100px 100px ,但是如果将其设置为200px 200px则它将显示的图像大小加倍。

In your case it would look like this : 在您的情况下,它看起来像这样:

var spriteSize = { height: 224, width : 224 * 20 };
var imageSize = { height: 224, width : 224 };
var backgroundWidth = Math.floor((moveTail.object.width()  / imageSize.width) * spriteSize.width);
var backgroundHeight = moveTail.object.height();

moveTail.object.css("background-size", backgroundWidth + "px " +  backgroundHeight + "px");
moveTail.object.css('background-position', moveTail.counter * -moveTail.object.width() + "px 0px" );

The second one is to use the CSS 3 attribute transform with scaleX and scaleY . 第二个方法是将CSS 3属性transformscaleXscaleY If you want a div to double the size that is displayed you can do in CSS transform: scaleX(2) scaleY(2) . 如果要使div倍增显示的大小,可以在CSS transform: scaleX(2) scaleY(2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM