【发布时间】:2021-05-04 03:15:58
【问题描述】:
这是我的代码。我希望能够在不损失图像质量的情况下在画布上绘制图像。
const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");
canvas.width = 360px;
canvas.height = 360px;
const img = new Image();
img.src = "some-image.jpg" // image size is 3840*2594px
var scale = Math.min(
canvas.width / img.width,
canvas.height / img.height
);
// get the top left position of the image
var x = canvas.width / 2 - (img.width / 2) * scale;
var y = canvas.height / 2 - (img.height / 2) * scale;
context.drawImage(img, x, y, img.width * scale, img.height * scale);
【问题讨论】:
标签: javascript html css canvas