Комментарий: В этой статье в основном представлены примеры кода холста HTML5 для реализации кривых розовых кривых и узоров в форме сердца. Друзья, которые это нужно, могут ссылаться на это.
Изображение воспроизведения:
Совет: скопируйте код в HTML -файл и сохраните его и откройте его непосредственно, чтобы увидеть эффект.
Код реализации:
<! Doctype html>
<html>
<голова>
<meta charset = "gbk">
<title> html5 demo </title>
<стиль типа = "text/css">
#apdiv1 {
позиция: абсолютно;
Ширина: 120px;
Высота: 300px;
z-index: 1;
Слева: 840px;
Верх: 80px;
}
</style>
</head>
<тело>
<Canvas>
Ваш браузер не поддерживает элемент холста. </canvas>
<div>
<форма>
Уравнение кривой розы:
r = a+bsin (m/n*x)
Выберите параметры:
m: <input type = "number" min = "2" max = "29" value = "29"/>
n: <input type = "номер" min = "1" max = "12" value = "11"/>
A: <input type = "number" min = "0" max = "5" value = "1"/>
b: <input type = "number" min = "1" max = "7" value = "5"/>
<input type = "button" value = "draw">
<hr>
<input type = "button" value = "trawing 2">
<hr>
<input type = "button" value = "Heart Chart">
</form>
</div>
<script type = "text/javascript">
функция 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 ();
}
функция Drawrose (CTX, A, B, M, N) {
ctx.beginpath ();
var e = 0, c = 120;
var k = 2 * math.pi / 360;
делать {
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 ();
}
функция 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;
делать {
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 ();
}
функция 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;
делать {
y = -80*(math.sqrt (1 -x*x) + math.pow (x*x, 1/3));
x -= 0,001;
ctx.lineto (100*x, y);
} while (x> = -1);
делать {
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 () {
рисовать();
}
</script>
</body>
</html>