当前位置:网站首页>canvas常用原型方法及绘制图片应用
canvas常用原型方法及绘制图片应用
2022-07-28 08:35:00 【绝坊】
绘制线路
绘制三角形
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<canvas id="canvans" width="500" height="500"></canvas>
<img src="./img_the_scream.jpg" alt="">
<script>
var canvans = document.querySelector("#canvans")//获取canvans对象
if (canvans.getContext) {
//监测浏览器是否支持canvas
var ctx = canvans.getContext('2d');//获取2D画笔
//绘制封闭三角形
ctx.beginPath();//先抓起笔
ctx.moveTo(100, 100);//一条路径的开始
ctx.lineTo(200, 50);//连
ctx.lineTo(200, 150);//连
ctx.closePath(); //最终第三条边需要闭合
ctx.stroke();//结束绘画
}
</script>
</body>
</html>
注意每次线条绘画都需要ctx.beginPath()抓笔,然后ctx.stroke()放笔
绘制圆形
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<canvas id="canvans" width="500" height="500"></canvas>
<img src="./img_the_scream.jpg" alt="">
<script>
var canvans = document.querySelector("#canvans")//获取canvans对象
if (canvans.getContext) {
//监测浏览器是否支持canvas
var ctx = canvans.getContext('2d');//获取2D画笔
//圆形
ctx.beginPath();
ctx.arc(300,300,50,0,2*Math.PI,true)
ctx.stroke();
}
</script>
</body>
</html>
- 画园:arc()
- 画弧线:arc()
绘制图片
需求:假设现在有一个h5海报宣传,但是图片中的二维码是动态的,并且随时生成的。这个时候就要用到canvas的动态图片绘制技术。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<canvas id="canvans" width="500" height="500"></canvas>
<img src="./img_the_scream.jpg" alt="">
<script>
var canvans = document.querySelector("#canvans")//获取canvans对象
if (canvans.getContext) {
//监测浏览器是否支持canvas
var ctx = canvans.getContext('2d');//获取2D画笔
let img = document.querySelector('img')
//绘制图片
img.onload = function(){
ctx.drawImage(img,200,200)
}
}
</script>
</body>
</html>
<!-- ctx.
drawImage()是一个非常实用的原型方法
其他
canvas的绘制技术非常多,例如可以绘制阴影、线性渐变、径向渐变、颜色填充等
边栏推荐
- Learn to draw with nature communications -- complex violin drawing
- Design for failure常见的12种设计思想
- 【592. 分数加减运算】
- Overview of head pose estimation
- Deconstruction assignment of ES6 variables
- Prometheus TSDB analysis
- Implementation principle of golang synergy
- Sentry log management system installation and use tutorial
- 使用 GBase C API 执行存储过程是怎样的?
- 从开发转测试:我从零开始,一干就是6年的自动化测试历程
猜你喜欢

台大林轩田《机器学习基石》习题解答和代码实现 | 【你值得拥有】

阿里云服务器搭建和宝塔面板连接

Go interface Foundation

Learn to draw with nature communications -- complex violin drawing

Design for failure常见的12种设计思想

Modify virtual machine IP address

v-bind指令的详细介绍

Linux initializes MySQL with fatal error: could not find my-default.cnf
![[附下载]推荐几款暴力破解和字典生成的工具](/img/c6/f4a9c566ff21a8e133a8a991108201.png)
[附下载]推荐几款暴力破解和字典生成的工具

DAPP safety summary and typical safety incident analysis
随机推荐
From development to testing: I started from scratch and worked for six years of automated testing
Prometheus TSDB analysis
01 tensorflow calculation model (I) - calculation diagram
Mysql5.7.38 start keepalived in the container
NPM and yarn use (official website, installation, command line, uploading your own package, detailed explanation of package version number, updating and uninstalling package, viewing all versions, equ
Get started quickly with flask (I) understand the framework flask, project structure and development environment
Feign调用异常[Running, pool size = 10, active threads = 10, queued tasks = 0, completed tasks = n]
Machine learning: self paced and fine tuning
js数组去重,id相同对某值相加合并
Marketing play is changeable, and understanding the rules is the key!
[附下载]推荐几款暴力破解和字典生成的工具
51单片机存储篇:EEPROM(I2C)
CSV文件存储
Vrrp+mstp configuration details [Huawei ENSP experiment]
Mobaxtermsession synchronization
(IROS 2022) 基于事件相机的单目视觉惯性里程计 / Event-based Monocular Visual Inertial Odometry
站在大佬的肩膀上,你可以看的更远
CSV file storage
看得清比走得快更重要,因为走得对才能走得远
修改虚拟机IP地址