【已解决】drawImage图像到画布比例问题

发布于 6 年前作者 baiping7525 次浏览最后编辑 6 年前来自 ask

使用drawImage接口把一个100 * 100的图像画到一个50 * 50的画布怎么按比例缩放,让图像全部显示在画布

1 回复
qiangdu
qiangdu1 楼5 年前

答案

ctx.save();
  let sx = 111
  let sy = 180
  let headWidth = 80
  let headHeight = 80
  let headX = (headWidth / 2) + sx
  let headY = (headHeight / 2) + sy
  ctx.arc(headX, headY, headHeight / 2, 0, Math.PI * 2, false);
  ctx.clip();
  ctx.drawImage(image, sx, sy, headWidth, headHeight);
  // ctx.restore();
  ctx.draw(true)