当前位置:网站首页>canvas基础2 - arc - 画弧线
canvas基础2 - arc - 画弧线
2022-07-06 09:21:00 【玲小叮当】
系列文章目录
本文是canvas基础2 - arc - 画弧线,通俗易懂,小白也没烦恼,根据慕课网liuyubobobo老师的视频课学习整理
视频课指路:慕课网 liuyubobobo老师 炫丽的倒计时效果Canvas绘图与动画基础
1、arc方法绘制弧线
context.arc(圆心坐标x,圆心坐标y,半径值,从哪一个弧度值为始,结束弧度,弧度为顺时针还是逆时针默认false为顺时针)
startingAngle和endingAngle说明
2、画圆
- 顺时针画圆
<canvas id="canvas" style="background-color: rgb(200,200,200);display: block;margin: 20px auto;">
当前浏览器不支持canvas,请更换浏览器后再试
</canvas>
<script> var canvas = document.getElementById('canvas') canvas.width = 1024 canvas.height = 768 if (canvas.getContext('2d')) {
var context = canvas.getContext('2d') context.lineWidth = 5 context.strokeStyle = '#005588' context.arc(300, 300, 200, 0, 1.5 * Math.PI) context.stroke() } </script>

2. 逆时针画圆
<canvas id="canvas" style="background-color: rgb(200,200,200);display: block;margin: 20px auto;">
当前浏览器不支持canvas,请更换浏览器后再试
</canvas>
<script> var canvas = document.getElementById('canvas') canvas.width = 1024 canvas.height = 768 if (canvas.getContext('2d')) {
var context = canvas.getContext('2d') context.lineWidth = 5 context.strokeStyle = '#005588' context.arc(300, 300, 200, 0, 1.5 * Math.PI, true) context.stroke() } </script>

3、closePath特性
closePath代表结束这个路径,如果当前路径没有封闭上,会自动封闭这个路径

4、2048棋盘demo
<canvas id="canvas" width="800" height="800" style="display: block; background-color: #eee;margin: 10px auto;">
不能使用canvas
</canvas>
<script> window.onload = function () {
var canvas = document.getElementById('canvas') var context = canvas.getContext('2d') // drawRoundRect(context, 10, 10, 600, 500, 50) // fillRoundRect(context, 50, 50, 600, 500, 50, '#500') fillRoundRect(context, 150, 150, 500, 500, 10, '#bbada0') for (var i = 0; i < 4; i++) for (var j = 0; j < 4; j++) fillRoundRect(context, 170 + i * 120, 170 + j * 120, 100, 100, 6, '#ccc0b3') } function drawRoundRect(cxt, x, y, width, height, radius) {
if (2 * radius > width || 2 * radius > height) return; cxt.save() cxt.translate(x, y) pathRoundRect(cxt, width, height, radius) cxt.strokeStyle = '#000' cxt.stroke() cxt.restore() } function fillRoundRect(cxt, x, y, width, height, radius, fillColor) {
if (2 * radius > width || 2 * radius > height) return; cxt.save() cxt.translate(x, y) pathRoundRect(cxt, width, height, radius) cxt.fillStyle = fillColor || '#000' cxt.fill() cxt.restore() } function pathRoundRect(cxt, width, height, radius) {
cxt.beginPath() cxt.arc(width - radius, height - radius, radius, 0, Math.PI / 2) cxt.lineTo(radius, height) cxt.arc(radius, height - radius, radius, Math.PI / 2, Math.PI) cxt.lineTo(0, radius) cxt.arc(radius, radius, radius, Math.PI, Math.PI * 3 / 2) cxt.lineTo(width - radius, 0) cxt.arc(width - radius, radius, radius, Math.PI * 3 / 2, Math.PI * 2) cxt.closePath() } </script>
总结
本文为canvas第二节,之后会持续更新,大家感觉还实用的话,关注或者点个赞都可以,谢谢啦
边栏推荐
- [中国近代史] 第六章测验
- Redis cache obsolescence strategy
- [the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
- 4.二分查找
- ABA问题遇到过吗,详细说以下,如何避免ABA问题
- C语言入门指南
- The latest tank battle 2022 - full development notes-3
- 受检异常和非受检异常的区别和理解
- [modern Chinese history] Chapter V test
- Network layer 7 protocol
猜你喜欢

3.C语言用代数余子式计算行列式
![[面試時]——我如何講清楚TCP實現可靠傳輸的機制](/img/d6/109042b77de2f3cfbf866b24e89a45.png)
[面試時]——我如何講清楚TCP實現可靠傳輸的機制

Redis的两种持久化机制RDB和AOF的原理和优缺点

Leetcode.3 无重复字符的最长子串——超过100%的解法

Custom RPC project - frequently asked questions and explanations (Registration Center)

View UI plus released version 1.3.0, adding space and $imagepreview components

Cloud native trend in 2022

The latest tank battle 2022 - Notes on the whole development -2

(super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow

3.猜数字游戏
随机推荐
Leetcode.3 无重复字符的最长子串——超过100%的解法
2.C语言初阶练习题(2)
8. C language - bit operator and displacement operator
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
MySQL limit x, -1 doesn't work, -1 does not work, and an error is reported
Redis cache obsolescence strategy
C language Getting Started Guide
MPLS experiment
7.数组、指针和数组的关系
There is always one of the eight computer operations that you can't learn programming
【手撕代码】单例模式及生产者/消费者模式
Application architecture of large live broadcast platform
1. C language matrix addition and subtraction method
2. Preliminary exercises of C language (2)
Redis的两种持久化机制RDB和AOF的原理和优缺点
[hand tearing code] single case mode and producer / consumer mode
Pit avoidance Guide: Thirteen characteristics of garbage NFT project
MySQL锁总结(全面简洁 + 图文详解)
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
9. Pointer (upper)