canvas实例应用100+
The column provides basic knowledge of canvas, advanced animation, related application extensions and other information.
As a part of HTML, canvas is an important foundation for the visualization of images, icons and maps. Learning canvas will be very important in other applications.
Article directory
How to draw a five-pointed star using canvas? Define the five-pointed star function. The angle of the trigonometric function in js needs to be converted into radians, curCanvas canvas reference, x, y five-pointed star offset, R five-pointed star's circumscribed large circle radius, r five-pointed star's inscribed small circle radius, rot five-pointed star's overall rotation angle
Example renderings
Sample source code (88 lines in total)
/*
* @Author: 大剑师兰特(xiaozhuanlan),还是大剑师兰特(CSDN)
* @此源代码版权归大剑师兰特所有,可供学习或商业项目中借鉴,未经授权,不得重复地发表到博客、论坛,问答,git等公共空间或网站中。
* @Email: [email protected]
* @weixin: gis-dajianshi
* @First published in CSDN
* @First published time: 2023-12-27
*/
<template>
<div class="container">
<div class="top">
<h3>canvas随机绘制100个五角星</h3>
<div>大剑师兰特, 还是大剑师兰特,gis-dajianshi</div>
<h4>
<el-button type="primary" size="mini" @click="draw100()">绘制星星</el-button>
</h4>
</div>
<div class="dajianshi ">
<canvas id="dajianshi" ref="mycanvas" width="980" height="400"></canvas>
</div>
</div>
</template>
<script>
export default {
data() {
return {
ctx: null,
}
},
mounted() {
this.setCanvas()
},
methods: {
setCanvas() {
var canvas = document.getElementById('dajianshi');
if (!canvas.getContext) return;
this.ctx = canvas.getContext("2d");
},
draw100() {
let c=this.$refs.mycanvas;
this.ctx.fillStyle = "#0088cc";
this.ctx.fillRect(0, 0, c.width, c.height);
for (var i = 0; i < 100; i++) {
//随机大圆半径R
var cR = Math.random() * 10 + 5;
var cx = Math.random() * c.width;
var cy = Math.random() * c.height;
//随机旋转角度
var ct = Math.random() * 360;
this.star(this.ctx, cx, cy, cR, cR / 2, ct);
}
},
star(curCanvas, x, y, R, r, rot) {
curCanvas.beginPath();
for (var i = 0; i < 5; i++) {
curCanvas.lineTo(Math.cos((18 + i * 72 - rot) / 180 * Math.PI) * R + x, -Math.sin((18 + i * 72 - rot) /
180 * Math.PI) * R + y);
//此处的(18+i*72-rot)为五角星外顶点与中心水平线夹角度数(不是弧度,假设旋转角度rot为0,相当于正五角星),外顶点x坐标每次变化72度
//五角星外顶点(x,y)偏移量
curCanvas.lineTo(Math.cos((54 + i * 72 - rot) / 180 * Math.PI) * r + x, -Math.sin((54 + i * 72 - rot) /
180 * Math.PI) * r + y);
//此处的(54+i*72-rot)为五角星内顶点与中心水平线夹角度数(不是弧度,假设旋转角度rot为0,相当于正五角星),内顶点x坐标每次变化72度
//五角星内顶点(x,y)偏移量
}
curCanvas.closePath();
curCanvas.fillStyle = "#fd5";
curCanvas.strokeStyle = "#fb5";
curCanvas.lineWidth = 3;
curCanvas.lineJoin = "round";
curCanvas.fill();
curCanvas.stroke();
}
}
}
</script>
<style scoped>
.container {
width: 1000px;
height: 600px;
margin: 50px auto;
border: 1px solid green;
position: relative;
}
.top {
margin: 0 auto 0px;
padding: 10px 0;
background:forestgreen;
color: #fff;
}
.dajianshi {
margin: 10px auto 0;
border: 1px solid #ccc;
width: 980px;
height: 400px;
background-color: #f9f9f9;
}
</style>
canvas basic properties
Attributes | Attributes | Attributes |
---|---|---|
canvas | fillStyle | filter |
font | globalAlpha | globalCompositeOperation |
height | lineCap | lineDashOffset |
lineJoin | lineWidth | miterLimit |
shadowBlur | shadowColor | shadowOffsetX |
shadowOffsetY | strokeStyle | textAlign |
textBaseline | width |