查看专栏目录
canvas示例教程100+专栏,提供canvas的基础知识,高级动画,相关应用扩展等信息。canvas作为html的一部分,是图像图标地图可视化的一个重要的基础,学好了canvas,在其他的一些应用上将会起到非常重要的帮助。
怎样使用canvas绘制美队盾牌呢?这里面要绘制四个不同大小的同心圆,同时在中间位置绘制一个五角星。文章来源:https://www.toymoban.com/news/detail-800944.html
示例效果图
文章来源地址https://www.toymoban.com/news/detail-800944.html
示例源代码(共100行)
/*
* @Author: 大剑师兰特(xiaozhuanlan),还是大剑师兰特(CSDN)
* @此源代码版权归大剑师兰特所有,可供学习或商业项目中借鉴,未经授权,不得重复地发表到博客、论坛,问答,git等公共空间或网站中。
* @Email: 2909222303@qq.com
* @weixin: gis-dajianshi
* @First published in CSDN
* @First published time: 2024-01-17
*/
<template>
<div class="djs_container">
<div class="top">
<h3>canvas绘制美队盾牌</h3>
<div>大剑师兰特, 还是大剑师兰特,gis-dajianshi</div>
<h4>
<el-button type="primary" 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="490"></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");
},
draw() {
let c = this.$refs.mycanvas;
this.drawCircle(this.ctx, 490, 245,200,'rgb(197,30,58)')
this.drawCircle(this.ctx, 490, 245,160,'rgb(224,222,223)')
this.drawCircle(this.ctx, 490, 245,120,'rgb(197,30,58)')
this.drawCircle(this.ctx, 490, 245,80,'rgb(1,43,127)')
this.draw5star(this.ctx, 490, 245, 78, 30, 0,'white','white');
},
drawCircle(ctx, x, y, radius,fillColor){
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.fillStyle = fillColor;
ctx.fill();
},
draw5star(ctx, x, y, R, r, angle,fillColor,strokeColor) { //中心点x,中心点y,大半径R,小半径r,旋转角度angle
ctx.beginPath();
for (var i = 0; i < 5; i++) {
ctx.lineTo(Math.cos((18 + i * 72 - angle) / 180 * Math.PI) * R + x, -Math.sin((18 + i * 72 - angle) /
180 * Math.PI) * R + y);
ctx.lineTo(Math.cos((54 + i * 72 - angle) / 180 * Math.PI) * r + x, -Math.sin((54 + i * 72 - angle) /
180 * Math.PI) * r + y);
}
ctx.closePath();
ctx.fillStyle = fillColor;
ctx.strokeStyle = strokeColor;
ctx.lineWidth = 3;
ctx.lineJoin = "round";
ctx.fill();
ctx.stroke();
}
}
}
</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: 980px;
height: 490px;
background-color: #f9f9f9;
}
</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() |
到了这里,关于canvas绘制美队盾牌的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!