html5 <canvas>第二篇 strokeRect strokeStyle() strokeStyle() createLinearGradient addColorStop 用法 實心圓,空心圓,漸層

來源:互聯網
上載者:User

標籤:vcl   getc   function   log   路徑   sheet   lin   code   開始   

<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>無標題文檔</title><link href="css.css" rel="stylesheet" type="text/css"><script type="text/javascript" language="jscript" src="js6.js"></script></head><body onLoad="pageload();"><canvas id="cnvMain" width="200px" height="150px"; onClick="cnvClick();"></canvas></body></html>
 1 @charset "utf-8"; 2 /* CSS Document */ 3  4 body{ 5     font-size:36px; 6 } 7 canvas{ 8     border:dashed 1px #F00; 9     cursor:pointer;10 }11 /*增加樣式*/12 p{13     position:14     absolute;15     height:23px;16     line-height:23px;17     margin-left:10px;18 }19 span{20     padding:3px;21     border:solid 1px #033;22     background-color:#00F;23     cursor:pointer;24     }

 

 1 // JavaScript Document 2 function $$(id){ 3     return document.getElementById(id); 4      5     } 6     function pageload(){ 7         var cnv=$$("cnvMain"); 8         var ctx=cnv.getContext("2d"); 9         //設定邊框10         ctx.strokeStyle="#3ef3ed";11         ctx.strokeRect(30,30,80,80);12         //設定背景13         ctx.fillStyle="#ccc";14         ctx.fillRect(30,30,80,80);15         }16         function cnvClick(){17             var cnv=$$("cnvMain");18         var ctx=cnv.getContext("2d");19         //清空圖形20         ctx.clearRect(36,36,50,50);21             }

這個是繪製一個帶邊框的矩形

 

繪製矩形邊框strokeRect(x,y,width,height);

參數為“x”,“y”為矩形的起點座標,“width”“height”分別為矩形的寬和高

設定邊框顏色strokeStyle();

清空映像中的顏色 clearRect();

繪製漸層顏色

1 var c=document.getElementById("myCanvas");2 var ctx=c.getContext("2d");3 4 var grd=ctx.createLinearGradient(0,0,170,0);5 grd.addColorStop(0,"black");6 grd.addColorStop(1,"white");7 8 ctx.fillStyle=grd;9 ctx.fillRect(20,20,150,100);
createLinearGradient(xStar, yStar,xEnd,YEnd)
xstar和ystar表示漸層開始的座標
xEnd和YEnd表示漸層結束的座標
addColorStop(value,color);表示漸層位移量以及漸層顏色

繪製圓
var cnv=$$("cnvMain");        var cxt=cnv.getContext("2d");        //清除畫布原有圖形1        cxt.clearRect(0,0,280,190);        //開始畫實心圓        cxt.beginPath();        cxt.arc(100,100,50,0,Math.PI*2,true);        cxt.closePath();        //設定填充背景色        cxt.fillStyle="#3ef3ed";        //進行填充        cxt.fill();

 

各個參數的意思;

1.getContext(“2d”);返回一個用於在畫布上繪圖的環境

2.beginPath():聲明開始繪製路徑

3.closePath();繪製完圖形後關閉繪製路徑。

4.填充(畫實心圓)

1 //設定填充背景色2         cxt.fillStyle="#3ef3ed";3         //進行填充4         cxt.fill();

描邊(畫空心圓)

//設定邊框色        cxt.strokeStyle="black";                //設定邊框寬度    cxt.lineWidth=5;        //進行描邊        cxt.stroke();    

 

 
 來自w3c
 

 

html5 <canvas>第二篇 strokeRect strokeStyle() strokeStyle() createLinearGradient addColorStop 用法 實心圓,空心圓,漸層

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.