查看专栏目录
canvas实例应用100+专栏,提供canvas的基础知识,高级动画,相关应用扩展等信息。canvas作为html的一部分,是图像图标地图可视化的一个重要的基础,学好了canvas,在其他的一些应用上将会起到非常重要的帮助。
文章目录
- 示例效果图
- 示例源代码(共94行)
- canvas基本属性
- canvas基础方法
如何使用canvas绘制正三边形,正四边形,正五边形…正N边形呢?这里封装了一个函数regulaPolygon(cxt, n, dx, dy, R, angle, djs_fillColor, djs_strokeColor),可以设定不同的值,来产生不同正多边形。
示例效果图
示例源代码(共94行)
/*
* @Author: 大剑师兰特(xiaozhuanlan),还是大剑师兰特(CSDN)
* @此源代码版权归大剑师兰特所有,可供学习或商业项目中借鉴,未经授权,不得重复地发表到博客、论坛,问答,git等公共空间或网站中。
* @Email: 2909222303@qq.com
* @weixin: gis-dajianshi
* @First published in CSDN
* @First published time: 2024-01-21
*/
<template><div class="djs_container"><div class="top"><h3>canvas绘制正N边形</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() {this.regulaPolygon(this.ctx, 3, 100, 245, 70, -10, 'red', 'black');this.regulaPolygon(this.ctx, 4, 250, 245, 70, 10, 'orange', 'black');this.regulaPolygon(this.ctx, 5, 400, 245, 70, 20, 'yellow', 'black');this.regulaPolygon(this.ctx, 6, 550, 245, 70, -20, 'green', 'black');this.regulaPolygon(this.ctx, 7, 700, 245, 70, 90, 'blue', 'black');this.regulaPolygon(this.ctx, 8, 850, 245, 70, 90, 'purple', 'black');},//边数n中心点dx,中心点dy,半径R,旋转角度angleregulaPolygon(cxt, n, dx, dy, R, angle, djs_fillColor, djs_strokeColor) {cxt.beginPath();let degree = (2 * Math.PI) / n;let rangle = (angle * 2 * Math.PI) / 360;for (let i = 0; i < n; i++) {let x = Math.cos(i * degree - rangle);let y = Math.sin(i * degree - rangle);cxt.lineTo(x * R + dx, y * R + dy);}cxt.closePath();cxt.fillStyle = djs_fillColor;cxt.fill();cxt.strokeStyle = djs_strokeColor;cxt.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() |