javascript - Increase HTML5 Canvas Resolution
问题描述
I have tried everything I saw in here, but nothing seems to work on my code, or maybe I just don't know how to apply it.
I am trying to get a better resolution in my html canvas, because the rectangles look a little bit "blurred".
Here's my code: html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Canvas Resize</title>
<style>
canvas {
border: 1px solid black;
}
body {
margin: 0px;
}
</style>
</head>
<body>
<canvas id="sig-canvas" width="1280" height="739"></canvas>
<p id="timer"></p>
<script src="canvas.js"></script>
</body>
</html>
javascript:
var canvas = document.getElementById("sig-canvas");
var ctx = canvas.getContext("2d");
var drawing = false;
var mousePos = { x:0, y:0 };
var lastPos = mousePos;
canvas.addEventListener("mousemove", function (e) {
mousePos = getMousePos(canvas, e);
drawing = true;
}, false);
// Get the position of the mouse relative to the canvas
function getMousePos(canvasDom, mouseEvent) {
var rect = canvasDom.getBoundingClientRect();
return {
x: mouseEvent.clientX - rect.left,
y: mouseEvent.clientY - rect.top
};
}
// Get a regular interval for drawing to the screen
window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimaitonFrame ||
function (callback) {
window.setTimeout(callback, 1000/60);
};
})();
// Draw to the canvas
function renderCanvas() {
if (drawing) {
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(mousePos.x, mousePos.y, 25, 25);
ctx.strokeStyle = "#000000";
ctx.lineWidth = 2;
ctx.strokeRect(mousePos.x, mousePos.y, 25, 25);
lastPos = mousePos;
}
}
// Allow for animation
(function drawLoop () {
requestAnimFrame(drawLoop);
renderCanvas();
})();
Here's my fiddle: https://jsfiddle.net/8cp5qmob/
Thank you!
解决方案
为了提高画布的分辨率,我将 CSS(对于它在屏幕上占用的像素数)canvas.width
和canvas.height
(对于它内部使用的像素数)进行了很好的混合。它看起来像这样:
CSS:
#sig-canvas {
width: 1280px;
height: 739px;
}
JavaScript:
var canvas = document.getElementById("sig-canvas");
var ctx = canvas.getContext("2d")
var scale = 2;
canvas.width = 1280 * scale;
canvas.height = 739 * scale;
使用这种技术的棘手之处在于,在屏幕坐标到画布坐标之间转换时,您必须乘以或除以比例。在这种情况下,我将屏幕坐标转换为画布坐标getMousePos
:
function getMousePos(canvasDom, mouseEvent) {
var rect = canvasDom.getBoundingClientRect();
return {
x: (mouseEvent.clientX - rect.left) * scale,
y: (mouseEvent.clientY - rect.top) * scale
};
}
这是一个片段,它显示了所有这些:
var canvas = document.getElementById("sig-canvas");
var ctx = canvas.getContext("2d")
var scale = 2;
canvas.width = 1280 * scale;
canvas.height = 739 * scale;
var drawing = false;
var mousePos = { x:0, y:0 };
var lastPos = mousePos;
canvas.addEventListener("mousemove", function (e) {
mousePos = getMousePos(canvas, e);
drawing = true;
}, false);
// Get the position of the mouse relative to the canvas
function getMousePos(canvasDom, mouseEvent) {
var rect = canvasDom.getBoundingClientRect();
return {
x: (mouseEvent.clientX - rect.left) * scale,
y: (mouseEvent.clientY - rect.top) * scale
};
}
// Get a regular interval for drawing to the screen
window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimaitonFrame ||
function (callback) {
window.setTimeout(callback, 1000/60);
};
})();
// Draw to the canvas
function renderCanvas() {
if (drawing) {
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(mousePos.x, mousePos.y, 25, 25);
ctx.strokeStyle = "#000000";
ctx.lineWidth = 2;
ctx.strokeRect(mousePos.x, mousePos.y, 25, 25);
lastPos = mousePos;
}
}
// Allow for animation
(function drawLoop () {
requestAnimFrame(drawLoop);
renderCanvas();
})();
#sig-canvas {
width: 1280px;
height: 739px;
}
canvas {
border: 1px solid black;
}
body {
margin: 0px;
}
<canvas id="sig-canvas"></canvas>
推荐阅读
- javascript - 从 React 中的嵌套函数内部获取返回值时遇到问题。可能是范围,但可能是异步问题
- javascript - 在套接字 io 函数中重用变量
- c# - 如何跨多个 TPL DataFlow 块跨越 MaxDegreeOfParallelism?
- html - 为什么这个动画在打开时运行流畅,但在关闭时会卡住?
- sql - 在 SQL 中将 Varchar 转换为十进制错误
- powershell - 跳过大括号的调试器
- python - 使用 Anaconda Navigator 安装 Tensorflow 会为 Python 3.7 提供 UnsatisfiableError
- performance - 如何在 Hive 中的视图上运行查询?
- c# - 使用 TCP 和套接字等待输入时如何避免指定字节数组长度
- regex - 如何通过正则表达式匹配从多个字符(可以嵌套)开始的字符串?