canvas绘制旋转的大风车

在这里插入图片描述

查看专栏目录

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

如何使用canvas绘制旋转的大风车呢?这里先通过画半圆的方式,绘制6个半圆组成一个风车的形状。这里面最重要的两点是:使用translate将原点错位到中心,否则rotate会出现问题。 第二点是clearCanvas时候,由于错位了,响应的清除位置要改变一下,设置为translate的负数。

示例效果图

在这里插入图片描述

示例源代码(共105行)

/*
* @Author: 大剑师兰特(xiaozhuanlan),还是大剑师兰特(CSDN)
* @此源代码版权归大剑师兰特所有,可供学习或商业项目中借鉴,未经授权,不得重复地发表到博客、论坛,问答,git等公共空间或网站中。
* @Email: 2909222303@qq.com
* @weixin: gis-dajianshi
* @First published in CSDN
* @First published time: 2024-01-26
*/
<template>
	<div class="djs_container">
		<div class="top">
			<h3>canvas绘制旋转的大风车</h3>
			<div>大剑师兰特, 还是大剑师兰特,gis-dajianshi</div>
			<h4>
				<el-button type="primary" size="mini" @click="start()">开始</el-button>
				<el-button type="danger" size="mini" @click="stop()">停止</el-button>
			</h4>
		</div>
		<div class="dajianshi ">
			<canvas id="dajianshi" ref="mycanvas" width="490" height="490"></canvas>
		</div>
	</div>
</template>
<script>
	export default {
   
		data() {
   
			return {
   
				ctx: null,
				canvas: null,
				timer: null,
				colors: [],
			}
		},
		mounted() {
   
			this.setCanvas();
		},
		methods: {
   
			clearCanvas() {
   
				this.ctx.clearRect(-245, -245, 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(245, 245); //将原点设置为(490,245)
				this.colors = ['red', 'orange', 'yellow', 'green', 'cyan', 'blue']

			},
			start() {
   
				this.drawWindmill(this.ctx);
				this.timer = setInterval(() => {
   
					this.clearCanvas();
					this.ctx.rotate(2 * Math.PI / 180);
					this.drawWindmill(this.ctx);
				}, 20);
			},
			stop() {
   
				clearInterval(this.timer);
				this.clearCanvas();
			},
			drawWindmill(djs_cxt) {
   
				for (let i = 0; i < 6; i++) {
   
					//画扇叶					
					djs_cxt.beginPath();
					djs_cxt.arc(80, 0, 80, 0, Math.PI, false)
					djs_cxt.fillStyle = this.colors[i];
					djs_cxt.fill();
					//将坐标系旋转60度
					djs_cxt.rotate(60 * Math.PI / 180);
				}

				//画中心圆
				djs_cxt.fillStyle = "white";
				djs_cxt.beginPath();
				djs_cxt.arc(0, 0, 20, 0, Math.PI * 2, true);
				djs_cxt.fill();

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

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

	.dajianshi {
   
		margin: 5px auto 0;
		border: 1px solid #ccc;
		width: 490px;
		height: 490px;
		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()

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-29 10:42:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-29 10:42:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-29 10:42:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-29 10:42:03       18 阅读

热门阅读

  1. 计算机视觉(CV)技术的优势和挑战

    2024-01-29 10:42:03       31 阅读
  2. 【漏洞复现】金蝶云星空-AppDesigner-反序列化-rce

    2024-01-29 10:42:03       35 阅读
  3. Vue3+Echarts实现实时曲线及开始与暂停功能

    2024-01-29 10:42:03       31 阅读
  4. Qlik Sense : IntervalMatch(离散匹配)

    2024-01-29 10:42:03       29 阅读
  5. Leetcode 3021. Alice and Bob Playing Flower Game

    2024-01-29 10:42:03       36 阅读
  6. 记录 | ubuntu查看系统信息如系统版本、cpu信息

    2024-01-29 10:42:03       33 阅读
  7. ubuntu 添加 sudo 权限

    2024-01-29 10:42:03       31 阅读
  8. Matlab自学笔记二十六:sprintf函数用法简介

    2024-01-29 10:42:03       38 阅读