我已经尝试了我在这里看到的所有内容,但似乎没有任何关于我的代码,或者我只是不知道如何应用它。
我试图在我的html画布中获得更好的分辨率,因为矩形看起来有点“模糊”。
这是我的代码: 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();
})();
这是我的小提琴:https://jsfiddle.net/8cp5qmob/
谢谢!
答案 0 :(得分:1)
为了提高画布的分辨率,我将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();
})();
&#13;
#sig-canvas {
width: 1280px;
height: 739px;
}
canvas {
border: 1px solid black;
}
body {
margin: 0px;
}
&#13;
<canvas id="sig-canvas"></canvas>
&#13;
答案 1 :(得分:0)
您应该让canvas
元素使用%
来定义大小,以便它始终是页面上可以提供的最大尺寸,这也有助于解决问题
答案 2 :(得分:0)
据我所知,他们似乎对我来说并不模糊。但如果你想确定你可以随时替换
ctx.fillStyle = "#FFFFFF"; & ctx.strokeStyle = "#000000";
使用
ctx.fillStyle = "rgba(255, 255, 255, 1)"; & ctx.strokeStyle = "rgba(0, 0, 0, 1)";
这应该使该行始终处于最大不透明度。