【题目】【5天】canvas元素
题目一:绘制雪人,传递一个Boolean
值,true
是填充,false
是圆圈。
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var fillCricle = function(x,y,lineWidth,radius,boolean,color){ ctx.lineWidth = lineWidth; ctx.beginPath(); ctx.arc(x,y,radius,0,Math.PI*2,false); if(boolean){ ctx.fillStyle = color; ctx.fill(); }else{ ctx.strokeStyle = color; ctx.stroke(); } }; fillCricle(100,100,4,60,false,"black"); fillCricle(100,110,4,10,true,"red"); fillCricle(80,80,4,10,true,"green"); fillCricle(120,80,4,10,true,"green"); fillCricle(100,240,4,80,false,"black"); fillCricle(100,240,4,10,true,"green"); fillCricle(100,208,4,10,true,"green"); fillCricle(100,272,4,10,true,"green");
题目二:编写一个函数,他将来接受一个数组:point =[[50,50],[50,100],[125,120],[100,50],[70,90],[100,90],[70,120]],绘制一条直线连接这些店。
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var point =[[50,50],[50,100],[125,120],[100,50],[70,90],[100,90],[70,120]]; var drawPoint = function (point) { //ctx.moveTo(point[0][0],point[0][1]); for(var i = 0; i < point.length; i++){ ctx.lineTo(point[i][0],point[i][1]); ctx.stroke(); } }; drawPoint(point);
这题花了一些时间,主要问题是:
- 绘制直线的思路是没理顺,应该是绘制第一个点,第二个点,第三个点,一次往后,最后连接起来;而当时我的思路是,两个点两个点连接,就一直在
moveTo
、lineTo
循环中。 - 不需要
moveTo
也可以绘制直线。
相关推荐
星星有所不知 2020-10-12
MIKUScallion 2020-07-05
jinxiutong 2020-05-10
MIKUScallion 2020-04-11
MIKUScallion 2020-02-22
jinxiutong 2020-02-13
大地飞鸿 2020-02-11
jinxiutong 2020-02-10
大地飞鸿 2020-11-12
jinxiutong 2020-07-26
songfens 2020-07-05
songfens 2020-06-11
songfens 2020-06-08
northwindx 2020-05-31
northwindx 2020-05-31
northwindx 2020-05-27
northwindx 2020-05-25