canvas详解
时间:2022-07-28
本文章向大家介绍canvas详解,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
*{padding: 0px;margin: 0px;}
body
{
background: yellow;
}
canvas
{
background: white;
}
</style>
</head>
<body>
<canvas width="500" height="500" id="canvas">nonono,浏览器不行.</canvas>
<script type="text/javascript">
var canvas=document.querySelector("#canvas");
var obj=canvas.getContext("2d");
obj.beginPath();
obj.strokeStyle="blue";
obj.lineWidth=5;
obj.moveTo(50,50);
obj.lineTo(100,100);
obj.lineTo(110,110);
obj.lineTo(220,400);
obj.stroke();
obj.beginPath();
obj.lineWidth=5;
obj.strokeStyle="red";
obj.moveTo(220,400);
obj.lineTo(220,500);
obj.stroke();
</script>
</body>
</html>
什么情况下重开路径,不同颜色2的情况下。长度由位置。
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
*{margin: 0;padding: 0;list-style: none;}
body{
background: black;
}
canvas{
background: white;
}
</style>
</head>
<body>
<canvas width="500" height="500" id="canvas">您的浏览器版本过低,请更新浏览器</canvas>
<input type="color" id="color">
<input type="range" id="range">
<script type="text/javascript">
var obj=canvas.getContext("2d");
color.oninput=function()
{
obj.strokeStyle=this.value;
}
range.oninput=function()
{
obj.lineWidth=this.value;
}
canvas.onmousedown=function(e)
{
var ev=e||window.event;
obj.beginPath();
obj.moveTo(ev.clientX,ev.clientY);
document.onmousemove=function(e)
{
var ev=e||window.event;
obj.lineTo(ev.clientX,ev.clientY);
obj.stroke();
}
document.onmouseup=function()
{
document.onmousemove=document.onmouseup=null;
}
return false;
}
</script>
</body>
</html>
核心:按下的与移动完毕的链接到一起就行了。并且要记住,抬起就null,为什么,像是冲洗开一条路径把。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
*{margin: 0;padding: 0;list-style: none;}
body{
background: black;
cursor: url(1.cur),auto;
}
canvas{
background: white;
}
</style>
</head>
<body>
<canvas width=500 height=500 id='canvas'>您的浏览器版本过低,请更新浏览器</canvas>
<script type="text/javascript">
function random(min,max){//5-10 56789
//console.log(min,max)
return parseInt(min+Math.random()*(max-min));
}//随机算法.
obj=canvas.getContext("2d");
setInterval(function()
{
obj.beginPath();
obj.lineWidth=random(1,10);
obj.strokeStyle="rgb("+random(0,256)+","+random(0,256)+","+random(0,256)+")";
obj.moveTo(random(0,canvas.width),random(0,canvas.height));
obj.lineTo(random(0,canvas.width),random(0,canvas.height));
obj.stroke();
},10);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
*{margin: 0;padding: 0;list-style: none;}
body{
background: black;
cursor: url(1.cur),auto;
}
canvas{
background: white;
}
</style>
</head>
<body>
<canvas width=500 height=500 id='canvas'>您的浏览器版本过低,请更新浏览器</canvas>
<script type="text/javascript">
obj=canvas.getContext("2d");
obj.beginPath();
obj.rect(50,50,200,100);//位置 宽高
obj.strokeStyle="red";
obj.lineWidth=20;
obj.fillStyle="yellow";
obj.fill();
obj.stroke();
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
*{margin: 0;padding: 0;list-style: none;}
body{
background: black;
cursor: url(1.cur),auto;
}
canvas{
background: white;
}
</style>
</head>
<body>
<canvas width=500 height=500 id='canvas'>您的浏览器版本过低,请更新浏览器</canvas>
<script type="text/javascript">
obj=canvas.getContext("2d");
obj.beginPath();
obj.rect(50,50,200,100);
obj.strokeStyle="red";
obj.lineWidth=20;
obj.fillStyle="yellow";
obj.fill();
obj.stroke();
obj.beginPath();
obj.clearRect(0,0,200,170);
obj.stroke();
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
*{margin: 0;padding: 0;list-style: none;}
body{
background: black;
cursor: url(1.cur),auto;
}
canvas{
background: white;
}
</style>
</head>
<body>
<canvas width=500 height=500 id='canvas'>您的浏览器版本过低,请更新浏览器</canvas>
<script type="text/javascript">
var offset={
x:50,
y:50
};
obj=canvas.getContext("2d");
obj.beginPath();
obj.rect(offset.x,offset.y,200,100);
obj.strokeStyle="blue";
obj.lineWidth=1;
obj.fillStyle="red";
obj.fill();
obj.stroke();
canvas.onmousedown=function(e)
{
var ev=e||event;
var l=ev.clientX-offset.x;
var t=ev.clientY-offset.y;
document.onmousemove=function(e)
{
var ev=e||event;
clear();
obj.beginPath();
obj.rect(ev.clientX-l,ev.clientY-t,200,100);
obj.strokeStyle="blue";
obj.lineWidth=1;
obj.fillStyle="red";
obj.fill();
obj.stroke();
};
document.onmouseup=function(e)
{
offset.x=e.clientX-l;
offset.y=e.clientY-t;
this.onmousemove=this.onmouseup=null;
};
return false;
}
function clear()
{
obj.beginPath();
obj.clearRect(0,0,canvas.width,canvas.height);
obj.stroke();
}
</script>
</body>
</html>
核心在于;
offset.x=e.clientX-l;
offset.y=e.clientY-t;,
不然up后重新跑到50 50的位置,而加上这一句就会到up时的地方重新开始拖放.
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
*{margin: 0;padding: 0;list-style: none;}
body{
background: black;
cursor: url(1.cur),auto;
}
canvas{
background: white;
}
</style>
</head>
<body>
<canvas width="500" height="500" id="canvas">您的浏览器版本过低,请更新浏览器</canvas>
<script type="text/javascript">
var obj=canvas.getContext("2d");
var img=new Image();
img.onload=function()
{
obj.beginPath();
obj.drawImage(img,0,0,55,37,200,200,55,37);/*对象 图的什么地方开始(0,0),图显示的宽高(55,37).画在canvas的哪一个位置(200,200),画多大的。(55,37)*/
obj.stroke();
}
img.onerror=function()
{
alert(1);
}
img.src="fish1.png";
</script>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
*{margin: 0;padding: 0;list-style: none;}
body{
background: black;
cursor: url(1.cur),auto;
}
canvas{
background: white;
}
</style>
</head>
<body>
<canvas width=500 height=500 id='canvas'>您的浏览器版本过低,请更新浏览器</canvas>
<script type="text/javascript">
var obj = canvas.getContext('2d');
var img = new Image();
//console.log(img)
var i = 0;
var a = 0;
img.onload = function(){
//alert(2);
setInterval(function (){
obj.beginPath();
a++;
obj.clearRect(0,0,canvas.width,canvas.height);
obj.stroke();
if(i == 4)i = 0;
obj.beginPath();
//console.log(obj)
obj.drawImage(img,0,37*i,55,37,100+a*1.213,100,55,37);
i++;
obj.stroke();
},100);
};
img.onerror = function(){
alert(1);
}
img.src='fish1.png';
</script>
</body>
</html>
核心在于:obj.clearRect(0,0,canvas.width,canvas.height);
原因是先清除·之前的就有一种动的感觉了.
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.beginPath();
ctx.arc(100,75,50,0,2*Math.PI);
ctx.stroke();
</script>
</body>
</html>
人是圆的半径。
- 仿经济学人——矩阵气泡图
- 左手用R右手Python系列——模拟登陆教务系统
- R语言数据清洗实战——复杂数据结构与list解析
- R语言爬虫实战——知乎live课程数据爬取实战
- Python爬虫系列(二)Quotes to Scrape(谚语网站的爬取实战)
- R语言数据清洗实战——世界濒危遗产地数据爬取案例
- Leetcode-Easy 437. Path Sum III
- R语言爬虫实战——网易云课堂数据分析课程板块数据爬取
- 左手用R右手Python系列14——日期与时间处理
- 如何使用管道操作符优雅的书写R语言代码
- 第四周编程作业(二)-Deep Neural Network for Image Classification: ApplicationDeep Neural Network for Image Cl
- 第四周编程作业(一)-Building your Deep Neural Network: Step by StepBuilding your Deep Neural Network: Step by
- Python数据抓取与可视化实战——网易云课堂人工智能与大数据板块课程实战
- 商务图表案例——仿经济学人分组漏斗图~
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法