Comentario: Este artículo presenta principalmente ejemplos de código de lienzo HTML5 para implementar curvas de rosas y patrones en forma de corazón. Los amigos que lo necesitan pueden referirse a él.
Imagen de reproducción:
Consejo: Copie el código en un archivo HTML y guárdelo, y ábralo directamente para ver el efecto.
Código de implementación:
<! Doctype html>
<html>
<Evista>
<meta charset = "gbk">
<title> html5 demo </title>
<style type = "text/css">
#apdiv1 {
Posición: Absoluto;
Ancho: 120px;
Altura: 300px;
índice z: 1;
Izquierda: 840px;
Arriba: 80px;
}
</style>
</ablo>
<Body>
<Canvas>
Su navegador no admite el elemento Canvas. </able>
<div>
<form>
Ecuación de la curva de rosa:
r = a+bsin (m/n*x)
Seleccionar parámetros:
m: <input type = "número" min = "2" max = "29" valor = "29"/>
n: <input type = "number" min = "1" max = "12" valor = "11"/>
A: <input type = "number" min = "0" max = "5" valor = "1"/>
b: <input type = "number" min = "1" max = "7" valor = "5"/>
<input type = "button" value = "dibujar">
<hr>
<input type = "button" value = "Drawing 2">
<hr>
<input type = "button" value = "Heart Chart">
</form>
</div>
<script type = "text/javaScript">
function draw () {
var ctx = document.getElementById ('Canvas'). getContext ('2d');
ctx.save ();
CTX. Translato (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;
dibujo (ctx, a, b, m, n);
ctx.restore ();
}
función dibujose (ctx, a, b, m, n) {
ctx.beginpath ();
var e = 0, c = 120;
var k = 2 * math.pi / 360;
hacer {
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. Translato (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;
hacer {
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. Translato (400,300);
CTX.ClearRect (-400, -300,800,600);
ctx.Strokestyle = "#FF0000";
ctx.beginpath ();
var x = 1, y;
hacer {
y = -80*(math.sqrt (1 -x*x) + math.pow (x*x, 1/3));
x -= 0.001;
ctx.lineto (100*x, y);
} while (x> = -1);
hacer {
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 gradi = 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 () {
dibujar();
}
</script>
</body>
</html>