canvas绘制横竖坐标轴(带有箭头和刻度)

在这里插入图片描述

查看专栏目录

canvas实例应用100+专栏,提供canvas的基础知识,高级动画,相关应用扩展等信息。canvas作为html的一部分,是图像图标地图可视化的一个重要的基础,学好了canvas,在其他的一些应用上将会起到非常重要的帮助。

canvas如何绘制带有箭头和标有刻度的横竖坐标轴呢? 首先使用for循环遍历横坐标轴的刻度位置,然后使用beginPath()、moveTo()和lineTo()方法绘制刻度线,接着使用font、textAlign和fillText()方法设置字体样式、对齐方式和填充文本内容,最后使用stroke()方法将它们渲染到画布上。同样的操作也应用于纵坐标轴的刻度。

示例效果图

在这里插入图片描述

示例源代码(共79行)


/*
* @Author: 大剑师兰特(xiaozhuanlan),还是大剑师兰特(CSDN)
* @此源代码版权归大剑师兰特所有,可供学习或商业项目中借鉴,未经授权,不得重复地发表到博客、论坛,问答,git等公共空间或网站中。
* @Email: [email protected]
* @weixin: gis-dajianshi
* @First published in CSDN
* @First published time: 2024-02-08
*/
<template>
	<div class="djs_container">
		<div class="top">
			<h3>canvas绘制带有箭头和标有刻度的横竖坐标轴</h3>
			<div>大剑师兰特, 还是大剑师兰特,gis-dajianshi</div>
			<h4>
				<el-button type="success" size="mini" @click="draw()">绘制</el-button>
				<el-button type="danger" size="mini" @click="clearCanvas()">清除画屏</el-button>
			</h4>
		</div>
		<div class="dajianshi ">
			<canvas id="dajianshi" ref="mycanvas" width="980" height="480"></canvas>
		</div>
	</div>
</template>
<script>
	export default {
    
    
		data() {
    
    
			return {
    
    
				ctx: null,
				canvas: null,
			}
		},
		mounted() {
    
    
			this.setCanvas()
		},
		methods: {
    
    
			clearCanvas() {
    
    
				this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
			},
			setCanvas() {
    
    
				this.canvas = document.getElementById('dajianshi');
				if (!this.canvas.getContext) return;
				this.ctx = this.canvas.getContext("2d");
				this.ctx.translate(200,50)
			},

			draw() {
    
    
				let ctx=this.ctx
				// 绘制横坐标轴
				ctx.beginPath();
				ctx.moveTo(50, 350);
				ctx.lineTo(680, 350);
				ctx.stroke();

				// 绘制纵坐标轴
				ctx.beginPath();
				ctx.moveTo(50, 50);
				ctx.lineTo(50, 350);
				ctx.stroke();

				// 绘制横坐标轴刻度
				for (let i = 1; i <= 15; i++) {
    
    
					const x = 50 + i * 40;
					ctx.beginPath();
					ctx.moveTo(x, 350);
					ctx.lineTo(x, 360);
					ctx.stroke();
					  // 绘制刻度值
					  ctx.font = '12px Arial';
					  ctx.textAlign = 'center';
					  ctx.fillText(i, x, 370);
					  
				}
				ctx.fillText('0', 50, 370);

				// 绘制纵坐标轴刻度
				for (let i = 1; i <= 9; i++) {
    
    
					const y = 350 - i * 30;
					ctx.beginPath();
					ctx.moveTo(50, y);
					ctx.lineTo(40, y);
					ctx.stroke();
					  // 绘制刻度值
					  ctx.font = '12px Arial';
					  ctx.textAlign = 'right';
					  ctx.fillText(i, 45, y);
				}

				// 绘制横坐标轴箭头
				ctx.beginPath();
				ctx.moveTo(680, 340);
				ctx.lineTo(690, 350);
				ctx.lineTo(680, 360);
				ctx.closePath();
				ctx.fillStyle = 'red';
				ctx.fill();

				// 绘制纵坐标轴箭头
				ctx.beginPath();
				ctx.moveTo(40, 55);
				ctx.lineTo(50, 45);
				ctx.lineTo(60, 55);
				ctx.closePath();
				ctx.fillStyle = 'red';
				ctx.fill();
			},

		}
	}
</script>
<style scoped>
	.djs_container {
    
    
		width: 1000px;
		height: 680px;
		margin: 50px auto;
		border: 1px solid #222;
		position: relative;
	}

	.top {
    
    
		margin: 0 auto 0px;
		padding: 10px 0;
		background: #222;
		color: #fff;
	}

	.dajianshi {
    
    
		margin: 5px auto 0;
		border: 1px solid #cde;
		width: 980px;
		height: 480px;
		background-color: #eee;
	}

</style>




canvas基本属性

属性 属性 属性
canvas fillStyle filter
font globalAlpha globalCompositeOperation
height lineCap lineDashOffset
lineJoin lineWidth miterLimit
shadowBlur shadowColor shadowOffsetX
shadowOffsetY strokeStyle textAlign
textBaseline width

canvas基础方法

方法 方法 方法
arc() arcTo() addColorStop()
beginPath() bezierCurveTo() clearRect()
clip() close() closePath()
createImageData() createLinearGradient() createPattern()
createRadialGradient() drawFocusIfNeeded() drawImage()
ellipse() fill() fillRect()
fillText() getImageData() getLineDash()
isPointInPath() isPointInStroke() lineTo()
measureText() moveTo() putImageData()
quadraticCurveTo() rect() restore()
rotate() save() scale()
setLineDash() setTransform() stroke()
strokeRect() strokeText() transform()
translate()

猜你喜欢

转载自blog.csdn.net/cuclife/article/details/136003203