|
| 1 | +/** |
| 2 | + * Created by evolution on 15-9-19. |
| 3 | + */ |
| 4 | +window.onload = function (){ |
| 5 | + //创建canvas对象 |
| 6 | + var canvasObj = new Canvas('div1') |
| 7 | + var utils = new Utils() |
| 8 | + canvasObj.width = 1000 |
| 9 | + canvasObj.height = 890 |
| 10 | + //生成canvas标签 |
| 11 | + canvasObj.c() |
| 12 | + var ctx = canvasObj.getInstance() |
| 13 | + //hash环上的点阵数量 |
| 14 | + var keyNum = Math.pow(2,32)-1 |
| 15 | + //每个点阵的度数 |
| 16 | + var oneKeyDegree = 360/keyNum |
| 17 | + |
| 18 | + //clear canvas |
| 19 | + ctx.clearRect(0,0,canvasObj.width,canvasObj.height) |
| 20 | + |
| 21 | + //画外层大圆 |
| 22 | + var x = canvasObj.width/2 |
| 23 | + var y = canvasObj.height/2 |
| 24 | + var r = canvasObj.width/2.3 |
| 25 | + ctx.beginPath() |
| 26 | + ctx.arc(x,y,r,utils.hd(0),utils.hd(360)) |
| 27 | + ctx.closePath() |
| 28 | + ctx.stroke() |
| 29 | + |
| 30 | + //添加文字 |
| 31 | + function addText(){ |
| 32 | + ctx.save() |
| 33 | + ctx.font = '60px impact' |
| 34 | + ctx.textBaseLine = 'top' |
| 35 | + ctx.fillStyle = 'rgba(57,9,9,0.7)' |
| 36 | + ctx.shadowOffsetX = 10 |
| 37 | + ctx.shadowOffsetY = 10 |
| 38 | + ctx.shadowColor = 'rgba(5,9,9,0.7)' |
| 39 | + ctx.shadowBlur = 5 |
| 40 | + var w = ctx.measureText('一致性Hash算法演示').width |
| 41 | + ctx.fillText('一致性Hash算法演示',(x*2-w)/2, y+25) |
| 42 | + ctx.restore() |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * 根据索引画圆 |
| 47 | + * @param index |
| 48 | + * @param type |
| 49 | + * @constructor |
| 50 | + */ |
| 51 | + function DrawCycle(index,type){ |
| 52 | + //画圆上的坐标点 |
| 53 | + //圆O的圆心为(a,b),半径为R,点A与到X轴的为角α. |
| 54 | + //则点A的坐标为(a+R*cosα,b+R*sinα) |
| 55 | + |
| 56 | + if(type==1){ |
| 57 | + var radius = 8 |
| 58 | + var cycleColor = 'rgba(255,0,0,0.7)' |
| 59 | + }else if(type==2){ |
| 60 | + var radius = 4 |
| 61 | + var cycleColor = 'rgba(0,255,0,0.7)' |
| 62 | + } |
| 63 | + |
| 64 | + ctx.beginPath() |
| 65 | + ctx.moveTo(x,y) |
| 66 | + var linex = Math.cos(oneKeyDegree*index*Math.PI/180)*r+x |
| 67 | + var liney = Math.sin(oneKeyDegree*index*Math.PI/180)*r+y |
| 68 | + ctx.lineTo(linex,liney) |
| 69 | + ctx.arc(linex,liney,radius,utils.hd(0),utils.hd(360)) |
| 70 | + ctx.fillStyle = cycleColor |
| 71 | + ctx.strokeStyle = cycleColor |
| 72 | + ctx.fill() |
| 73 | + ctx.stroke() |
| 74 | + } |
| 75 | + |
| 76 | + //添加文字 |
| 77 | + addText() |
| 78 | + //加载缓存 |
| 79 | + |
| 80 | + localStorage.setItem("key","value") |
| 81 | + //alert(localStorage.getItem("key")) |
| 82 | + //localStorage.removeItem("key") |
| 83 | + //localStorage.clear() |
| 84 | + |
| 85 | + function addServer(){ |
| 86 | + var val = document.getElementById('serverval').value |
| 87 | + getIndex(val,1) |
| 88 | + } |
| 89 | + |
| 90 | + function addKey(){ |
| 91 | + var val = document.getElementById('keyval').value |
| 92 | + getIndex(val,2) |
| 93 | + } |
| 94 | + |
| 95 | + function getIndex(val,type){ |
| 96 | + $.get("http://local.demo.com/php_file/hash/consistency_hash/hash.php?key=21", { "key": val }, |
| 97 | + function(req) { |
| 98 | + DrawCycle(req,type) |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + document.getElementById('addserver').onclick = addServer |
| 103 | + document.getElementById('addkey').onclick = addKey |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
0 commit comments