我在学校学习javascript的基础知识,现在我正在画布上工作
This is my code个
canvas = document.getElementById("myCanvas")
ctx = canvas.getContext("2d")
offset = canvas.width / 2
function assi() {
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.beginPath()
ctx.moveTo(offset, 0)
ctx.lineTo(offset, offset * 2)
ctx.moveTo(0, offset)
ctx.lineTo(offset * 2, offset)
ctx.stroke()
}
function f(x) {
return Math.pow(2, x) * -1;
}
function draw() {
assi()
ctx.beginPath()
ctx.strokeStyle = "red"
moveTo(offset, f(0) + offset)
for (let x = -offset; x < offset; x++) {
ctx.lineTo(x + offset, f(x) + offset)
}
ctx.stroke()
}
<body onload="draw()">
<canvas height="800" width="800" id="myCanvas"></canvas>
<script src="script.js"></script>
</body>
This is my question
why in the function f(x)
if I leave *-1
it doesn't draw anything, while if I delete *-1
it draws something?
它画的是线性函数,但是指数函数给出了问题,而不是Math.pow()
问题,因为如果我使用1作为基数,它就可以工作(实际上画了一条线,但是正确的)
The 100 is needed to mirror the y-axis of canvas axis system (up to down) into cartesian axis system (down to up)