扫码关注官方订阅号
需要做一个canvas的绘图,四个圆环组成的一个日期选择器,点击选择器上的日期后,跳出一个js窗口显示日期。小E不会做,求大神指导,附照片和要求,谢谢!
认证0级讲师
其实一个圆环是一个圆形,然后小圆覆盖在上面,根据鼠标点击的位置去判断点了哪里,我是这么想的
小E呢通过大神的指导,和询问了一些同学之后终于做出来了,所以呢,我把我现在的成品拿出来~~,谢谢P_chou和Fackship两位大神的帮助!!!
<script> function draw(id) { var myCanvas = document.getElementById('myCanvas'); var r0 = 240; var r1 = 190; var r2 = 150; var r3 = 120; var ctx = myCanvas.getContext('2d'); //定义基础颜色 var color = []; for(var i=1;i<=24;i++){ var p = 30+(70/24)*i; color.push('hsl(170,' + p + '%,' + p + '%)'); } //把坐标轴移动到(250,250)这个点上 ctx.translate(250,250); drawSector(4,r0,[2009,2010,2011,2012]); drawSector(12,r1,[1,2,3,4,5,6,7,8,9,10,11,12]); drawSector(7,r2,['Mon','Tue','Wed','Thu','Fri','Sat','Sun']); drawSector(24,r3,[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]); //画白色区域 ctx.save(); ctx.fillStyle = "#fff"; ctx.beginPath(); ctx.arc(0,0,80,0,2*Math.PI); ctx.fill() ctx.restore(); myCanvas.onmousedown=function(e) { var text=[2010,2011,2012,2009]; var text1=[12,11,10,9,8,7,6,5,4,3,2,1]; var text2=['Sun','Sat','Fri','Thu','Wed','Tue','Mon']; var text3=[24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]; var x=e.clientX-250; var y=250-e.clientY; //当鼠标位置在半径190以外的区域 if((x*x+y*y)>190*190){ var id1=Math.ceil(Math.atan2(y,x)/(Math.PI*2/4)); if(id1<=0){ id1=id1+4;} alert(text[id1-1]+"年")} //当鼠标位置在半径150以外,190以内的区域 if((x*x+y*y)>(150*150)&&(x*x+y*y)<(190*190)){ var id1=Math.ceil(Math.atan2(y,x)/(Math.PI*2/4)); var id2=Math.ceil(Math.atan2(y,x)/(Math.PI*2/12)); if(id1<=0){ id1=id1+4;} if(id2<=0){ id2=id2+12;} alert(text[id1-1]+"年"+text1[id2-1]+"月")} //当鼠标位置在半径120以外,150以内的区域 if((x*x+y*y)>(120*120)&&(x*x+y*y)<(150*150)){ var id1=Math.ceil(Math.atan2(y,x)/(Math.PI*2/4)); var id2=Math.ceil(Math.atan2(y,x)/(Math.PI*2/12)); var id3=Math.ceil(Math.atan2(y,x)/(Math.PI*2/7)); if(id1<=0){ id1=id1+4;} if(id2<=0){ id2=id2+12;} if(id3<=0){ id3=id3+7;} alert(text[id1-1]+"年"+text1[id2-1]+"月"+text2[id3-1]+",")} //当鼠标位置在半径80以外,120以内的区域 if((x*x+y*y)>(80*80)&&(x*x+y*y)<(120*120)){ var id1=Math.ceil(Math.atan2(y,x)/(Math.PI*2/4)); var id2=Math.ceil(Math.atan2(y,x)/(Math.PI*2/12)); var id3=Math.ceil(Math.atan2(y,x)/(Math.PI*2/7)); var id4=Math.ceil(Math.atan2(y,x)/(Math.PI*2/24)); if(id1<=0){ id1=id1+4;} if(id2<=0){ id2=id2+12;} if(id3<=0){ id3=id3+7;} if(id4<=0){ id4=id4+24;} alert(text[id1-1]+"年"+text1[id2-1]+"月"+text2[id3-1]+","+text3[id4-1]+"时")} }; function drawSector (split,r,text) { // body... ctx.save(); ctx.strokeStyle = '#fff'; for(i=1;i<=split;i++){ ctx.fillStyle = color[i]; ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(r,0); ctx.arc(0,0,r,0,2*Math.PI/split); ctx.fill(); ctx.stroke(); if(text){ ctx.rotate(Math.PI/split); ctx.save(); ctx.fillStyle = "#000"; ctx.fillText(text[i-1],r-25,0); ctx.restore(); ctx.rotate(Math.PI/split); } else{ ctx.rotate(2*Math.PI/split); } } ctx.restore(); } } </script> </head> <body onload="draw('myCanvas');"> <canvas id="myCanvas" width = 500 height = 500/> </body> </html>
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
其实一个圆环是一个圆形,然后小圆覆盖在上面,根据鼠标点击的位置去判断点了哪里,我是这么想的
小E呢通过大神的指导,和询问了一些同学之后终于做出来了,所以呢,我把我现在的成品拿出来~~,谢谢P_chou和Fackship两位大神的帮助!!!