H5新特性,canvas被用来绘制图形,制作图片集合,甚至用来实现动画效果
只有width和height属性
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Documenttitle>
<style>
canvas{
margin: 0 auto;
border: 1px solid #000;
display: block;
}
style>
head>
<body>
<canvas width="500" height="500">canvas>
<script>
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
// 画虚线
for (let i = 0; i < 20; i++) {
drawLine(100+10*i,100,105+10*i,100,'green',2)
}
function drawLine(x1,y1,x2,y2,color,width){
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.lineTo(x2,y2);
ctx.strokeStyle=color;
ctx.lineWidth=width;
ctx.stroke();
ctx.closePath();
}
script>
body>
html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Documenttitle>
<style>
canvas{
margin: 0 auto;
border: 1px solid #000;
display: block;
}
style>
head>
<body>
<canvas width="600" height="600">canvas>
<script>
// 绘制饼状图并显示比例
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const data = [{
value:'苹果',
color:'red',
num:0.1,
},{
value: '香蕉',
color:'yellow',
num:0.3,
},{
value: '梨子',
color: 'green',
num:0.4,
},{
value: '橘子',
color: 'orange',
num:0.2
}]
let temAngle=-90;
for (let i = 0; i < data.length; i++) {
const angle=data[i].num*360;
drawArc(300,300,200,angle,data[i].color,data[i].num)
}
function drawArc(x1,y1,radius,angle,color,num){
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.fillStyle=color;
const startAngle=temAngle*Math.PI/180;
const endAngle=(temAngle+angle)*Math.PI/180;
ctx.arc(x1,y1,radius,startAngle,endAngle);
// 绘制文字
const text=num*100+'%';
const textAngle=temAngle+1/2*angle;
const x=x1+Math.cos(textAngle*Math.PI/180)*(radius+20);
const y=y1+Math.sin(textAngle*Math.PI/180)*radius;
if(textAngle>90&&textAngle<270){
ctx.textAlign='end'
}
ctx.fillText(text,x,y)
ctx.fill();
temAngle+=angle;
}
script>
body>
html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<style>
canvas{
margin: 0 auto;
border: 1px solid #000;
display: block;
}
</style>
</head>
<body>
<canvas width="500" height="500"></canvas>
<script>
// 绘制帧动画
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const img=new Image();
img.src='./imgs/ying.jpeg';
img.onload=function (){
let frameIndex=0;
let dirIndex=0;
setInterval(()=>{
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.drawImage(
img,
frameIndex*130,
dirIndex*164,
130,
164,
100,
100,
130*2,
164*2
)
frameIndex++;
frameIndex%=3;
console.log(frameIndex)
if(frameIndex===2){
dirIndex++
}
dirIndex%=2;
},1000/4)
}
</script>
</body>
</html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Documenttitle>
<style>
canvas{
margin: 0 auto;
border: 1px solid #000;
display: block;
}
style>
head>
<body>
<canvas width="500" height="500">canvas>
<script>
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle='rgba(255,0,0,0.9)';
ctx.shadowColor='teal';
ctx.shadowBlur=10;
ctx.shadowOffsetX=10;
ctx.shadowOffsetY=10;
// 创建线性渐变
const grd=ctx.createLinearGradient(0,0,170,0);
grd.addColorStop(0,'black'); // 添加渐变颜色
grd.addColorStop(0.5,'red')
grd.addColorStop(1,'white');
ctx.fillStyle=grd;
ctx.fillRect(0,0,300,300)
script>
body>
html>
ctx.translate(x,y) 方法重新映射画布上的 (0,0) 位置
context.rotate(angle); 方法旋转当前的绘图
context.scale(scalewidth,scaleheight)
ctx.save() 保存当前环境的状态
可以把当前绘制环境进行保存到缓存中。
ctx.restore() 返回之前保存过的路径状态和属性
获取最近缓存的 ctx;需要和save()配合使用
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>裁剪图片title>
<style>
*{
margin: 0;
padding: 0;
}
.wrap{
width: 800px;
margin: 0 auto;
}
.canvasBox{
width: 600px;
height: 600px;
background-color: #eee;
position: relative;
}
.mark{
width: 50%;
height: 50%;
background-color: rgba(0,0,0,0.5);
position: absolute;
top: 150px;
left: 150px;
/*display: none;*/
}
ul{
display: flex;
width: 600px;
justify-content: space-between;
}
li{
list-style: none;
}
style>
head>
<body>
<div class="wrap">
<div class="canvasBox">
<canvas>canvas>
<div class="mark">div>
div>
<ul>
<li>
<label for="file">选择图片label>
<input
id="file"
type="file"
accept="image/*"
style="display: none"
/>
li>
<li onclick="scale(1)">放大li>
<li onclick="scale(0)">缩小li>
<li onclick="save()">保存li>
ul>
<img src="" id="img"/>
div>
<script>
/**
* 准备数据
* 1. 画布大小,遮罩大小和位置,上传大小和位置
*/
let img;
let imgW;
let imgH;
let imgOriginW;
let imgOriginH;
let startX;
let startY;
const canvasW=600;
const canvasH=600;
const fileNode=document.querySelector('#file');
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const canvasBoxNode=document.querySelector('.canvasBox');
const imgNode=document.querySelector('#img');
// 拿到file文件
fileNode.onchange=function (e){
const file=e.target.files[0];
if(!file) return;
canvas.width=canvasW;
canvas.height=canvasH;
// 文件读取
const fileExample=new FileReader();
fileExample.readAsDataURL(file);
fileExample.onload=(e)=>{
img=new Image();
img.src=e.target.result;
img.onload=()=>{
imgW=img.width;
imgH=img.height;
imgOriginW=100;
imgOriginH=100;
drawImage()
}
}
fileNode.value='';
}
// 绘制canvas
function drawImage(){
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.drawImage(img,imgOriginW,imgOriginH,imgW,imgH)
}
// 缩放
function scale(flag){
if(!img) return;
let n=imgW/imgH;
const n1=20;
const n2=n1/n;
if(flag){
imgW+=n1;
imgH+=n2;
}else {
imgW-=n1;
imgH-=n2;
}
drawImage()
}
// 刚移入记录位置
canvasBoxNode.ontouchstart=function (e){
const point=e.changedTouches[0];
startX=point.clientX;
startY=point.clientY;
}
// 移动记录位置
canvasBoxNode.ontouchmove=function (e){
if(!img) return;
const point=e.changedTouches[0];
const x=point.clientX-startX;
const y=point.clientX-startY;
console.log(x,y)
if(Math.abs(x)<20||Math.abs(y)<20) return;
imgOriginW+=x;
imgOriginH+=y;
drawImage();
startX=point.clientX;
startY=point.clientY;
}
// 保存遮罩内图片
function save(){
if(!img) return;
const imageData=ctx.getImageData(150,150,300,300);
const canvas2=document.createElement('canvas');
const canvas2Ctx=canvas2.getContext('2d');
canvas2.width=300;
canvas2.height=300;
canvas2Ctx.putImageData(imageData,0,0,0,0,300,300);
imgNode.src=canvas2.toDataURL('image/png')
console.log(canvas2.toDataURL('image/png'))
}
script>
body>
html>