重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
如果當做一物件的話,數就是物件本身,樹干、樹葉、樹根、樹枝都是樹的屬性
南華網站制作公司哪家好,找創新互聯!從網頁設計、網站建設、微信開發、APP開發、響應式網站等網站項目制作,到程序開發,運營維護。創新互聯2013年至今到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創新互聯。
你可以依據這些屬性,再判斷是中給他不同的 css 樣式,而通常會先定義好類別
最后用 javascript 依照個屬性的不同,套上不同狀態的類別
不懂想問詳細可以私信我
用html5開發隨機生成的大樹,你應該沒想到40+行代碼就可以搞定了吧~接下來就跟大家說說這棵大樹是如何在html5開發中實現的。
同樣必須要有html容器。新建Index.html,代碼如下:
、html
1 、head
2 、meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /
3 、titlecanvas tree
4 、/head
5 、body
6 、script type="text/javascript" src="tree.js"
7 、/body
8 、/html
接下來咱們開始tree.js:
、var canvas = document.createElement("canvas");
9 var ctx = canvas.getContext("2d");
10 canvas.width = 640;
11 canvas.height = 480;
12 document.body.appendChild(canvas);
代碼很好理解,創建一個canvas畫布,然后選擇為2d畫布,設置長寬,最后將這個畫布添加到body標簽下。
這個腳本最重要的函數在下面,大樹就是遞歸調用這個函數實現的,調用一次畫一條線段:
var drawTree = function (ctx, startX, startY, length, angle, depth, branchWidth){
13 var rand = Math.random,
14 newLength, newAngle, newDepth, maxBranch = 3,
15 endX, endY, maxAngle = 2 * Math.PI / 4,
16 subBraches;
17 ctx.beginPath();
18 ctx.moveTo(startX, startY);
19 endX = startX + length * Math.cos(angle);
20 endY = startY + length * Math.sin(angle);
21 ctx.lineCap = 'round';
22 ctx.lineWidth = branchWidth;
23 ctx.lineTo(endX, endY);
24 if (depth = 2){
25 ctx.strokeStyle = 'rgb(0,' + (((rand() * 64) + 128) 0) + ',0)';
26 } else {
27 ctx.strokeStyle = 'rgb(' + (((rand() * 64) + 64) 0) + ',50,25)';
28 }
29 ctx.stroke();
30 newDepth = depth - 1;
31 if (!newDepth)
32 return;
33 subBranches = (rand() * (maxBranch - 1)) + 1;
34 branchWidth *= 0.7;
35 for (var i = 0; i subBranches; i++){
36 newAngle = angle + rand() * maxAngle - maxAngle * 0.5;
37 newLength = length * (0.7 + rand() * 0.3);
38 drawTree(ctx, endX, endY, newLength, newAngle, newDepth, branchWidth);
39 }
40 }
接下來一點點解釋:
首先,解釋下各個變量的含義。ctx就是前面我們的2d畫布;startX是線段開始的橫坐標,同理startY是縱坐標;length是線段長度;angle是角度;depth是深度,葉子深度為1,樹干為12(可自己設定);branchWidth就線段的粗細。有了這些信息,其實就描述了一個線段,通過這些信息我們才能畫一個線段。
接下來又很可恥地一大段定義:
var rand = Math.random,
41 newLength, newAngle, newDepth, maxBranch = 3,
42 endX, endY, maxAngle = 2 * Math.PI / 4,
43 subBraches;
rand其實就是隨機一個0~1之間的實數,顧名思義,接下來這些new的就是下一節線段的各種參數。maxBranch就是最多有3個分叉,最大的角度 PI/2 即為,下一級調整角度在90%范圍內。subBranches就是分叉的個數。
好了,重要可以畫了:
ctx.beginPath();
44 ctx.moveTo(startX, startY);
45 endX = startX + length * Math.cos(angle);
46 endY = startY + length * Math.sin(angle);
47 ctx.lineCap = 'round';
48 ctx.lineWidth = branchWidth;
49 ctx.lineTo(endX, endY);
beginPath()表示告訴瀏覽器“我要開始畫了!”,把之前的記錄放棄了,這點有點像ps。moveTo()把光標移動到(startX, startY),再計算終點坐標,endX,endY,有點像高中學的參數方程。然后告訴瀏覽器,lineCap要round,線段的兩頭要是圓形的。有多粗呢?等于branchWidth。線段一直畫到(endX, endY)。
if (depth = 2){
50 ctx.strokeStyle = 'rgb(0,' + (((rand() * 64) + 128) 0) + ',0)';
51 } else {
52 ctx.strokeStyle = 'rgb(' + (((rand() * 64) + 64) 0) + ',50,25)';
53 }
如果是已經畫到了最后兩級,即為葉子,那么就rgb就為(0, 128~192, 0)(rgb代表顏色,分別為紅綠藍,red green blue)。還沒的話,就在(64~128, 50 ,25)中取。大家可能發現了,rgb必須為整數,但是rand()只能rand實數。大家其實也注意到了有個” 0″,js當中表示位運算,整體向右移動n位,0就是移動0位。其實它的作用和Math.floor()一樣,但是速度更快。
動手畫!
ctx.stroke();
這個線段就畫好了,是時候準備下它的分叉的時候了。
newDepth = depth - 1;
54 if (!newDepth)
55 return;
如果這個線段是最后一級,就沒有分叉了,也是一個遞歸的終止條件。
subBranches = (rand() * (maxBranch - 1)) + 1;
56 branchWidth *= 0.7;
57 for (var i = 0; i subBranches; i++){
58 newAngle = angle + rand() * maxAngle - maxAngle * 0.5;
59 newLength = length * (0.7 + rand() * 0.3);
60 drawTree(ctx, endX, endY, newLength, newAngle, newDepth, branchWidth);
61 }
分叉數是1~3中的一個數。然后有多少個分叉,就畫幾條線段,newAngle為原角度調整90度之內,新長度為原長度的0.7~1.0之間。
最后畫出主干,這棵樹就可以開始畫了。
drawTree(ctx, 320, 470, 60, -Math.PI / 2, 12, 12);
大家可能注意到角度為負,不符合傳統觀念。但你要知道,畫布的縱坐標和傳統的坐標軸正好是相反的。
好了,html5開發隨機生成的大樹代碼就這樣完成了,怎么樣,一點都難吧!
站內搜索: (僅支持單關鍵字)
用HTML5 Canvas制作擺動的樹
下載源代碼
〖 作者:cyclegtx 〗〖 發布日期:2014-07-05 〗
根據工作的需要,制作一個擺動的樹做為頁面的背景。為了增加頁面的交互性,我又為背景中的樹增加了鼠標(觸控)事件,使他能夠根據鼠標(觸控)做出相應的動作,當手指做上下或者左右滑動的時候樹會跟著擺動。先看看最終效果。
Step1.完成HTML頁面,新建一個Tree類
完成HTML頁面后新建一個Tree類用來記錄樹的各個屬性。其中x,y為樹根部的坐標值,branchLen,branchWidth分別是樹枝的長度與寬度,depth為樹枝的層數,canvas用來接頁面中的canvas元素(默認是ID為canvas的元素)。
html
meta charset="utf-8" /
head
style
body {
margin: 0;
background: #7ACFFA;
}
#canvas {
position: absolute;
top: 0; left: 0;
}
/style/headbody
canvas id="canvas" width="1" height="1"/canvas
script type='text/javascript'
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
function Tree(x,y,branchLen,branchWidth,depth,canvas){
this.canvas = canvas || document.getElementById('canvas');
this.ctx = this.canvas.getContext('2d');
this.x = x||0;
this.y = y||0;
this.branchLen = branchLen||0;
this.branchWidth = branchWidth||0;
var depth = depth || 5;
}
/script
/body/html
Step2.添加drawRoot方法,用來繪制樹干
首先在drawRoot中畫第一個枝干。drawRoot的參數意義同上。并且在Tree類的構造函數中運行drawRoot并把Tree接受到的參數傳入。最后new一個Tree類,使樹根位于屏幕的底部正中心,樹枝長100px,樹枝寬度為8px,樹枝層數為8層(暫時用不上)。
var atree = new Tree(canvas.width/2-4,canvas.height,100,8,8,canvas);
在drawRoot中我們需要用lineTo()畫出樹枝。樹枝的起始的坐標值(x,y)已經給出,結束的坐標值(toX,toY)需要進行計算。第一個畫的是樹干,由于樹干垂直于地面所以結束坐標toX等于初始坐標x,而結束坐標toY等于初始y減去樹干長度branchLen(注意坐標的0,0點在canvas的左上角)。
var toX = x;var toY = y-branchLen;
function Tree(x,y,branchLen,branchWidth,depth,canvas){
this.canvas = canvas || document.getElementById('canvas');
this.ctx = this.canvas.getContext('2d');
this.x = x||0;
this.y = y||0;
this.branchLen = branchLen||0;
this.branchWidth = branchWidth||0;
var depth = depth || 5;
this.drawRoot(this.x,this.y,this.branchLen,this.branchWidth);
}
Tree.prototype.drawRoot = function(x,y,branchLen,branchWidth){
var toX = x;
var toY = y-branchLen;
this.ctx.save();
this.ctx.strokeStyle="rgba(37, 141, 194, 0.93)";
this.ctx.beginPath();
this.ctx.lineCap = "butt";
this.ctx.lineJoin="round";
this.ctx.lineWidth = branchWidth;
this.ctx.moveTo(x,y);
this.ctx.lineTo(toX,toY);
this.ctx.closePath();
this.ctx.stroke();
this.ctx.restore();
}
var atree = new Tree(canvas.width/2-4,canvas.height,100,8,8,canvas);
運行代碼:
Step3.添加drawBranch方法,用來繪制樹枝
drawBranch同樣是根據初始與結束坐標畫出一條直線代表樹枝。與樹干不同的是樹枝不再是垂直與地面而是與樹干保持一定的角度,而且樹枝的初始值是樹干的結束點(toX,toY)。所以在drawBranch中我們加入新參數angle用來表示樹枝與樹干的垂直夾角α,這樣就可以根據α算出toX與toY。請看圖。
這樣我們在畫完樹干后再分別畫兩個不同角度的樹枝,一個是30°一個-30°。并將傳給樹枝的寬度branchWidth減小一個像素,使其與樹干粗細不同。
Tree.prototype.drawRoot = function(x,y,branchLen,branchWidth){
var toX = x;
var toY = y-branchLen;
this.ctx.save();
this.ctx.strokeStyle="rgba(37, 141, 194, 0.93)";
this.ctx.beginPath();
this.ctx.lineCap = "butt";
this.ctx.lineJoin="round";
this.ctx.lineWidth = branchWidth;
this.ctx.moveTo(x,y);
this.ctx.lineTo(toX,toY);
this.ctx.closePath();
this.ctx.stroke();
this.ctx.restore();
this.drawBranch(toX,toY,branchLen,branchWidth-1,30);
this.drawBranch(toX,toY,branchLen,branchWidth-1,-30);
}
Tree.prototype.drawBranch = function(x,y,branchLen,branchWidth,angle){
var angle = angle || 0;
var radian = (90-angle)*(Math.PI/180);
var toX = x+Math.cos(radian)*branchLen;
var toY = y-Math.sin(radian)*branchLen;
this.ctx.save();
this.ctx.strokeStyle="rgba(37, 141, 194, 0.93)";
this.ctx.beginPath();
this.ctx.lineCap = "butt";
this.ctx.lineJoin="round";
this.ctx.lineWidth = branchWidth;
this.ctx.moveTo(x,y);
this.ctx.lineTo(toX,toY);
this.ctx.closePath();
this.ctx.stroke();
this.ctx.restore();
}
運行代碼:
Step4.修改drawBranch函數,重復畫樹枝
在drawBranch函數的最后再次調用兩次drawBranch
this.drawBranch(toX,toY,branchLen,branchWidth-1,angle+30);
this.drawBranch(toX,toY,branchLen,branchWidth-1,angle-30);
使其調用自己完成遞歸,注意這里傳入的角度是在之前的角度的基礎上在增加或者減少30度。
為了使遞歸停下來我們需要一個停止條件,就是之前一直沒有用到的depth參數。我們在每次畫下一層之前使其減1表示已經完成了一層樹枝的繪制,直至depth減小到0表示繪制完所有的層數。
function Tree(x,y,branchLen,branchWidth,depth,canvas){
this.canvas = canvas || document.getElementById('canvas');
this.ctx = this.canvas.getContext('2d');
this.x = x||0;
this.y = y||0;
this.branchLen = branchLen||0;
this.branchWidth = branchWidth||0;
var depth = depth || 5;
this.drawRoot(this.x,this.y,this.branchLen,this.branchWidth,depth);
}
Tree.prototype.drawRoot = function(x,y,branchLen,branchWidth,depth){
var toX = x;
var toY = y-branchLen;
var depth = depth||5;
this.ctx.save();
this.ctx.strokeStyle="rgba(37, 141, 194, 0.93)";
this.ctx.beginPath();
this.ctx.lineCap = "butt";
this.ctx.lineJoin="round";
this.ctx.lineWidth = branchWidth;
this.ctx.moveTo(x,y);
this.ctx.lineTo(toX,toY);
this.ctx.closePath();
this.ctx.stroke();
this.ctx.restore();
depth--;
if(depth0){
this.drawBranch(toX,toY,branchLen,branchWidth-1,30,depth);
this.drawBranch(toX,toY,branchLen,branchWidth-1,-30,depth);
}
}
Tree.prototype.drawBranch = function(x,y,branchLen,branchWidth,angle,depth){
var angle = angle || 0;
var radian = (90-angle)*(Math.PI/180);
var toX = x+Math.cos(radian)*branchLen;
var toY = y-Math.sin(radian)*branchLen;
this.ctx.save();
this.ctx.strokeStyle="rgba(37, 141, 194, 0.93)";
this.ctx.beginPath();
this.ctx.lineCap = "butt";
this.ctx.lineJoin="round";
this.ctx.lineWidth = branchWidth;
this.ctx.moveTo(x,y);
this.ctx.lineTo(toX,toY);
this.ctx.closePath();
this.ctx.stroke();
this.ctx.restore();
depth--;
if(depth0){
this.drawBranch(toX,toY,branchLen,branchWidth-1,angle+30,depth);
this.drawBranch(toX,toY,branchLen,branchWidth-1,angle-30,depth);
}
}
運行代碼:
由于樹之間角度過大,而且所有樹枝長度都相等,看起來并不像一棵樹。所以我們需要在Tree的構造函數中加入幾個參數用來調整樹的姿態。
function Tree(x,y,branchLen,branchWidth,depth,canvas){
......
this.branchLenFactor = 0.8;
this.rootLenFactor = 1.2;
this.branchAngle = 20;
......
}
branchLenFactor:畫每一層樹枝的時候乘在branchLen上面,用來控制樹枝長度。rootLenFactor:畫樹根的時候乘在branchLen上面,用來控制樹根長度。branchAngle: 用來控制樹枝之間的角度
Tree.prototype.drawRoot = function(x,y,branchLen,branchWidth,depth){
var toX = x;
var toY = y-branchLen*this.rootLenFactor;
var depth = depth||5;
this.ctx.save();
this.ctx.strokeStyle="rgba(37, 141, 194, 0.93)";
this.ctx.beginPath();
this.ctx.lineCap = "butt";
this.ctx.lineJoin="round";
this.ctx.lineWidth = branchWidth;
this.ctx.moveTo(x,y);
this.ctx.lineTo(toX,toY);
this.ctx.closePath();
this.ctx.stroke();
this.ctx.restore();
depth--;
if(depth0){
this.drawBranch(toX,toY,branchLen*this.branchLenFactor,branchWidth-1,this.branchAngle,depth);
this.drawBranch(toX,toY,branchLen*this.branchLenFactor,branchWidth-1,-this.branchAngle,depth);
}
}
Tree.prototype.drawBranch = function(x,y,branchLen,branchWidth,angle,depth){
var angle = angle || 0;
var radian = (90-angle)*(Math.PI/180);
var toX = x+Math.cos(radian)*branchLen;
var toY = y-Math.sin(radian)*branchLen;
this.ctx.save();
this.ctx.strokeStyle="rgba(37, 141, 194, 0.93)";
this.ctx.beginPath();
this.ctx.lineCap = "butt";
this.ctx.lineJoin="round";
this.ctx.lineWidth = branchWidth;
this.ctx.moveTo(x,y);
this.ctx.lineTo(toX,toY);
this.ctx.closePath();
this.ctx.stroke();
this.ctx.restore();
depth--;
if(depth0){
this.drawBranch(toX,toY,branchLen*this.branchLenFactor,branchWidth-1,angle+this.branchAngle,depth);
this.drawBranch(toX,toY,branchLen*this.branchLenFactor,branchWidth-1,angle-this.branchAngle,depth);
}
}
運行代碼:(查看效果)