HTML5 Canvas如何实现玫瑰曲线和心形图案

发布时间:2022-03-08 10:12:36 作者:小新
来源:亿速云 阅读:184

这篇文章主要介绍HTML5 Canvas如何实现玫瑰曲线和心形图案,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

提示:把代码复制到一个html文件中并保存,直接打开即可看到效果。

实现代码:

<!DOCTYPE html>

<html>

<head>

<meta charset =“ gbk”>

<title> HTML5演示</ title>

<style type =“ text / css”>

#apDiv1 {

position:absolute;

宽度:120像素;

高度:300像素;

z-index:1;

左:840px;

顶部:80px;

}

</ style>

</ head>

<body>

<canvas id =“ canvas” width =“ 800” height =“ 600” style =“ border:1px solid#c3c3c3;”>

您的浏览器不支持canvas元素。</ canvas>

<div id =“ apDiv1”>

<form>

函数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。

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(){

draw();

}

</ script>

</ body>

</ html>

以上是“HTML5 Canvas如何实现玫瑰曲线和心形图案”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. 推荐14款强大的HTML5素描及绘图工具
  2. HTML5 canvas 详细

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

html5 canvas

上一篇:怎么用Python的ini配置文件

下一篇:HTML5 Canvas裁剪clip怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》