canvas.scale放大后,再使用canvas.translate移动时方向就相反了

当您 用于放大画布,然后用于移动画布时,方向可能看起来是相反的。这是因为该方法根据其当前坐标系移动画布,该坐标系已由该方法缩放。

为了更好地理解这一点,请考虑以下示例:

// Translate the canvas by 50 pixels to the right and 50 pixels down
ctx.translate(50, 50);

// Set the scaling factor to 2
ctx.scale(2, 2);

// Draw a rectangle
ctx.fillRect(0, 0, 50, 50);

在此示例中,该方法将画布的大小加倍,因此矩形的大小将是不缩放时的两倍。但是,该方法基于其原始坐标系(而不是缩放的坐标系)移动画布。因此,当我们向右平移 50 像素和向下平移 50 像素时,它似乎在每个方向上移动了 100 像素,因为坐标系已缩放了 2 倍。

若要解决此问题,可以颠倒 和 方法的顺序。例如:scale()translate()

// Set the scaling factor to 2
ctx.scale(2, 2);

// Translate the canvas by 50 pixels to the right and 50 pixels down
ctx.translate(50, 50);

// Draw a rectangle
ctx.fillRect(0, 0, 50, 50);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值