怎么用HTML5Canvas實現玫瑰曲線和心形圖案

本篇內容介紹了“怎么用HTML5 Canvas實現玫瑰曲線和心形圖案”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

10年積累的成都網站設計、成都網站建設經驗,可以快速應對客戶對網站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網絡服務。我雖然不認識你,你也不認識我。但先網站設計后付款的網站建設流程,更有包河免費網站建設讓你可以放心的選擇與我們合作。

效果圖:
怎么用HTML5 Canvas實現玫瑰曲線和心形圖案 

怎么用HTML5 Canvas實現玫瑰曲線和心形圖案

提示:把代碼復制到一個html文件中并保存,直接打開即可看到效果。

實現代碼:
 

<!DOCTYPE html>
<html>
<head>
<meta charset = "gbk">
<title>HTML5 Demo</title>
<style type="text/css">
#apDiv1 {
position:absolute;
width:120px;
height:300px;
z-index:1;
left: 840px;
top: 80px;
}
</style>
</head>
<body>
<canvas id="canvas" width="800" height="600" style="border:1px solid #c3c3c3;"> 
Your browser does not support the canvas element. </canvas> 
<div id="apDiv1">
<form>
玫瑰曲線方程:<br>
r=a+bsin(m/n*x)<br><br>
選擇參數:<br><br>
m: <input type="number" name="m" min="2" max="29" value="29"/><br><br>
n: <input type="number" name="n" min="1" max="12" value="11"/><br><br>
a: <input type="number" name="a" min="0" max="5" value="1"/><br><br>
b: <input type="number" name="b" min="1" max="7" value="5"/><br><br>
<input type="button" value=" 畫 圖 " onClick="draw();"><br><br>
<hr><br>
<input type="button" value=" 畫 圖 2 " onClick="draw2();"><br><br>
<hr><br>
<input type="button" value=" 心形圖 " onClick="draw3();"><br>
</form>
</div>
<script type="text/javascript"> 
function draw() { 
var ctx = document.getElementById('canvas').getContext('2d'); 
ctx.save(); 
ctx.translate(400,300); 
ctx.clearRect(-400,-300,800,600);
ctx.strokeStyle = "#cc0000"; 
var a = 0, b = 1, m = 6, n = 1;
m = document.forms[0].m.value;
n = document.forms[0].n.value;
a = document.forms[0].a.value; 
b = document.forms[0].b.value; 
drawRose(ctx,a,b,m,n); 
ctx.restore(); 
} 
function drawRose(ctx,a,b,m,n){ 
ctx.beginPath(); 
var e = 0, c = 120; 
var k = 2 * Math.PI / 360;
do { 
var r = a/b + Math.sin( m * e / n * k);
r = r * c; 
var x = r * Math.cos( e * k );
var y = r * Math.sin( e * k );
e += 0.1; 
ctx.lineTo(x,y); 
} while ( e <= 4320 ); 
ctx.stroke(); 
} 
function draw2(){ 
var ctx = document.getElementById('canvas').getContext('2d'); 
ctx.save(); 
ctx.translate(400,300); 
ctx.clearRect(-400,-300,800,600);
ctx.strokeStyle = "#cc0000"; 
ctx.beginPath(); //ctx.moveTo(0,0);
var e = 0, c = 150; 
var k = 2 * Math.PI / 360;
do { 
x = 150*Math.cos( 5/2 * e*k ) + 50*Math.cos( 15/16 * 5/2 * e*k ); 
y = 150*Math.sin( 5/2 * e*k ) - 50*Math.sin( 15/16 * 5/2 * e*k ); 
e += 0.1; 
ctx.lineTo(x,y); 
} while ( e <= 3600 ); 
ctx.stroke(); 
ctx.restore();
} 
function draw3(){ 
var ctx = document.getElementById('canvas').getContext('2d'); 
ctx.save(); 
ctx.translate(400,300); 
ctx.clearRect(-400,-300,800,600);
ctx.strokeStyle = "#ff0000"; 
ctx.beginPath(); 
var x = 1, y;
do { 
y = -80*(Math.sqrt(1-x*x) + Math.pow(x*x,1/3)); 
x -= 0.001; 
ctx.lineTo(100*x,y); 
} while ( x >= -1 ); 

do { 
y = 80*(Math.sqrt(1-x*x) - Math.pow(x*x,1/3)); 
x += 0.001; 
ctx.lineTo(100*x,y); 
} while ( x <= 1 );
ctx.closePath(); 

var grad = ctx.createRadialGradient(-40,-60,10,-40,-40,200);
grad.addColorStop(0, "#ffcc00"); 
grad.addColorStop(1, "#ff0000"); 
ctx.fillStyle = grad;
ctx.fill();
// ctx.stroke(); 
ctx.restore();
} 
window.onload = function (){ 
draw();
} 
</script>
</body>
</html>

“怎么用HTML5 Canvas實現玫瑰曲線和心形圖案”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注創(chuàng)新互聯網站,小編將為大家輸出更多高質量的實用文章!

當前標題:怎么用HTML5Canvas實現玫瑰曲線和心形圖案
鏈接地址:http://bm7419.com/article12/gippdc.html

成都網站建設公司_創(chuàng)新互聯,為您提供移動網站建設、靜態(tài)網站、虛擬主機網站排名、商城網站

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

網站托管運營