html制作心形状图片,HTML5 Canvas实现玫瑰曲线和心形图案的代码实例

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

实现代码:

复制代码

代码如下:

HTML5演示 title>

#apDiv1 {

position:absolute;

宽度:120像素;

高度:300像素;

z-index:1;

左:840px;

顶部:80px;

}

style>

head>

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值