萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML5 Canvas 繪製玫瑰曲線與心形圖案完整代碼範例

本文提供一個完整的 HTML5 Canvas 範例,展示如何繪製參數化的玫瑰曲線(Rhodonea curve)以及心形圖案。透過互動式表單,您可以即時調整參數觀察圖形變化,適合作為 Canvas 繪圖入門教學或數學視覺化參考。

效果預覽

玫瑰曲線示意圖
圖 1:玫瑰曲線效果
心形圖案示意圖
圖 2:心形圖案效果

使用說明

將下方完整代碼複製儲存為 .html 檔案,直接以瀏覽器開啟即可運作。頁面包含一個 800×600 的 Canvas 畫布,右側提供參數輸入表單:

  • 畫圖 1:依據輸入的 m、n、a、b 參數繪製玫瑰曲線(極座標方程 r = a/b + sin(m/n × θ))。
  • 畫圖 2:繪製固定參數的複合曲線(類似花瓣狀軌跡)。
  • 心形圖:使用笛卡兒坐標方程繪製填滿漸層色彩的心形。

完整原始碼

<!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>
玫瑰曲線方程:
r=a+bsin(m/n*x)

選擇參數:

m: <input type="number" name="m" min="2" max="29" value="29"/>

n: <input type="number" name="n" min="1" max="12" value="11"/>

a: <input type="number" name="a" min="0" max="5" value="1"/>

b: <input type="number" name="b" min="1" max="7" value="5"/>

<input type="button" value=" 畫 圖 " onClick="draw();">

<hr> <input type="button" value=" 畫 圖 2 " onClick="draw2();">

<hr> <input type="button" value=" 心形圖 " onClick="draw3();"> </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(); 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.restore(); } window.onload = function (){ draw(); } </script> </body> </html>

關鍵技術點

  • 使用 ctx.translate(400,300) 將座標原點移至畫布中心,方便對稱繪圖。
  • 玫瑰曲線採用極座標參數方程,透過 Math.sin 與 Math.cos 轉換為直角坐標。
  • 心形圖案利用 Math.sqrt(1-x*x) 與 Math.pow(x*x,1/3) 組合形成上下兩半弧,並套用 createRadialGradient 產生放射狀漸層填色。
  • 所有繪圖函式均在 ctx.save()/ctx.restore() 配對中執行,確保狀態隔離。
copyright © 萬盛學電腦網 all rights reserved