利用canvas画直角坐标系

先看效果图:

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Canvas</title>
	<style>
	</style>
</head>

<body>
	<canvas id="canvas" style="border:1px solid #ccc"></canvas>
</body>
<script>
let canvasObj = {
	setup: function(){
		this.width = 800
		this.height = 450
		this.padding = {
			left: 50,
			right: 20,
			top: 20,
			bottom:30
		}
		this.arrowWidth = 2
	},
	init: function(id){
		this.setup()
		this.canvas = document.getElementById(id)
		this.canvas.width = this.width
		this.canvas.height = this.height
		this.ctx = this.canvas.getContext("2d")
		this.ctx.strokeStyle = "#00f"
		this.ctx.fillStyle = "#00f"
		this.drawXY()
		this.drawArrow()
		this.drawText()
	},
	// 画XY轴
	drawXY: function(){
		position = this.setPosition()
		this.ctx.beginPath();
		this.ctx.moveTo(position.yPt.startX,position.yPt.startY)
		this.ctx.lineTo(position.yPt.endX,position.yPt.endY)
		this.ctx.lineTo(position.xPt.endX,position.xPt.endY)
		this.ctx.stroke()
	},
	// 画箭头
	drawArrow: function(){
		position = this.setPosition()
		this.ctx.beginPath();
		this.ctx.moveTo(position.xArrow.startX,position.xArrow.startY)
		this.ctx.lineTo(position.xArrow.middleX,position.xArrow.middleY)
		this.ctx.lineTo(position.xArrow.endX,position.xArrow.endY)
		this.ctx.fill()
		this.ctx.beginPath();
		this.ctx.moveTo(position.yArrow.startX,position.yArrow.startY)
		this.ctx.lineTo(position.yArrow.middleX,position.yArrow.middleY)
		this.ctx.lineTo(position.yArrow.endX,position.yArrow.endY)
		this.ctx.fill()
	},
	// 写刻度和文字
	drawText: function(){		
		for(let i = 0; i<this.canvas.width/80-1; i++){
			if (i > 0)
				// 写刻度
				this.ctx.beginPath();
				this.ctx.moveTo(80*i+this.padding.left, this.canvas.height-40)
				this.ctx.lineTo(80*i+this.padding.left, this.canvas.height-30)
				this.ctx.stroke()
				// 写文字
				xText =  (0.0+i*0.8).toFixed(2) + "mm"
				this.ctx.fillText(xText, 80*i+this.padding.left-20, this.canvas.height-12)
		}
		for(let i = 0; i<(this.canvas.height-(this.padding.top + this.padding.bottom))/20; i++){
			this.ctx.beginPath();
			this.ctx.moveTo(this.padding.left, 20*i + this.padding.bottom)
			this.ctx.lineTo(this.padding.left-10, 20*i + this.padding.bottom)
			this.ctx.stroke()
			this.ctx.textAlign = "right"
				yText = (0.075+i*0.005).toFixed(3)
				this.ctx.fillText(yText, this.padding.left-15, this.canvas.height-36-20*i)
			if (i > 0){
			}	
		}
	},
	setPosition: function(){
		xPt = {
			startX: this.padding.left,
			startY: this.canvas.height - this.padding.bottom,
			endX: this.canvas.width - this.padding.right,
			endY: this.canvas.height - this.padding.bottom
		}
		yPt = {
			startX: this.padding.left,
			startY: this.padding.top,
			endX: this.padding.left,
			endY: this.canvas.height- this.padding.bottom
		}
		xArrow = {
			startX: xPt.endX,
			startY: xPt.endY - this.arrowWidth,
			middleX: xPt.endX + 10,
			middleY: xPt.endY,
			endX: xPt.endX,
			endY: xPt.endY + this.arrowWidth
		}
		yArrow = {
			startX: yPt.startX - this.arrowWidth,
			startY: yPt.startY,
			middleX: yPt.startX,
			middleY: yPt.startY - 10,
			endX: yPt.startX + this.arrowWidth,
			endY: yPt.startY
		}
		return {xPt, yPt, xArrow, yArrow}
	}

}
canvasObj.init("canvas")
</script>
</html>

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以帮您解答这个问题。下面是利用Canvas进行绘制XY坐标系的代码示例: ```html <!DOCTYPE html> <html> <head> <title>Canvas绘制XY坐标系</title> <style> canvas { border: 1px solid #000; } </style> </head> <body> <canvas id="myCanvas" width="400" height="400"></canvas> <script> // 获取canvas元素 var canvas = document.getElementById("myCanvas"); // 获取canvas上下文 var ctx = canvas.getContext("2d"); // 绘制X轴 ctx.beginPath(); ctx.moveTo(20, 380); ctx.lineTo(380, 380); ctx.stroke(); // 绘制Y轴 ctx.beginPath(); ctx.moveTo(20, 20); ctx.lineTo(20, 380); ctx.stroke(); // 绘制X轴箭头 ctx.beginPath(); ctx.moveTo(380, 380); ctx.lineTo(370, 370); ctx.moveTo(380, 380); ctx.lineTo(370, 390); ctx.stroke(); // 绘制Y轴箭头 ctx.beginPath(); ctx.moveTo(20, 20); ctx.lineTo(10, 30); ctx.moveTo(20, 20); ctx.lineTo(30, 30); ctx.stroke(); // 绘制X轴刻度 var stepX = 40; for (var i = 0; i < 8; i++) { ctx.beginPath(); ctx.moveTo(20 + stepX * (i + 1), 380); ctx.lineTo(20 + stepX * (i + 1), 370); ctx.stroke(); } // 绘制Y轴刻度 var stepY = 40; for (var i = 0; i < 8; i++) { ctx.beginPath(); ctx.moveTo(20, 380 - stepY * (i + 1)); ctx.lineTo(30, 380 - stepY * (i + 1)); ctx.stroke(); } // 标注X轴坐标 ctx.font = "12px Arial"; ctx.fillText("0", 10, 395); ctx.fillText("1", 50, 395); ctx.fillText("2", 90, 395); ctx.fillText("3", 130, 395); ctx.fillText("4", 170, 395); ctx.fillText("5", 210, 395); ctx.fillText("6", 250, 395); ctx.fillText("7", 290, 395); ctx.fillText("8", 330, 395); // 标注Y轴坐标 ctx.fillText("0", 5, 385); ctx.fillText("1", 5, 345); ctx.fillText("2", 5, 305); ctx.fillText("3", 5, 265); ctx.fillText("4", 5, 225); ctx.fillText("5", 5, 185); ctx.fillText("6", 5, 145); ctx.fillText("7", 5, 105); ctx.fillText("8", 5, 65); </script> </body> </html> ``` 这段代码使用了Canvas的API来绘制XY坐标系,其中包括绘制坐标轴、箭头、刻度和标注坐标等步骤。您可以将代码复制到一个HTML文件中并在浏览器中打开来查看效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东成2022

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值