HTML5画布Canvas
2023-11-30 20:14:38
一、Canvas概念介绍
1、概念 Canvas : 画布
2、作用 : HTML5 Canvas 元素用于图形的绘制, 通过脚本(通常是JavaScript)来完成。它本身只是个图形容器,必须使用脚本来绘制图像
二、画图步骤
1、创建一个画布
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;"></canvas>
注意:<canvas>元素没有边框和内容, 使用 style 属性来添加边框
2、使用JavaScript来绘制图像
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body> <canvas id="myCanvas" width="200" height="100" style="border:1px solid #d3d3d3;">
HTML5 canvas 标签。</canvas> <script> var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d"); ctx.font="30px Arial";
ctx.fillText("Hello World",10,50);
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,20,25); </script> </body>
</html>
JavaScript画图代码步骤 :
①、首先, 找到canvas元素
var c=document.getElementById("myCanvas");
②、创建Context对象
var ctx=c.getContext("2d");
getContext("2d") 对象是内建的HTML5对象, 拥有多种绘制路径、矩形、图形、字符及添加图像的方法
③、业务代码
三、Canvas用法
1、介绍Canvas常用属性
①、fillStyle属性 : 可以是CSS颜色、渐变、图案等
②、font : 定义字体
2、介绍Canvas常用方法
①、fillRect(x, y, width, height): 定义矩形的填充方式
②、fillText(text, x, y): 在canvas绘制文本
③、drawImage(image, x, y): 图像画到画布上
四、参考文献
1、http://www.runoob.com/html/html5-canvas.html
2、http://www.runoob.com/tags/ref-canvas.html
最新文章
- IOS 杂笔-14(被人遗忘的owner)
- ajax同步的实现
- 大M法(Big M Method)
- Codeforces Round #226 (Div. 2)A. Bear and Raspberry
- iOS全景
- Linux企业级项目实践之网络爬虫(6)——将程序设计成为守护进程
- android混淆打包配置(忽略第三方jar)
- 06、NetCore2.0依赖注入(DI)之整合Autofac
- android View层的绘制流程
- 根据不同访问设备跳转到PC页面或手机页面
- js 格式化数字(每三位加逗号)
- python入门学习:7.函数
- C#多线程技术提高RabbitMQ消费吞吐率
- tortoisegit安装
- 《SQL基础教程》
- golang学习笔记15 golang用strings.Split切割字符串
- js 正则常用函数 会正则得永生
- 57.纯 CSS 创作一双黑暗中的眼睛
- [T-ARA][너너너][你你你]
- RedHat 7 安装PostgreSQL 10.5
热门文章
- listview默认选择第一项,点击换子项背景图
- LeetCode: Sqrt
- w3c盒子模型与ie盒子模型
- 去除 waring Method &#39;CreateNew&#39; hides virtual method of base type &#39;TCustomForm&#39;
- hadoop2.0中无法启动datanode的问题
- Super关键字
- linux命令——rm
- codeforces 687D Dividing Kingdom II 带权并查集(dsu)
- 软件测试技术(五)——Software Review
- Richedit使用大全