我想从网址加载图片并在画布上绘制。我的画布占据了完整的窗口大小,我只希望图像显示在鼠标位置,大小为100x100。以下是我的代码:
drawImage(imageUrl) {
const ctx = this.canvas.getContext('2d');
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
const image = new Image();
image.onload = () => {
ctx.imageSmoothingEnabled = false;
ctx.drawImage(image, this.state.mousePos.x, this.state.mousePos.y,
100, 100);
};
image.src = 'https://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png';
}
图像可以在网页上绘制,但质量非常低。以下是上面链接中的orianl图片:
下面是我的画布上显示的图像:
您可以比较两张图片,它们的质量完全不同。如何在画布上绘制高质量的图像?
答案 0 :(得分:0)
let canvas = document.getElementById('c'),ctx = canvas.getContext('2d');
const image = new Image();
image.onload = () => {
ctx.imageSmoothingEnabled = false;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(image, 50, 50,500,500);
};
image.src = 'https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.svg?v=6e4af45f4d66';
canvas{
border : 2px solid black;
}
<canvas id='c' width=500 height=500/>
如果您转发图像质量,请使用svg。