##在canvas中插入圖片(須要image對象)
1.canvas操做圖片時,必需要等圖片加載完才能操做
2.drawImage(image, x, y, width, height)
其中 image 是 image 或者 canvas 對象,x 和 y 是其在目標 canvas 裏的起始座標。
這個方法多了2個參數:width 和 height,這兩個參數用來控制 當像canvas畫入時應該縮放的大小javascript
window.onload=function(){ var canvas=document.querySelector("#test"); if(canvas.getContext){ var ctx=canvas.getContext("2d"); var img= new Image(); img.src="img/tg.png"; img.onload=function(){ draw(); } function draw(){ ctx.drawImage(img,0,0,img.width,img.height); } } }
window.onload=function(){ var canvas=document.querySelector("#test"); if(canvas.getContext){ var ctx=canvas.getContext("2d"); var img= new Image(); img.src="img/tg.png"; img.onload=function(){ draw(); } function draw(){ ctx.drawImage(img,0,0,img.width,img.height); } } }
###在canvas中設置背景(須要image對象)
1.createPattern(image, repetition)
image:圖像源
epetition:
"repeat"
"repeat-x"
"repeat-y"
"no-repeat"
通常狀況下,咱們都會將createPattern返回的對象做爲fillstyle的值css
function draw(){ var pattern=ctx.createPattern(img,"no-repeat")//不加;號 ctx.fillStyle=pattern; ctx.fillRect(0,0,300,300); }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> html,body{ height: 100%; overflow: hidden; } body{ background: greenyellow; } #test{ position: absolute; top: 0; left: 0; right: 0; bottom:0; margin: auto; background: gray; } </style> </head> <body> <canvas id="test" width="300" height="300"> <span>您的瀏覽器不支持畫布元素 請您換成萌萌的谷歌</span> </canvas> </body> <script type="text/javascript"> window.onload=function(){ var canvas=document.querySelector("#test"); if(canvas.getContext){ var ctx=canvas.getContext("2d"); var img= new Image(); img.src="img/tg.png"; img.onload=function(){ draw(); } function draw(){ var pattern=ctx.createPattern(img,"no-repeat")//不加;號 ctx.fillStyle=pattern; ctx.fillRect(0,0,300,300); } } } </script> </html>
###漸變
canvas漸變(線性漸變)
createLinearGradient(x1, y1, x2, y2)
表示漸變的起點 (x1,y1) 與終點 (x2,y2)
gradient.addColorStop(position, color)
gradient :createLinearGradient的返回值
addColorStop 方法接受 2 個參數,
position 參數必須是一個 0.0 與 1.0 之間的數值,表示漸變中顏色所在的相對位置。
例如,0.5 表示顏色會出如今正中間。
color 參數必須是一個有效的 CSS 顏色值(如 #FFF, rgba(0,0,0,1),等等)html
// function draw(){
var gradient = ctx.createLinearGradient(0, 0, 200, 200);
gradient.addColorStop(0,"red");
gradient.addColorStop(0.5,"yellow");
gradient.addColorStop(0.7,"pink");
gradient.addColorStop(1,"green");
ctx.fillStyle=gradient;
ctx.fillRect(0,0,300,300);java
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; } html,body{ height: 100%; overflow: hidden; } body{ background: pink; } #test{ background: gray; position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto; } </style> </head> <body> <canvas id="test" width="300" height="300"> <span>您的瀏覽器不支持畫布元素 請您換成萌萌的谷歌</span> </canvas> </body> <script type="text/javascript"> window.onload=function(){ var canvas = document.querySelector("#test"); if(canvas.getContext){ var ctx = canvas.getContext("2d"); /*var img = new Image(); img.src="tg.png"; img.onload=function(){ draw(); }*/ // function draw(){ var gradient = ctx.createLinearGradient(0, 0, 200, 200); gradient.addColorStop(0,"red"); gradient.addColorStop(0.5,"yellow"); gradient.addColorStop(0.7,"pink"); gradient.addColorStop(1,"green"); ctx.fillStyle=gradient; ctx.fillRect(0,0,300,300); // } } }
canvas漸變(徑向漸變)
createRadialGradient(x1, y1, r1, x2, y2, r2)
前三個參數則定義另外一個以(x1,y1) 爲原點,半徑爲 r1 的圓,
後三個參數則定義另外一個以 (x2,y2) 爲原點,半徑爲 r2 的圓。canvas
var gradient = ctx.createRadialGradient(150, 150, 50, 150, 150, 100) gradient.addColorStop(0,"red"); gradient.addColorStop(0.5,"yellow"); gradient.addColorStop(0.7,"pink"); gradient.addColorStop(1,"green"); ctx.fillStyle=gradient; ctx.fillRect(0,0,300,300);
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; } html,body{ height: 100%; overflow: hidden; } body{ background: pink; } #test{ background: gray; position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto; } </style> </head> <body> <canvas id="test" width="300" height="300"> <span>您的瀏覽器不支持畫布元素 請您換成萌萌的谷歌</span> </canvas> </body> <script type="text/javascript"> window.onload=function(){ var canvas = document.querySelector("#test"); if(canvas.getContext){ var ctx = canvas.getContext("2d"); /*var img = new Image(); img.src="tg.png"; img.onload=function(){ draw(); }*/ // function draw(){ var gradient = ctx.createRadialGradient(150, 150, 50, 150, 150, 100) gradient.addColorStop(0,"red"); gradient.addColorStop(0.5,"yellow"); gradient.addColorStop(0.7,"pink"); gradient.addColorStop(1,"green"); ctx.fillStyle=gradient; ctx.fillRect(0,0,300,300); // } } } </script> </html>