当前位置:网站首页>《canvas》之第1章 canvas概述
《canvas》之第1章 canvas概述
2022-06-24 06:44:00 【yxqq378287007】
《canvas》之第1章 canvas概述
第1章 canvas概述
1.1 canvas是什么
1.1.1 canvas简介
canvas(画布),html5中,canvas元素结合JavaScript绘制各种图形。
- 绘制图形(矩形、曲线、圆等)
- 绘制图表
- 动画效果
- 游戏开发
1.1.2 canvas与SVG
- canvas使用JavaScript动态生成,SVG使用XML静态描述。
- canvas基于位图,适用于像素和动态渲染,放大会失真;SVG基于矢量,适用静态描述,放大不会失真。
- 发生修改,canvas需要重绘,SVG不需要。
- 类似美术与几何的关系。
1.2 canvas元素
htaml5中使用canvas元素绘制图形三步骤:
- 获取canvas对象。
- 获取上下文环境对象context。
- 开始绘制图形。
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8"/>
<script type="text/javascript"> window.onload = function () {
//1、获取canvas对象 var cnv = document.getElementById("canvas"); //2、获取上下文环境对象context var cxt = cnv.getContext("2d"); //3、开始绘制图形 cxt.moveTo(50, 100); cxt.lineTo(150, 50); cxt.stroke(); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;"></canvas>
</body>
</html>
1.2.1 canvas元素简介
行内块元素(inline-block),一般需指定3个属性,id、width(默认300px)和height(默认150px)。
实际开发中,width和height应该在HTML属性中定义,不要在CSS样式中定义。
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8"/>
<style type="text/css"> canvas {
width: 200px; height: 150px; } </style>
<script type="text/javascript"> window.onload = function () {
var cnv = document.getElementById("canvas"); var str = "canvas的宽度为:" + cnv.width + ",高度为:" + cnv.height; alert(str); //无法获取CSS中width和height,使用默认值300,150。 } </script>
</head>
<body>
<canvas id="canvas" style="border:1px dashed gray;"></canvas>
</body>
</html>
1.2.2 canvas对象
- 常用属性
width、height - 常用方法
getContext(“2d”),获取Canvas 2D上下文环境对象(context对象)
toDataURL(),获取canvas对象产生的位图的字符串
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta charset="utf-8"/>
<script type="text/javascript"> window.onload = function () {
var cnv = document.getElementById("canvas"); var str = "Canvas的宽度为:" + cnv.width + ",高度为:" + cnv.height; alert(str); } </script>
</head>
<body>
<canvas id="canvas" width="200" height="160" style="border:1px dashed gray"></canvas>
</body>
</html>
解惑
- html5 canvas暂时只提供2D绘图API,3D绘图使用html5中WebGL实现。
- IE9及以上版本支持html5 canvas,IE7及IE8兼容性通过explorercanvas扩展解决,会有许多功能限制,无无法使用fillText()方法等。
<!--[if IE]> <script src="https://github.com/arv/explorercanvas/excanvas.js"></script> <![end if]-->
- 推荐canvas教程。
W3C官网:www.w3.org/TR/2dcontext/
边栏推荐
- Spark stage and shuffle for daily data processing
- Global and Chinese market of inline drip irrigation 2022-2028: Research Report on technology, participants, trends, market size and share
- 阿里云全链路数据治理
- [WordPress website] 6 Article content copy prevention
- RDD基础知识点
- [image fusion] image fusion based on directional discrete cosine transform and principal component analysis with matlab code
- Knowledge points of 2022 system integration project management engineer examination: ITSS information technology service
- Dichotomous special training
- 20 not to be missed ES6 tips
- Accessing user interface settings using systemparametersinfo
猜你喜欢

get_started_3dsctf_2016
![[image fusion] image fusion based on pseudo Wigner distribution (PWD) with matlab code](/img/e0/14cd7982fb3059fed235470d91126e.png)
[image fusion] image fusion based on pseudo Wigner distribution (PWD) with matlab code

More than 60 million shovel excrement officials, can they hold a spring of domestic staple food?

Alibaba cloud full link data governance
![[equalizer] bit error rate performance comparison simulation of LS equalizer, def equalizer and LMMSE equalizer](/img/45/61258aa20cd287047c028f220b7f7a.png)
[equalizer] bit error rate performance comparison simulation of LS equalizer, def equalizer and LMMSE equalizer

Learning to use BACnet gateway of building control system is not so difficult

jarvisoj_level2

RDD basic knowledge points

Face pincher: a hot meta universe stylist

PIP install XXX on the terminal but no module named XXX on pycharm
随机推荐
Global and Chinese markets for maritime transport of perishable goods 2022-2028: Research Report on technology, participants, trends, market size and share
10 common malware detection and analysis platforms
How to open the soft keyboard in the computer, and how to open the soft keyboard in win10
[image fusion] image fusion based on NSST and PCNN with matlab code
Knowledge points of 2022 system integration project management engineer examination: ITSS information technology service
Win10 build webservice
相机标定(标定目的、原理)
Event related | reveal how Ti-One's support ability for large-scale events is developed
Spark stage and shuffle for daily data processing
[GUET-CTF2019]zips
buuctf misc [UTCTF2020]docx
How can win11 set the CPU performance to be fully turned on? How does win11cpu set high performance mode?
Dichotomous special training
What should I pay attention to after the live broadcast system source code is set up?
Only two lines are displayed, and the excess part is displayed with Ellipsis
[WUSTCTF2020]alison_likes_jojo
[pointnet] matlab simulation of 3D point cloud target classification and recognition based on pointnet
Deploy L2TP in VPN (medium)
PNAs: Geometric renormalization reveals the self similarity of multi-scale human connectome
A penetration test of c/s Architecture - Request encryption, decryption and test