Last active
March 20, 2016 03:53
-
-
Save 910JQK/c3824870a72d9b815cec to your computer and use it in GitHub Desktop.
Draw a slide rule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="ruler.js"></script> | |
<script type="text/javascript"> | |
window.addEventListener('load', function(){ | |
var ctx = canvas.getContext('2d'); | |
draw_button.addEventListener('click', function(){ | |
ctx.clearRect(0, 0, 1000, 100); | |
draw(Number(max.value), Number(zoom.value), ctx); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<div> | |
<canvas id="canvas" width="1000" height="100"></canvas> | |
</div> | |
Max: | |
<input type="text" id="max" /> | |
Zoom: | |
<input type="text" id="zoom" /> | |
<button id="draw_button">Draw</button> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const DPI = 101.39; | |
function cm2px(x){ | |
return x*DPI/2.54; | |
} | |
function draw_line(pos, len, num, ctx){ | |
ctx.textAlign = 'center'; | |
ctx.beginPath(); | |
ctx.moveTo(pos, 0); | |
ctx.lineTo(pos, len); | |
ctx.stroke(); | |
if(num != 0) | |
ctx.fillText(num, pos, len+12); | |
} | |
function draw(max, zoom, ctx){ | |
var length, num; | |
for(var i=1; i<=max*10; i++){ | |
num = 0; | |
if(i%10 == 0){ | |
length = 0.7; | |
num = i/10; | |
}else if(i%5 == 0){ | |
length = 0.5; | |
}else{ | |
length = 0.4; | |
} | |
draw_line(cm2px(zoom*Math.log(0.1*i)), cm2px(length), num, ctx); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment