有几个问题不太懂,麻烦解答下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | ( function (){ //给window定义一个全局变量,因为是全局变量,所以外面可以调用,就是给传入的对象定义宽和高 window.canvasLock = function (obj){ this .height = obj.height; this .width = obj.width; //指一行圆圈的个数,已经在调用这个函数的里面传入了chooseType的值,所以可以直接使用 this .chooseType = obj.chooseType; }; //js方式动态生成dom canvasLock.prototype.initDom = function (){ //document.createElement()是创建一个元素对象,这里是创建了一个div var wrap = document.createElement( "div" ); //定义了一个h4标签,以及它的样式 var str = "<h4 id='title' class='title'>绘制解锁图案</h4>" ; //为div设置样式属性 wrap.setAttribute( "style" , "position:absolute;top:0;left:0;right:0;bottom:0;" ); //创建一个canvas var canvas = document.createElement( "canvas" ); //为canvas设置id canvas.setAttribute( "id" , "canvas" ); //为canvas添加样式 canvas.style.cssText = "background-color:#305066;diaplay:inline-block;margin-top:15px;" ; //把h4标签放到了div中 wrap.innerHTML = str; //将canvas生成到div中 wrap.appendChild(canvas); var width = this .width || 300; var height = this .height || 300; //将div放在body中 document.body.appendChild(wrap); canvas.style.width = width+ "px" ; canvas.style.height = height+ "px" ; //将canvas的宽和高都等于上面定义的宽和高 canvas.width = width; canvas.height = height; } //画圆的函数 canvasLock.prototype.drawCle = function (x,y){ this .ctx.strokeStyle = "#CFE6FF" ; this .ctx.lineWidth = 2; this .ctx.beginPath(); //画出圆 this .ctx.arc(x,y, this .r,0,2*Math.PI*2, true ); this .ctx.closePath(); this .ctx.stroke(); } //定义生成圆圈的函数 canvasLock.prototype.createCircle = function (){ //定义变量n为圆圈的个数 var n = this .chooseType; var count = 0; //定义圆的半径为r,根据圆的半径和canvas宽度的关系计算出这个公式 this .r = this .ctx.canvas.width/(2+4*n); this .lastPoint = []; //定义圆的中心点坐标为一个数组 this .arr = []; this .restPoint = []; var r = this .r; for ( var i=0;i<n;i++){ for ( var j=0;j<n;j++){ count++; var obj = { x: j*4*r+3*r, y: i*4*r+3*r, index: count }; //将obj的值存入arr中 this .arr.push(obj); } } //清除画布中的内容 this .ctx.clearRect(0,0, this .ctx.canvas.width, this .ctx.canvas.height); //循环每个arr值,将值传入画圆的函数中 for ( var i=0;i< this .arr.length;i++){ this .drawCle( this .arr[i].x, this .arr[i].y); } } //给canvasLock原型上定义一个init函数,就是调用initDom,程序初始化 canvasLock.prototype.init = function (){ this .initDom(); //获取canvas this .canvas = document.getElementById( "canvas" ); this .ctx = this .canvas.getContext( "2d" ); //调用生成圆圈的函数 this .createCircle(); } })() |
10
收起
正在回答 回答被采纳积分+1
2回答
HTML5&CSS3进阶与常用框架 2018
- 参与学习 315 人
- 提交作业 136 份
- 解答问题 626 个
如果你有H5、CSS3、JS基础,热爱前端并希望在前端应用方面不断进步,那就来吧,本路径为你带来的课程有HTML5进阶、CSS3进阶、JS面向对象、jQ基础、Less等,助你进一步提升前端开发技能
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧