在下面的代码片段中,我无法获得正在进行中的图形计算器的y轴厚度,以便对变量axes.y.thickness做出正确响应。
var axes = {
x: {
show: true,
keepOnScreen: false,
thickness: 1,
color: {
red: 0,
blue: 0,
green: 0
}
},
y: {
show: true,
keepOnScreen: false,
thickness: 10,
color: {
red: 0,
blue: 0,
green: 0
}
}
};
function drawAxes() {
strokeWeight(axes.x.thickness);
strokeColor(axes.x.color.red, axes.x.color.green, axes.x.color.blue);
if (axes.x.show) {
if (axes.x.keepOnScreen && sign(range.x.min) === sign(range.x.max) && sign(range.x.min) !== 0) {
if (sign(range.x.min) === -1) {
line(400, 0, 400, 400);
} else {
line(0, 0, 0, 400);
}
} else {
line(map(0, range.x.min, range.x.max, 0, 400), 0, map(0, range.x.min, range.x.max, 0, 400), 400);
}
}
strokeWeight(axes.y.thickness);
strokeColor(axes.y.color.red, axes.y.color.green, axes.y.color.blue);
if (axes.y.show) {
if (axes.y.keepOnScreen && sign(range.y.min) === sign(range.y.max) && sign(range.y.min) !== 0) {
if (sign(range.y.min) === -1) {
line(0, 0, 400, 0);
} else {
line(0, 400, 400, 400);
}
} else {
line(0, map(0, range.y.min, range.y.max, 0, 400), 400, map(0, range.y.min, range.y.max, 0, 400));
}
}
};
drawAxes();
这是整个计划:
<!DOCTYPE html>
<html>
<head>
<style>
canvas {
border: 1px solid #000000;
}
</style>
<meta charset="utf-8">
<title>Graphing Calculator 2.0</title>
</head>
<body>
<canvas id="canvas" width="400" height="400">Error: Your browser does not support HTML 5 canvases.</canvas>
<script>
//reference: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_usage
//math object: http://www.w3schools.com/js/js_math.asp
//TODO: define map and lerp functions
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
function rgb(red, green, blue) {
return "rgb(" + red + "," + green + "," + blue + ")";
}
function map(value, low1, high1, low2, high2) {
return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
}
function lerp(num1, num2, amount) {
return map(amount, 0, 1, num1, num2);
}
function strokeColor(red, green, blue) {
ctx.strokeStyle = rgb(red, green, blue);
}
function strokeWeight(weight) {
ctx.lineWidth = weight;
}
function line(x1, y1, x2, y2) {
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
var colors = {
background: {
red: 0,
green: 0,
blue: 255
},
f: {
red: 0,
green: 171,
blue: 0
}
};
var pos = {
x: 0,
y: 0
};
var sign = function(number) {
if (number === 0) {
return 0;
} else {
return (abs(number)) / number;
}
};
var range = {
x: {
max: 50,
min: -10
},
y: {
max: 10,
min: -10
}
};
var axes = {
x: {
show: true,
keepOnScreen: false,
thickness: 1,
color: {
red: 0,
blue: 0,
green: 0
}
},
y: {
show: true,
keepOnScreen: false,
thickness: 10,
color: {
red: 0,
blue: 0,
green: 0
}
}
};
function f(x, y) {
var leftSide = x;
var rightSide = sq(y) - 5;
return abs(leftSide - rightSide) <= 1;
}
function drawAxes() {
strokeWeight(axes.x.thickness);
strokeColor(axes.x.color.red, axes.x.color.green, axes.x.color.blue);
if (axes.x.show) {
if (axes.x.keepOnScreen && sign(range.x.min) === sign(range.x.max) && sign(range.x.min) !== 0) {
if (sign(range.x.min) === -1) {
line(400, 0, 400, 400);
} else {
line(0, 0, 0, 400);
}
} else {
line(map(0, range.x.min, range.x.max, 0, 400), 0, map(0, range.x.min, range.x.max, 0, 400), 400);
}
}
strokeWeight(axes.y.thickness);
strokeColor(axes.y.color.red, axes.y.color.green, axes.y.color.blue);
if (axes.y.show) {
if (axes.y.keepOnScreen && sign(range.y.min) === sign(range.y.max) && sign(range.y.min) !== 0) {
if (sign(range.y.min) === -1) {
line(0, 0, 400, 0);
} else {
line(0, 400, 400, 400);
}
} else {
line(0, map(0, range.y.min, range.y.max, 0, 400), 400, map(0, range.y.min, range.y.max, 0, 400));
}
}
};
drawAxes();
</script>
</body>
</html>
如果有人可以帮我弄清楚为什么变量axes.x.thickness完美响应,但变量axes.y.thickness仅在其值大于axes.x.thickness的值时才有效,那将是大。此外,解决方案会有所帮助,但可能没有必要。
答案 0 :(得分:0)
您的问题在于,在绘制每一行之前忘记在您的上下文中执行beginPath()
。将此添加到line
函数中可以解决问题:
function line(x1, y1, x2, y2) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}