利用构造函数对canvas里面矩形与扇形的绘制进行一个封装
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>封装矩形构造函数,扇形构造函数</title> <style> canvas{ border: 1px solid green; } </style> </head> <body> <!-- 封装矩形构造函数,扇形构造函数 --> <canvas width="500" height="500" id="canvas"></canvas> </body> <script> var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); function toAngle(radian){ return radian*180/Math.PI; } function toRadian(angle){ return angle*Math.PI/180; } // 封装矩形 function Rect(obj){ for(var key in obj){ this[key] = obj[key]; } } Rect.prototype = { constuctor : Rect, stroke : function(){ if(this.strokeStyle){ this.ctx.strokeStyle = this.strokeStyle; } this.ctx.strokeRect(this.x0,this.y0,this.width,this.height); }, fill : function(){ if(this.fillStyle){ this.ctx.fillStyle = this.fillStyle; } this.ctx.fillRect(this.x0,this.y0,this.width,this.height) } } var rect = new Rect({ ctx : ctx, x0 : 100, y0 : 100, width : 100, height : 100 }) rect.stroke(); //rect.fill(); //扇形封装 function Shan(obj){ for(var key in obj){ this[key] = obj[key]; } } Shan.prototype = { constructor : Shan, stroke : function(){ this.ctx.moveTo(this.x0,this.y0); this.ctx.arc(this.x0,this.y0,this.randius,toRadian(this.start),toRadian(this.end)); this.ctx.closePath(); this.ctx.stroke(); }, fill : function(){ this.ctx.moveTo(this.x0,this.y0); this.ctx.arc(this.x0,this.y0,this.randius,toRadian(this.start),toRadian(this.end)); this.ctx.fill(); } } var shan = new Shan({ ctx : ctx, x0 : 200, y0 : 340, randius : 50, start : -90, end : 60 }) shan.stroke(); //shan.fill(); </script> </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具