您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

在WEB里绘制爱心

2024/6/15 17:11:02发布397次查看
这次给大家带来的是怎样在web里绘制爱心,话不多说,下面直接献上案例,给大家好好分析一下。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>使用桃心形方程绘制爱心</title> </head> <body> <canvas></canvas> <script> var canvas = document.queryselector('canvas'); var ctx = canvas.getcontext('2d'); canvas.width = window.innerwidth; canvas.height = window.innerheight; var heart = function(x, y) { this.x = x; this.y = y; this.vertices = []; for(let i=0; i<30; i++) { var step = i / 30 * (math.pi * 2);//设置心上面两点之间的角度 var vector = { x : (15 * math.pow(math.sin(step), 3)), y : -(13 * math.cos(step) - 5 * math.cos(2 * step) - 2 * math.cos(3 * step) - math.cos(4 * step)) } this.vertices.push(vector); } } heart.prototype.draw = function() { ctx.translate(-1000,this.y);//这一步跟ctx.shadowoffsetx必须一起使用 ctx.beginpath(); for(let i=0; i<30; i++) { var vector = this.vertices[i]; ctx.lineto(vector.x, vector.y); } ctx.shadowcolor = "red"; ctx.shadowoffsetx = this.x+1000; ctx.fill(); } canvas.onmousedown = function(e) { var x = e.offsetx; var y = e.offsety; var heart = new heart(x, y); heart.draw(); } </script> </body> </html>
相信看了以上介绍你已经掌握了方法,更多精彩请关注其它相关文章!
相关阅读:
驼峰命名与js的问题解答
js里的布尔值、关系运算符、逻辑运算符的详解及实例
js的使用过程中如何自定义console对象
以上就是在web里绘制爱心的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product