canvas知識(shí)總結(jié)
1.基礎(chǔ)知識(shí)
canvas元素繪制圖像的時(shí)候有兩種方法,分別是
context.fill()//填充
context.stroke()//繪制邊框
style:在進(jìn)行圖形繪制前,要設(shè)置好繪圖的樣式
context.fillStyle//填充的樣式
context.strokeStyle//邊框樣式
context.lineWidth//圖形邊框?qū)挾?/pre>
context.arc(centerx圓心橫左邊,centery圓心縱坐標(biāo),radius半徑,startingAngle起始弧度值,endingAngle結(jié)束弧度值,anticlockwise='false'順時(shí)針默認(rèn)false)
2.繪制非填充線段
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" >
<link rel="stylesheet" href="styles/lianxi.css">
<script src="http://r01.uzaicdn.com/content/v1/scripts/core.js"></script>
<script src="scripts/lianxi.js"></script>
<!--[if lt IE 9]><script src="http://r.uzaicdn.com/content/libs/html5shiv.js"></script><![endif]-->
<!--[if IE 6]><script src="http://r.uzaicdn.com/content/libs/dd_belatedpng_0.0.8a-min.js" type="text/javascript"></script><script>DD_belatedPNG.fix('.png');</script><![endif]-->
<style type="text/css">
.canvas{border: 1px solid #000;display: block;margin: 0 auto;margin-top: 50px;}
</style>
<script>
window.onload=function(){
function draw(){
var canvas = document.getElementById('canvas');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
canvas.width=300;
canvas.height=300;
ctx.beginPath(); //一個(gè)繪畫開始
ctx.moveTo(50,50);//線段起點(diǎn)
ctx.lineTo(100,100);//終點(diǎn)1
ctx.lineTo(50,100);//終點(diǎn)2
ctx.lineTo(50,50);//終點(diǎn)3
ctx.lineWidth=5;//邊框?qū)挾?
ctx.strokeStyle="red"; //邊框樣式
ctx.closePath(); //一個(gè)繪畫結(jié)束
ctx.stroke();//繪制線段
}else{
alert('當(dāng)前瀏覽器不支持,請(qǐng)更換瀏覽器');
}
}
draw();
}
</script>
<style tyrp="text/css">
canvas{ border: 1px solid black;margin: 0 auto;display: block;}
</style>
</head>
<body>
<canvas id="canvas">當(dāng)前瀏覽器不支持,請(qǐng)更換瀏覽器</canvas>
</body>
</html>
3.繪制填充圖形
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" >
<link rel="stylesheet" href="styles/lianxi.css">
<script src="http://r01.uzaicdn.com/content/v1/scripts/core.js"></script>
<script src="scripts/lianxi.js"></script>
<!--[if lt IE 9]><script src="http://r.uzaicdn.com/content/libs/html5shiv.js"></script><![endif]-->
<!--[if IE 6]><script src="http://r.uzaicdn.com/content/libs/dd_belatedpng_0.0.8a-min.js" type="text/javascript"></script><script>DD_belatedPNG.fix('.png');</script><![endif]-->
<style type="text/css">
.canvas{border: 1px solid #000;display: block;margin: 0 auto;margin-top: 50px;}
</style>
<script>
window.onload=function(){
function draw(){
var canvas = document.getElementById('canvas');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
canvas.width=300;
canvas.height=300;
ctx.beginPath(); //一個(gè)繪畫開始
ctx.moveTo(50,50);//線段起點(diǎn)
ctx.lineTo(100,100);//終點(diǎn)1
ctx.lineTo(50,100);//終點(diǎn)2
ctx.lineTo(50,50);//終點(diǎn)3
ctx.fillStyle='red';
ctx.fill();
//邊框添加
ctx.lineWidth=5;//邊框?qū)挾?
ctx.strokeStyle="blue"; //邊框樣式
ctx.closePath(); //一個(gè)繪畫結(jié)束
ctx.stroke();//繪制線段
}else{
alert('當(dāng)前瀏覽器不支持,請(qǐng)更換瀏覽器');
}
}
draw();
}
</script>
<style tyrp="text/css">
canvas{ border: 1px solid black;margin: 0 auto;display: block;}
</style>
</head>
<body>
<canvas id="canvas">當(dāng)前瀏覽器不支持,請(qǐng)更換瀏覽器</canvas>
</body>
</html>
4.繪制圓弧
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" >
<link rel="stylesheet" href="styles/lianxi.css">
<script src="http://r01.uzaicdn.com/content/v1/scripts/core.js"></script>
<script src="scripts/lianxi.js"></script>
<!--[if lt IE 9]><script src="http://r.uzaicdn.com/content/libs/html5shiv.js"></script><![endif]-->
<!--[if IE 6]><script src="http://r.uzaicdn.com/content/libs/dd_belatedpng_0.0.8a-min.js" type="text/javascript"></script><script>DD_belatedPNG.fix('.png');</script><![endif]-->
<style type="text/css">
canvas{border: 1px solid #000;display: block;margin: 0 auto;margin-top: 50px;}
</style>
<script>
window.onload=function(){
function draw(){
var canvas = document.getElementById('canvas');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
canvas.width=800;
canvas.height=800;
ctx.beginPath(); //開始一個(gè)新的繪畫
ctx.lineWidth=5;//邊框?qū)挾?
ctx.strokeStyle="red"; //邊框樣式
ctx.arc(100, 100, 30, 0, 1.5*Math.PI);
ctx.closePath(); //一個(gè)繪畫結(jié)束,如果繪畫不是封閉的,就封閉起來
ctx.stroke();//繪制線段
ctx.beginPath(); //開始一個(gè)新的繪畫
ctx.lineWidth=5;//邊框?qū)挾?
ctx.strokeStyle="red"; //邊框樣式
ctx.arc(200, 100, 30, 0, 2*Math.PI);
ctx.closePath(); //一個(gè)繪畫結(jié)束,如果繪畫不是封閉的,就封閉起來
ctx.stroke();//繪制線段
ctx.beginPath(); //開始一個(gè)新的繪畫
ctx.lineWidth=5;//邊框?qū)挾?
ctx.strokeStyle="red"; //邊框樣式
ctx.arc(300, 100, 30, 0, 0.5*Math.PI);
ctx.closePath(); //一個(gè)繪畫結(jié)束,如果繪畫不是封閉的,就封閉起來
ctx.stroke();//繪制線段
ctx.beginPath(); //開始一個(gè)新的繪畫
ctx.lineWidth=5;//邊框?qū)挾?
ctx.strokeStyle="red"; //一個(gè)繪畫結(jié)束,如果繪畫不是封閉的,就封閉起來
ctx.arc(400, 100, 30, 0, 0.5*Math.PI,true);//注意:0*PI,0.5*PI,1*PI,1。5*PI,2*PI所占據(jù)的位置是固定的
ctx.closePath(); //一個(gè)繪畫結(jié)束
ctx.stroke();//繪制線段
ctx.beginPath(); //開始一個(gè)新的繪畫
ctx.fillStyle="red"; //邊框樣式
ctx.arc(500, 100, 30, 0, 1.5*Math.PI);
ctx.closePath(); //一個(gè)繪畫結(jié)束,如果繪畫不是封閉的,就封閉起來
ctx.fill();//繪制填充
ctx.beginPath(); //開始一個(gè)新的繪畫
ctx.lineWidth=5;//邊框?qū)挾?
ctx.strokeStyle="red"; //邊框樣式
ctx.arc(600, 100, 30, 0, 1.5*Math.PI);
ctx.stroke();//繪制線段
}else{
alert('當(dāng)前瀏覽器不支持,請(qǐng)更換瀏覽器');
}
}
draw();
}
</script>
</head>
<body>
<canvas id="canvas">當(dāng)前瀏覽器不支持,請(qǐng)更換瀏覽器</canvas>
</body>
</html>
5.繪制矩形
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" >
<link rel="stylesheet" href="styles/lianxi.css">
<script src="http://r01.uzaicdn.com/content/v1/scripts/core.js"></script>
<script src="scripts/lianxi.js"></script>
<!--[if lt IE 9]><script src="http://r.uzaicdn.com/content/libs/html5shiv.js"></script><![endif]-->
<!--[if IE 6]><script src="http://r.uzaicdn.com/content/libs/dd_belatedpng_0.0.8a-min.js" type="text/javascript"></script><script>DD_belatedPNG.fix('.png');</script><![endif]-->
<style type="text/css">
canvas{border: 1px solid #000;display: block;margin: 0 auto;margin-top: 50px;}
</style>
<script>
window.onload=function(){
function draw(){
var canvas = document.getElementById('canvas');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
canvas.width=500;
canvas.height=500;
ctx.fillRect(25,25,100,100);//繪制一個(gè)填充的矩形
ctx.clearRect(45,45,60,60);//清除指定矩形區(qū)域,讓清除部分完全透明
ctx.strokeRect(50,50,50,50); //繪制一個(gè)矩形的邊框
}else{
alert('當(dāng)前瀏覽器不支持,請(qǐng)更換瀏覽器');
}
}
draw();
}
</script>
</head>
<body>
<canvas id="canvas">當(dāng)前瀏覽器不支持,請(qǐng)更換瀏覽器</canvas>
</body>
</html>
6.繪制文本
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" >
<link rel="stylesheet" href="styles/lianxi.css">
<script src="http://r01.uzaicdn.com/content/v1/scripts/core.js"></script>
<script src="scripts/lianxi.js"></script>
<!--[if lt IE 9]><script src="http://r.uzaicdn.com/content/libs/html5shiv.js"></script><![endif]-->
<!--[if IE 6]><script src="http://r.uzaicdn.com/content/libs/dd_belatedpng_0.0.8a-min.js" type="text/javascript"></script><script>DD_belatedPNG.fix('.png');</script><![endif]-->
<style type="text/css">
canvas{border: 1px solid #000;display: block;margin: 0 auto;margin-top: 50px;}
</style>
<script>
window.onload=function(){
function draw(){
var canvas = document.getElementById('canvas');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
canvas.width=500;
canvas.height=500;
ctx.font = "48px serif";
ctx.fillText("Hello world", 10, 50);
}else{
alert('當(dāng)前瀏覽器不支持,請(qǐng)更換瀏覽器');
}
}
draw();
}
</script>
</head>
<body>
<canvas id="canvas">當(dāng)前瀏覽器不支持,請(qǐng)更換瀏覽器</canvas>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" >
<link rel="stylesheet" href="styles/lianxi.css">
<script src="http://r01.uzaicdn.com/content/v1/scripts/core.js"></script>
<script src="scripts/lianxi.js"></script>
<!--[if lt IE 9]><script src="http://r.uzaicdn.com/content/libs/html5shiv.js"></script><![endif]-->
<!--[if IE 6]><script src="http://r.uzaicdn.com/content/libs/dd_belatedpng_0.0.8a-min.js" type="text/javascript"></script><script>DD_belatedPNG.fix('.png');</script><![endif]-->
<style type="text/css">
canvas{border: 1px solid #000;display: block;margin: 0 auto;margin-top: 50px;}
</style>
<script>
window.onload=function(){
function draw(){
var canvas = document.getElementById('canvas');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
canvas.width=500;
canvas.height=500;
ctx.font = "48px serif";
ctx.strokeText("Hello world", 10, 50);
}else{
alert('當(dāng)前瀏覽器不支持,請(qǐng)更換瀏覽器');
}
}
draw();
}
</script>
</head>
<body>
<canvas id="canvas">當(dāng)前瀏覽器不支持,請(qǐng)更換瀏覽器</canvas>
</body>
</html>
7.圖片操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" >
<link rel="stylesheet" href="styles/lianxi.css">
<script src="http://r01.uzaicdn.com/content/v1/scripts/core.js"></script>
<script src="scripts/lianxi.js"></script>
<!--[if lt IE 9]><script src="http://r.uzaicdn.com/content/libs/html5shiv.js"></script><![endif]-->
<!--[if IE 6]><script src="http://r.uzaicdn.com/content/libs/dd_belatedpng_0.0.8a-min.js" type="text/javascript"></script><script>DD_belatedPNG.fix('.png');</script><![endif]-->
<style type="text/css">
canvas{border: 1px solid #000;display: block;margin: 0 auto;margin-top: 50px;}
</style>
<script>
window.onload=function(){
function draw(){
var canvas = document.getElementById('canvas');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
canvas.width=500;
canvas.height=500;
var img=new Image();
img.src='http://gzdl.cooco.net.cn/files/down/test/imggzdl/312/15812.jpg'
img.onload=function(){
ctx.drawImage(img,0,0);
ctx.beginPath();
ctx.moveTo(30,96);
ctx.lineTo(70,66);
ctx.lineTo(103,76);
ctx.lineTo(170,15);
ctx.stroke();
}
}else{
alert('當(dāng)前瀏覽器不支持,請(qǐng)更換瀏覽器');
}
}
draw();
}
</script>
</head>
<body>
<canvas id="canvas">當(dāng)前瀏覽器不支持,請(qǐng)更換瀏覽器</canvas>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
- canvas繪制表盤時(shí)鐘
- canvas繪制的直線動(dòng)畫
- 微信小程序 Canvas增強(qiáng)組件實(shí)例詳解及源碼分享
- canvas學(xué)習(xí)之API整理筆記(二)
- canvas學(xué)習(xí)之API整理筆記(一)
- 微信小程序 wxapp畫布 canvas詳細(xì)介紹
- 淺談jquery中使用canvas的問題
- js Canvas實(shí)現(xiàn)圓形時(shí)鐘教程
- JS Canvas定時(shí)器模擬動(dòng)態(tài)加載動(dòng)畫
- 快速解決Canvas.toDataURL 圖片跨域的問題
- JavaScript+html5 canvas實(shí)現(xiàn)本地截圖教程
- js+canvas繪制五角星的方法
相關(guān)文章
javascript判斷并獲取注冊(cè)表中可信任站點(diǎn)的方法
這篇文章主要介紹了javascript判斷并獲取注冊(cè)表中可信任站點(diǎn)的方法,可實(shí)現(xiàn)針對(duì)域名和IP的可信任站點(diǎn)判斷功能,需要的朋友可以參考下2015-06-06
12個(gè)非常有創(chuàng)意的JavaScript小游戲
JavaScript 在Web開發(fā)過程中已經(jīng)是必不可少的重要分子,他推動(dòng)著Web的交互性往越來越高的層次發(fā)展,現(xiàn)在的很多Web游戲也基于這類語言開發(fā)。2010-03-03
12個(gè)非常實(shí)用的JavaScript小技巧【推薦】
下面小編就為大家?guī)硪黄?2個(gè)非常實(shí)用的JavaScript小技巧【推薦】。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05

