首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >HTML5旋转方法倾斜图像

HTML5旋转方法倾斜图像
EN

Stack Overflow用户
提问于 2016-11-25 06:56:56
回答 1查看 38关注 0票数 0

我正在为Javascript编写一个游戏引擎,并试图允许我的精灵旋转。

然而,当我旋转我的精灵时,图像会倾斜。它会旋转,但它不是正确的尺寸。

我遵循的基本逻辑与在Java中使用Image对象和2d库编程时使用的基本逻辑相同,但得到的结果不同。(图像倾斜,不应该...只需旋转即可)

代码语言:javascript
运行
复制
function Sprite(imgg,w,h)
{
    this.img = imgg;
    this.x = 350;//Math.random()*700;
    this.y = 350;//Math.random()*700;
    this.vx = 0;//Math.random()*8-4;
    this.vy = 0;//Math.random()*8-4;
    this.width = w;
    this.height = h;
    this.rotatespeed = 0;//0.01;
    this.rotate = 40;

}

function drawSprite(sprite, ctx)
{

    ctx.save();

    ctx.translate(sprite.x,sprite.y);
    ctx.rotate(sprite.rotate);


    ctx.drawImage(sprite.img,0,0,sprite.img.width,sprite.img.height,-sprite.width/2,-sprite.height,sprite.width,sprite.height);


    ctx.restore();
}

function drawGame(g)
{
    var gameLoop = setInterval(function(){

        g.context.clearRect(0,0,g.canvas.width, g.canvas.height);
        g.context.save();


        g.context.translate(g.canvas.width/2, g.canvas.height/2);
        g.context.scale(g.camera.scale,g.camera.scale);
        g.context.rotate(g.camera.rotate);
        g.context.translate(g.camera.x,g.camera.y);


        for(var i=0;i<g.objects.length;i++)
        {
            updateSprite(g.objects[i]);
            drawSprite(g.objects[i], g.context);
        }
        g.context.restore();

    },setIntervalAmount); 

}
EN

回答 1

Stack Overflow用户

发布于 2016-11-25 12:44:27

事实证明一切都很好,但web浏览器在一个维度上非常紧张,因为我打开了开发人员控制台,所以这都是一种视觉错觉。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40795516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档